Creating a coolbar in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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.Group;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
 
public class Main {
   public static void main (String [] args) {
      Display display = new Display ();
      Shell shell = new Shell (display);
      shell.setLayout(new GridLayout());
 
      final CoolBar coolBar = new CoolBar(shell, SWT.BORDER);
      coolBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));  
 
      // create the first ToolBar
      ToolBar toolBar = new ToolBar(coolBar, SWT.NULL);
      ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);      
      toolItem.setImage(getImage("jpgIcon.gif"));
      toolItem = new ToolItem (toolBar, SWT.SEPARATOR);
      toolItem = new ToolItem(toolBar, SWT.PUSH);      
      toolItem.setImage(getImage("gifIcon.gif"));
      toolItem = new ToolItem (toolBar, SWT.SEPARATOR);
      toolItem = new ToolItem(toolBar, SWT.PUSH);      
      toolItem.setImage(getImage("tiffIcon.gif"));
      toolItem = new ToolItem (toolBar, SWT.SEPARATOR);
       
      CoolItem coolItem = new CoolItem(coolBar, SWT.NONE);
      Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
      size = coolItem.computeSize(size.x, size.y);
      coolItem.setSize(size);
      coolItem.setMinimumSize(toolItem.getWidth(), size.y);
      coolItem.setControl(toolBar);
            
      // create the second ToolBar
      toolBar = new ToolBar(coolBar, SWT.BORDER);
      ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
      item.addSelectionListener(new DropDownSelectionListener(shell));
      item.setImage(getImage("jpgIcon.gif"));
      item = new ToolItem(toolBar, SWT.DROP_DOWN);
      item.addSelectionListener(new DropDownSelectionListener(shell));
      item.setImage(getImage("gifIcon.gif"));
      
      coolItem = new CoolItem(coolBar, SWT.NONE);
      size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
      size = coolItem.computeSize(size.x, size.y);
      coolItem.setSize(size);
      coolItem.setMinimumSize(toolItem.getWidth(), size.y);
      coolItem.setControl(toolBar);
       
      // all the indices that will begin on a new row      
      coolBar.setWrapIndices(new int[] {0, 1});
 
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Button button = ((Button) event.widget);
            coolBar.setLocked(button.getSelection());
         };
      };
      
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));      
      Button lockButton = new Button(buttonGroup, SWT.CHECK);
      lockButton.setText("Lock the CoolBar!");
      lockButton.addSelectionListener(selectionListener);
                 
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
   }
   
   public static Image getImage(String imageName) {
      ImageData source = new ImageData(Main.class.getResourceAsStream(imageName));
      ImageData mask = source.getTransparencyMask();
      Image image = new Image(null, source, mask);
      return image;
   }   
   
   
   static class DropDownSelectionListener extends SelectionAdapter {
      private Menu menu = null;
      private Shell shell;
      
      public DropDownSelectionListener(Shell shell) {
         this.shell = shell;
      }
      
      public void widgetSelected(SelectionEvent event) {
         System.out.println("Widget is clicked on!");
         if (menu == null) {
            // create menu if it doesn't exist already
            menu = new Menu(shell);
            MenuItem menuItem1 = new MenuItem(menu, SWT.NONE);
            menuItem1.setText("App #1");
            MenuItem menuItem2 = new MenuItem(menu, SWT.NONE);
            menuItem2.setText("App #2");
            MenuItem separatorItem = new MenuItem(menu, SWT.SEPARATOR);
            MenuItem menuItem3 = new MenuItem(menu, SWT.NONE);
            menuItem3.setText("Run...");
            
            menuItem1.addSelectionListener(new SelectionAdapter() {
               public void widgetSelected(SelectionEvent e) {
                  menu.setVisible(true);
               }
            });
         }
         
         // if user clicked on down arrow, show menu
         if (event.detail == SWT.ARROW) {
            System.out.println("... and it's the arrow!");
            if (menu.getVisible()) {
               menu.setVisible(false);
            } else { 
               final ToolItem toolItem = (ToolItem) event.widget;
               final ToolBar  toolBar = toolItem.getParent();
               
               Rectangle toolItemBounds = toolItem.getBounds();
               Point point = toolBar.toDisplay(new Point(toolItemBounds.x, toolItemBounds.y));
               menu.setLocation(point.x, point.y + toolItemBounds.height);
               menu.setVisible(true);
            }
         }
      }
   }   
}