DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Frameworks
  4. Disable or Enable ActionSets on Perspective Change in an Eclipse RCP Application

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

Dimitri Missoh user avatar by
Dimitri Missoh
·
Jan. 14, 09 · Interview
Like (0)
Save
Tweet
Share
80.67K Views

Join the DZone community and get the full member experience.

Join For Free

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/

Rich client platform application Eclipse

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Cloud Performance Engineering
  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • Fargate vs. Lambda: The Battle of the Future
  • 10 Easy Steps To Start Using Git and GitHub

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: