Often it is useful to activate a direct editing feature directly after object creation. For example after creating a class the text-editor is opened and the user can immediately change the class name.
It is very simple to invoke the direct editing feature automatically, but it depends on the implementation of the shape how to do this.
Prerequisite: the shape of the EClass is implemented by features, and we already implemented a direct editing feature, which can edit the name of the EClass.
As an example we want to activate this direct editing of the name after creating a EClass.
At creation time of a new shape different features are called: create feature, add feature and update feature.
For the automatic switch into the direct editing mode we need to collect some information in those features and store it in the interface IDirectEditingInfo. All features have access to this direct editing info calling getFeatureProvider().getDirectEditingInfo().
In the create feature the automatic direct editing for this creation process has to be activated. This has to be done at the end of the create method.
Note, that previously in this tutorial we implemented the create method in a way, that at the beginning the user is asked for the class name in a popup-dialog. This becomes obsolete now and has to be deleted.
You can see the new create method here:
public Object[] create(ICreateContext
context) {
// create new 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);
// do the add
addGraphicalRepresentation(context,
newClass);
// activate
direct editing after object creation
getFeatureProvider().getDirectEditingInfo().setActive(true);
// return newly created business object(s)
return new Object[] { newClass };
}
In the add feature the outer container shape of the newly created object must be set (the main pictogram element of the EClass). Additionally the shape (pictogram element) and its graphics algorithm have to be specified, where the direct editing editor shall be opened.
You have to do this at the end of the add method as shown here:
public PictogramElement add(IAddContext
context) {
// ...
EXISTING CODING ...
IPeCreateService peCreateService
= Graphiti.getPeCreateService();
IGaService gaService
= Graphiti.getGaService();
// SHAPE WITH TEXT
{
// create shape for text
Shape shape = peCreateService.createShape(containerShape,
false);
// create and set text graphics algorithm
Text text = gaService.createText(shape, addedClass.getName());
text.setForeground(manageColor(E_CLASS_TEXT_FOREGROUND));
text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER
);
// vertical alignment has as default value "center"
text.setFont(gaService.manageDefaultFont(getDiagram(),
false, true));
gaService.setLocationAndSize(text, 0, 0, width, 20);
// create link and wire it
link(shape, addedClass);
// provide information to support direct-editing directly
// after object creation
(must be activated additionally)
IDirectEditingInfo directEditingInfo =
getFeatureProvider().getDirectEditingInfo();
// set container shape for direct editing after object
creation
directEditingInfo.setMainPictogramElement(containerShape);
// set shape and graphics algorithm where the editor
for
// direct editing shall
be opened after object creation
directEditingInfo.setPictogramElement(shape);
directEditingInfo.setGraphicsAlgorithm(text);
}
// ... EXISTING CODING ...
}
Open a diagram and create a new EClass from the palette. The shape for the EClass should immediately allow the direct editing of the class name.