use of org.osate.ge.internal.diagram.runtime.DiagramModification in project osate2 by osate.
the class PasteAction method copyClipboardContents.
/**
* Copies the clipboard contents to the destination business object and diagram element.
* @return the diagram elements which were created. Does not include children of created diagram elements.
*/
private Collection<DiagramElement> copyClipboardContents(final EObject dstBoToModify, final DiagramNode dstDiagramNode, final DiagramModification m, final ReferenceBuilderService refBuilder) {
// Determine the minimum coordinates from the elements whose positions will be copied
// The minimum coordinates is null if none of the copied diagram elements have an absolute position. This is reasonable because the minimum coordinates
// are only needed if a copied diagram element has an absolute position.
final Point minCoordinates = getCopiedDiagramElements().stream().map(CopiedDiagramElement::getAbsolutePosition).filter(Predicates.notNull()).reduce((a, b) -> new Point(Math.min(a.x, b.x), Math.min(a.y, b.y))).orElse(null);
// This list will contain the diagram elements that are created by the copying process. Does not contain their children.
final List<DiagramElement> newDiagramElements = new ArrayList<>();
// Copy each copied diagram element into the diagram and model.
for (final CopiedDiagramElement copiedDiagramElement : getCopiedDiagramElements()) {
final DiagramElement newDiagramElement;
if (copiedDiagramElement.getCopiedBusinessObject() == null) {
newDiagramElement = CopyAndPasteUtil.copyDiagramElement(copiedDiagramElement.getDiagramElement(), dstDiagramNode, copiedDiagramElement.getDiagramElement().getRelativeReference(), refBuilder);
} else {
final Object boFromCopiedDiagramElement = copiedDiagramElement.getCopiedBusinessObject();
final RelativeBusinessObjectReference newRelativeRef;
if (boFromCopiedDiagramElement instanceof EmbeddedBusinessObject) {
// The relative reference will be assigned when copying the diagram element.
newRelativeRef = null;
} else if (boFromCopiedDiagramElement instanceof EObject) {
// Get the list that to which the copied object will be added
final EStructuralFeature compatibleFeature = getCompatibleStructuralFeature(copiedDiagramElement.getContainingFeature(), dstBoToModify.eClass());
final Object containingFeatureValue = dstBoToModify.eGet(compatibleFeature);
if (!(containingFeatureValue instanceof Collection)) {
throw new RuntimeException("Unexpected case. Value of containing feature was not a collection");
}
@SuppressWarnings("unchecked") final Collection<EObject> containingFeatureValueCollection = (Collection<EObject>) containingFeatureValue;
final EObject copiedEObject = EcoreUtil.copy((EObject) boFromCopiedDiagramElement);
containingFeatureValueCollection.add(copiedEObject);
ensureBusinessObjectHasUniqueName(copiedEObject, copiedDiagramElement.getDiagramElement().getBusinessObjectHandler());
ensurePackagesAreImported(copiedEObject);
newRelativeRef = refBuilder.getRelativeReference(copiedEObject);
} else {
throw new RuntimeException("Unsupported case: " + boFromCopiedDiagramElement);
}
newDiagramElement = CopyAndPasteUtil.copyDiagramElement(copiedDiagramElement.getDiagramElement(), dstDiagramNode, newRelativeRef, refBuilder);
}
// Set the position of the new diagram element. They are positioned relative to each other at a fixed offset within the new parent.
final Point cp = copiedDiagramElement.getAbsolutePosition();
final Point newPosition = cp == null ? null : new Point(cp.x - minCoordinates.x + 50, cp.y - minCoordinates.y + 50);
DiagramElementLayoutUtil.moveElement(m, newDiagramElement, newPosition);
// Remove existing element
final DiagramElement existingDiagramElement = dstDiagramNode.getChildByRelativeReference(newDiagramElement.getRelativeReference());
if (existingDiagramElement != null) {
m.removeElement(existingDiagramElement);
}
// Add the new diagram element to the diagram.
m.addElement(newDiagramElement);
newDiagramElements.add(newDiagramElement);
}
return newDiagramElements;
}
Aggregations