Eclipse Zone is brought to you in partnership with:

Dimitri has posted 2 posts at DZone. View Full User Profile

Disable or Enable ActionSets on Perspective Change in an Eclipse RCP Application

01.14.2009
| 21563 views |
  • submit to reddit

Here is the challenge: I would like to enable or disable some action-sets according to the perspective the user activates. This should be done through a customization plug-in, because I'm not allowed to directly modify the code of the RCP application. I can only contribute my modifications through a new plug-in. Let's walk through the necessary implementation steps.

First we need to register our plug-in as a perspective change listener. The second step will be to find a way to programmatically enable or disable a given action-set according to the new perspective. Since I cannot modify the code of the existent application, I will create a new plug-in and extend the early start-up extension point org.eclipse.ui.startup to be able to run some code when the workbench starts. This extension point requires a class that implements the org.eclipse.ui.IStartup interface. The following snippet shows how the plug-in descriptor file plugin.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension
point="org.eclipse.ui.startup">
<startup
class="perspectiveactionsets.startup.EarlyStartup"></startup>
</extension>
</plugin>

To be notified on a perspective change event, a org.eclipse.ui.IPerspectiveListener will be registered to the active window of the workbench IWorkbenchWindow in the form of an anonymous class. I use the org.eclipse.ui.PerspectiveAdapter which provides default implementations for the methods described by the IPerspectiveListener interface. There is a very important point we should not forget when trying to retrieve the active workbench window (IWorkbenchWindow) using the static call PlatformUI.getWorkbench().getActiveWorkbenchWindow(). Let's seek the javadoc for the getActiveWorkbenchwindow() method of the WorkbenchWindow class:

/**
* Returns the currently active window for this workbench (if any). Returns
* null if there is no active workbench window. Returns
* null if called from a non-UI thread.
*
* @return the active workbench window, or null if there is
* no active workbench window or if called from a non-UI thread
*/
public IWorkbenchWindow getActiveWorkbenchWindow();
That is, we have to retrieve the active workbench window within the UI thread. Here is approximately how the earlyStartup() looks like:
/* (non-Javadoc)
* @see org.eclipse.ui.IStartup#earlyStartup()
*/
public void earlyStartup() {
/*
* The registration of the listener should have been done in the UI thread
* since PlatformUI.getWorkbench().getActiveWorkbenchWindow() returns null
* if it is called outside of the UI thread.
* */
Display.getDefault().asyncExec(new Runnable() {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
final IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (workbenchWindow != null) {
workbenchWindow.addPerspectiveListener(new PerspectiveAdapter() {
/* (non-Javadoc)
* @see org.eclipse.ui.PerspectiveAdapter#perspectiveActivated(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor)
*/
@Override
public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspectiveDescriptor) {
super.perspectiveActivated(page, perspectiveDescriptor);
// TODO implement the task to execute when the perspective change
}
});
}
}
});
}

Now let us face the second problem. I would like to disable or enable some action-sets on perspective change. In the eclipse IDE, you can customize a given perspective using the right mouse click on the corresponding perspective icons (see the screenshot below).

Customize a perspective using the context menu on the correpsonding icon

This opens a dialog where you can select the command groups (e.g. action-sets) that should be disabled in the workbench as shown in the following screenshot.

Disable command groups with the Customize Perspective Dialog

If we can manually customize it, it should also be possible to achieve this task with some lines of code. Let us consult the best eclipse book ever, i.e. the eclipse code itself. A quick search reveals that the problem has been solved in the class org.eclipse.ui.internal.dialogs.CustomizePerspectiveDialog. Having a look at the okPressed() in this class gives us the solution: a method turnOnActionSets() can be used on the perspective object to enable or disable an action-set. After some modifications, the earlyStartup() method has now the form:

/* (non-Javadoc)
* @see org.eclipse.ui.IStartup#earlyStartup()
*/
public void earlyStartup() {
/*
* The registration of the listener should have been done in the UI thread
* since PlatformUI.getWorkbench().getActiveWorkbenchWindow() returns null
* if it is called outside of the UI thread.
* */
Display.getDefault().asyncExec(new Runnable() {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
final IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (workbenchWindow != null) {
workbenchWindow.addPerspectiveListener(new PerspectiveAdapter() {
/* (non-Javadoc)
* @see org.eclipse.ui.PerspectiveAdapter#perspectiveActivated(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor)
*/
@Override
public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspectiveDescriptor) {
super.perspectiveActivated(page, perspectiveDescriptor);
if (perspectiveDescriptor.getId().indexOf("org.eclipse.debug.ui.DebugPerspective") > -1) {
if (workbenchWindow.getActivePage() instanceof WorkbenchPage) {
WorkbenchPage worbenchPage = (WorkbenchPage) workbenchWindow.getActivePage();
// Get the perspective
Perspective perspective = worbenchPage.findPerspective(perspectiveDescriptor);
ArrayList toRemove = new ArrayList();
if (perspective != null) {
for (IActionSetDescriptor actionSetDescriptor : perspective.getAlwaysOnActionSets()) {
if (actionSetDescriptor.getId().indexOf("org.eclipse.search.searchActionSet") > -1) {
// Add the action set descriptor to the list of the action sets to remove
toRemove.add(actionSetDescriptor);
}
}
perspective.turnOffActionSets((IActionSetDescriptor[]) toRemove.toArray(new IActionSetDescriptor[toRemove.size()]));
}
}
}
}
});
}
}
});
}

P.S.: In the example above, all search related actions (denoted by the ID 'org.eclipse.search.searchActionSet') will disappear if you set the debug perspective (denoted by the ID 'org.eclipse.debug.ui.DebugPerspective') as the active one. For more details check out the source code of the project PerspectiveActionSets from the google code repository http://homeworks.googlecode.com/svn/trunk/ or download the plug-in project under the link below.

Download Projects Files.

 

See more blog entries on http://swarmy.free.fr/wordpress/

Published at DZone with permission of its author, Dimitri Missoh.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)