Hiding menu and tool bars in Eclipse RCP Application

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

8 Replies to “Hiding menu and tool bars in Eclipse RCP Application”

  1. Casting to WorkbenchWindow is discouraged (now). Cast it to ApplicationWindow, instead. I agree, getMenuBarManager should be available on an interface somewhere.

Leave a Reply

Social