Search in sources :

Example 21 with Point

use of org.osate.ge.graphics.Point in project osate2 by osate.

the class CopyAction method run.

@Override
public void run() {
    final List<CopiedDiagramElement> copiedElements = new ArrayList<>();
    for (final DiagramElement de : getElementsToCopy()) {
        final DiagramElement copiedElement = CopyAndPasteUtil.copyDiagramElement(de, null, de.getRelativeReference(), referenceBuilder);
        final Object bo = de.getBusinessObject();
        final Point position = CopyAndPasteUtil.getPositionToCopy(de);
        if (bo instanceof EObject) {
            final EObject boEObj = (EObject) de.getBusinessObject();
            copiedElements.add(new CopiedDiagramElement(copiedElement, de.getBusinessObject(), EcoreUtil.copy(boEObj), boEObj.eContainingFeature(), position));
        } else if (bo instanceof EmbeddedBusinessObject) {
            // Don't need to copy object again because it was copied as part of copying the diagram element.
            copiedElements.add(new CopiedDiagramElement(copiedElement, de.getBusinessObject(), copiedElement.getBusinessObject(), null, position));
        } else {
            throw new RuntimeException("Unsupported case: " + bo);
        }
    }
    clipboard.setContents(new CopiedDiagramElements(ImmutableList.copyOf(copiedElements)));
}
Also used : DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) EObject(org.eclipse.emf.ecore.EObject) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) EmbeddedBusinessObject(org.osate.ge.internal.model.EmbeddedBusinessObject) Point(org.osate.ge.graphics.Point)

Example 22 with Point

use of org.osate.ge.graphics.Point 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)

Example 23 with Point

use of org.osate.ge.graphics.Point in project osate2 by osate.

the class CopyDiagramElementsHandler method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    // Get clipboard. The clipboard service stores the clipboard weakly but this handler does not need to
    // because the paste action stores a strong reference to the clipboard. This isn't stored in a field by
    // the constructor because the handler is not instantiated for each editor.
    final Bundle bundle = FrameworkUtil.getBundle(getClass());
    final IEclipseContext context = EclipseContextFactory.getServiceContext(bundle.getBundleContext());
    final Clipboard clipboard = Objects.requireNonNull(context.get(ClipboardService.class), "Unable to get clipboard service").getClipboard();
    final ReferenceBuilderService refBuilder = Objects.requireNonNull(context.get(ReferenceBuilderService.class), "Unable to get reference builder service");
    final List<CopiedDiagramElement> copiedElements = new ArrayList<>();
    for (final DiagramElement de : AgeHandlerUtil.getSelectedDiagramElements()) {
        final DiagramElement copiedElement = CopyAndPasteUtil.copyDiagramElement(de, null, de.getRelativeReference(), refBuilder);
        final Point position = CopyAndPasteUtil.getPositionToCopy(de);
        copiedElements.add(new CopiedDiagramElement(copiedElement, de.getBusinessObject(), position));
    }
    clipboard.setContents(new CopiedDiagramElements(ImmutableList.copyOf(copiedElements)));
    return null;
}
Also used : DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) CopiedDiagramElement(org.osate.ge.internal.ui.editor.actions.CopiedDiagramElement) ReferenceBuilderService(org.osate.ge.services.ReferenceBuilderService) Bundle(org.osgi.framework.Bundle) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) CopiedDiagramElement(org.osate.ge.internal.ui.editor.actions.CopiedDiagramElement) ArrayList(java.util.ArrayList) CopiedDiagramElements(org.osate.ge.internal.ui.editor.actions.CopiedDiagramElements) Clipboard(org.osate.ge.internal.services.ClipboardService.Clipboard) Point(org.osate.ge.graphics.Point)

Example 24 with Point

use of org.osate.ge.graphics.Point in project osate2 by osate.

the class ArrangeInCircleHandler method getCenter.

private static Point getCenter(final List<DiagramElement> diagramElements, final double radius) {
    final int boarderPadding = 10;
    double xMin = Double.MAX_VALUE, xMax = Double.MIN_VALUE, yMin = Double.MAX_VALUE, yMax = Double.MIN_VALUE;
    double maxWidth = Double.MIN_VALUE, maxHeight = Double.MIN_VALUE;
    // Get max dimension of shapes
    for (final DiagramElement diagramElement : diagramElements) {
        final double width = diagramElement.getWidth() / 2;
        final double height = diagramElement.getHeight() / 2;
        final double x = diagramElement.getX() + width;
        final double y = diagramElement.getY() + height;
        if (x < xMin) {
            xMin = x;
        }
        if (x > xMax) {
            xMax = x;
        }
        if (y < yMin) {
            yMin = y;
        }
        if (y > yMax) {
            yMax = y;
        }
        if (maxWidth < width) {
            maxWidth = width;
        }
        if (maxHeight < height) {
            maxHeight = height;
        }
    }
    // Calculate center point
    double xCenter = (xMin + xMax) / 2;
    double yCenter = (yMax + yMin) / 2;
    // Move center point to appropriate location if necessary
    if (xCenter - radius - maxWidth < 0) {
        xCenter += Math.abs(xCenter - radius - maxWidth) + boarderPadding;
    }
    if (yCenter - radius - maxHeight < 0) {
        yCenter += Math.abs(yCenter - radius - maxHeight) + boarderPadding;
    }
    return new Point(xCenter, yCenter);
}
Also used : DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) Point(org.osate.ge.graphics.Point) Point(org.osate.ge.graphics.Point)

Example 25 with Point

use of org.osate.ge.graphics.Point in project osate2 by osate.

the class ArrangeInCircleHandler method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
    final AgeDiagram diagram = UiUtil.getDiagram(selectedDiagramElements);
    if (diagram == null) {
        throw new RuntimeException("Unable to get diagram");
    }
    diagram.modify("Arrange in Circle", m -> {
        // Calculate and sets the position of the selected shapes into a radial layout.
        final List<Double> angles = new ArrayList<>();
        final double padding = 5;
        double radius = 0;
        double sizeOfShapes = 0;
        double circumference = -1;
        // Find appropriate radius for shapes
        while ((sizeOfShapes > circumference) || (Double.isNaN(sizeOfShapes))) {
            sizeOfShapes = 0;
            angles.clear();
            radius++;
            circumference = 2 * Math.PI * radius;
            for (final DiagramElement e1 : selectedDiagramElements) {
                // Get largest dimension of shape
                final double shapeDimension = ((e1.getWidth() > e1.getHeight() ? e1.getWidth() : e1.getHeight()) + padding) / 2;
                // Calculate diagonal of shape
                final double shapeSize = Math.sqrt((Math.pow(shapeDimension, 2) + Math.pow(shapeDimension, 2)));
                // Calculate angle for shape diagonal using Law of Cosines and double it
                final double angle = Math.acos((Math.pow(radius, 2) + Math.pow(radius, 2) - Math.pow(shapeSize, 2)) / (2 * radius * radius)) * 2;
                angles.add(angle);
                sizeOfShapes += (circumference * (angle / (2 * Math.PI)));
            }
        }
        final Point center = getCenter(selectedDiagramElements, radius);
        double placementAngle = 0;
        int i = 0;
        for (final DiagramElement e2 : selectedDiagramElements) {
            double spacingAngle = angles.get(i++);
            // Center of placement
            placementAngle += spacingAngle / 2;
            // Calculate where to place shape
            final long x = Math.round(center.x + radius * Math.cos(placementAngle) - e2.getWidth() / 2);
            final long y = Math.round(center.y + radius * Math.sin(placementAngle) - e2.getHeight() / 2);
            // End of placement
            placementAngle += spacingAngle / 2;
            DiagramElementLayoutUtil.moveElement(m, e2, new Point((int) x, (int) y));
        }
    });
    return null;
}
Also used : DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) ArrayList(java.util.ArrayList) Point(org.osate.ge.graphics.Point) Point(org.osate.ge.graphics.Point)

Aggregations

Point (org.osate.ge.graphics.Point)27 DiagramElement (org.osate.ge.internal.diagram.runtime.DiagramElement)19 DiagramNode (org.osate.ge.internal.diagram.runtime.DiagramNode)9 AgeDiagram (org.osate.ge.internal.diagram.runtime.AgeDiagram)8 Dimension (org.osate.ge.graphics.Dimension)7 DockArea (org.osate.ge.internal.diagram.runtime.DockArea)7 List (java.util.List)5 ElkLabel (org.eclipse.elk.graph.ElkLabel)5 Style (org.osate.ge.graphics.Style)5 AgeShape (org.osate.ge.graphics.internal.AgeShape)5 GraphicalEditorException (org.osate.ge.internal.GraphicalEditorException)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 Objects (java.util.Objects)4 Collectors (java.util.stream.Collectors)4 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)3 ImmutableList (com.google.common.collect.ImmutableList)3 Lists (com.google.common.collect.Lists)3 Comparator (java.util.Comparator)3