Search in sources :

Example 1 with ElkShape

use of org.eclipse.elk.graph.ElkShape in project elk by eclipse.

the class ElkUtil method applyConfiguredNodeScaling.

/**
 * Applies the scaling factor configured in terms of {@link CoreOptions#SCALE_FACTOR} to {@code node}'s
 * size data, and updates the layout data of {@code node}'s ports and labels accordingly.<br>
 * <b>Note:</b> The scaled layout data won't be reverted during the layout process, see
 * {@link CoreOptions#SCALE_FACTOR}.
 *
 * @param node
 *            the node to be scaled
 */
public static void applyConfiguredNodeScaling(final ElkNode node) {
    final double scalingFactor = node.getProperty(CoreOptions.SCALE_FACTOR);
    if (scalingFactor == 1) {
        return;
    }
    node.setDimensions(scalingFactor * node.getWidth(), scalingFactor * node.getHeight());
    final Iterable<ElkLabel> portLabels = Iterables.concat(Iterables.transform(node.getPorts(), p -> p.getLabels()));
    for (ElkShape shape : Iterables.concat(node.getLabels(), node.getPorts(), portLabels)) {
        shape.setLocation(scalingFactor * shape.getX(), scalingFactor * shape.getY());
        shape.setDimensions(scalingFactor * shape.getWidth(), scalingFactor * shape.getHeight());
        final KVector anchor = shape.getProperty(CoreOptions.PORT_ANCHOR);
        if (anchor != null) {
            anchor.x *= scalingFactor;
            anchor.y *= scalingFactor;
        }
    }
}
Also used : CoreOptions(org.eclipse.elk.core.options.CoreOptions) Iterables(com.google.common.collect.Iterables) ListIterator(java.util.ListIterator) PortSide(org.eclipse.elk.core.options.PortSide) ContentAlignment(org.eclipse.elk.core.options.ContentAlignment) ElkGraphFactory(org.eclipse.elk.graph.ElkGraphFactory) ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) PortLabelPlacement(org.eclipse.elk.core.options.PortLabelPlacement) KVectorChain(org.eclipse.elk.core.math.KVectorChain) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) ElkRectangle(org.eclipse.elk.core.math.ElkRectangle) Strings(com.google.common.base.Strings) LabelAdapter(org.eclipse.elk.core.util.adapters.GraphAdapters.LabelAdapter) Lists(com.google.common.collect.Lists) Map(java.util.Map) GraphValidationException(org.eclipse.elk.core.validation.GraphValidationException) PortAdapter(org.eclipse.elk.core.util.adapters.GraphAdapters.PortAdapter) Iterator(java.util.Iterator) PortConstraints(org.eclipse.elk.core.options.PortConstraints) ElkLabel(org.eclipse.elk.graph.ElkLabel) KVector(org.eclipse.elk.core.math.KVector) Collection(java.util.Collection) SizeOptions(org.eclipse.elk.core.options.SizeOptions) Set(java.util.Set) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) EObject(org.eclipse.emf.ecore.EObject) ElkShape(org.eclipse.elk.graph.ElkShape) Maps(com.google.common.collect.Maps) File(java.io.File) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) List(java.util.List) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) NodeLabelPlacement(org.eclipse.elk.core.options.NodeLabelPlacement) ElkBendPoint(org.eclipse.elk.graph.ElkBendPoint) Direction(org.eclipse.elk.core.options.Direction) IValidatingGraphElementVisitor(org.eclipse.elk.core.validation.IValidatingGraphElementVisitor) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) EdgeLabelPlacement(org.eclipse.elk.core.options.EdgeLabelPlacement) GraphIssue(org.eclipse.elk.core.validation.GraphIssue) ElkEdge(org.eclipse.elk.graph.ElkEdge) Pattern(java.util.regex.Pattern) ElkGraphUtil(org.eclipse.elk.graph.util.ElkGraphUtil) ElkLabel(org.eclipse.elk.graph.ElkLabel) ElkShape(org.eclipse.elk.graph.ElkShape) KVector(org.eclipse.elk.core.math.KVector)

Example 2 with ElkShape

use of org.eclipse.elk.graph.ElkShape in project elk by eclipse.

the class GraphTestUtils method haveOverlaps.

/**
 * Returns whether any of the shapes in the list overlap.
 */
public static boolean haveOverlaps(final List<? extends ElkShape> shapes) {
    // Obtain absolute rectangles for us to do the comparisons on
    List<Rectangle2D> shapeRects = new ArrayList<>(shapes.size());
    for (ElkShape shape : shapes) {
        KVector pos = ElkUtil.absolutePosition(shape);
        shapeRects.add(new Rectangle2D.Double(pos.x, pos.y, shape.getWidth(), shape.getHeight()));
    }
    for (int first = 0; first < shapeRects.size(); first++) {
        Rectangle2D firstRect = shapeRects.get(first);
        for (int second = first + 1; second < shapeRects.size(); second++) {
            Rectangle2D secondRect = shapeRects.get(second);
            if (firstRect.intersects(secondRect)) {
                return true;
            }
        }
    }
    return false;
}
Also used : ElkShape(org.eclipse.elk.graph.ElkShape) Rectangle2D(java.awt.geom.Rectangle2D) ArrayList(java.util.ArrayList) KVector(org.eclipse.elk.core.math.KVector)

Example 3 with ElkShape

use of org.eclipse.elk.graph.ElkShape in project osate2 by osate.

the class DiagramElementLayoutUtil method applyConnectionLayout.

private static void applyConnectionLayout(final LayoutMapping mapping, final DiagramModification m) {
    // Modify Connections
    for (Entry<ElkGraphElement, Object> e : mapping.getGraphMap().entrySet()) {
        final ElkGraphElement elkElement = e.getKey();
        final Object mappedValue = e.getValue();
        if (!(elkElement instanceof ElkEdge)) {
            continue;
        }
        final ElkEdge edge = (ElkEdge) elkElement;
        // Ignore edges which do not have exactly one section. This is usually the case where it is a long hierarchical connection that has 0 sections
        if (edge.getSections().size() != 1) {
            continue;
        }
        final ElkEdgeSection edgeSection = edge.getSections().get(0);
        if (!(mappedValue instanceof DiagramElement)) {
            continue;
        }
        final DiagramElement de = (DiagramElement) mappedValue;
        if (!(de.getGraphic() instanceof AgeConnection)) {
            continue;
        }
        final AgeConnection connection = (AgeConnection) de.getGraphic();
        // Flow indicators have a position representing where the indicator ends.
        if (connection.isFlowIndicator && edge.getTargets().size() == 1) {
            final ElkPort flowIndicatorEndPort = (ElkPort) edge.getTargets().get(0);
            final ElkShape flowIndicatorEndPortShape = (ElkShape) flowIndicatorEndPort.eContainer();
            m.setPosition(de, new Point(flowIndicatorEndPortShape.getX(), flowIndicatorEndPortShape.getY()));
        }
        // Don't update connections if it wasn't updated. This prevents updating bendpoints to invalid values if an edge is not layed out.
        if (edgeSection.eIsSet(ElkGraphPackage.eINSTANCE.getElkEdgeSection_StartX()) && edgeSection.eIsSet(ElkGraphPackage.eINSTANCE.getElkEdgeSection_EndX())) {
            final List<Point> bendpointsInParentCoordinateSystem = edgeSection.getBendPoints().stream().map(bp -> new Point(bp.getX(), bp.getY())).collect(Collectors.toCollection(LinkedList::new));
            // 
            // Set bendpoints
            // 
            // Add the start and end points to the bendpoints list if the the start/end element is not a port.
            // For ports the start and end points are unnecessary and will actually be located inside the port graphic.
            final boolean srcIsPort = edge.getSources().size() == 1 ? edge.getSources().get(0) instanceof ElkPort : false;
            final boolean dstIsPort = edge.getTargets().size() == 1 ? edge.getTargets().get(0) instanceof ElkPort : false;
            if (!srcIsPort) {
                bendpointsInParentCoordinateSystem.add(0, new Point(edgeSection.getStartX(), edgeSection.getStartY()));
            }
            if (!dstIsPort) {
                bendpointsInParentCoordinateSystem.add(new Point(edgeSection.getEndX(), edgeSection.getEndY()));
            }
            // Adjust newly added bendpoints so that the connection arrows will face the appropriate direction
            if (!srcIsPort && bendpointsInParentCoordinateSystem.size() >= 2) {
                bendpointsInParentCoordinateSystem.set(0, getAdjacentPoint(bendpointsInParentCoordinateSystem.get(0), bendpointsInParentCoordinateSystem.get(1), START_AND_END_BENDPOINT_DISTANCE));
            }
            if (!dstIsPort && bendpointsInParentCoordinateSystem.size() >= 2) {
                bendpointsInParentCoordinateSystem.set(bendpointsInParentCoordinateSystem.size() - 1, getAdjacentPoint(bendpointsInParentCoordinateSystem.get(bendpointsInParentCoordinateSystem.size() - 1), bendpointsInParentCoordinateSystem.get(bendpointsInParentCoordinateSystem.size() - 2), START_AND_END_BENDPOINT_DISTANCE));
            }
            // Get the absolute coordinate in the diagram of the edge's ELK container.
            final Point elkContainerPosition;
            if (edge.getContainingNode() == mapping.getLayoutGraph()) {
                // Node available. Use the first and only child of the top level ELK node.
                if (mapping.getLayoutGraph().getChildren().size() == 1) {
                    final ElkNode topLayoutElkNode = mapping.getLayoutGraph().getChildren().get(0);
                    final Point topLayoutElkNodePosition = getAbsolutePosition((DiagramNode) mapping.getGraphMap().get(topLayoutElkNode));
                    elkContainerPosition = new Point(topLayoutElkNodePosition.x - topLayoutElkNode.getX(), topLayoutElkNodePosition.y - topLayoutElkNode.getY());
                } else {
                    elkContainerPosition = new Point(0, 0);
                }
            } else {
                elkContainerPosition = getAbsolutePosition((DiagramNode) mapping.getGraphMap().get(edge.getContainingNode()));
            }
            final List<Point> bendpointsInAbsoluteCoordinateSystem = bendpointsInParentCoordinateSystem.stream().map(p -> new Point(p.x + elkContainerPosition.x, p.y + elkContainerPosition.y)).collect(Collectors.toList());
            m.setBendpoints(de, bendpointsInAbsoluteCoordinateSystem);
            // For the midpoint calculation, the start and end points are needed. Add them if they have not already been added.
            if (srcIsPort) {
                bendpointsInParentCoordinateSystem.add(0, new Point(edgeSection.getStartX(), edgeSection.getStartY()));
            }
            if (dstIsPort) {
                bendpointsInParentCoordinateSystem.add(new Point(edgeSection.getEndX(), edgeSection.getEndY()));
            }
            // Set Label Positions
            setLabelPositionsForEdge(mapping, m, edge, findMidpoint(bendpointsInParentCoordinateSystem));
        }
    }
}
Also used : CoreOptions(org.eclipse.elk.core.options.CoreOptions) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) PortSide(org.eclipse.elk.core.options.PortSide) LayoutMapping(org.eclipse.elk.core.service.LayoutMapping) ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) IGraphElementVisitor(org.eclipse.elk.core.util.IGraphElementVisitor) RecursiveGraphLayoutEngine(org.eclipse.elk.core.RecursiveGraphLayoutEngine) DockArea(org.osate.ge.internal.diagram.runtime.DockArea) IStatus(org.eclipse.core.runtime.IStatus) BusinessObjectContext(org.osate.ge.BusinessObjectContext) Graphic(org.osate.ge.graphics.Graphic) DiagramNodePredicates(org.osate.ge.internal.diagram.runtime.DiagramNodePredicates) StatusManager(org.eclipse.ui.statushandlers.StatusManager) IEditorPart(org.eclipse.ui.IEditorPart) EnumSet(java.util.EnumSet) Collection(java.util.Collection) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) Point(org.osate.ge.graphics.Point) Collectors(java.util.stream.Collectors) DiagramElementUtil(org.osate.ge.internal.util.DiagramElementUtil) DockingPosition(org.osate.ge.DockingPosition) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) NodeLabelPlacement(org.eclipse.elk.core.options.NodeLabelPlacement) GraphicalEditorException(org.osate.ge.internal.GraphicalEditorException) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) Entry(java.util.Map.Entry) Optional(java.util.Optional) ElkEdge(org.eclipse.elk.graph.ElkEdge) AgeConnection(org.osate.ge.graphics.internal.AgeConnection) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) Dimension(org.osate.ge.graphics.Dimension) ElkGraphPackage(org.eclipse.elk.graph.ElkGraphPackage) ModeGraphic(org.osate.ge.graphics.internal.ModeGraphic) DiagramModification(org.osate.ge.internal.diagram.runtime.DiagramModification) HashSet(java.util.HashSet) ElkUtil(org.eclipse.elk.core.util.ElkUtil) Style(org.osate.ge.graphics.Style) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) AgeShape(org.osate.ge.graphics.internal.AgeShape) LinkedList(java.util.LinkedList) Activator(org.osate.ge.internal.Activator) DiagramElementPredicates(org.osate.ge.internal.diagram.runtime.DiagramElementPredicates) ElkLabel(org.eclipse.elk.graph.ElkLabel) KVector(org.eclipse.elk.core.math.KVector) Label(org.osate.ge.graphics.internal.Label) BasicProgressMonitor(org.eclipse.elk.core.util.BasicProgressMonitor) ElkShape(org.eclipse.elk.graph.ElkShape) Adapters(org.eclipse.core.runtime.Adapters) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) AgeDiagramUtil(org.osate.ge.internal.diagram.runtime.AgeDiagramUtil) Comparator(java.util.Comparator) StyleProvider(org.osate.ge.internal.diagram.runtime.styling.StyleProvider) Collections(java.util.Collections) StyleCalculator(org.osate.ge.internal.diagram.runtime.styling.StyleCalculator) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) Point(org.osate.ge.graphics.Point) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) AgeConnection(org.osate.ge.graphics.internal.AgeConnection) ElkShape(org.eclipse.elk.graph.ElkShape) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 4 with ElkShape

use of org.eclipse.elk.graph.ElkShape in project osate2 by osate.

the class DiagramElementLayoutUtil method applyShapeLayout.

private static void applyShapeLayout(final LayoutMapping mapping, final DiagramModification m) {
    // Modify shapes
    for (Entry<ElkGraphElement, Object> e : mapping.getGraphMap().entrySet()) {
        final ElkGraphElement elkElement = e.getKey();
        final Object mappedValue = e.getValue();
        final boolean isTopLevelElement = isTopLevel(elkElement);
        if (!(elkElement instanceof ElkShape)) {
            continue;
        }
        final ElkShape elkShape = (ElkShape) elkElement;
        if (!(mappedValue instanceof DiagramElement)) {
            continue;
        }
        final DiagramElement de = (DiagramElement) mappedValue;
        if (!(de.getGraphic() instanceof AgeShape)) {
            continue;
        }
        if (de.getGraphic() instanceof Label) {
            continue;
        }
        // Set Position. Don't set the position of top level elements
        if (!isTopLevelElement && DiagramElementPredicates.isMoveableShape(de)) {
            // Determine position for the element
            double x = elkShape.getX();
            double y = elkShape.getY();
            // If the diagram element has a parent port, subtract the parent port position from the ELK port position to determine the relative position
            if (de.getDockArea() == DockArea.GROUP) {
                final ElkPort parentPort = (ElkPort) mapping.getGraphMap().inverse().get(de.getParent());
                if (parentPort != null) {
                    final PortSide side = parentPort.getProperty(CoreOptions.PORT_SIDE);
                    if (PortSide.SIDES_NORTH_SOUTH.contains(side)) {
                        x = elkShape.getX() - parentPort.getX();
                    } else if (PortSide.SIDES_EAST_WEST.contains(side)) {
                        y = elkShape.getY() - parentPort.getY();
                    } else {
                        throw new GraphicalEditorException("Unexpected side: " + side);
                    }
                }
            }
            DiagramElementLayoutUtil.moveElement(m, de, new Point(x, y));
            // Set the dock area
            if (de.getDockArea() != DockArea.GROUP && de.getDockArea() != null) {
                final DockArea newDockArea = PortSideUtil.getDockArea(elkShape.getProperty(CoreOptions.PORT_SIDE));
                if (newDockArea != null) {
                    m.setDockArea(de, newDockArea);
                }
            }
        }
        // Set the size
        if (DiagramElementPredicates.isResizeable(de)) {
            m.setSize(de, new Dimension(elkShape.getWidth(), elkShape.getHeight()));
        }
    }
}
Also used : ElkPort(org.eclipse.elk.graph.ElkPort) AgeShape(org.osate.ge.graphics.internal.AgeShape) ElkLabel(org.eclipse.elk.graph.ElkLabel) Label(org.osate.ge.graphics.internal.Label) Point(org.osate.ge.graphics.Point) Dimension(org.osate.ge.graphics.Dimension) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) ElkShape(org.eclipse.elk.graph.ElkShape) DockArea(org.osate.ge.internal.diagram.runtime.DockArea) PortSide(org.eclipse.elk.core.options.PortSide) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) GraphicalEditorException(org.osate.ge.internal.GraphicalEditorException)

Aggregations

KVector (org.eclipse.elk.core.math.KVector)3 PortSide (org.eclipse.elk.core.options.PortSide)3 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)3 ElkLabel (org.eclipse.elk.graph.ElkLabel)3 ElkShape (org.eclipse.elk.graph.ElkShape)3 Lists (com.google.common.collect.Lists)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Set (java.util.Set)2 CoreOptions (org.eclipse.elk.core.options.CoreOptions)2 NodeLabelPlacement (org.eclipse.elk.core.options.NodeLabelPlacement)2 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)2 ElkPort (org.eclipse.elk.graph.ElkPort)2 Dimension (org.osate.ge.graphics.Dimension)2 Point (org.osate.ge.graphics.Point)2 AgeShape (org.osate.ge.graphics.internal.AgeShape)2 Label (org.osate.ge.graphics.internal.Label)2 GraphicalEditorException (org.osate.ge.internal.GraphicalEditorException)2 DiagramElement (org.osate.ge.internal.diagram.runtime.DiagramElement)2