Skip to content

Creating a GEF Editor – Part 8: Delete, Undo and Redo

Last updated on 2014-09-05

Previous Tutorial: Creating a GEF Editor – Part 7: Moving Elements and Direct Editing

In this tutorial we are going to add functionality to delete diagram entities and also to undo and redo editing operations. This is going to be a short tutorial because thankfully most of the functionality that we want to add only has to be configured in the framework.

  1. Delete functionality is implemented like all other functionality: we have to add a new Command to change the model, create a new EditPolicy (or extend an existing one) which creates the Command and install the command in the desired EditPart.
  2. First we implement the command, which is fairly simple. In EMF, a model instance that is disconnected from all of the model’s resource is not saved and therefore will get deleted (much like java garbage collection):
    package com.vainolo.phd.opm.gef.editor.command;
    
    import org.eclipse.gef.commands.Command;
    
    import com.vainolo.phd.opm.model.OPMObjectProcessDiagram;
    import com.vainolo.phd.opm.model.OPMThing;
    
    public class OPMThingDeleteCommand extends Command {
    
    	private OPMThing thing;
    	private OPMObjectProcessDiagram opd;
    
    	@Override
    	public void execute() {
    		thing.setOpd(null);
    	}
    
    	@Override
    	public void undo() {
    		thing.setOpd(opd);
    	}
    
    	public void setThing(OPMThing thing) {
    		this.thing = thing;
    		this.opd = thing.getOpd();
    	}
    }
    
  3. Request to delete diagram entities in GEF is handled by a ComponentEditPolicy:
    package com.vainolo.phd.opm.gef.editor.policy;
    
    import org.eclipse.gef.commands.Command;
    import org.eclipse.gef.editpolicies.ComponentEditPolicy;
    import org.eclipse.gef.requests.GroupRequest;
    
    import com.vainolo.phd.opm.gef.editor.command.OPMThingDeleteCommand;
    import com.vainolo.phd.opm.model.OPMThing;
    
    public class OPMThingComponentEditPolicy extends ComponentEditPolicy {
    
    	@Override protected Command createDeleteCommand(GroupRequest deleteRequest) {
    		OPMThingDeleteCommand thingDeleteCommand = new OPMThingDeleteCommand();
    		thingDeleteCommand.setThing((OPMThing) getHost().getModel());
    		return thingDeleteCommand;
    	}
    }
    
  4. And to finish, we install the new EditPolicy in the OPMThingEditPart:

    	@Override protected void createEditPolicies() {
    		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new OPMThingDirectEditPolicy());
    		installEditPolicy(EditPolicy.COMPONENT_ROLE, new OPMThingComponentEditPolicy());		
    	}
    
  5. You could try now to execute your editor and delete a diagram entity… But no matter how hard you press your Del button, nothing will happen. You see, delete actions are not regular actions that are applied to the model, but must be activated from the outside (for example from a menu, toolbar or key press). So we’ll be adding the delete action to the menu and toolbar of the eclipse workbench, and while we are at it we’ll also add undo and redo functionality
  6. Eclipse editors that want to provide actions to the menus and toolbars must provide a class which implements the IEditorActionBarContributor interface. GEF provides us with a basic implementation of this interface for use with our GEF editor, called ActionBarContributor (the comment in this class says that it is subject to change… but so is everything else in life. It would be nice to know why they think this class is going to change in the future). Since the three operations we are implementing are pretty standard editor operations, the framework also provides us with RetargetAction classes that can be used in the menus and toolbars.
    package com.vainolo.phd.opm.gef.editor;
    
    import org.eclipse.gef.ui.actions.ActionBarContributor;
    import org.eclipse.gef.ui.actions.DeleteRetargetAction;
    import org.eclipse.gef.ui.actions.RedoRetargetAction;
    import org.eclipse.gef.ui.actions.UndoRetargetAction;
    import org.eclipse.jface.action.IToolBarManager;
    import org.eclipse.ui.actions.ActionFactory;
    
    public class OPMGraphicalEditorActionBarContributor extends ActionBarContributor {
    
    	@Override
    	protected void buildActions() {
    		addRetargetAction(new UndoRetargetAction());
    		addRetargetAction(new RedoRetargetAction());
    		addRetargetAction(new DeleteRetargetAction());
    	}
    	
    	@Override
    	public void contributeToToolBar(IToolBarManager toolBarManager) {
    		super.contributeToToolBar(toolBarManager);
    		toolBarManager.add(getAction(ActionFactory.UNDO.getId()));
    		toolBarManager.add(getAction(ActionFactory.REDO.getId()));
    		toolBarManager.add(getAction(ActionFactory.DELETE.getId()));
    	}
    
    	@Override
    	protected void declareGlobalActionKeys() {
    		// Do nothing. This is an abstract function in the parent class.
    	}
    }
    

    The buildActions initializes the actions that are provided by our editor (and also makes them available in the menus). After they are initialized, we can also add them to the toolbar using the contributeToToolBar method.

  7. Last thing we need to do is connect action contributor class we just created to the editor. This is done in the plugin definition file, in the same place we defined the model editor, setting our class as the OPM GEF Editor’s contributorClass:
  8. That’s it. Execute your eclipse project and mode some things around. The Undo action should be available right after you do this. Undo something and now the Redo action should be available. Select a thing and delete it. Works? great!

You can find the final project files here.

As usual, hope you enjoyed this tutorial. Next in line: implementing connections between things in the model. Don’t miss it :-).

Next Tutorial: Creating a GEF Editor – Part 9: Connections

Published inProgramming

Be First to Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.