Usually a new business object is created graphically by using the corresponding creation tool from the diagram’s tool palette. When using Graphiti you only have to provide a so called create feature. The integration into the platform’s UI is done by the framework.
A create feature has to implement the interface ICreateFeature.Instead of implementing it directly it should extend one of the available base classes. In this example we extend the base class AbstractCreateFeature.
In this case we have to implement/overwrite two methods:
In this example we want create a EClass via the palette. The creation should be possible anywhere on the diagram.
You can see the complete implementation of the create feature here:
package org.eclipse.graphiti.examples.tutorial.features;
public class TutorialCreateEClassFeature
extends AbstractCreateFeature {
private static final String
TITLE = "Create
class";
private
static final String USER_QUESTION
= "Enter new class name";
public TutorialCreateEClassFeature(IFeatureProvider
fp) {
// set name and description of the creation feature
super(fp, "EClass", "Create EClass");
}
public boolean
canCreate(ICreateContext context) {
return context.getTargetContainer()
instanceof Diagram;
}
public Object[] create(ICreateContext context)
{
//
ask user for EClass name
String newClassName = ExampleUtil.askString(TITLE,
USER_QUESTION,
"");
if (newClassName == null || newClassName.trim().length()
== 0) {
return EMPTY;
}
// create EClass
EClass newClass = EcoreFactory.eINSTANCE.createEClass();
// Add model element to resource.
// We add the model element to the resource of the diagram for
// simplicity's sake. Normally, a customer would use its own
// model persistence layer for storing the business model separately.
getDiagram().eResource().getContents().add(newClass);
newClass.setName(newClassName);
// do the add
addGraphicalRepresentation(context, newClass);
// return newly created business object(s)
return new Object[] { newClass };
}
}
Additionally the feature provider has to deliver our newly created feature (overwrite the method getCreateFeatures).
This implementation can be seen here:
@Override
public ICreateFeature[]
getCreateFeatures() {
return new
ICreateFeature[] { new TutorialCreateEClassFeature(this)
};
}
Now start the editor again. "EClass" should be available in the palette of the diagram. It should be possible to create a EClass: Select the palette entry "EClass" and then take the mouse and create size and location of the new object in the diagram. Insert a name and press OK. The new EClass should be visible in the diagram.
As you can see the size of the created EClass is not correct. To correct this it’s necessary to update the implementation of the add feature. The add method of the add feature must use the given size and location.
The implementation of the changed add-method can be seen here:
public PictogramElement add(IAddContext
context) {
final EClass addedClass = (EClass)
context.getNewObject();
final Diagram
targetDiagram = (Diagram) context.getTargetContainer();
// CONTAINER SHAPE WITH ROUNDED RECTANGLE
final IPeCreateService peCreateService = Graphiti.getPeCreateService();
final ContainerShape containerShape =
peCreateService.createContainerShape(targetDiagram,
true);
// check whether the context has a size (e.g. from
a create feature)
// otherwise define
a default size for the shape
final int width = context.getWidth() <= 0 ? 100 :
context.getWidth();
final int height = context.getHeight() <= 0 ? 50
: context.getHeight();
// ... EXISTING CODING COMES HERE ...
return containerShape;
}
Start the editor again and see if it works correctly.