Last updated on 2014-09-05
Previous tutorial: Creating an OPM GEF Editor – Part 21: Adding Keyboard Shortcuts
Today I was working on a model using my OPM GEF editor, and wanted to select all elements of the model… but for some strange reason the “Select All” action in the “Edit” menu was disabled… So I started to investigate.
First, the “Select All” action should be similar to other eclipse standard actions, like “Undo” and “Delete”, so I expected some treatment similar to these actions – a SelectAllRetargetAction
or something similar. There is a SelectAllAction
class in the GEF framework, but I was not sure how to plug it in. First, I saw that this action was already registered in the actionRegistry
in GraphicalEditor.createActions()
function. So why was it disabled? More code reading showed me that also the undo and redo actions are registered there, but I also had to create for them a retargetable action in the ActionBarContributor
of my editor. So I added this code to the OPMGraphicalEditorActionBarContributor.buildActions()
method (the last line):
@Override protected void buildActions() { addRetargetAction(new UndoRetargetAction()); addRetargetAction(new RedoRetargetAction()); addRetargetAction(new DeleteRetargetAction()); addRetargetAction(new RetargetAction(GEFActionConstants.TOGGLE_GRID_VISIBILITY, GEFMessages.ToggleGrid_Label, IAction.AS_CHECK_BOX)); addRetargetAction(new RetargetAction(GEFActionConstants.TOGGLE_SNAP_TO_GEOMETRY, GEFMessages.ToggleSnapToGeometry_Label, IAction.AS_CHECK_BOX)); addRetargetAction(new RetargetAction(GEFActionConstants.SELECT_ALL, GEFMessages.SelectAllAction_Label)); }
The “Select All” menu item became available and the action worked, even with the expected “Ctrl+A” keyboard accelerator. But the editor showed me that the GEFActionConstants.SELECT_ALL
constant is deprecated, and that I should use the ActionFactory.SELECT_ALL.getId()
… I scratched my head a bit… I had a new lead on my problem, but where to search for clues? Obviously, the org.eclipse.gef.examples.logic
project! And there it was: the ActionBarContributor.declareGlobalActionKeys()
function calls addGlobalActionKey(ActionFactory.SELECT_ALL.getId())
. In two minutes I removed my old line and added this one to my contributor class, and there it was again, the “Select All” action worked, and the code was clean of warnings.
Seems strange to me that although the action was already registered, only when I added here the action became available. The name of the function (and it’s location) is also confusing: why the Bar
in ActionBarContributor
? And another nice thing in the code of this class is the warning in the class comment:
/** * Contributes actions to the workbench. !!Warning: This class is subject to * change. */
But the “Select All” action works, and that is what matters.
Next tutorial: Creating an OPM GEF Editor – Part 23: Drag & Drop from the Palette
It is also possible to use:
addRetargetAction(new RetargetAction(ActionFactory.SELECT_ALL.getId(),
GEFMessages.SelectAllAction_Label));
However I don’t even know the difference between RetargetAction and Action
A
RetargetAction
is an action that re-targets a typical global workbench action, like cut/paste. You can read more about them here:http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/wrkAdv_retarget_setting.htm