Menus and Toolbars (Coolbar)

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Menus and Toolbars (Coolbar)

Postby tagewerk » Tue Oct 07, 2003 12:10 am

Hi :D ,

now I have two more questions:

* How can I add Menus and Toolsbars in an SWT ApplicationWindow ( not an SWT Application)?

One can find the following (generated) source :

Code: Select all
   protected MenuManager createMenuManager()
   {
      MenuManager result = new MenuManager("myMenu");
      // DESIGNER: Add controls before this line.
      return result;
   }
   protected ToolBarManager createToolBarManager(int arg0)
   {
      ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT | SWT.WRAP);
      // DESIGNER: Add controls before this line.
      return toolBarManager;
   }

but I don' know how to add i.e. a menu to the menumanager using the designer.

* When I try to add a Coolbar (an item and a button into it), I can see the result in test mode but i can't see it in my running app.

Source :

Code: Select all
public class MyNewAppWindow {

   public static void main(String[] args) {
      final Display display = new Display();
      final Shell shell = new Shell();
      final GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 2;
      shell.setLayout(gridLayout);
      shell.setText("SWT Application");
      {
         Menu menubar=new Menu(shell, SWT.BAR);
         shell.setMenuBar(menubar);
         {
            final MenuItem menuItem = new MenuItem(menubar, SWT.CASCADE);
            menuItem.setText("item");
            {
               Menu popupmenu=new Menu(menuItem);
               menuItem.setMenu(popupmenu);
            }
         }
         {
            final MenuItem menuItem = new MenuItem(menubar, SWT.CASCADE);
            menuItem.setText("item");
            {
               Menu popupmenu=new Menu(menuItem);
               menuItem.setMenu(popupmenu);
            }
         }
      }
      {
         final ToolBar toolBar = new ToolBar(shell, SWT.NONE);
         {
            final ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
            toolItem.setText("New item");
         }
      }
      {
         final CoolBar coolBar = new CoolBar(shell, SWT.NONE);
         coolBar.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL));
         {
            final CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
            coolItem.setText("New item");
            {
               final Button button = new Button(coolBar, SWT.CHECK);
               button.setSelection(true);
               coolItem.setControl(button);
               button.setText("check button");
            }
         }
      }
      // DESIGNER: Add controls before this line.
      shell.open();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
            display.sleep();
      }
   }


:?:
a CRAY is only a computer...
... that runs an endless job in just 4 hours
tagewerk
 
Posts: 4
Joined: Sat Oct 04, 2003 2:08 pm
Location: Bonn, Germany

Postby admin » Tue Oct 07, 2003 2:10 am

Here is small piece of code that works (on my computer where I have images). Most probably problem is with following calls in constructor:

Code: Select all
      addToolBar(SWT.NONE);
      addMenuBar();


BTW, we have good (I hope) support for editing ApplicationWindow, there are two tabs on "Design" tab: "Content" and "Actions".

Code: Select all
package applications;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import com.swtdesigner.ResourceManager;
/**
* @author scheglov_ke
*/
public class TestApplicationWindow extends ApplicationWindow {
   private Action m_UpAction;
   private Action m_ExitAction;
   private Action m_AboutAction;
   public TestApplicationWindow() {
      super(null);
      createActions();
      addToolBar(SWT.NONE);
      addMenuBar();
   }
   protected Control createContents(Composite parent) {
      Composite composite = new Composite(parent, SWT.NONE);
      final GridLayout gridLayout = new GridLayout();
      gridLayout.marginWidth = 0;
      gridLayout.marginHeight = 0;
      composite.setLayout(gridLayout);
      {
         final Label label = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
         label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
      }
      {
         final Group group = new Group(composite, SWT.NONE);
         group.setText("111");
         group.setLayoutData(new GridData(GridData.FILL_BOTH));
      }
      {
         final Group group = new Group(composite, SWT.NONE);
         group.setText("222");
         group.setLayout(new FillLayout(SWT.HORIZONTAL));
         group.setLayoutData(new GridData(GridData.FILL_BOTH));
         {
            final Button button = new Button(group, SWT.NONE);
            button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
            button.setText("button1");
         }
         {
            final Button button = new Button(group, SWT.NONE);
            button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
            button.setText("button2");
         }
      }
      // DESIGNER: Add controls before this line.
      return composite;
   }
   private void createActions() {
      {
         m_ExitAction = new Action() {
            public void run() {
               close();
            }
         };
         m_ExitAction.setText("Exit\tAlt+F4");
         m_ExitAction.setToolTipText("Close application");
         m_ExitAction.setImageDescriptor(
            ResourceManager.getImageDescriptor(TestApplicationWindow.class, "images/exit.gif"));
      }
      {
         m_AboutAction = new Action() {
            public void run() {
               close();
            }
         };
         m_AboutAction.setToolTipText("Show information about application");
         m_AboutAction.setImageDescriptor(
            ResourceManager.getImageDescriptor(TestApplicationWindow.class, "images/synced.gif"));
         m_AboutAction.setText("About...");
      }
      {
         m_UpAction = new Action() {
            public void run() {
            }
         };
         m_UpAction.setImageDescriptor(
            ResourceManager.getImageDescriptor(TestApplicationWindow.class, "images/up_nav.gif"));
         m_UpAction.setText("Action");
      }
      // DESIGNER: Add controls before this line.
   }
   protected MenuManager createMenuManager() {
      MenuManager mainMenu = new MenuManager("menu");
      {
         final MenuManager menuManager = new MenuManager("&File");
         mainMenu.add(menuManager);
         menuManager.add(m_UpAction);
         menuManager.add(new Separator());
         menuManager.add(m_ExitAction);
      }
      {
         final MenuManager menuManager = new MenuManager("&Help");
         mainMenu.add(menuManager);
         menuManager.add(m_AboutAction);
      }
      // DESIGNER: Add controls before this line.
      return mainMenu;
   }
   protected ToolBarManager createToolBarManager(int arg0) {
      ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT | SWT.WRAP);
      toolBarManager.add(m_AboutAction);
      toolBarManager.add(m_ExitAction);
      toolBarManager.add(new Separator());
      toolBarManager.add(m_ExitAction);
      toolBarManager.add(m_UpAction);
      // DESIGNER: Add controls before this line.
      return toolBarManager;
   }
   public void run() {
      create();
      open();
      //
      Display display = Display.getCurrent();
      Shell shell = getShell();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
            display.sleep();
      }
   }
   public static void main(String args[]) {
      Display display = new Display();
      TestApplicationWindow window = new TestApplicationWindow();
      window.run();
   }
   protected void configureShell(Shell newShell) {
      super.configureShell(newShell);
      newShell.setText("123");
   }
}
admin
Moderator
 
Posts: 166
Joined: Thu Jul 24, 2003 12:25 am

Postby admin » Tue Oct 07, 2003 2:13 am

...and about coolbar. See below one more example.
Here trick is in:
Code: Select all
         ResourceManager.fixCoolBarSize(coolBar);

You can look in its source and notice, that we set right size for each coolitem.

Code: Select all
package applications;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import com.swtdesigner.ResourceManager;
/**
* @author scheglov_ke
*/
public class TestCoolBar {
   static class ContentProvider implements IStructuredContentProvider {
      public Object[] getElements(Object inputElement) {
         return new Object[] { "item_0", "item_1", "item_2" };
      }
      public void dispose() {
      }
      public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
      }
   }
   public static void main(String[] args) {
      final Display display = new Display();
      final Shell shell = new Shell();
      shell.setLayout(new GridLayout());
      shell.setText("SWT Application");
      {
         final CoolBar coolBar = new CoolBar(shell, SWT.FLAT);
         coolBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL));
         {
            final CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
            {
               final ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT | SWT.RIGHT);
               coolItem.setControl(toolBar);
               {
                  final ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
                  toolItem.setToolTipText("111111111111");
                  toolItem.setText("New item");
               }
               {
                  final ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
                  toolItem.setToolTipText("222222222");
                  toolItem.setText("New item");
               }
            }
         }
         {
            final CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
            coolItem.setText("New item");
            {
               final Text text = new Text(coolBar, SWT.BORDER);
               coolItem.setControl(text);
               text.setText("text");
            }
         }
         {
            final CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
            {
               final Button button = new Button(coolBar, SWT.NONE);
               coolItem.setControl(button);
               button.setText("button 2");
            }
         }
         {
            final CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
            {
               final Button button = new Button(coolBar, SWT.NONE);
               coolItem.setControl(button);
               button.setText("button 1");
            }
         }
         {
            final CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
            coolItem.setText("New item");
         }
         ResourceManager.fixCoolBarSize(coolBar);
      }
      {
         final CoolBar coolBar = new CoolBar(shell, SWT.NONE);
         coolBar.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
         {
            final CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
            coolItem.setText("New item");
            {
               final TableViewer tableViewer = new TableViewer(coolBar, SWT.BORDER);
               tableViewer.setContentProvider(new ContentProvider());
               final Table table = tableViewer.getTable();
               table.setLinesVisible(true);
               table.setHeaderVisible(true);
               coolItem.setControl(tableViewer.getControl());
               {
                  final TableColumn tableColumn = new TableColumn(table, SWT.NONE);
                  tableColumn.setWidth(100);
                  tableColumn.setText("New column");
               }
               {
                  final TableColumn tableColumn = new TableColumn(table, SWT.NONE);
                  tableColumn.setWidth(100);
                  tableColumn.setText("New column");
               }
               tableViewer.setInput(new Object());
            }
         }
         {
            final CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
            coolItem.setText("New item");
         }
         ResourceManager.fixCoolBarSize(coolBar);
      }
      {
         final ToolBar toolBar = new ToolBar(shell, SWT.NONE);
         toolBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
         {
            final ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
            toolItem.setToolTipText("1111111111");
            toolItem.setText("New item");
         }
         {
            final ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
            toolItem.setToolTipText("2222222");
            toolItem.setText("New item");
         }
      }
      // DESIGNER: Add controls before this line.
      shell.open();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
            display.sleep();
      }
   }
}
admin
Moderator
 
Posts: 166
Joined: Thu Jul 24, 2003 12:25 am

Postby tagewerk » Tue Oct 07, 2003 6:31 am

Thanks for your tips. I didn't notice the action tab :oops: . Now its nice an easy to use.

I hope the hint
Code: Select all
ResourceManager.fixCoolBarSize(coolBar);

will be included in future release of SWT-Designer.

Jan-Holger
a CRAY is only a computer...
... that runs an endless job in just 4 hours
tagewerk
 
Posts: 4
Joined: Sat Oct 04, 2003 2:08 pm
Location: Bonn, Germany


Return to SWT Designer

Who is online

Users browsing this forum: Google [Bot] and 1 guest