Providing Move Functionality

Create a Move Feature

In our current diagram the move functionality is already provided by the framework. Nevertheless this tutorial should explain how the move behavior can be customized. Therefore we constructed the following example behavior:

Moving EClasses should be restricted to EClasses whose name is longer than one character (just an example without logical reason).

Another possibility would be to allow, that a EClass can be moved onto another EClass (which is not allowed in the default implementation). This does not make sense in our example, but in other scenarios this is a quite typical usage for a move feature.

For that purpose we need a special move feature which is used by the diagram if a move gesture (a drag) is received as interaction. A move feature has to implement the interface IMoveFeature or one of the more specialized interfaces. Instead of implementing them directly you should extend one of the available base classes. In this example we extend the base class DefaultMoveShapeFeature which implements the interface IMoveShapeFeature.

In this case we only have to overwrite/implement one method:

You can see the complete implementation of the move feature here:

 

package org.eclipse.graphiti.examples.tutorial.features;
 
public class TutorialMoveEClassFeature extends DefaultMoveShapeFeature {
 
    public TutorialMoveEClassFeature(IFeatureProvider fp) {
        super(fp);
    }
 
    @Override
    public boolean canMoveShape(IMoveShapeContext context) {
        boolean canMove = super.canMoveShape(context);
 
        // perform further check only if move allowed by default feature
        if (canMove) {
            // don't allow move if the class name has the length of 1
            Shape shape = context.getShape();
            Object bo = getBusinessObjectForPictogramElement(shape);
            if (bo instanceof EClass) {
                EClass c = (EClass) bo;
                if (c.getName() != null && c.getName().length() == 1) {
                    canMove = false;
                }
            }
        }
        return canMove;
    }
}

 

Additionally the feature provider has to deliver our newly created feature (overwrite the method getMoveShapeFeature.

This implementation can be seen here:

 

@Override
public IMoveShapeFeature getMoveShapeFeature(IMoveShapeContext context) {
    Shape shape = context.getShape();
    Object bo = getBusinessObjectForPictogramElement(shape);
    if (bo instanceof EClass) {
        return new TutorialMoveEClassFeature(this);
    }
    return super.getMoveShapeFeature(context);
}

 

Test: Move EClass Is Restricted

Now start the editor again and test it: Just create a EClass whose name is only one character long. Moving this EClass should not be possible. Create another EClass whose name is longer than one character. Moving this EClass must still be possible.