.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

How to customize the Tool bar Eclipse RCP


In a standard way, creating an eclipse RCP application have to include creating these files:

  • Activator
  • Application
  • WorkbenchWindowAdvisor
  • ActionBarAdvisor
  • WorkbenchAdvisor
  • As for me, you should hide all the default actions and commands.
    You can do that in the file WorkbenchWindowAdvisor which contains the postWindowOpen() function:

    public void postWindowOpen()
        {
            super.postWindowOpen();
            final String[] removeActionSets = new String[] { "org.eclipse.search.searchActionSet",
                "org.eclipse.ui.cheatsheets.actionSet", "org.eclipse.ui.actionSet.keyBindings",
                "org.eclipse.ui.edit.text.actionSet.navigation", "org.eclipse.ui.edit.text.actionSet.annotationNavigation",
                "org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo",
                "org.eclipse.ui.edit.text.actionSet.openExternalFile", "org.eclipse.ui.externaltools.ExternalToolsSet",
                "org.eclipse.ui.WorkingSetActionSet", "org.eclipse.update.ui.softwareUpdates",
                "org.eclipse.ui.actionSet.openFiles", "org.eclipse.mylyn.tasks.ui.navigation", };
            
            IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
            for (int i = 0; i < windows.length; ++i)
            {
                IWorkbenchPage page = windows[i].getActivePage();
                if (page != null)
                {
                    for (int j = 0; j < removeActionSets.length; j++)
                    {
                        page.hideActionSet(removeActionSets[j]);
                    }
                }
            }
        }
    


    After this, your application run with nothing on the menu bar and action bar.
    Next we do an example of adding SAVE action to the tool bar. This is certainly done with the ActionBarAdvisor file.
    Firstly, we define an action named saveAction:

    /** The save action. */
    private IWorkbenchAction saveAction;
    

    Secondly, in the makeActions function, we instance it.

    @Override
    protected void makeActions(final IWorkbenchWindow window)
    {
         this.saveAction = ActionFactory.SAVE.create(window);
            this.register(this.saveAction);
    }
    

    because, SAVE action is an default action supported by the workbench, so it is so easy to create it simply. With your other actions, you should create it as an implementation of IAction, and declare it at the makeActions too.
    Lastly, the final thing you have to do is add it into the cool bar via the fillCoolBar function:

        @Override
        protected void fillCoolBar(final ICoolBarManager coolBar)
        {
            IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
            coolBar.add(new ToolBarContributionItem(toolbar, "main"));
            toolbar.add(this.saveAction);
        }
    

    As you can see in the above snippet, you have to create an toolbar which wraps the action inside. Then, you add the toolbar into the cool bar, and then add actions in to the tool bar.
    As the the resume, you can figure out the structure of the cool bar like:
    COOL BAR (contains many) TOOL BAR and
    TOOL BAR (contains many) ACTIONS

    Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: