Search in sources :

Example 41 with ElkLabel

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

the class GmfLayoutEditPolicy method getCommand.

@Override
public Command getCommand(final Request request) {
    if (ApplyLayoutRequest.REQ_APPLY_LAYOUT.equals(request.getType())) {
        if (request instanceof ApplyLayoutRequest) {
            ApplyLayoutRequest layoutRequest = (ApplyLayoutRequest) request;
            IGraphicalEditPart hostEditPart = (IGraphicalEditPart) getHost();
            GmfLayoutCommand command = new GmfLayoutCommand(hostEditPart.getEditingDomain(), "Automatic Layout", new EObjectAdapter((View) hostEditPart.getModel()));
            double scale = layoutRequest.getScale();
            // retrieve layout data from the request and compute layout data for the command
            for (Pair<ElkGraphElement, GraphicalEditPart> layoutPair : layoutRequest.getElements()) {
                if (layoutPair.getFirst() instanceof ElkNode) {
                    addShapeLayout(command, (ElkShape) layoutPair.getFirst(), layoutPair.getSecond(), scale);
                } else if (layoutPair.getFirst() instanceof ElkPort) {
                    addShapeLayout(command, (ElkPort) layoutPair.getFirst(), layoutPair.getSecond(), scale);
                } else if (layoutPair.getFirst() instanceof ElkEdge) {
                    addEdgeLayout(command, (ElkEdge) layoutPair.getFirst(), (ConnectionEditPart) layoutPair.getSecond(), scale);
                } else if (layoutPair.getFirst() instanceof ElkLabel) {
                    addLabelLayout(command, (ElkLabel) layoutPair.getFirst(), layoutPair.getSecond(), scale);
                }
            }
            // TODO Make this configurable?
            command.setObliqueRouting(true);
            pointListMap.clear();
            return new ICommandProxy(command);
        } else {
            return null;
        }
    } else {
        return super.getCommand(request);
    }
}
Also used : IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) ICommandProxy(org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy) ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) EObjectAdapter(org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter) View(org.eclipse.gmf.runtime.notation.View) ElkLabel(org.eclipse.elk.graph.ElkLabel) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) GraphicalEditPart(org.eclipse.gef.GraphicalEditPart) IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 42 with ElkLabel

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

the class ElkGraphTransformer method importElkShape.

/**
 * Transforms a {@link ElkNode}, {@link ElkLabel} or {@link ElkPort} into a {@link DCElement} without destroying it.
 * An offset may be applied, as some of these elements may have coordinates relative to another element, but the
 * {@link DCGraph} only works with absolute coordinates.
 *
 * @param element
 *            Elemment to be transformed
 * @param considerWhenApplyingOffset
 *            true - a key-value pair consisting of the original element and its {@link DCElement} representation
 *            will be saved and used later by {@link ElkGraphTransformer#applyLayout()} to set the original element
 *            to its new position after the layouting process has finished. false - the original element has
 *            coordinates relative to another element already moved and doesn't need new coordinates after
 *            layouting.
 * @param offsetX
 *            X-Coordinate of the offset needed to make the given element's coordinates absolute (only needed, when
 *            the element has coordinates not relative to its parent {@link ElkNode}).
 * @param offsetY
 *            Y-Coordinate of the offset needed to make the given element's coordinates absolute (only needed, when
 *            the element has coordinates not relative to its parent {@link ElkNode}).
 * @return result of transformation
 * @throws IllegalArgumentException
 *             A {@link ElkGraphElement} other than {@link ElkNode}, {@link ElkLabel} or {@link ElkPort} has been
 *             given as an argument.
 */
private <E extends ElkShape> DCElement importElkShape(final E element, final boolean considerWhenApplyingOffset, final double offsetX, final double offsetY) throws IllegalArgumentException {
    if (!(element instanceof ElkNode || element instanceof ElkLabel || element instanceof ElkPort)) {
        throw new IllegalArgumentException("Method only works for ElkNode-, ElkLabel and ElkPort-objects.");
    }
    double halfComponentSpacing = componentSpacing / 2;
    double x0 = element.getX() + offsetX - halfComponentSpacing;
    double y0 = element.getY() + offsetY - halfComponentSpacing;
    double x1 = x0 + element.getWidth() + componentSpacing;
    double y1 = y0 + element.getHeight() + componentSpacing;
    KVectorChain coords = new KVectorChain();
    coords.add(newPoint(x0, y0));
    coords.add(newPoint(x0, y1));
    coords.add(newPoint(x1, y1));
    coords.add(newPoint(x1, y0));
    DCElement shape = new DCElement(coords);
    // copy all properties of original layout
    shape.copyProperties(element);
    if (considerWhenApplyingOffset) {
        elementMapping.put(element, shape);
    }
    return shape;
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkLabel(org.eclipse.elk.graph.ElkLabel) ElkPort(org.eclipse.elk.graph.ElkPort) KVectorChain(org.eclipse.elk.core.math.KVectorChain) DCElement(org.eclipse.elk.alg.disco.graph.DCElement)

Example 43 with ElkLabel

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

the class ElkGraphTransformer method applyLayout.

@Override
public void applyLayout() {
    KVector graphDimensions = transformedGraph.getDimensions();
    double newWidth = graphDimensions.x;
    double newHeight = graphDimensions.y;
    double oldWidth = parent.getWidth();
    double oldHeight = parent.getHeight();
    // Adjust size of layout
    parent.setDimensions(graphDimensions.x, graphDimensions.y);
    double xFactor = newWidth / oldWidth;
    double yFactor = newHeight / oldHeight;
    for (ElkLabel label : parent.getLabels()) {
        label.setX(label.getX() * xFactor);
        label.setY(label.getY() * yFactor);
    }
    for (ElkPort port : parent.getPorts()) {
        double px = port.getX();
        double py = port.getY();
        if (px > 0) {
            port.setX(px * xFactor);
        }
        if (py > 0) {
            port.setY(py * yFactor);
        }
    }
    // Apply offsets, whenever necessary.
    elementMapping.forEach(new OffsetApplier());
    List<ElkPort> adjustedPorts = Lists.newArrayList();
    ElkPort portToAdjust;
    for (Entry<ElkEdge, DCExtension> inEntry : incomingExtensionsMapping.entrySet()) {
        ElkEdge edge = inEntry.getKey();
        DCDirection dir = inEntry.getValue().getDirection();
        ElkEdgeSection edgeSection = ElkGraphUtil.firstEdgeSection(edge, false, false);
        KVectorChain newPoints = adjustFirstSegment(ElkGraphUtil.getSourceNode(edge), ElkUtil.createVectorChain(edgeSection), dir);
        ElkUtil.applyVectorChain(newPoints, edgeSection);
        portToAdjust = ElkGraphUtil.getSourcePort(edge);
        if (portToAdjust != null && !adjustedPorts.contains(portToAdjust)) {
            adjustedPorts.add(portToAdjust);
            adjustRelatedPort(portToAdjust, newPoints.getFirst(), dir);
        }
    }
    for (Entry<ElkEdge, DCExtension> outEntry : outgoingExtensionsMapping.entrySet()) {
        ElkEdge edge = outEntry.getKey();
        DCDirection dir = outEntry.getValue().getDirection();
        ElkEdgeSection edgeSection = ElkGraphUtil.firstEdgeSection(edge, false, false);
        KVectorChain newPoints = adjustFirstSegment(ElkGraphUtil.getTargetNode(edge), KVectorChain.reverse(ElkUtil.createVectorChain(edgeSection)), dir);
        newPoints = KVectorChain.reverse(newPoints);
        ElkUtil.applyVectorChain(newPoints, edgeSection);
        portToAdjust = ElkGraphUtil.getTargetPort(edge);
        if (portToAdjust != null && !adjustedPorts.contains(portToAdjust)) {
            adjustedPorts.add(portToAdjust);
            adjustRelatedPort(portToAdjust, newPoints.getLast(), dir);
        }
    }
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel) ElkPort(org.eclipse.elk.graph.ElkPort) KVectorChain(org.eclipse.elk.core.math.KVectorChain) DCExtension(org.eclipse.elk.alg.disco.graph.DCExtension) DCDirection(org.eclipse.elk.alg.disco.graph.DCDirection) KVector(org.eclipse.elk.core.math.KVector) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 44 with ElkLabel

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

the class ElkGraphTransformer method importElkEdge.

/**
 * Transforms a {@link ElkEdge} into a {@link DCElement} without destroying it. Edges can have their own
 * {@link ElkLabel ElkLabels}, so they will be transformed, too.
 *
 * @param edge
 *            Edge to be transformed into a {@link DCElement}
 * @param newComponent
 *            Collection representing the component the edge and its associated labels (if any) belong to. Newly
 *            generated {@link DCElement DCElements} will be added to it
 * @return {@link DCElement} resulting from the transformation
 */
private DCElement importElkEdge(final ElkEdge edge, final Collection<DCElement> newComponent) {
    ElkEdgeSection edgeSection = ElkGraphUtil.firstEdgeSection(edge, false, false);
    List<KVector> points = ElkUtil.createVectorChain(edgeSection);
    double thickness = edge.getProperty(DisCoOptions.EDGE_THICKNESS);
    KVectorChain contour = getContour(points, thickness + componentSpacing);
    DCElement shape = new DCElement(contour);
    shape.copyProperties(edge);
    elementMapping.put(edge, shape);
    newComponent.add(shape);
    // ElkEdges can have labels, too!
    List<ElkLabel> labels = edge.getLabels();
    for (ElkLabel label : labels) {
        // "true" - ElkLabels belonging to an ElkEdge have absolute coordinates and have to be considered when
        // applying
        // changes to the DCGraph back to the original graph.
        DCElement componentLabel = importElkShape(label, true, 0.0f, 0.0f);
        newComponent.add(componentLabel);
    }
    return shape;
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel) KVectorChain(org.eclipse.elk.core.math.KVectorChain) DCElement(org.eclipse.elk.alg.disco.graph.DCElement) KVector(org.eclipse.elk.core.math.KVector) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection)

Example 45 with ElkLabel

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

the class ElkGraphTransformer method importExtension.

/**
 * Transforms a {@link ElkEdge} into a {@link DCElement} without destroying it. Edges can have their own
 * {@link ElkLabel ElkLabels}, so they will be transformed, too.
 *
 * @param edge
 *            Edge to be transformed into a {@link DCElement}
 * @param newComponent
 *            Collection representing the component the edge and its associated labels (if any) belong to. Newly
 *            generated {@link DCElement DCElements} will be added to it
 * @return {@link DCElement} resulting from the transformation
 */
/**
 * Transforms a short hierarchical {@link ElkEdge} into a {@link DCExtension} without destroying it. Edges can have
 * their own {@link ElkLabel ElkLabels}, so they will be transformed, too.
 *
 * @param edge
 *            Edge to be transformed into a {@link DCExtension}
 * @param newComponent
 *            component the extension will belong to
 * @param outgoingExtension
 *            true, if the edge should be handled as an outgoing extension; false, otherwise
 */
private void importExtension(final ElkEdge edge, final Collection<DCElement> newComponent, final boolean outgoingExtension) {
    ElkEdgeSection edgeSection = ElkGraphUtil.firstEdgeSection(edge, false, false);
    KVectorChain points = ElkUtil.createVectorChain(edgeSection);
    if (outgoingExtension) {
        points = KVectorChain.reverse(points);
    }
    // Is going to hold the extension
    DCElement shape;
    double thickness = edge.getProperty(DisCoOptions.EDGE_THICKNESS);
    KVector outerPoint = points.getFirst();
    KVector innerPoint = points.get(1);
    if (points.size() > 2) {
        List<KVector> fixedEdgePoints = Lists.newArrayList();
        fixedEdgePoints.addAll(points.subList(1, points.size()));
        KVectorChain contour = getContour(fixedEdgePoints, thickness + componentSpacing);
        shape = new DCElement(contour);
        shape.copyProperties(edge);
        newComponent.add(shape);
    } else {
        if (outgoingExtension) {
            shape = elementMapping.get(ElkGraphUtil.getSourceNode(edge));
        } else {
            shape = elementMapping.get(ElkGraphUtil.getTargetNode(edge));
        }
    }
    // Construct the extension and add to mapping
    ElkNode extParent = ElkGraphUtil.getSourceNode(edge);
    if (outgoingExtension) {
        extParent = ElkGraphUtil.getTargetNode(edge);
    }
    DCDirection dir = nearestSide(outerPoint, extParent);
    double extensionWidth = thickness + componentSpacing;
    KVector middlePos;
    if (dir.isHorizontal()) {
        // West or east extension
        extensionWidth += Math.abs(outerPoint.y - innerPoint.y);
        middlePos = new KVector(innerPoint.x, (innerPoint.y + outerPoint.y) / 2);
    } else {
        extensionWidth += Math.abs(outerPoint.x - innerPoint.x);
        middlePos = new KVector((innerPoint.x + outerPoint.x) / 2, innerPoint.y);
    }
    if (outgoingExtension) {
        outgoingExtensionsMapping.put(edge, new DCExtension(shape, dir, middlePos, extensionWidth));
    } else {
        incomingExtensionsMapping.put(edge, new DCExtension(shape, dir, middlePos, extensionWidth));
    }
    elementMapping.put(edge, shape);
    // ElkEdges can have labels, too!
    List<ElkLabel> labels = edge.getLabels();
    for (ElkLabel label : labels) {
        // "true" - ElkLabels belonging to an ElkEdge have absolute coordinates and have to be considered when
        // applying
        // changes to the DCGraph back to the original graph.
        DCElement componentLabel = importElkShape(label, true, 0.0f, 0.0f);
        newComponent.add(componentLabel);
    }
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkLabel(org.eclipse.elk.graph.ElkLabel) KVectorChain(org.eclipse.elk.core.math.KVectorChain) DCExtension(org.eclipse.elk.alg.disco.graph.DCExtension) DCElement(org.eclipse.elk.alg.disco.graph.DCElement) DCDirection(org.eclipse.elk.alg.disco.graph.DCDirection) KVector(org.eclipse.elk.core.math.KVector) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection)

Aggregations

ElkLabel (org.eclipse.elk.graph.ElkLabel)64 ElkNode (org.eclipse.elk.graph.ElkNode)33 KVector (org.eclipse.elk.core.math.KVector)25 ElkPort (org.eclipse.elk.graph.ElkPort)23 ElkEdge (org.eclipse.elk.graph.ElkEdge)20 ElkEdgeSection (org.eclipse.elk.graph.ElkEdgeSection)14 KVectorChain (org.eclipse.elk.core.math.KVectorChain)12 ElkPadding (org.eclipse.elk.core.math.ElkPadding)10 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)9 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)7 PortSide (org.eclipse.elk.core.options.PortSide)6 List (java.util.List)5 LLabel (org.eclipse.elk.alg.layered.graph.LLabel)5 LPort (org.eclipse.elk.alg.layered.graph.LPort)5 ElkBendPoint (org.eclipse.elk.graph.ElkBendPoint)5 ElkConnectableShape (org.eclipse.elk.graph.ElkConnectableShape)5 Test (org.junit.Test)5 Lists (com.google.common.collect.Lists)4 Rectangle (org.eclipse.draw2d.geometry.Rectangle)4 DCElement (org.eclipse.elk.alg.disco.graph.DCElement)4