Search in sources :

Example 51 with ElkLabel

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

the class LayoutDotExporter method setEdgeLabels.

@Override
protected void setEdgeLabels(final ElkEdge kedge, final List<Attribute> attributes, final boolean isVertical) {
    super.setEdgeLabels(kedge, attributes, isVertical);
    // set label distance and angle
    if (Iterables.any(kedge.getLabels(), (ElkLabel label) -> {
        EdgeLabelPlacement elp = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
        return elp == EdgeLabelPlacement.HEAD || elp == EdgeLabelPlacement.TAIL;
    })) {
        double distance = kedge.getProperty(GraphvizMetaDataProvider.LABEL_DISTANCE);
        if (distance >= 0.0) {
            attributes.add(createAttribute(Attributes.LABELDISTANCE, distance));
        }
        double angle = kedge.getProperty(GraphvizMetaDataProvider.LABEL_ANGLE);
        attributes.add(createAttribute(Attributes.LABELANGLE, angle));
    }
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel) EdgeLabelPlacement(org.eclipse.elk.core.options.EdgeLabelPlacement)

Example 52 with ElkLabel

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

the class DotExporter method applyEdgeLabelPos.

/**
 * Applies the edge label positions for the given edge.
 *
 * @param elkedge edge for which labels are processed
 * @param posString string with label position
 * @param placement label placement to choose
 * @param offsetx x offset added to positions
 * @param offsety y offset added to positions
 */
private void applyEdgeLabelPos(final ElkEdge elkedge, final String posString, final EdgeLabelPlacement placement, final KVector offset) {
    double combinedWidth = 0.0;
    double combinedHeight = 0.0;
    for (ElkLabel label : elkedge.getLabels()) {
        EdgeLabelPlacement elp = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
        if (elp == placement) {
            combinedWidth = Math.max(combinedWidth, label.getWidth());
            combinedHeight += label.getHeight();
        }
    }
    try {
        KVector pos = new KVector();
        pos.parse(posString);
        double xpos = pos.x - combinedWidth / 2 + offset.x;
        double ypos = pos.y - combinedHeight / 2 + offset.y;
        for (ElkLabel label : elkedge.getLabels()) {
            EdgeLabelPlacement elp = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
            if (elp == placement) {
                double xoffset = (combinedWidth - label.getWidth()) / 2;
                label.setX(xpos + xoffset);
                label.setY(ypos);
                ypos += label.getHeight();
            }
        }
    } catch (IllegalArgumentException exception) {
    // ignore exception
    }
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel) EdgeLabelPlacement(org.eclipse.elk.core.options.EdgeLabelPlacement) KVector(org.eclipse.elk.core.math.KVector)

Example 53 with ElkLabel

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

the class DotExporter method setEdgeLabels.

/**
 * Set edge labels for the given edge.
 *
 * @param elkedge edge whose labels shall be set
 * @param attributes edge attribute list to which the labels are added
 * @param isVertical indicates whether vertical layout direction is active
 */
protected void setEdgeLabels(final ElkEdge elkedge, final List<Attribute> attributes, final boolean isVertical) {
    if (elkedge.getLabels().isEmpty()) {
        return;
    }
    // as Graphviz only supports positioning of one label per label placement, all labels
    // are stacked to one big label as workaround
    StringBuilder midLabel = new StringBuilder(), headLabel = new StringBuilder(), tailLabel = new StringBuilder();
    String fontName = null;
    int fontSize = 0;
    boolean isCenterFontName = false, isCenterFontSize = false;
    for (ElkLabel label : elkedge.getLabels()) {
        StringBuilder buffer = midLabel;
        EdgeLabelPlacement placement = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
        boolean takeFontName = false, takeFontSize = false;
        switch(placement) {
            case HEAD:
                takeFontName = fontName == null;
                takeFontSize = fontSize <= 0;
                buffer = headLabel;
                break;
            case TAIL:
                takeFontName = fontName == null;
                takeFontSize = fontSize <= 0;
                buffer = tailLabel;
                break;
            default:
                // CENTER
                takeFontName = fontName == null || !isCenterFontName;
                isCenterFontName = true;
                takeFontSize = fontSize <= 0 || !isCenterFontSize;
                isCenterFontSize = true;
                break;
        }
        if (buffer.length() > 0) {
            buffer.append("\n");
        }
        buffer.append(label.getText());
        if (takeFontName) {
            fontName = label.getProperty(CoreOptions.FONT_NAME);
        }
        if (takeFontSize) {
            fontSize = label.getProperty(CoreOptions.FONT_SIZE);
            // increase the font size to let Graphviz prepare more room for the label
            if (fontSize > 0) {
                fontSize *= FONT_SIZE_MULT;
            }
        }
    }
    // edge overlapping
    if (midLabel.length() > 0) {
        double labelSpacing = elkedge.getProperty(CoreOptions.SPACING_EDGE_LABEL);
        if (labelSpacing < 1) {
            labelSpacing = 0;
        }
        int charsToAdd = (int) labelSpacing - 1;
        for (int i = 0; i < charsToAdd; i++) {
            midLabel.append(isVertical ? "O" : "\nO");
        }
        attributes.add(createAttribute(Attributes.LABEL, createString(midLabel.toString())));
    }
    // set head label
    if (headLabel.length() > 0) {
        attributes.add(createAttribute(Attributes.HEADLABEL, createString(headLabel.toString())));
    }
    // set tail label
    if (tailLabel.length() > 0) {
        attributes.add(createAttribute(Attributes.TAILLABEL, createString(tailLabel.toString())));
    }
    // set font name
    if (fontName != null && fontName.length() > 0) {
        attributes.add(createAttribute(Attributes.FONTNAME, "\"" + fontName + "\""));
    }
    // set font size
    if (fontSize > 0) {
        attributes.add(createAttribute(Attributes.FONTSIZE, fontSize));
    }
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel) EdgeLabelPlacement(org.eclipse.elk.core.options.EdgeLabelPlacement) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint)

Example 54 with ElkLabel

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

the class DotImporter method transformEdge.

/**
 * Transforms an edge.
 *
 * @param statement an edge statement
 * @param parent the parent node
 * @param transData the transformation data instance
 * @param defaultProps default values for edge options
 */
private void transformEdge(final EdgeStatement statement, final ElkNode parent, final IDotTransformationData<GraphvizModel, ElkNode> transData, final IPropertyHolder defaultProps) {
    String sourceName = statement.getSourceNode().getName();
    ElkNode source = transformNode(sourceName, parent, transData);
    ElkPort sourcePort = null;
    if (statement.getSourceNode().getPort() != null) {
        String portName = statement.getSourceNode().getPort().getCompass_pt();
        if (portName == null) {
            portName = statement.getSourceNode().getPort().getName();
        }
        sourcePort = transformPort(portName, source, transData);
    }
    ListIterator<EdgeTarget> targetIter = statement.getEdgeTargets().listIterator();
    while (targetIter.hasNext()) {
        EdgeTarget edgeTarget = targetIter.next();
        ElkEdge elkedge = ElkGraphUtil.createEdge(null);
        if (sourcePort != null) {
            elkedge.getSources().add(sourcePort);
        } else {
            elkedge.getSources().add(source);
        }
        ElkNode target;
        ElkPort targetPort = null;
        Node edgeTargetNode = edgeTarget.getTargetnode();
        String targetName;
        if (edgeTargetNode == null) {
            targetName = edgeTarget.getTargetSubgraph().getName();
            target = transformNode(targetName, parent, transData);
        } else {
            targetName = edgeTargetNode.getName();
            target = transformNode(targetName, parent, transData);
            if (edgeTargetNode.getPort() != null) {
                String portName = edgeTargetNode.getPort().getCompass_pt();
                if (portName == null) {
                    portName = edgeTargetNode.getPort().getName();
                }
                targetPort = transformPort(portName, target, transData);
            }
        }
        if (targetPort != null) {
            elkedge.getTargets().add(targetPort);
        } else {
            elkedge.getTargets().add(target);
        }
        elkedge.copyProperties(defaultProps);
        if (targetIter.previousIndex() == 0) {
            // this is the first target - just store the edge statement
            elkedge.setProperty(PROP_STATEMENT, statement);
        } else {
            // the edge statement has more that one target - create a copy
            EdgeStatement newStatement = DotFactory.eINSTANCE.createEdgeStatement();
            Node sourceNode = DotFactory.eINSTANCE.createNode();
            sourceNode.setName(sourceName);
            newStatement.setSourceNode(sourceNode);
            targetIter.remove();
            newStatement.getEdgeTargets().add(edgeTarget);
            for (Attribute attr : statement.getAttributes()) {
                newStatement.getAttributes().add(EcoreUtil.copy(attr));
            }
            elkedge.setProperty(PROP_STATEMENT, newStatement);
        }
        // evaluate attributes for the new edge
        for (Attribute attr : statement.getAttributes()) {
            String value = trimValue(attr);
            if (Attributes.LABEL.equals(attr.getName())) {
                ElkLabel label = ElkGraphUtil.createLabel(value, elkedge);
                label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.CENTER);
            } else if (Attributes.HEADLABEL.equals(attr.getName())) {
                ElkLabel label = ElkGraphUtil.createLabel(value, elkedge);
                label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.HEAD);
            } else if (Attributes.TAILLABEL.equals(attr.getName())) {
                ElkLabel label = ElkGraphUtil.createLabel(value, elkedge);
                label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.TAIL);
            } else {
                transformAttribute(elkedge, attr, transData);
            }
        }
        // the edge target is the source for the next edge target
        source = target;
        sourceName = targetName;
        sourcePort = targetPort;
    }
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) Attribute(org.eclipse.elk.alg.graphviz.dot.dot.Attribute) ElkLabel(org.eclipse.elk.graph.ElkLabel) ElkPort(org.eclipse.elk.graph.ElkPort) EdgeTarget(org.eclipse.elk.alg.graphviz.dot.dot.EdgeTarget) ElkNode(org.eclipse.elk.graph.ElkNode) Node(org.eclipse.elk.alg.graphviz.dot.dot.Node) EdgeStatement(org.eclipse.elk.alg.graphviz.dot.dot.EdgeStatement) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 55 with ElkLabel

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

the class RandomGraphGenerator method addEdgeLabel.

/**
 * Creates a {@link KLabel} with random text and adds it to the edge.
 *
 * @param edge
 *            the edge to add the label to.
 */
private void addEdgeLabel(final ElkEdge edge) {
    ElkLabel label = ElkGraphUtil.createLabel(BlindTextGenerator.generate(), edge);
    edge.getLabels().add(label);
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel)

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