【问题标题】:In Eclipse, how do I contribute a context/pop-up menu to a control in a dialog?在 Eclipse 中,如何为对话框中的控件提供上下文/弹出菜单?
【发布时间】:2016-02-15 17:39:54
【问题描述】:

通常,在编辑器中,可以提供一个上下文菜单(在 org.eclipse.ui.menus 扩展中声明),内容如下。

MenuManager menuManager = new MenuManager();
Control menuParent = ... ;//some Control in the editor
Menu contextMenu = menuManager.createContextMenu(menuParent);
menuParent.setMenu(contextMenu);
getEditorSite().registerContextMenu(CONTEXT_MENU_ID, menuManager, getMySelectionProvider(), false);

我想在对话框中做类似的事情。

显然,我今天的谷歌搜索技能有所欠缺,因为除了一个人在 DZone 上问同样的问题而没有成功,我似乎找不到任何东西。

是否可以通过扩展点为对话框中的控件提供菜单?

【问题讨论】:

    标签: eclipse-plugin eclipse-rcp jface


    【解决方案1】:

    使用工作台窗口的IMenuService

    IMenuService mSvc = (IMenuService) window.getService(IMenuService.class);
    MenuManager mgr = new MenuManager();
    mSvc.populateContributionManager(mgr, "popup:my.dialog.menu");
    control.setMenu(mgr.createContextMenu(control));
    

    【讨论】:

    • 太棒了!谢谢你。现在我只需要弄清楚某种处理程序启用问题,但我相信这无关紧要。
    • @Jesse 我不知道如何在没有工作台部件的情况下为工作台提供选择,因此您可能无法根据当前选择定义启用表达式。或者,您可以使用contexts
    • +1:这是我在使用 e4 桥接器(即 DIViewPart)时向混合 e3/e4 应用程序中的 e4 部件添加弹出菜单的唯一方法。您必须 @Inject IMenuService 而不是 EMenuService。
    【解决方案2】:

    这是我为ViewPart 所做的。我不知道它是否适用于Dialog

    protected void createContextMenu() {
        MenuManager menuMgr = new MenuManager("#PopupMenu");
        menuMgr.setRemoveAllWhenShown(true);
        menuMgr.addMenuListener(new IMenuListener() {
            public void menuAboutToShow(IMenuManager m) {
                WorkAssignmentView.this.fillContextMenu(m);
            }
        });
        Menu menu = menuMgr.createContextMenu(activeViewer.getControl());
        activeViewer.getControl().setMenu(menu);
        getSite().registerContextMenu(menuMgr, activeViewer);
    }
    

    其中WorkAssignmentView 是我的ViewPart 类的名称,activeViewerTableViewer

    如果你能做到control.getControl().setMenu(menu);,那么你应该可以把它用作模型。

    这是填充上下文菜单的方法。

    protected void fillContextMenu(IMenuManager menuMgr) {
    
        WorkAssignmentViewRow[] row = this.getSelectedAssignments();
    
        menuMgr.add(addAction);
    
            if (status.equals(WorkAssignmentViewRow.ACTIVE)) {
                menuMgr.add(new Separator());
                menuMgr.add(modifyAction);
                menuMgr.add(changeAction);
                menuMgr.add(new Separator());
                menuMgr.add(deleteAction);
                menuMgr.add(new Separator());
                menuMgr.add(whoIsModifyingAction);
                menuMgr.add(new Separator());
                menuMgr.add(displayAction);
                menuMgr.add(displayFullAction);
                menuMgr.add(new Separator());
                menuMgr.add(updateAction);
                menuMgr.add(commitAction);
                menuMgr.add(new Separator());
                menuMgr.add(developmentMoveAction);
                menuMgr.add(new Separator());
                menuMgr.add(testMoveAction);
                menuMgr.add(new Separator());
                menuMgr.add(productionMoveAction);
                menuMgr.add(displayProductionAction);
                menuMgr.add(new Separator());
            } else if (status.equals(WorkAssignmentViewRow.INACTIVE)){
                menuMgr.add(new Separator());
                menuMgr.add(changeAction);
                menuMgr.add(new Separator());
                menuMgr.add(deleteAction);
                menuMgr.add(new Separator());
                menuMgr.add(displayAction);
                menuMgr.add(displayFullAction);
                menuMgr.add(new Separator());
            } else {
                menuMgr.add(new Separator());
                menuMgr.add(displayAction);
                menuMgr.add(displayFullAction);
                menuMgr.add(new Separator());
            }
    
        menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    }
    

    【讨论】:

    • 感谢您的回复。我应该更具体。我已经在 View 中使用的 org.eclipse.ui.menus 扩展中声明了菜单。我想在对话框中重新使用它。对话框没有站点,所以我无法调用 registerContextMenu()
    • 正如我所说,我不知道这是否适用于对话框。你说得对,registerContextMenu() 只适用于编辑器和视图。
    【解决方案3】:

    我已经设法在自定义表中显示上下文菜单(我也在视图中使用) 首选项对话框中的控件。

    在我的 ViewPart 我有方法:

    public static MyView getView() {
        MyView myView = (MyView) 
        Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().
        getActivePage().findView(MyView.ID);
    
        return myView;
    }
    

    在我的 RCP 应用程序中,视图始终可见,因此该方法永远不应该 返回空值。

    然后在我的偏好页面的 ctor 中,在对话框中打开:

        m_viewPart = MyView.getView();
    

    在首选项页面中创建控件时,我注册上下文菜单 我的视图部分:

        Control control = new ...
        MenuManager menuManager = new MenuManager();
        Menu menu = menuManager.createContextMenu(control);
        control.setMenu(menu);
        viewPart.getSite().registerContextMenu("si.test.myCtxMenu", 
                                               menuManager, 
                                               selectionProvider);
    

    也许在对话框中注册组件的上下文菜单不是最好的主意 到 ViewPart,但它很简单而且很有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2019-02-16
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多