Disable or Enable ActionSets on Perspective Change in an Eclipse RCP Application
Join the DZone community and get the full member experience.
Join For Freehere 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:
/**that is, we have to retrieve the active workbench window within the ui thread. here is approximately how the
* 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, ornull
if there is
* no active workbench window or if called from a non-ui thread
*/
public iworkbenchwindow getactiveworkbenchwindow();
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).
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.
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.
see more blog entries on http://swarmy.free.fr/wordpress/
Opinions expressed by DZone contributors are their own.
Comments