动态上下文菜单项

时间:2010-04-30 12:56:34

标签: java eclipse-rcp

我正在使用Eclipse RCP来构建桌面应用程序。当用户调用弹出菜单时,我想在菜单中添加一些项目。类似于针对问题采取的“建议操作”列表。弹出窗口在桌子上,它已经有命令。 实现这个的正确方法是什么?

1 个答案:

答案 0 :(得分: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()));
....
}