Alexey Romanov
Alexey Romanov

Reputation: 170815

Add dropdown menu in a view's ToolBarManager

I want to add several dynamically created actions to a view. This works to add them to the View Menu in the top right corner:

private void fillActionBars() {
  IActionBars bars = getViewSite().getActionBars();
  IMenuManager manager = bars.getMenuManager();
  IMenuManager myMenu = new MenuManager("Menu title", MY_MENU_ID);
  // add actions to myMenu
  manager.add(myMenu);
  bars.updateActionBars();
}

This works fine. However, I want to add the actions to a dropdown menu in the toolbar instead (so the user can see them immediately). If I replace the third line with

IToolbarManager manager = bars.getToolBarManager();

the menu doesn't show up.

Upvotes: 3

Views: 5575

Answers (2)

Max
Max

Reputation: 2907

You need to use IToolbarManager.add(IContributionItem) with a class that implements IContributionItem. See org.eclipse.ui.internal.FastViewBarContextMenuContribution as an example.

Upvotes: 0

JP Moresmau
JP Moresmau

Reputation: 7403

You're right, this doesn't work. A workaround that works fine, not using a MenuManager but a drop down action and a menu creator:

IActionBars bars = getViewSite().getActionBars();
IToolbarManager manager = bars.getToolBarManager();
Action act=new Action("Menu title",SWT.DROP_DOWN){};
act.setMenuCreator(new MyMenuCreator());
manager.add(act);

class MyMenuCreator implements IMenuCreator{
    public Menu getMenu(Control ctrl){
     ...
    }
}

Upvotes: 2

Related Questions