It is common for a plug-in's views and editors to implement actions that are semantically similar to existing workbench actions, such as clipboard cut/copy/paste, view refresh, or properties. The popup menu for views and editors can become quite cluttered if every view or editor has to define unique actions for these operations and include them in their menus.
To solve this problem, the workbench defines retargetable (also called global) actions that can be handled by any view or editor. When a view or editor is active, its handler will be run when the user chooses the action from the workbench menu or toolbar. This allows views and editors to share workbench menu space for semantically similar actions.
IWorkbenchActionConstants documents all of the workbench actions and denotes retargetable actions as global. For example, here is the definition of the Properties action.
public static final String PROPERTIES = "properties"; // Global action.
The following table summarizes some of the more common retargetable actions that are implemented by views and editors:
File menu | Edit menu | Navigate menu | Project menu | |
views | move rename refresh properties |
go into go to resource sync with editor back forward up next previous |
open close build rebuild |
|
editors | revert |
find | ||
views and editors | cut copy paste delete select all undo redo |
Retargetable actions are created using RetargetAction. The following snippet is from WorkbenchActionBuilder.
propertiesAction = createGlobalAction(IWorkbenchActionConstants.PROPERTIES, "file", false);
The createGlobalAction method shows us exactly how to make a RetargetAction.
private RetargetAction createGlobalAction(String id, String actionDefPrefix, boolean labelRetarget) { RetargetAction action; if (labelRetarget) { action = new LabelRetargetAction(id, WorkbenchMessages.getString("Workbench." + id)); } else { action = new RetargetAction(id, WorkbenchMessages.getString("Workbench." + id)); } ... return action; }
When creating a retargetable action, the workbench assigns the id for the action and the default label. Note that there are two styles of retarget actions. RetargetAction simply allows a view or editor to reimplement an action. LabelRetargetAction also allows views and editors to reset the label of the action. This is useful for making the menu label more specific, such as relabeling an Undo action as Undo Typing.
Now we know how the retarget actions are defined by the workbench. Let's look next at how your view or editor can provide an implementation for a retargetable action. This is done by setting a global action handler.