Search in sources :

Example 36 with ElkLabel

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

the class PortLabelPlacementVariantsTest method assertCentered.

private void assertCentered(final ElkPort port) {
    final ElkLabel label = getLabel(port);
    if (PortSide.SIDES_EAST_WEST.contains(port.getProperty(CoreOptions.PORT_SIDE))) {
        final double portCenter = port.getHeight() / 2.0;
        final double labelCenter = label.getY() + label.getHeight() / 2.0;
        assertThat(labelCenter, OrderingComparison.comparesEqualTo(portCenter));
    } else if (PortSide.SIDES_NORTH_SOUTH.contains(port.getProperty(CoreOptions.PORT_SIDE))) {
        final double portCenter = port.getWidth() / 2.0;
        final double labelCenter = label.getX() + label.getWidth() / 2.0;
        assertThat(labelCenter, OrderingComparison.comparesEqualTo(portCenter));
    } else {
        assertTrue(false);
    }
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel)

Example 37 with ElkLabel

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

the class GmfDiagramLayoutConnector method createNodeLabel.

/**
 * Create a node label while building the layout graph.
 *
 * @param mapping
 *            the layout mapping
 * @param labelEditPart
 *            the label edit part
 * @param nodeEditPart
 *            the parent node edit part
 * @param elknode
 *            the layout node for which the label is set
 * @return the created label
 */
protected ElkLabel createNodeLabel(final LayoutMapping mapping, final IGraphicalEditPart labelEditPart, final IGraphicalEditPart nodeEditPart, final ElkNode elknode) {
    IFigure labelFigure = labelEditPart.getFigure();
    String text = null;
    Font font = null;
    if (labelFigure instanceof WrappingLabel) {
        WrappingLabel wrappingLabel = (WrappingLabel) labelFigure;
        text = wrappingLabel.getText();
        font = wrappingLabel.getFont();
    } else if (labelFigure instanceof Label) {
        Label label = (Label) labelFigure;
        text = label.getText();
        font = label.getFont();
    }
    if (text != null) {
        ElkLabel label = ElkGraphUtil.createLabel(elknode);
        label.setText(text);
        mapping.getGraphMap().put(label, labelEditPart);
        Rectangle labelBounds = getAbsoluteBounds(labelFigure);
        Rectangle nodeBounds = getAbsoluteBounds(nodeEditPart.getFigure());
        label.setLocation(labelBounds.x - nodeBounds.x, labelBounds.y - nodeBounds.y);
        try {
            Dimension size = labelFigure.getPreferredSize();
            label.setDimensions(size.width, size.height);
            if (font != null && !font.isDisposed()) {
                label.setProperty(CoreOptions.FONT_NAME, font.getFontData()[0].getName());
                label.setProperty(CoreOptions.FONT_SIZE, font.getFontData()[0].getHeight());
            }
        } catch (SWTException exception) {
        // ignore exception and leave the label size to (0, 0)
        }
        return label;
    }
    return null;
}
Also used : SWTException(org.eclipse.swt.SWTException) ElkLabel(org.eclipse.elk.graph.ElkLabel) Label(org.eclipse.draw2d.Label) WrappingLabel(org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel) ElkLabel(org.eclipse.elk.graph.ElkLabel) Rectangle(org.eclipse.draw2d.geometry.Rectangle) Dimension(org.eclipse.draw2d.geometry.Dimension) WrappingLabel(org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel) Font(org.eclipse.swt.graphics.Font) IFigure(org.eclipse.draw2d.IFigure)

Example 38 with ElkLabel

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

the class GmfDiagramLayoutConnector method processEdgeLabels.

/**
 * Process the labels of an edge.
 *
 * @param mapping
 *            the layout mapping
 * @param connection
 *            the connection edit part
 * @param edge
 *            the layout edge
 * @param placement
 *            predefined placement for all labels, or {@code Optional#empty()} if the placement shall be derived
 *            from the edit part
 * @param offset
 *            the offset for coordinates
 */
protected void processEdgeLabels(final LayoutMapping mapping, final ConnectionEditPart connection, final ElkEdge edge, final Optional<EdgeLabelPlacement> placement, final KVector offset) {
    /*
         * ars: source and target is exchanged when defining it in the gmfgen file. So if Emma sets
         * a label to be placed as target on a connection, then the label will show up next to the
         * source node in the diagram editor. So correct it here, very ugly.
         */
    for (Object obj : connection.getChildren()) {
        if (obj instanceof LabelEditPart) {
            LabelEditPart labelEditPart = (LabelEditPart) obj;
            IFigure labelFigure = labelEditPart.getFigure();
            // Check if the label is visible in the first place
            if (labelFigure == null || !labelFigure.isVisible()) {
                continue;
            }
            Rectangle labelBounds = getAbsoluteBounds(labelFigure);
            String labelText = null;
            Dimension iconBounds = null;
            if (labelFigure instanceof WrappingLabel) {
                WrappingLabel wrappingLabel = (WrappingLabel) labelFigure;
                labelText = wrappingLabel.getText();
                if (wrappingLabel.getIcon() != null) {
                    iconBounds = new Dimension();
                    iconBounds.width = wrappingLabel.getIcon().getBounds().width + wrappingLabel.getIconTextGap();
                    iconBounds.height = wrappingLabel.getIcon().getBounds().height;
                    // Add more characters to the text for layouters that need the text to
                    // determine the label size.
                    labelText = "O " + labelText;
                }
            } else if (labelFigure instanceof Label) {
                Label label = (Label) labelFigure;
                labelText = label.getText();
                if (label.getIcon() != null) {
                    iconBounds = label.getIconBounds().getSize();
                    iconBounds.width += label.getIconTextGap();
                    // Add more characters to the text for layouters that need the text to
                    // determine the label size.
                    labelText = "O " + labelText;
                }
            }
            if (labelText != null && labelText.length() > 0) {
                ElkLabel label = ElkGraphUtil.createLabel(edge);
                if (!placement.isPresent()) {
                    switch(labelEditPart.getKeyPoint()) {
                        case ConnectionLocator.SOURCE:
                            label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.HEAD);
                            break;
                        case ConnectionLocator.MIDDLE:
                            label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.CENTER);
                            break;
                        case ConnectionLocator.TARGET:
                            label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.TAIL);
                            break;
                    }
                } else {
                    label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, placement.get());
                }
                Font font = labelFigure.getFont();
                if (font != null && !font.isDisposed()) {
                    label.setProperty(CoreOptions.FONT_NAME, font.getFontData()[0].getName());
                    label.setProperty(CoreOptions.FONT_SIZE, font.getFontData()[0].getHeight());
                }
                label.setLocation(labelBounds.x - offset.x, labelBounds.y - offset.y);
                if (iconBounds != null) {
                    label.setWidth(labelBounds.width + iconBounds.width);
                } else {
                    label.setWidth(labelBounds.width);
                }
                label.setHeight(labelBounds.height);
                // We would set the modified flag to false here, but that doesn't exist anymore
                label.setText(labelText);
                mapping.getGraphMap().put(label, labelEditPart);
            } else {
                // add the label to the mapping anyway so it is reset to its reference location
                ElkLabel label = ElkGraphUtil.createLabel(null);
                mapping.getGraphMap().put(label, labelEditPart);
            }
        }
    }
}
Also used : LabelEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart) ElkLabel(org.eclipse.elk.graph.ElkLabel) Rectangle(org.eclipse.draw2d.geometry.Rectangle) Label(org.eclipse.draw2d.Label) WrappingLabel(org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel) ElkLabel(org.eclipse.elk.graph.ElkLabel) EObject(org.eclipse.emf.ecore.EObject) Dimension(org.eclipse.draw2d.geometry.Dimension) WrappingLabel(org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel) Font(org.eclipse.swt.graphics.Font) IFigure(org.eclipse.draw2d.IFigure)

Example 39 with ElkLabel

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

the class GmfDiagramLayoutConnector method createPort.

/**
 * Create a port while building the layout graph.
 *
 * @param mapping
 *            the layout mapping
 * @param portEditPart
 *            the port edit part
 * @param nodeEditPart
 *            the parent node edit part
 * @param elknode
 *            the corresponding layout node
 * @return the created port
 */
protected ElkPort createPort(final LayoutMapping mapping, final AbstractBorderItemEditPart portEditPart, final IGraphicalEditPart nodeEditPart, final ElkNode elknode) {
    ElkPort port = ElkGraphUtil.createPort(elknode);
    // set the port's layout, relative to the node position
    Rectangle portBounds = getAbsoluteBounds(portEditPart.getFigure());
    Rectangle nodeBounds = getAbsoluteBounds(nodeEditPart.getFigure());
    double xpos = portBounds.x - nodeBounds.x;
    double ypos = portBounds.y - nodeBounds.y;
    port.setLocation(xpos, ypos);
    port.setDimensions(portBounds.width, portBounds.height);
    // We would set the modified flag to false here, but that doesn't exist anymore
    mapping.getGraphMap().put(port, portEditPart);
    // store all the connections to process them later
    addConnections(mapping, portEditPart);
    // set the port label
    for (Object portChildObj : portEditPart.getChildren()) {
        if (portChildObj instanceof IGraphicalEditPart) {
            IFigure labelFigure = ((IGraphicalEditPart) portChildObj).getFigure();
            String text = null;
            if (labelFigure instanceof WrappingLabel) {
                text = ((WrappingLabel) labelFigure).getText();
            } else if (labelFigure instanceof Label) {
                text = ((Label) labelFigure).getText();
            }
            if (text != null) {
                ElkLabel portLabel = ElkGraphUtil.createLabel(port);
                portLabel.setText(text);
                mapping.getGraphMap().put(portLabel, (IGraphicalEditPart) portChildObj);
                // set the port label's layout
                Rectangle labelBounds = getAbsoluteBounds(labelFigure);
                portLabel.setLocation(labelBounds.x - portBounds.x, labelBounds.y - portBounds.y);
                try {
                    Dimension size = labelFigure.getPreferredSize();
                    portLabel.setDimensions(size.width, size.height);
                } catch (SWTException exception) {
                // ignore exception and leave the label size to (0, 0)
                }
            // We would set the modified flag to false here, but that doesn't exist anymore
            }
        }
    }
    return port;
}
Also used : IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) SWTException(org.eclipse.swt.SWTException) ElkLabel(org.eclipse.elk.graph.ElkLabel) ElkPort(org.eclipse.elk.graph.ElkPort) Rectangle(org.eclipse.draw2d.geometry.Rectangle) Label(org.eclipse.draw2d.Label) WrappingLabel(org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel) ElkLabel(org.eclipse.elk.graph.ElkLabel) EObject(org.eclipse.emf.ecore.EObject) Dimension(org.eclipse.draw2d.geometry.Dimension) WrappingLabel(org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel) IFigure(org.eclipse.draw2d.IFigure)

Example 40 with ElkLabel

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

the class GmfDiagramLayoutConnector method buildLayoutGraph.

/**
 * Creates the actual mapping given an edit part which functions as the root for the layout.
 *
 * @param layoutRootPart the layout root edit part
 * @param selection a selection of contained edit parts to process, or {@code null} if the whole
 *          content shall be processed
 * @param workbenchPart the workbench part, or {@code null}
 * @return a layout graph mapping
 */
protected LayoutMapping buildLayoutGraph(final IGraphicalEditPart layoutRootPart, final List<ShapeNodeEditPart> selection, final IWorkbenchPart workbenchPart) {
    LayoutMapping mapping = new LayoutMapping(workbenchPart);
    mapping.setProperty(CONNECTIONS, new LinkedList<ConnectionEditPart>());
    mapping.setParentElement(layoutRootPart);
    // find the diagram edit part
    mapping.setProperty(DIAGRAM_EDIT_PART, getDiagramEditPart(layoutRootPart));
    ElkNode topNode;
    if (layoutRootPart instanceof ShapeNodeEditPart) {
        // start with a specific node as root for layout
        topNode = createNode(mapping, (ShapeNodeEditPart) layoutRootPart, null, null, null);
    } else {
        // start with the whole diagram as root for layout
        topNode = ElkGraphUtil.createGraph();
        Rectangle rootBounds = layoutRootPart.getFigure().getBounds();
        if (layoutRootPart instanceof DiagramEditPart) {
            String labelText = ((DiagramEditPart) layoutRootPart).getDiagramView().getName();
            if (labelText.length() > 0) {
                ElkLabel label = ElkGraphUtil.createLabel(topNode);
                label.setText(labelText);
            }
        } else {
            topNode.setLocation(rootBounds.x, rootBounds.y);
        }
        topNode.setDimensions(rootBounds.width, rootBounds.height);
        mapping.getGraphMap().put(topNode, layoutRootPart);
    }
    mapping.setLayoutGraph(topNode);
    if (selection != null && !selection.isEmpty()) {
        // layout only the selected elements
        double minx = Integer.MAX_VALUE;
        double miny = Integer.MAX_VALUE;
        Maybe<ElkPadding> kinsets = new Maybe<>();
        for (ShapeNodeEditPart editPart : selection) {
            ElkNode node = createNode(mapping, editPart, layoutRootPart, topNode, kinsets);
            minx = Math.min(minx, node.getX());
            miny = Math.min(miny, node.getY());
            buildLayoutGraphRecursively(mapping, editPart, node, editPart);
        }
        mapping.setProperty(COORDINATE_OFFSET, new KVector(minx, miny));
    } else {
        // traverse all children of the layout root part
        buildLayoutGraphRecursively(mapping, layoutRootPart, topNode, layoutRootPart);
    }
    // transform all connections in the selected area
    processConnections(mapping);
    return mapping;
}
Also used : ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) Maybe(org.eclipse.elk.core.util.Maybe) ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) ElkNode(org.eclipse.elk.graph.ElkNode) Rectangle(org.eclipse.draw2d.geometry.Rectangle) DiagramEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart) ElkLabel(org.eclipse.elk.graph.ElkLabel) KVector(org.eclipse.elk.core.math.KVector) LayoutMapping(org.eclipse.elk.core.service.LayoutMapping) ElkPadding(org.eclipse.elk.core.math.ElkPadding)

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