Friday, July 11, 2008

org.eclipse.ui.menus

Recently I’ve been guiding our off-shore team on moving our pop-up menu actions from using org.eclipse.ui.popupMenus extension point to a newer mechanism. Eclipse 3.3 provides a new mechanism, a lot easier to use, through the org.eclipse.ui.menus extension point. This extension point adds UI contributions to the main menu, main toolbars, view menus, view toolbars, context menus, etc. and replaces the following (legacy) extension points: org.eclipse.ui.ActionSets, org.eclipse.ui.EditorActions, org.eclipse.ui.popupMenus (including 'objectContributions') and org.eclipse.ui.viewActions.

Below is a summary of the steps for migrating to the new mechanism. Since there are a number of pop-up items, the migration converts one action at a time. Here are the steps:

1) Contribute a command using org.eclipse.ui.commands extension point.
2) Refactor the action delegate class to a handler delegate class.
3) Contribute the converted handler class using org.eclipse.ui.handlers extension point and specifying the command from step 1).
4) Then, add the command to your particular menu using org.eclipse.ui.menus. Here the trick is specifying the locationURI. For example: locationURI="menu:org.eclipse.ui.main.menu?after=additions" adds a command to the main menu and locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions" add a command to the main toolbar.

The org.eclipse.core.commands.ExecutionEvent in the new handler’s execute(ExecutionEvent) method can be passed to class org.eclipse.ui.handlers.HandlerUtil. HandlerUtil provides static methods for obtaining the context when the command is executed, such as the shell, active editors, parts, menus, etc.

After testing the conversion of the old actions to the new mechanism, the next step may be adding key bindings by extending org.eclipse.ui.bindings. Remember to specify a sequence that has not been bound already.

Another consideration is enablement, as some commands may not be available all the time. Method isEnabled() in the new handler may be overridden, but when this method is called it is not possible to obtain the active editor. In cases when the user enables or disables the action through a preference page, method isEnabled() may be used. An approach to enablement is as follows:
1) Create a property tester using org.eclipse.core.expressions.propertyTesters extension point
2) Add the clause enabledWhen in org.eclipse.ui.handlers extension point and test for the above property, for example:
... inside a given extension point

variable="selection">
value="*">


property="com.example.objetToTest.propertyName">



...
May be used for testing that a selection (like a resource) has a particular property, that is command may applied to the selection.


At this point, the conversion should have gone fine. I hope this post helps you as a reference.


Until next…

No comments: