Providing Add Connection Functionality

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:

You can see the complete implementation of the add connection feature here:

package org.eclipse.graphiti.examples.tutorial.features;
 
public class TutorialAddEReferenceFeature extends AbstractAddFeature {

    private static final IColorConstant E_REFERENCE_FOREGROUND = new ColorConstant(98, 131, 167);
 
    public TutorialAddEReferenceFeature (IFeatureProvider fp) {
        super(fp);
    }
 
    public PictogramElement add(IAddContext context) {
        IAddConnectionContext addConContext = (IAddConnectionContext) context;
        EReference addedEReference = (EReference) context.getNewObject();
        IPeCreateService peCreateService = Graphiti.getPeCreateService();
       
        // CONNECTION WITH POLYLINE
        Connection connection = peCreateService
            .createFreeFormConnection(getDiagram());
        connection.setStart(addConContext.getSourceAnchor());
        connection.setEnd(addConContext.getTargetAnchor());
 
        IGaService gaService = Graphiti.getGaService();
        Polyline polyline = gaService.createPolyline(connection);
        polyline.setLineWidth(2);
        polyline.setForeground(manageColor(E_REFERENCE_FOREGROUND));
 
        // 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 (extend the existing method getAddFeature() with the else-part below).

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.