Example #1
0
 /** Initializing singleton constructor */
 private MainMenu() {
   menu = new Menu(TrayManager.getInstance().getShell(), SWT.POP_UP);
   menu.addListener(
       SWT.Hide,
       new Listener() {
         public void handleEvent(final Event event) {
           new Thread(
                   new Runnable() {
                     public void run() {
                       if (!menu.isDisposed()) {
                         menu.getDisplay()
                             .asyncExec(
                                 new Runnable() {
                                   public void run() {
                                     try {
                                       Thread.sleep(10l);
                                       hiddenSince = System.currentTimeMillis();
                                     } catch (InterruptedException e) {
                                       log.warn("Could not sleep", e);
                                     }
                                     if (!(state instanceof MenuClosedState)) {
                                       setState(MenuClosedState.getInstance());
                                     }
                                   }
                                 });
                       }
                     }
                   })
               .start();
         }
       });
 }
  private void hookColumnChooserMenu(final Table table) {
    // we're hooking up a submenu using straight-up swt (as opposed to contribution items)
    // since this is how it will eventually work in swt post-3.2 when table supports
    // attaching a pop-up menu to the column headers.
    final Menu menu = table.getMenu();

    menu.addListener(
        SWT.Show,
        new Listener() {

          private MenuItem columnsCascadeItem;

          public void handleEvent(Event e) {
            if (menu.getItemCount() > 0
                && menu.getItem(menu.getItemCount() - 1)
                    .equals(columnsCascadeItem)) // this menu already has our additions
            return;

            if (menu.getItemCount() != 0) {
              new MenuItem(menu, SWT.SEPARATOR);
            }

            columnsCascadeItem = new MenuItem(menu, SWT.CASCADE);
            columnsCascadeItem.setText(CHOOSE_COLUMNS_LABEL.getText());
            Menu columnsCascadeMenu = new Menu(table.getShell(), SWT.DROP_DOWN);
            columnsCascadeItem.setMenu(columnsCascadeMenu);

            for (final TableColumn column : table.getColumns()) {
              boolean columnIsInitialZeroWidthColumn =
                  isColumnHidden(column)
                      && (column.getText() == null || column.getText().length() == 0);
              if (!columnIsInitialZeroWidthColumn) {
                MenuItem item = new MenuItem(columnsCascadeMenu, SWT.CHECK);
                item.setText(column.getText());
                item.setEnabled(true);
                item.setSelection(!isColumnHidden(column));

                item.addListener(
                    SWT.Selection,
                    new Listener() {
                      public void handleEvent(Event event) {
                        if (isColumnHidden(column)) showColumn(column);
                        else hideColumn(column);
                      }
                    });
              }
            }

            new MenuItem(columnsCascadeMenu, SWT.SEPARATOR);

            MenuItem moreColumnsItem = new MenuItem(columnsCascadeMenu, SWT.PUSH);
            moreColumnsItem.setText(MORE_COLUMNS_LABEL.getText());
            moreColumnsItem.setEnabled(false);
          }
        });
  }
 private void setupPopupMenu() {
   final Menu menuPopup = new Menu(this.getShell(), SWT.POP_UP);
   final MenuItem itemDelete = new MenuItem(menuPopup, SWT.PUSH);
   itemDelete.setText("删除联系人...");
   menuPopup.addListener(
       SWT.Show,
       new Listener() {
         @Override
         public void handleEvent(Event event) {
           TreeItem[] treeItems = tree.getSelection();
           List<ContactInfo> infoList = new ArrayList<ContactInfo>();
           if (treeItems != null) {
             for (TreeItem treeItem : treeItems) {
               ContactInfo info = (ContactInfo) treeItem.getData();
               if (info != null) {
                 infoList.add(info);
               }
             }
           }
           if (infoList.size() > 0) {
             itemDelete.setEnabled(true);
           } else {
             itemDelete.setEnabled(false);
           }
         }
       });
   itemDelete.addSelectionListener(
       new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
           TreeItem[] treeItems = tree.getSelection();
           List<ContactInfo> infoList = new ArrayList<ContactInfo>();
           if (treeItems != null) {
             for (TreeItem treeItem : treeItems) {
               ContactInfo info = (ContactInfo) treeItem.getData();
               if (info != null) {
                 infoList.add(info);
               }
             }
           }
           if (infoList.size() > 0) {
             MessageBox mb = new MessageBox(getShell(), SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
             mb.setMessage("确定要删除选择的联系人?");
             mb.setText("提示");
             if (mb.open() == SWT.OK) {
               deleteContact(infoList);
             }
           }
         }
       });
   tree.setMenu(menuPopup);
 }
 /*
  * (non-Javadoc)
  *
  * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
  */
 public Menu getMenu(Menu parent) {
   createDropDownMenuMgr();
   final Menu menu = new Menu(parent);
   menu.addListener(
       SWT.Show,
       new Listener() {
         public void handleEvent(Event event) {
           if (menu.isDisposed()) {
             return;
           }
           MenuItem[] items = menu.getItems();
           for (int i = 0; i < items.length; i++) {
             items[i].dispose();
           }
           IContributionItem[] contributions = getContributionItems();
           for (int i = 0; i < contributions.length; i++) {
             contributions[i].fill(menu, -1);
           }
           new ActionContributionItem(new OpenDialogAction()).fill(menu, -1);
         }
       });
   return menu;
 }