views:

944

answers:

1

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.

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

+3  A: 

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();
    }
}
Rich Seller
Impress... wait, I have said that before, haven't I? +1
VonC
thanks, you're very kind
Rich Seller
Wow, this is fantastic! I will let you know if this works out. Thanks much!
AlbertoPL