2010-04-30 126 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())); 
.... 
}