Search in sources :

Example 16 with Attribute

use of org.eclipse.elk.alg.graphviz.dot.dot.Attribute 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 17 with Attribute

use of org.eclipse.elk.alg.graphviz.dot.dot.Attribute in project elk by eclipse.

the class DotImporter method transformNode.

/**
 * Transforms a node.
 *
 * @param statement a node statement
 * @param parent the parent node
 * @param transData the transformation data instance
 * @param defaultProps default values for node options
 */
private void transformNode(final NodeStatement statement, final ElkNode parent, final IDotTransformationData<GraphvizModel, ElkNode> transData, final IPropertyHolder defaultProps) {
    ElkNode elknode = transformNode(statement.getNode().getName(), parent, transData);
    if (elknode.getProperty(PROP_STATEMENT) != null) {
        transData.log("Discarding node \"" + statement.getNode().getName() + "\" since its id is already used.");
    } else {
        elknode.copyProperties(defaultProps);
        elknode.setProperty(PROP_STATEMENT, statement);
        Float defWidth = defaultProps.getProperty(PROP_DEF_WIDTH);
        if (defWidth != null) {
            elknode.setWidth(defWidth);
        }
        Float defHeight = defaultProps.getProperty(PROP_DEF_HEIGHT);
        if (defHeight != null) {
            elknode.setHeight(defHeight);
        }
        // evaluate attributes for the new node
        for (Attribute attr : statement.getAttributes()) {
            String value = trimValue(attr);
            try {
                if (Attributes.LABEL.equals(attr.getName())) {
                    ElkGraphUtil.createLabel(value, elknode);
                } else if (Attributes.POS.equals(attr.getName())) {
                    KVector pos = new KVector();
                    pos.parse(value);
                    pos.scale(DotExporter.DPI);
                    elknode.setLocation(pos.x, pos.y);
                } else if (Attributes.WIDTH.equals(attr.getName())) {
                    elknode.setWidth(Float.parseFloat(value) * DotExporter.DPI);
                } else if (Attributes.HEIGHT.equals(attr.getName())) {
                    elknode.setHeight(Float.parseFloat(value) * DotExporter.DPI);
                } else {
                    transformAttribute(elknode, attr, transData);
                }
            } catch (NumberFormatException exception) {
                transData.log("Discarding attribute \"" + attr.getName() + "\" for node \"" + statement.getNode().getName() + "\" since its value could not be parsed correctly.");
            } catch (IllegalArgumentException exception) {
                transData.log("Discarding attribute \"" + attr.getName() + "\" for node \"" + statement.getNode().getName() + "\" since its value could not be parsed correctly.");
            }
        }
    }
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) Attribute(org.eclipse.elk.alg.graphviz.dot.dot.Attribute) KVector(org.eclipse.elk.core.math.KVector)

Aggregations

Attribute (org.eclipse.elk.alg.graphviz.dot.dot.Attribute)17 KVector (org.eclipse.elk.core.math.KVector)8 ElkNode (org.eclipse.elk.graph.ElkNode)8 AttributeStatement (org.eclipse.elk.alg.graphviz.dot.dot.AttributeStatement)7 EdgeStatement (org.eclipse.elk.alg.graphviz.dot.dot.EdgeStatement)7 Node (org.eclipse.elk.alg.graphviz.dot.dot.Node)5 NodeStatement (org.eclipse.elk.alg.graphviz.dot.dot.NodeStatement)5 Subgraph (org.eclipse.elk.alg.graphviz.dot.dot.Subgraph)5 EdgeTarget (org.eclipse.elk.alg.graphviz.dot.dot.EdgeTarget)3 Statement (org.eclipse.elk.alg.graphviz.dot.dot.Statement)3 ElkPadding (org.eclipse.elk.core.math.ElkPadding)3 ElkEdge (org.eclipse.elk.graph.ElkEdge)3 NoSuchElementException (java.util.NoSuchElementException)2 StringTokenizer (java.util.StringTokenizer)2 ElkLabel (org.eclipse.elk.graph.ElkLabel)2 LinkedList (java.util.LinkedList)1 Graph (org.eclipse.elk.alg.graphviz.dot.dot.Graph)1 GraphvizModel (org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel)1 Port (org.eclipse.elk.alg.graphviz.dot.dot.Port)1 DotSwitch (org.eclipse.elk.alg.graphviz.dot.dot.util.DotSwitch)1