【问题标题】:How to contribute a context menu to a custom view in an eclipse plug-in project?如何在 Eclipse 插件项目中为自定义视图贡献上下文菜单?
【发布时间】:2015-08-02 12:56:51
【问题描述】:

在一个 Eclipse 插件项目中,我贡献了一个名为 Favorites 的视图,ID 为 com.qualityeclipse.favorites.views.FavoritesView

然后我想通过使用popup:com.qualityeclipse.favorites.views.FavoritesView?after=additionsFavorites 视图贡献一个上下文菜单。
但是,在 Favorites 视图中单击鼠标右键不会显示上下文菜单。

我将其更改为popup:org.eclipse.ui.popup.any?after=additions 进行测试。这次上下文菜单在其他视图(例如ProblemsConsoleDeclaration)中按预期出现,而不是我自己的Favorites 视图。

如何为自定义视图提供上下文菜单?

【问题讨论】:

    标签: eclipse debugging view eclipse-plugin popup


    【解决方案1】:

    您必须在视图代码中创建上下文菜单并将其注册到视图站点。比如:

    ISelectionProvider provider = ... a selection provider such as a TreeViewer or TableViewer
    
    Control control = control to own the menu - usually the TreeViewer or TableViewer control
    
    MenuManager menuMgr = new MenuManager("#PopUp");
    menuMgr.setRemoveAllWhenShown(true);
    
    menuMgr.addMenuListener(new IMenuListener() {
        @Override
        public void menuAboutToShow(IMenuManager manager) {
           // Additions placeholder
           manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    
           // Note: you can add other menu items directly here
        }
    });
    
    Menu menu = menuMgr.createContextMenu(control);
    control.setMenu(menu);
    
    // register the context menu such that other plug-ins may contribute to it
    getSite().registerContextMenu(menuMgr, provider);
    

    【讨论】:

    • 谢谢。这可以以声明方式而不是编程方式完成吗?这里的代码看起来比较常规。
    【解决方案2】:

    你需要使用:

    popup:org.eclipse.ui.popup.any?after=additions
    

    然后,为每个命令添加一些条件(right click > New > Visible When

    当活动部分是您的视图时,它会使其可见

         <visibleWhen
            checkEnabled="false">
         <with
               variable="activePartId">
            <equals
                  value="com.qualityeclipse.favorites.views.FavoritesView">
            </equals>
         </with>
      </visibleWhen>
    

    【讨论】:

    • 这并不能直接解决我的问题。 @greg-449 的答案使上下文菜单在我自己的视图中右键单击时显示。您的答案可用于将“任何”上下文菜单限制为我自己的视图。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多