2010-04-30 120 views
1

我使用Eclipse RCP構建桌面應用程序。當用戶調用彈出式菜單時,我想添加一些項目到菜單中。類似於一系列「建議行動」來解決問題。彈出窗口在桌面上,它已經有了命令。 執行此操作的正確方法是什麼?動態上下文菜單項

回答

1
在ViewPart時(例如)

您可以添加

public void createPartControl(Composite parent) { 
    ... 
    final Action a = new Action("") {}; 
    final MenuManager mgr = new MenuManager(); 
    mgr.setRemoveAllWhenShown(true); 

    mgr.addMenuListener(new IMenuListener() { 
     public void menuAboutToShow(IMenuManager manager) { 
     final IStructuredSelection selection = (IStructuredSelection) listViewer 
         .getSelection(); 
     if (!selection.isEmpty()) { 
         // example Action, here delete... 
      Action deleteAction = new Action("Delete") { 
       public void run() { 
       ....  
           } 
      }; 
      mgr.add(deleteAction); 

         // *** decide here which actions to add by *** 
         // *** evaluation of some of your variables *** 

      mgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 

     } 
     }); 
     tableViewer.getControl().setMenu(
       mgr.createContextMenu(tableViewer.getControl())); 
.... 
}