Search code examples
eclipseeclipse-pluginswteclipse-rcp

Eclipse RCP. How to make different ToolBarItems for different Perspectives


I am quite new to Eclipse RCP development. In my Eclipse RCP application there are different perspectives. I want them to have different ToolBarItems. According to the official documentation the contents of this toolbar change based on the active perspective. But after tons of googling I still have no idea how.

My best idea is as follows:

First I create the items and I add them to toolbar and coolbar

protected void fillCoolBar(ICoolBarManager coolBar) {
    IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);

    ActionContributionItem saveItem = new ActionContributionItem(saveAction);
    saveItem.setId(ApplicationActionBarAdvisor.SAVE);
    toolbar.add(saveItem);

    ActionContributionItem saveAllItem = new ActionContributionItem(saveAllAction);
    saveAllItem.setId("saveAllItem");
    toolbar.add(saveAllItem);

    coolBar.add(new ToolBarContributionItem(toolbar, "main"));
}

and then, in the Perspective class I override the createInitialLayout() method and I just hide the unnecessary item.

@Override
public void createInitialLayout(IPageLayout layout) {
    String editorArea = layout.getEditorArea();
    PageLayout pl = (PageLayout) layout;
    pl.addHiddenToolBarItemId(ApplicationActionBarAdvisor.SAVE);
    pl.setEditorAreaVisible(false); 
    pl.addStandaloneView(View.ID,  false, IPageLayout.LEFT, 0.25f, editorArea);
    pl.getViewLayout(View.ID).setCloseable(false);
}

It does not work, but I have no idea what I am missing. Any help is highly appreciated.


Solution

  • Use the org.eclipse.ui.perspectiveExtensions extension point and contriubte an actionSet to the perspective.

    For example, this is part of the JDT plugin:

     <extension
         point="org.eclipse.ui.perspectiveExtensions">
    
      <perspectiveExtension targetID="org.eclipse.debug.ui.DebugPerspective">
         <perspectiveShortcut id="org.eclipse.jdt.ui.JavaPerspective"/>
         <perspectiveShortcut id="org.eclipse.jdt.ui.JavaBrowsingPerspective"/>
         <actionSet id="org.eclipse.jdt.ui.JavaActionSet"/>
         <showInPart id="org.eclipse.jdt.ui.PackageExplorer"/>
      </perspectiveExtension>
    

    Note: Do not use internal classes such as PageLayout they may change at any time and in fact this class was in Eclipse 3 but has been deleted in Eclipse 4.