Search in sources :

Example 16 with ElkConnectableShape

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

the class GmfLayoutEditPolicy method getRelativeSourcePoint.

/**
 * Create a vector that contains the relative position of the source point to the corresponding source node or
 * port.
 *
 * @param edge
 *            an edge
 * @return the relative source point
 */
private KVector getRelativeSourcePoint(final ElkEdge edge) {
    // The edge should have exactly one source shape
    ElkConnectableShape sourceShape = edge.getSources().get(0);
    // The edge should have one edge section after layout
    ElkEdgeSection edgeSection = edge.getSections().get(0);
    KVector sourcePoint = new KVector(edgeSection.getStartX(), edgeSection.getStartY());
    // We will now make the source point absolute, and then relative to the source node
    ElkUtil.toAbsolute(sourcePoint, edge.getContainingNode());
    ElkUtil.toRelative(sourcePoint, ElkGraphUtil.connectableShapeToNode(sourceShape));
    // 1 being at the right / bottom
    if (sourceShape instanceof ElkPort) {
        ElkPort sourcePort = (ElkPort) sourceShape;
        // calculate the relative position to the port size
        if (sourcePort.getWidth() <= 0) {
            sourcePoint.x = 0;
        } else {
            sourcePoint.x = (sourcePoint.x - sourcePort.getX()) / sourcePort.getWidth();
        }
        if (sourcePort.getHeight() <= 0) {
            sourcePoint.y = 0;
        } else {
            sourcePoint.y = (sourcePoint.y - sourcePort.getY()) / sourcePort.getHeight();
        }
    } else {
        // calculate the relative position to the node size
        if (sourceShape.getWidth() <= 0) {
            sourcePoint.x = 0;
        } else {
            sourcePoint.x /= sourceShape.getWidth();
        }
        if (sourceShape.getHeight() <= 0) {
            sourcePoint.y = 0;
        } else {
            sourcePoint.y /= sourceShape.getHeight();
        }
    }
    // check the bound of the relative position
    return sourcePoint.bound(0, 0, 1, 1);
}
Also used : ElkPort(org.eclipse.elk.graph.ElkPort) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) KVector(org.eclipse.elk.core.math.KVector) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection)

Example 17 with ElkConnectableShape

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

the class GmfLayoutEditPolicy method getRelativeTargetPoint.

/**
 * Create a vector that contains the relative position of the target point to the corresponding
 * target node or port.
 *
 * @param edge
 *            an edge
 * @return the relative target point
 */
private KVector getRelativeTargetPoint(final ElkEdge edge) {
    // The edge should have exactly one source shape
    ElkConnectableShape targetShape = edge.getTargets().get(0);
    // The edge should have one edge section after layout
    ElkEdgeSection edgeSection = edge.getSections().get(0);
    KVector targetPoint = new KVector(edgeSection.getEndX(), edgeSection.getEndY());
    // We will now make the source point absolute, and then relative to the source node
    ElkUtil.toAbsolute(targetPoint, edge.getContainingNode());
    ElkUtil.toRelative(targetPoint, ElkGraphUtil.connectableShapeToNode(targetShape));
    // 1 being at the right / bottom
    if (targetShape instanceof ElkPort) {
        ElkPort targetPort = (ElkPort) targetShape;
        // calculate the relative position to the port size
        if (targetPort.getWidth() <= 0) {
            targetPoint.x = 0;
        } else {
            targetPoint.x = (targetPoint.x - targetPort.getX()) / targetPort.getWidth();
        }
        if (targetPort.getHeight() <= 0) {
            targetPoint.y = 0;
        } else {
            targetPoint.y = (targetPoint.y - targetPort.getY()) / targetPort.getHeight();
        }
    } else {
        // calculate the relative position to the node size
        if (targetShape.getWidth() <= 0) {
            targetPoint.x = 0;
        } else {
            targetPoint.x /= targetShape.getWidth();
        }
        if (targetShape.getHeight() <= 0) {
            targetPoint.y = 0;
        } else {
            targetPoint.y /= targetShape.getHeight();
        }
    }
    // check the bound of the relative position
    return targetPoint.bound(0, 0, 1, 1);
}
Also used : ElkPort(org.eclipse.elk.graph.ElkPort) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) KVector(org.eclipse.elk.core.math.KVector) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection)

Example 18 with ElkConnectableShape

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

the class RandomGraphGenerator method split.

/**
 * Splits an edge by inserting a new node and a new edge. The old edge will have all the old
 * sources and just the new node as a target and the new edge will have all the old targets and
 * just the new node as a source. The parent of the new node will be the node containing a whole
 * edge.
 *
 * @param edge
 *            the edge
 * @return a pair containing the new node and the new edge
 */
private Pair<ElkNode, ElkEdge> split(final ElkEdge edge) {
    ElkNode newNode = createNode(edge.getContainingNode());
    ElkEdge newEdge = connect(newNode, edge.getTargets().get(0));
    ElkConnectableShape oldTarget = edge.getTargets().get(0);
    edge.getTargets().remove(0);
    newEdge.getTargets().addAll(edge.getTargets());
    edge.getTargets().clear();
    edge.getTargets().add(oldTarget);
    moveTarget(edge, edge.getTargets().get(0), newNode);
    ElkGraphUtil.updateContainment(newEdge);
    return new Pair<ElkNode, ElkEdge>(newNode, newEdge);
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) ElkEdge(org.eclipse.elk.graph.ElkEdge) Pair(org.eclipse.elk.core.util.Pair)

Example 19 with ElkConnectableShape

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

the class RandomGraphGenerator method moveSource.

/**
 * Changes the source of a given edge to a given node.
 *
 * @param edge
 *            the edge
 * @param node
 *            the new source node
 */
private void moveSource(final ElkEdge edge, final ElkConnectableShape oldConnectShape, final ElkConnectableShape newConnectShape) {
    ElkPort port = null;
    ElkNode oldNode = null;
    ElkNode newNode = null;
    ElkPort oldPort = null;
    ElkPort newPort = null;
    if (get(GeneratorOptions.ENABLE_PORTS)) {
        if (!edge.getSources().isEmpty()) {
            if (oldConnectShape instanceof ElkNodeImpl) {
                oldNode = (ElkNode) oldConnectShape;
                for (ElkConnectableShape source : edge.getSources()) {
                    if (source instanceof ElkPortImpl) {
                        port = (ElkPortImpl) source;
                        if (port.getParent().equals(oldNode)) {
                            edge.getSources().remove(port);
                            if (port.getOutgoingEdges().size() == 0 && port.getIncomingEdges().size() == 1) {
                                port.getParent().getPorts().remove(port);
                            }
                        }
                    }
                }
            } else if (oldConnectShape instanceof ElkPortImpl) {
                oldPort = (ElkPort) oldConnectShape;
                if (edge.getSources().contains(oldPort)) {
                    edge.getSources().remove(oldPort);
                }
                if (oldPort.getOutgoingEdges().size() == 0 && oldPort.getIncomingEdges().size() == 1) {
                    oldPort.getParent().getPorts().remove(oldPort);
                }
            }
        }
        if (newConnectShape instanceof ElkNodeImpl) {
            newNode = (ElkNode) oldConnectShape;
            newPort = retrievePort(newNode, true);
        } else if (newConnectShape instanceof ElkPortImpl) {
            newPort = (ElkPort) oldConnectShape;
        }
        if (!isHypernode(newNode)) {
            // edge.setSourcePort(newPort);
            edge.getSources().add(newPort);
        }
    } else {
        // edge.setSource(node);
        if (oldConnectShape instanceof ElkNodeImpl) {
            oldNode = (ElkNode) oldConnectShape;
        } else if (oldConnectShape instanceof ElkPortImpl) {
            oldPort = (ElkPort) oldConnectShape;
            oldNode = oldPort.getParent();
        }
        if (edge.getSources().contains(oldNode)) {
            edge.getSources().remove(oldNode);
        }
        if (newConnectShape instanceof ElkNodeImpl) {
            newNode = (ElkNode) newConnectShape;
        } else if (newConnectShape instanceof ElkPortImpl) {
            newPort = (ElkPort) newConnectShape;
            newNode = newPort.getParent();
        }
        edge.getSources().add(newNode);
    }
    ElkGraphUtil.updateContainment(edge);
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) ElkNodeImpl(org.eclipse.elk.graph.impl.ElkNodeImpl) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) ElkPortImpl(org.eclipse.elk.graph.impl.ElkPortImpl)

Example 20 with ElkConnectableShape

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

the class ElkGraphBuilder method createElkGraphElementsForConnections.

/**
 * Creates ELK edges for connection diagram nodes which are descendants of the specified node.
 * Even though the results of the ELK edge routing are not used, it is still important because it affects the placements of shapes.
 */
private void createElkGraphElementsForConnections(final DiagramNode dn, final LayoutMapping mapping) {
    for (final DiagramElement de : dn.getChildren()) {
        if (de.getGraphic() instanceof AgeConnection) {
            final AgeConnection connection = (AgeConnection) de.getGraphic();
            // Flow indicators are represented by a node in the container which has a port and an edge connecting that port to the starting element
            final ElkConnectableShape edgeStart = getConnectableShape(de.getStartElement(), mapping);
            ElkConnectableShape edgeEnd = null;
            if (connection.isFlowIndicator) {
                // Find the first undocked ancestor for the flow indicator
                final DiagramElement undockedContainer = DiagramElementUtil.getUndockedDiagramElement(de.getParent());
                if (undockedContainer == null) {
                    // Ignore the flow indicator if unable to find a containing element which isn't docked.
                    continue;
                }
                // Find the ELK shape for the ancestor
                final ElkConnectableShape endContainer = getConnectableShape(undockedContainer, mapping);
                if (!(endContainer instanceof ElkNode)) {
                    // Ignore the flow indicator if the container isn't a node.
                    continue;
                }
                // Create the node for the end of the flow indicator
                final ElkNode endNode = ElkGraphUtil.createNode((ElkNode) endContainer);
                endContainer.setDimensions(0, 0);
                endNode.setProperty(CoreOptions.NODE_SIZE_CONSTRAINTS, EnumSet.noneOf(SizeConstraint.class));
                endNode.setProperty(CoreOptions.NODE_SIZE_OPTIONS, EnumSet.noneOf(SizeOptions.class));
                // Create port
                final ElkPort endPort = ElkGraphUtil.createPort(endNode);
                endPort.setProperty(CoreOptions.PORT_SIDE, PortSide.WEST);
                endPort.setX(0);
                endPort.setY(0);
                endPort.setWidth(0);
                endPort.setHeight(0);
                edgeEnd = endPort;
            } else {
                edgeEnd = getConnectableShape(de.getEndElement(), mapping);
            }
            if (edgeStart != null && edgeEnd != null) {
                final ElkConnectableShape start = edgeStart;
                final ElkConnectableShape end = edgeEnd;
                boolean insideSelfLoopsYo = true;
                // An example of this sort of edge is a steady state state transition in the EMV2
                if (start == end) {
                    insideSelfLoopsYo = false;
                }
                final ElkEdge newEdge = ElkGraphUtil.createSimpleEdge(start, end);
                // Allow edges with the same start and end shape because they layout as intended.
                if (start == end && start instanceof ElkPort) {
                    continue;
                }
                // Ensure the edge has at least one section. Fixes NPE that can occur when laying out connections
                // with the same source and destination port.
                ElkGraphUtil.createEdgeSection(newEdge);
                newEdge.setProperty(CoreOptions.INSIDE_SELF_LOOPS_YO, insideSelfLoopsYo);
                mapping.getGraphMap().put(newEdge, de);
                createElkLabels(de, newEdge, mapping);
                // along with other edges
                if (connection.isFlowIndicator && newEdge.getLabels().isEmpty()) {
                    final ElkLabel spacingLabel = createElkLabel(newEdge, "<Spacing>", new Dimension(10, 10));
                    if (!layoutConnectionLabels) {
                        spacingLabel.setProperty(CoreOptions.NO_LAYOUT, true);
                    }
                }
            }
        }
        createElkGraphElementsForConnections(de, mapping);
    }
}
Also used : DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) AgeConnection(org.osate.ge.graphics.internal.AgeConnection) ElkNode(org.eclipse.elk.graph.ElkNode) SizeOptions(org.eclipse.elk.core.options.SizeOptions) ElkLabel(org.eclipse.elk.graph.ElkLabel) ElkPort(org.eclipse.elk.graph.ElkPort) Dimension(org.osate.ge.graphics.Dimension) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Aggregations

ElkConnectableShape (org.eclipse.elk.graph.ElkConnectableShape)22 ElkNode (org.eclipse.elk.graph.ElkNode)13 ElkPort (org.eclipse.elk.graph.ElkPort)11 ElkEdge (org.eclipse.elk.graph.ElkEdge)10 ElkEdgeSection (org.eclipse.elk.graph.ElkEdgeSection)6 KVector (org.eclipse.elk.core.math.KVector)4 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)4 ElkLabel (org.eclipse.elk.graph.ElkLabel)3 HashMap (java.util.HashMap)2 ElkBendPoint (org.eclipse.elk.graph.ElkBendPoint)2 ElkNodeImpl (org.eclipse.elk.graph.impl.ElkNodeImpl)2 ElkPortImpl (org.eclipse.elk.graph.impl.ElkPortImpl)2 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1 LEdge (org.eclipse.elk.alg.layered.graph.LEdge)1 LGraphElement (org.eclipse.elk.alg.layered.graph.LGraphElement)1 LLabel (org.eclipse.elk.alg.layered.graph.LLabel)1