Monday, February 2, 2009

Eclipse RCP: Removing unwanted actionSets from menubar

Eclipse provides some default actionSets when you include the plugins like org.eclipse.ui. In many cases, the application does not need these actions.
So how do we remove these actions from the menubar and toolbar? Well, there is this ugly code which removes the unwanted action sets.


final ActionSetRegistry reg = WorkbenchPlugin.getDefault().getActionSetRegistry();
final IActionSetDescriptor[] actionSets = reg.getActionSets();
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", };

for (int i = 0; i < actionSets.length; i++) {
boolean found = false;
for (int j = 0; j <; removeActionSets.length; j++) { if(removeActionSets[j].equals(actionSets[i].getId())) { found= true; } } if(!found) { continue; } final IExtension ext = actionSets[i].getConfigurationElement().getDeclaringExtension(); reg.removeExtension(ext, new Object[] { actionSets[i] }); }

But this code needs to access restricted API from eclipse internal packages. When the eclipse activities are introduced the above discouraged can be declaratively done inside the plugin.xml file. If you wanted to remove the search menu from the menubar, define an activity and use the pattern org.eclipse.search.* as the activity pattern as below.




<extension point="org.eclipse.ui.activities">
<activity id="com.sequenom.ivd.actitivities.unwantedActionSet1" name="Unwanted Search ActionSet">
<enabledwhen>
<with variable="activePartId">
<equals value="com.sequenom.ivd.actitivities.unwantedActionSet">
</equals>
</with></enabledwhen></activity></extension>

<activitypatternbinding activityid="com.sequenom.ivd.actitivities.unwantedActionSet1" pattern="org.eclipse.search.*">
</activitypatternbinding>

This will remove the search menu from your application. Have fun !

3 comments:

Binu VM said...

This particular code where I can put?Is it inside the Activator class?
could you please explain?

KSG said...

You can put it inside the fillMenubar (override this)method of your application's ActionBarAdvisor class.

Scubba said...

Fantastic! I wish I had seen this 3 years ago.