Search in sources :

Example 6 with DiagramModification

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;
}
Also used : Element(org.osate.aadl2.Element) BusinessObjectHandler(org.osate.ge.businessobjecthandling.BusinessObjectHandler) RenameUtil(org.osate.ge.internal.ui.RenameUtil) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) DiagramElementLayoutUtil(org.osate.ge.internal.diagram.runtime.layout.DiagramElementLayoutUtil) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) ClipboardService(org.osate.ge.internal.services.ClipboardService) ClassifierCreationHelper(org.osate.ge.aadl2.internal.util.classifiers.ClassifierCreationHelper) Classifier(org.osate.aadl2.Classifier) ComponentType(org.osate.aadl2.ComponentType) DiagramNodePredicates(org.osate.ge.internal.diagram.runtime.DiagramNodePredicates) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) Bundle(org.osgi.framework.Bundle) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) Collection(java.util.Collection) PackageSection(org.osate.aadl2.PackageSection) EObject(org.eclipse.emf.ecore.EObject) AadlPackage(org.osate.aadl2.AadlPackage) AadlImportsUtil(org.osate.ge.aadl2.AadlImportsUtil) Point(org.osate.ge.graphics.Point) DiagramElementUtil(org.osate.ge.internal.util.DiagramElementUtil) Objects(java.util.Objects) List(java.util.List) AadlModificationService(org.osate.ge.internal.services.AadlModificationService) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) EclipseContextFactory(org.eclipse.e4.core.contexts.EclipseContextFactory) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) Feature(org.osate.aadl2.Feature) ComponentImplementation(org.osate.aadl2.ComponentImplementation) DiagramModification(org.osate.ge.internal.diagram.runtime.DiagramModification) ArrayList(java.util.ArrayList) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) EClass(org.eclipse.emf.ecore.EClass) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Predicates(com.google.common.base.Predicates) GetNameContext(org.osate.ge.businessobjecthandling.GetNameContext) SimpleModifier(org.osate.ge.internal.services.AadlModificationService.SimpleModifier) Subcomponent(org.osate.aadl2.Subcomponent) XtextResource(org.eclipse.xtext.resource.XtextResource) AadlNameUtil(org.osate.ge.aadl2.internal.util.AadlNameUtil) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) Action(org.eclipse.jface.action.Action) CanRenameContext(org.osate.ge.businessobjecthandling.CanRenameContext) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) AgeHandlerUtil(org.osate.ge.internal.ui.handlers.AgeHandlerUtil) ComponentTypeRename(org.osate.aadl2.ComponentTypeRename) ActionFactory(org.eclipse.ui.actions.ActionFactory) NamedElement(org.osate.aadl2.NamedElement) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) ArrayList(java.util.ArrayList) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) Point(org.osate.ge.graphics.Point) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) EObject(org.eclipse.emf.ecore.EObject) Collection(java.util.Collection) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) EObject(org.eclipse.emf.ecore.EObject)

Aggregations

Collection (java.util.Collection)6 Collections (java.util.Collections)6 List (java.util.List)6 Objects (java.util.Objects)6 HashSet (java.util.HashSet)5 LinkedList (java.util.LinkedList)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Point (org.osate.ge.graphics.Point)5 AgeDiagram (org.osate.ge.internal.diagram.runtime.AgeDiagram)5 DiagramElement (org.osate.ge.internal.diagram.runtime.DiagramElement)5 DiagramModification (org.osate.ge.internal.diagram.runtime.DiagramModification)5 DiagramNode (org.osate.ge.internal.diagram.runtime.DiagramNode)5 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)4 ImmutableList (com.google.common.collect.ImmutableList)4 Lists (com.google.common.collect.Lists)4 Comparator (java.util.Comparator)4 EnumSet (java.util.EnumSet)4 Entry (java.util.Map.Entry)4 Optional (java.util.Optional)4