If the editor developer wants to add some editor specific functionality (e.g. context menu actions which can change the business and/or the pictogram model) the predefined features are not enough. In this case we need a more general feature. For that purpose the framework provides a custom feature. Examples for a custom feature are rename, change color, check out, …
A custom feature has to implement the interface ICustomFeature. Instead of implementing it directly it should extend one of the available base classes. In this example we extend the base class AbstractCustomFeature.
In this case we have to implement/overwrite 4 methods:
The method canExecute has to check whether the custom feature can be executed on the current pictogram element of the given context.
The method execute has to perform the custom specific stuff.
These two methods receive a custom context as parameter. Interface ICustomContext provides the inner pictogram element and the inner graphics algorithm additionally to the (selected) pictogram element. These are the inner elements the mouse pointer is placed / clicked on.
Currently only custom features receive the information on the inner elements additionally to the selected/selectable ones.
The methods getName / getDescription have to return the information for the UI representation.
In this example we want to implement a custom feature which opens a popup-dialog to change the EClass name. This custom feature should be available and enabled in the context menu if exactly one EClass is selected.
You can see the complete implementation of the custom feature here:
package org.eclipse.graphiti.examples.tutorial.features;
public class TutorialRenameEClassFeature extends AbstractCustomFeature {
public TutorialRenameEClassFeature(IFeatureProvider fp) { super(fp); }
@Override public String getName() { return "Rename EClass"; }
@Override public String getDescription() { return "Change the name of the EClass"; }
@Override public boolean canExecute(ICustomContext context) { // allow rename if exactly one pictogram element // representing a EClass is selected boolean ret = false; PictogramElement[] pes = context.getPictogramElements(); if (pes != null && pes.length == 1) { Object bo = getBusinessObjectForPictogramElement(pes[0]); if (bo instanceof EClass) { ret = true; } } return ret; }
public void execute(ICustomContext context) { PictogramElement[] pes = context.getPictogramElements(); if (pes != null && pes.length == 1) { Object bo = getBusinessObjectForPictogramElement(pes[0]); if (bo instanceof EClass) { EClass eClass = (EClass) bo; String currentName = eClass.getName(); // ask user for a new class name String newName =SampleUtil.askString(getName(), getDescription(), currentName); if (newName != null) { eClass.setName(newName); } } } } }
|
Additionally the feature provider has to deliver our newly created custom feature (overwrite the method getCustomFeatures).
This implementation can be seen here:
@Override public ICustomFeature[] getCustomFeatures(ICustomContext context) { return new ICustomFeature[] { new TutorialRenameEClassFeature(this) }; }
|
Now start the editor and test this new custom feature: