In a previous chapter it was explained how an add feature is used to create graphical representations (in this case shapes with rectangles) for existing business-objects (EClasses).
In this chapter the same functionality shall be provided for connections. Concretely, an add feature for connections shall be implemented, which creates graphical representations (connections with polylines) for existing business objects (EReferences).
An add connection feature has to implement the interface IAddFeature. Instead of implementing it directly it should extend one of the available base classes. In this example we extend the base class AbstractAddFeature.
In this case we have to implement/overwrite two methods:
The method canAdd has to check the given context and therefore it decides if a business object can be added.
The method add finally has to create the pictogram element (connection) and has to establish the linkage to the business object. Additionally we have to create the graphics algorithm (polyline).
You can see the complete implementation of the add connection feature here:
package org.eclipse.graphiti.examples.tutorial.features;
public class TutorialAddEReferenceFeature extends AbstractAddFeature {
public TutorialAddEReferenceFeature (IFeatureProvider fp) { super(fp); }
public PictogramElement add(IAddContext context) { IAddConnectionContext addConContext = (IAddConnectionContext) context;
EReference
addedEReference = (EReference) context.getNewObject();
// CONNECTION WITH POLYLINE
Connection connection =
peCreateService connection.setStart(addConContext.getSourceAnchor()); connection.setEnd(addConContext.getTargetAnchor());
IGaService gaService = Graphiti.getGaService(); Polyline polyline = gaService.createPolyline(connection); polyline.setLineWidth(2); polyline.setForeground(manageColor(IColorConstant.BLACK));
// create link and wire it link(connection, addedEReference);
return connection; }
public boolean canAdd(IAddContext context) { // return true if given business object is an EReference // note, that the context must be an instance of IAddConnectionContext if (context instanceof IAddConnectionContext && context.getNewObject() instanceof EReference) { return true; } return false; } }
|
Additionally the feature provider has to deliver our newly created feature (overwrite the method getAddFeature).
This implementation can be seen here:
@Override public IAddFeature getAddFeature(IAddContext context) { // is object for add request a EClass or EReference? if (context.getNewObject() instanceof EClass) { return new TutorialAddEClassFeature(this); } else if (context.getNewObject() instanceof EReference) { return new TutorialAddEReferenceFeature(this); } return super.getAddFeature(context); }
|
Before this new add connection feature can be tested, we have to provide create connection functionality as described in the next chapter.