I wanted to hide/show menu bar in my WebAppRunner Eclipse application, depending on settings. I figured getting menu bar manager and calling setVisible method would do the job, but it was not sufficient.
Eclipse checks visibility of each menu manager in the menu bar and even if one of them is visible, it would not hide the menu bar after calling setVisible(false).
private void setMenubarVisible (boolean visible) { IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); IContributionItem[] items = ((WorkbenchWindow)workbenchWindow).getMenuBarManager().getItems(); for (IContributionItem item : items) { item.setVisible(visible); } ((WorkbenchWindow)workbenchWindow).getMenuBarManager().setVisible(visible); }
I really do not like typecasting to WorkbenchWindow in the above code. However I did not find any way to get menu manager from IWorkbenchWindow.
Also a quick tip about hiding toolbar in Eclipse 4.2 (Juno). Even if you do not contribute any toolbar buttons, you will notice that Juno creates the toolbar for ‘Quick Access’ box. I have hidden that in my application’s WorkbenchWindowAdvisor class as follows –
public void postWindowOpen() { try { IHandlerService service = (IHandlerService) PlatformUI .getWorkbench().getActiveWorkbenchWindow() .getService(IHandlerService.class); if (service != null) service.executeCommand("org.eclipse.ui.ToggleCoolbarAction", null); } catch (Exception e) { //handle error } }
-Ram Kulkarni
This was very helpful
Thanks for the “hiding the Quick Access box” sippet!
Hey … I cannot undo hiding the coolbar …I deleted the code but it is still gone. Can you help me?
Clear the workspace and try. Run eclipse with -clear option
Thanks but I allready tried that but the coolbar is still missing.
sorry… the workspace wasn’t cleaned… thank you
Casting to WorkbenchWindow is discouraged (now). Cast it to ApplicationWindow, instead. I agree, getMenuBarManager should be available on an interface somewhere.
Hiding menu is good way to remove unnecessary tools. You have provide information about hidden many and toolbars in this post which is very useful for me.