I will use one simple example to explain how to add context menu into
editor dynamically. "Dynamically" means the menu items will change
according to the selected object.
There are three steps:
1. Define the action. Please read the following example code:
/**
* What I want to realize is to provide a "copy" menu item in popup menu
* whenever the user selects the object "Example".
*/
public class CopyExampleAction extends org.eclipse.gef.ui.actions.SelectionAction
{
private static final String
COPY_REQUEST = "COPY"; //$NON-NLS-1$
public static final String
COPY = "COPY"; //$NON-NLS-1$
Request request;
public CopyExampleAction(IWorkbenchPart part) {
super(part);
request = new Request(COPY_REQUEST);
setText("copy Example");
setId(COPY);
setImageDescriptor(
ImageDescriptor.createFromFile(ExamplePlugin.class,"icons/copy.gif"));
//$NON-NLS-1$
setHoverImageDescriptor(getImageDescriptor());
}
protected boolean calculateEnabled() {
return canPerformAction();
}
private boolean canPerformAction() {
if (getSelectedObjects().isEmpty())
return false;
List parts = getSelectedObjects();
for (int i=0; i<parts.size(); i++){
Object o = parts.get(i);
if (!(o instanceof EditPart))
return false;
EditPart part = (EditPart)o;
// This
menu item will only be activated if an object of Typ "Example" is
selected.
if (!(part.getModel() instanceof Example))
return false;
}
}
return true;
}
// What happened for this action is defined by the edit part of this object.
private Command getCommand() {
List editparts = getSelectedObjects();
CompoundCommand cc = new CompoundCommand();
cc.setDebugLabel("Copy Example");//$NON-NLS-1$
for (int i=0; i < editparts.size(); i++) {
EditPart part = (EditPart)editparts.get(i);
cc.add(part.getCommand(request));
}
return cc;
}
public void run() {
execute(getCommand());
}
}
2. Implement the action. In this
step you will decide what will exactly happen after the user clicked
this menu item. As you have already seen from the code in the step 1,
the command for this action is defined in edit part for the object
"Example".
So the next you will do is to complete the ExampleEditPolicy for ExampleEditPart.
public class ExampleEditPolicy extends ComponentEditPolicy{
private static final String
COPY_REQUEST = "COPY", //$NON-NLS-1$
public Command getCommand(Request request) {
if (COPY_REQUEST.equals(request.getType()))
return getCopyExampleCommand();
return super.getCommand(request);
}
protected Command getCopyExampleCommand(){
// The implementation of CopyExampleCommand is simple, will not be explained here.
CopyExampleCommand command = new CopyExampleCommand();
command.setCopyObject((Example)getHost().getModel());
return command;
}
}
3. Add the action into Context Menu.
There is always a class to provider the context menu, the next step is to add this defined action into the context menu.
public class ExampleContextMenuProvider extends ContextMenuProvider {
private ActionRegistry actionRegistry;
public ExampleContextMenuProvider(EditPartViewer viewer, ActionRegistry registry) {
super(viewer);
setActionRegistry(registry);
}
public void buildContextMenu(IMenuManager manager) {
GEFActionConstants.addStandardActionGroups(manager);
IAction action;
action = getActionRegistry().getAction(CopyExampleAction.COPY);
if (action.isEnabled())
manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
}
private ActionRegistry getActionRegistry() {
return actionRegistry;
}
private void setActionRegistry(ActionRegistry registry) {
actionRegistry = registry;
}
}
4. Add the action into editor. There is a method "createActions" in editor. Add the action in it. That's all.
protected void createActions() {
super.createActions();
ActionRegistry registry = getActionRegistry();
IAction action;
action = new CopyExampleAction(this);
registry.registerAction(action);
getSelectionActions().add(action.getId());
}