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

Alb*_*oPL 17 eclipse eclipse-plugin eclipse-rcp menuitem

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

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

Ric*_*ler 16

您可以从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();
    }
}
Run Code Online (Sandbox Code Playgroud)