Eclipse RCP – Customize the toolbar and menus for a given Perspective: contributions using java code

In my previous post I talked about the need to customize the Eclipse toolbar and menus depending on the current perspective.
This post is about customizing buttons and menus contributed in java.

I will show in this post how to perform the customization for the toolbar buttons depending on the current perspective. The customization for the menu entries can be done just the same.


In a standard Eclipse RCP you should have these classes:
Application, WorkbenchAdvisor, WorkbenchWindowAdvisor, ActionBarAdvisor.

Normally you create your toolbar buttons in the ActionBarAdvisor#fillCoolBar(ICoolBarManager toolBar) method.
Here you have lines like:

 IToolBarManager toolBarX = new ToolBarManager();
 toolBarX.add(myAction);
 ...
toolBar.add(new ToolBarContributionItem(toolBarX,
					IWorkbenchActionConstants.TOOLBAR_FILE));

where myAction is a custom implementation, implementing IAction interface.

In order to decide weather to show or not a toolbar item ( in our case depending on the current perspective), we will do the following:

1. Implement a MyActionContributionItem class by extending the ActionContributionItem class
The ActionContributionItem provides a method isVisible() that we will override.
Here is mine:

public class WorkflowActionContributionItem extends ActionContributionItem {
	public WorkflowActionContributionItem(IAction action) {
		super(action);
	}

	@Override
	public boolean isVisible()
	{
		IWorkbenchWindow workbenchWindow =  PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (workbenchWindow==null)
			return false;
		IWorkbenchPage activePage = workbenchWindow.getActivePage();
		if (activePage==null)
			return false;
		if (activePage.getPerspective().getId().contains("com.activeeon.workflow.diagram.SchedDevPerspective"))
			return true;
		return false;
	}
}

2. Use the custom contribution Item when adding contribution to the toolbar

The code in ActionBarAdvisor#fillCoolBar(ICoolBarManager toolBar) method becomes:

 IToolBarManager toolBarX = new ToolBarManager();
 toolBarX.add(<b>new WorkflowActionContributionItem(myAction)</b>);
 ...
toolBar.add(new ToolBarContributionItem(toolBarX,
					IWorkbenchActionConstants.TOOLBAR_FILE));

I will use my WorkflowActionContributionItem to contribute all the actions that I want to selectively show.

3. Force Toolbar refresh on perspective change
Well, at beginning I thought the above solution should be enough for our customization. For some reason, on perspective change, the toolbar is not refreshed (actually it is, but with a “force” argument set to false, and, for some reason, my buttons are not refreshed). So we will add a listener for perspective change event to the workbench windows, and will force the refresh of the toolbar.

3.A.Implement a PerspectiveAdapter (a listener for perspective change event)

Here is mine:

public class WorkflowStudioPerspectiveAdapter extends PerspectiveAdapter {

....
....

@Override
	public void perspectiveActivated(IWorkbenchPage page,
			IPerspectiveDescriptor perspectiveDescriptor) {
		super.perspectiveActivated(page, perspectiveDescriptor);
		
		// --- Update main menu and cool bar
		IWorkbenchWindow workbenchWindow =  PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (workbenchWindow instanceof WorkbenchWindow)
		{
			((WorkbenchWindow)workbenchWindow).getMenuBarManager().update(true);
			((WorkbenchWindow)workbenchWindow).getCoolBarManager2().update(true);
		}
}

Subscribe the listener to the workbench window
In the DiagramEditorWorkbenchAdvisor add (or modify the existent) postStartup() method:


        @Override
	public void postStartup()
	{ 
		IWorkbenchWindow
		workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                WorkflowStudioPerspectiveAdapter perspectiveListener = new WorkflowStudioPerspectiveAdapter();
                workbenchWindow.addPerspectiveListener(perspectiveListener);
         }  

This is it. When I change the perspective, the isVisible() method is called for each of my toolbar contributions.

About these ads
This entry was posted in Eclipse RCP. Bookmark the permalink.

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