The copy & paste of graphical elements is integrated with the general copy and paste concept for models
Note, that copy & paste in graphical editors is always executed on the graphical model-elements. This gives the freedom to implement copy & paste with different semantics
Below we will explain an example, which provides copy & paste functionality for EClasses. For simplicity reasons we only create a duplicate of the graphical pictogram element and not also of the business-model element.
First we have to create a copy feature and make it available in the feature provider.
A copy feature has to implement the interface ICopyFeature. Instead of implementing it directly it should extend one of the available base classes. In this example we extend the base class AbstractCopyFeature, which offers methods to easily access the clipboard.
In this case we have to implement/overwrite two methods:
The method canCopy has to check if the given context (containing the selected elements) can be copied to the clipboard.
The method copy finally has to copy the given context to the clipboard.
You can see the complete implementation of the copy feature here:
package org.eclipse.graphiti.examples.tutorial.features;
public class TutorialCopyEClassFeature extends AbstractCopyFeature {
public TutorialCopyEClassFeature(IFeatureProvider fp) { super(fp); }
public boolean canCopy(ICopyContext context) { final PictogramElement[] pes = context.getPictogramElements(); if (pes == null || pes.length == 0) { // nothing selected return false; }
// return true, if all selected elements are a EClasses for (PictogramElement pe : pes) { final Object bo = getBusinessObjectForPictogramElement(pe); if (!(bo instanceof EClass)) { return false; } } return true; }
public void copy(ICopyContext context) { // get the business-objects for all pictogram-elements // we already verified, that all business-objets are EClasses PictogramElement[] pes = context.getPictogramElements(); Object[] bos = new Object[pes.length]; for (int i = 0; i < pes.length; i++) { PictogramElement pe = pes[i]; bos[i] = getBusinessObjectForPictogramElement(pe); } // put all business objects to the clipboard putToClipboard(bos); } }
|
Additionally the feature provider has to deliver our newly created feature (overwrite the method getCopyFeature).
This implementation can be seen here:
@Override public ICopyFeature getCopyFeature(ICopyContext context) { return new TutorialCopyEClassFeature(this); }
|
Now we have to create a corresponding paste feature and make it available in the feature provider.
A paste feature has to implement the interface IPasteFeature. Instead of implementing it directly it should extend one of the available base classes. In this example we extend the base class AbstractPasteFeature, which offers methods to easily access the clipboard.
In this case we have to implement/overwrite two methods:
The method canPaste has to check if the current clipboard contents can be pasted on the given context (containing the target object).
The method paste finally has to paste the current clipboard contents on the given context.
You can see the complete implementation of the paste feature here:
package org.eclipse.graphiti.examples.tutorial.features;
public class TutorialPasteEClassFeature extends AbstractPasteFeature {
public TutorialPasteEClassFeature(IFeatureProvider fp) { super(fp); }
public boolean canPaste(IPasteContext context) { // only support pasting directly in the diagram (nothing else selected) PictogramElement[] pes = context.getPictogramElements(); if (pes.length != 1 || !(pes[0] instanceof Diagram)) { return false; }
// can paste, if all objects on the clipboard are EClasses Object[] fromClipboard = getFromClipboard(); if (fromClipboard == null || fromClipboard.length == 0) { return false; } for (Object object : fromClipboard) { if (!(object instanceof EClass)) { return false; } } return true; }
public void paste(IPasteContext context) { // we already verified, that we paste directly in the diagram PictogramElement[] pes = context.getPictogramElements(); Diagram diagram = (Diagram) pes[0]; // get the EClasses from the clipboard without copying them // (only copy the pictogram element, not the business object) // then create new pictogram elements using the add feature Object[] objects = getFromClipboard(); for (Object object : objects) { AddContext ac = new AddContext(); ac.setLocation(0, 0); // for simplicity paste at (0, 0) ac.setTargetContainer(diagram); addGraphicalRepresentation(ac, object); } } }
|
Additionally the feature provider has to deliver our newly created feature (overwrite the method getPasteFeature).
This implementation can be seen here:
@Override public IPasteFeature getPasteFeature(IPasteContext context) { return new TutorialPasteEClassFeature(this); }
|
Now start the editor again