Change Menu Items Programmatically From Eclipse Plugin

后端 未结 2 1910
南旧
南旧 2020-12-08 05:07

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 o

相关标签:
2条回答
  • 2020-12-08 05:46

    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.

    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.

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-08 06:05

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

    If you have control over the plug-in that contributes the item(s) in question, then the easiest way is usually to use the visibleWhen expression associated with the menus extension point. If you have some internal state you want to check, then you can use the test element of the expression together with a 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.

    0 讨论(0)
提交回复
热议问题