Implementation of the Controller.

In GEF EditParts play the role of the Controllers.

Each model element shown in the editor has a corresponding EditPart. An EditPart maps a model element to a graphical representation (IFigure from Draw2D) and provides a number of edit policies, that determine how this model element can be modified.

The Shapes-Example uses three kinds of EditParts:

Subclasses of EditPartFactory are responsible for mapping model elements to EditParts.

How are new model elements created?

To create a new model elements several classes of your application work together:

  1. Uppon a mouse-click, the selected tool in the palette (see ShapesEditorPaletteFactory#createComponentsGroup()) issues a request to the EditPart (for example a CreateRequest) under the mouse pointer.
  2. The EditPart might process the request directly or forward the request to an EditPolicy. The request is processed and if the preconditions are met, it is answered by a command (for example a ShapeCreateCommand), otherwise null is returned (for an example see getCreateCommand(CreateRequest) in DiagramEditPart.ShapesXYLayoutEditPolicy).
  3. If the command can execute (depending on the return value of the canExecute() method), the execute() method will be called, which is responsible for adding a new model element instance into the model. As you can observe in ShapeCreateCommand, in our case there is no need to instantiate a new model element directly. This has already been done by the Factory associated with selected tool (e.g. the CombinedTemplateSelectionEntry, see code snippet 1). Therefore we can just retrieve the new instance from the creation request and put in into the model (see code snippet 2).
  4. When the Shape is added into the model (redo() method in snippet 2), a CHILD_ADDED_PROP property change event is fired by the ShapesDiagram instance (parent field in snippet 2). This property change event, will cause the DiagramEditPart to refresh its children (see DiagramEditPart#propertyChange(PropertyChangeEvent)) and thus the new shape will appear in the editor.
// snippet 1, taken from ShapesEditorPaletteFactory#createToolsGroup()
CombinedTemplateCreationEntry component = new CombinedTemplateCreationEntry(
   "Ellipse",
   "Create an elliptical shape",
   EllipticalShape.class,
   new SimpleFactory(EllipticalShape.class), // <-- responsible for creating a new instance
   ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/ellipse16.gif"),
   ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/ellipse24.gif"));

 

// snippet 2, taken from ShapeCreateCommand
public void execute() {
   newShape = (Shape) request.getNewObject();
   newShape.setSize(request.getSize()); // might be null!
   newShape.setLocation(request.getLocation());
   redo();
}
public void redo() {
   childAdded = parent.addChild(newShape);
}

More...

For detailed information on EditParts, EditPolicies, Request and many other classes mentioned here, please consult the GEF API-Reference.