eclipse - 通过Eclipse插件以编程方式更改菜单项

标签 eclipse eclipse-plugin eclipse-rcp menuitem

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

如果它可以满足我的需要,我该如何使用它们?在此先感谢您的协助。

最佳答案

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

您需要跟踪已删除的项目和项目索引以恢复它们。唯一的麻烦是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();
    }
}

关于eclipse - 通过Eclipse插件以编程方式更改菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1202197/

相关文章:

java - Eclipse 上有关 sun/misc/BASE64Encoder 的错误

java - 多个 JFrame 实例

eclipse - 不能添加luaeclipse

android - 我无法为 Eclipse 安装 PhoneGap 插件

java - 在 Eclipse RCP 应用程序中捕获终止信号

Android sendBroadcastAsUser() UserHandle.USER_CURRENT 错误

java - 有没有什么方法可以在 eclipse RCP 中创建一个无论焦点如何都有效的键绑定(bind)?

java - 如何在 Eclipse 中获取调试 View ?

java - eclipse 如何知道有哪些 OSGi 包可以依赖?

eclipse-rcp - Eclipse RCP 3.6.2/P2/Junit 测试的完整示例