从 Eclipse 插件以编程方式更改菜单项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1202197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:55:35  来源:igfitidea点击:

Change Menu Items Programmatically From Eclipse Plugin

eclipseeclipse-plugineclipse-rcpmenuitem

提问by AlbertoPL

I would like to be able to completely remove menu items upon startup of my eclipse plugin application. What I want to do is be able to add these menu items later depending on business logic based off of the user's actions. Is there a way to do this? I've looked at using contributions, but I feel like it's not going to be exactly what I want.

我希望能够在我的 eclipse 插件应用程序启动时完全删除菜单项。我想要做的是能够稍后根据基于用户操作的业务逻辑添加这些菜单项。有没有办法做到这一点?我已经考虑过使用贡献,但我觉得它不会完全是我想要的。

If it cando what I need it to do, how do I go about using them? Thanks in advance for any assistance.

如果它可以做我需要它做的事情,我该如何使用它们?在此先感谢您的帮助。

采纳答案by Rich Seller

You can obtain the Menu from the MenuManager and then modify the contributions. This snippet shows how to access the menu manager and remove a named item.

您可以从 MenuManager 获取菜单,然后修改贡献。此代码段显示了如何访问菜单管理器并删除命名项。

You'll need to keep track of the removed items and item indices to restore them. The only trouble is that the indexOf method is not visible. Adding this snippet to a type in the same package as MenuManager and adding it to a fragment is one way round that.

您需要跟踪已删除的项目和项目索引以恢复它们。唯一的麻烦是 indexOf 方法不可见。将此片段添加到与 MenuManager 相同的包中的类型并将其添加到片段是一种解决方法。

IWorkbenchWindow window = Workbench.getInstance().getActiveWorkbenchWindow()

if(window instanceof WorkbenchWindow) {
    MenuManager menuManager = ((WorkbenchWindow)window).getMenuManager();

    //TODO you may need to remove items from the coolbar as well
    ICoolBarManager coolBarManager = null;

    if(((WorkbenchWindow) window).getCoolBarVisible()) {
        coolBarManager = ((WorkbenchWindow)window).getCoolBarManager2();
    }

    Menu menu = menuManager.getMenu();

    //you'll need to find the id for the item
    String itemId = "menuId";
    IContributionItem item = menuManager.find(itemId);

    // remember position, TODO this is protected
    int controlIdx = menu.indexOf(mySaveAction.getId());

    if (item != null) {
        // clean old one
        menuManager.remove(item);

        // refresh menu gui
        menuManager.update();
    }
}

回答by Tonny Madsen

There are a number of ways to control the visibility of menu or toolbar items in an RCP application.

有多种方法可以控制 RCP 应用程序中菜单或工具栏项的可见性。

If you have control over the plug-in that contributes the item(s) in question, then the easiest way is usually to use the visibleWhenexpression associated with the menusextension point. If you have some internal state you want to check, then you can use the testelement of the expression together with a propertyTester.

如果您可以控制提供相关项目的插件,那么最简单的方法通常是使用visibleWhenmenus扩展点关联的表达式。如果您有一些要检查的内部状态,则可以test将表达式的元素与propertyTester.

An alternative is to use activities. These can control many other things of your application, thought you might need to re-implement some of the built-in dialogs. See this blog entry "Using activities for user management"for some details.

另一种方法是使用活动。这些可以控制应用程序的许多其他内容,认为您可能需要重新实现一些内置对话框。有关详细信息,请参阅此博客条目“使用活动进行用户管理”