Search in sources :

Example 1 with ConnectionEditPart

use of org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart in project elk by eclipse.

the class GmfDiagramLayoutConnector method processConnections.

/**
 * Creates new edges and takes care of the labels for each connection identified in the
 * {@code buildLayoutGraphRecursively} method.
 *
 * @param mapping
 *            the layout mapping
 */
protected void processConnections(final LayoutMapping mapping) {
    Map<EReference, ElkEdge> reference2EdgeMap = new HashMap<>();
    for (ConnectionEditPart connection : mapping.getProperty(CONNECTIONS)) {
        boolean isOppositeEdge = false;
        Optional<EdgeLabelPlacement> edgeLabelPlacement = Optional.empty();
        ElkEdge edge;
        // Check whether the edge belongs to an Ecore reference, which may have opposites.
        // This is required for the layout of Ecore diagrams, since the bend points of
        // opposite references are kept synchronized by the editor.
        EObject modelObject = connection.getNotationView().getElement();
        if (modelObject instanceof EReference) {
            EReference reference = (EReference) modelObject;
            edge = reference2EdgeMap.get(reference.getEOpposite());
            if (edge != null) {
                edgeLabelPlacement = Optional.of(EdgeLabelPlacement.TAIL);
                isOppositeEdge = true;
            } else {
                edge = ElkGraphUtil.createEdge(null);
                reference2EdgeMap.put(reference, edge);
            }
        } else {
            edge = ElkGraphUtil.createEdge(null);
        }
        BiMap<Object, ElkGraphElement> inverseGraphMap = mapping.getGraphMap().inverse();
        // find a proper source node and source port
        ElkGraphElement sourceElem;
        EditPart sourceObj = connection.getSource();
        if (sourceObj instanceof ConnectionEditPart) {
            sourceElem = inverseGraphMap.get(((ConnectionEditPart) sourceObj).getSource());
            if (sourceElem == null) {
                sourceElem = inverseGraphMap.get(((ConnectionEditPart) sourceObj).getTarget());
            }
        } else {
            sourceElem = inverseGraphMap.get(sourceObj);
        }
        ElkConnectableShape sourceShape = null;
        ElkPort sourcePort = null;
        ElkNode sourceNode = null;
        if (sourceElem instanceof ElkNode) {
            sourceNode = (ElkNode) sourceElem;
            sourceShape = sourceNode;
        } else if (sourceElem instanceof ElkPort) {
            sourcePort = (ElkPort) sourceElem;
            sourceNode = sourcePort.getParent();
            sourceShape = sourcePort;
        } else {
            continue;
        }
        // find a proper target node and target port
        ElkGraphElement targetElem;
        EditPart targetObj = connection.getTarget();
        if (targetObj instanceof ConnectionEditPart) {
            targetElem = inverseGraphMap.get(((ConnectionEditPart) targetObj).getTarget());
            if (targetElem == null) {
                targetElem = inverseGraphMap.get(((ConnectionEditPart) targetObj).getSource());
            }
        } else {
            targetElem = inverseGraphMap.get(targetObj);
        }
        ElkConnectableShape targetShape = null;
        ElkNode targetNode = null;
        ElkPort targetPort = null;
        if (targetElem instanceof ElkNode) {
            targetNode = (ElkNode) targetElem;
            targetShape = targetNode;
        } else if (targetElem instanceof ElkPort) {
            targetPort = (ElkPort) targetElem;
            targetNode = targetPort.getParent();
            targetShape = targetPort;
        } else {
            continue;
        }
        // calculate offset for edge and label coordinates
        ElkNode edgeContainment = ElkGraphUtil.findLowestCommonAncestor(sourceNode, targetNode);
        KVector offset = new KVector();
        ElkUtil.toAbsolute(offset, edgeContainment);
        if (!isOppositeEdge) {
            // set source and target
            edge.getSources().add(sourceShape);
            edge.getTargets().add(targetShape);
            // now that source and target are set, put the edge into the graph
            edgeContainment.getContainedEdges().add(edge);
            mapping.getGraphMap().put(edge, connection);
            // store the current coordinates of the edge
            setEdgeLayout(edge, connection, offset);
        }
        // process edge labels
        processEdgeLabels(mapping, connection, edge, edgeLabelPlacement, offset);
    }
}
Also used : ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) ElkNode(org.eclipse.elk.graph.ElkNode) HashMap(java.util.HashMap) ElkPort(org.eclipse.elk.graph.ElkPort) ResizableCompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ResizableCompartmentEditPart) ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) RootEditPart(org.eclipse.gef.RootEditPart) CompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart) RenderedDiagramRootEditPart(org.eclipse.gmf.runtime.diagram.ui.render.editparts.RenderedDiagramRootEditPart) EditPart(org.eclipse.gef.EditPart) IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) AbstractBorderItemEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart) LabelEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart) ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) DiagramEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject) EdgeLabelPlacement(org.eclipse.elk.core.options.EdgeLabelPlacement) KVector(org.eclipse.elk.core.math.KVector) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) EReference(org.eclipse.emf.ecore.EReference) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 2 with ConnectionEditPart

use of org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart in project elk by eclipse.

the class GmfDiagramLayoutConnector method addConnections.

/**
 * Adds all target connections and connected connections to the list of connections that must be
 * processed later.
 *
 * @param mapping
 *            the layout mapping
 * @param editPart
 *            an edit part
 */
protected void addConnections(final LayoutMapping mapping, final IGraphicalEditPart editPart) {
    for (Object targetConn : editPart.getTargetConnections()) {
        if (targetConn instanceof ConnectionEditPart) {
            ConnectionEditPart connectionEditPart = (ConnectionEditPart) targetConn;
            if (editPartFilter.filter(connectionEditPart)) {
                mapping.getProperty(CONNECTIONS).add(connectionEditPart);
                addConnections(mapping, connectionEditPart);
            }
        }
    }
}
Also used : ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) EObject(org.eclipse.emf.ecore.EObject)

Example 3 with ConnectionEditPart

use of org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart in project elk by eclipse.

the class GmfLayoutEditPolicy method addLabelLayout.

/**
 * Adds an edge label layout to the given command.
 *
 * @param command
 *            command to which the edge label layout shall be added
 * @param klabel
 *            label with layout data
 * @param labelEditPart
 *            edit part to which layout is applied
 * @param scale
 *            scale factor for coordinates
 */
private void addLabelLayout(final GmfLayoutCommand command, final ElkLabel klabel, final GraphicalEditPart labelEditPart, final double scale) {
    ElkGraphElement parent = klabel.getParent();
    // node and port labels are processed separately
    if (parent instanceof ElkNode || parent instanceof ElkPort) {
        View view = (View) labelEditPart.getModel();
        int xpos = (int) (klabel.getX() * scale);
        int ypos = (int) (klabel.getY() * scale);
        Object oldx = ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_X());
        Object oldy = ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_Y());
        if (oldx == null || oldy == null || xpos != (Integer) oldx || ypos != (Integer) oldy) {
            command.addShapeLayout(view, new Point(xpos, ypos), null);
        }
        return;
    } else if (parent instanceof ElkEdge) {
        // calculate direct new location of the label
        Rectangle targetBounds = new Rectangle(labelEditPart.getFigure().getBounds());
        targetBounds.x = (int) (klabel.getX() * scale);
        targetBounds.y = (int) (klabel.getY() * scale);
        ConnectionEditPart connectionEditPart = (ConnectionEditPart) labelEditPart.getParent();
        PointList bendPoints = getBendPoints((ElkEdge) parent, connectionEditPart.getFigure(), scale);
        EObject modelElement = connectionEditPart.getNotationView().getElement();
        EdgeLabelPlacement labelPlacement = klabel.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
        // the list of bend points must be reversed
        if (modelElement instanceof EReference && labelPlacement == EdgeLabelPlacement.TAIL) {
            bendPoints = bendPoints.getCopy();
            bendPoints.reverse();
        }
        // get the referencePoint for the label
        int fromEnd, keyPoint = ConnectionLocator.MIDDLE;
        if (labelEditPart instanceof LabelEditPart) {
            keyPoint = ((LabelEditPart) labelEditPart).getKeyPoint();
        }
        switch(keyPoint) {
            case ConnectionLocator.SOURCE:
                fromEnd = SOURCE_LOCATION;
                break;
            case ConnectionLocator.TARGET:
                fromEnd = TARGET_LOCATION;
                break;
            default:
                fromEnd = MIDDLE_LOCATION;
                break;
        }
        Point refPoint = PointListUtilities.calculatePointRelativeToLine(bendPoints, 0, fromEnd, true);
        // get the new relative location
        Point normalPoint = offsetFromRelativeCoordinate(targetBounds, bendPoints, refPoint);
        if (normalPoint != null) {
            command.addShapeLayout((View) labelEditPart.getModel(), normalPoint, null);
        }
    }
}
Also used : PointList(org.eclipse.draw2d.geometry.PointList) LabelEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart) ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) Rectangle(org.eclipse.draw2d.geometry.Rectangle) Point(org.eclipse.draw2d.geometry.Point) PrecisionPoint(org.eclipse.draw2d.geometry.PrecisionPoint) View(org.eclipse.gmf.runtime.notation.View) Point(org.eclipse.draw2d.geometry.Point) PrecisionPoint(org.eclipse.draw2d.geometry.PrecisionPoint) EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject) EdgeLabelPlacement(org.eclipse.elk.core.options.EdgeLabelPlacement) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) EReference(org.eclipse.emf.ecore.EReference) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 4 with ConnectionEditPart

use of org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart in project elk by eclipse.

the class GmfLayoutEditPolicy method addEdgeLayout.

/**
 * Adds an edge layout to the given command.
 *
 * @param command
 *            command to which an edge layout shall be added
 * @param elkEdge
 *            edge with layout data
 * @param connectionEditPart
 *            edit part to which layout is applied
 * @param scale
 *            scale factor for coordinates
 */
private void addEdgeLayout(final GmfLayoutCommand command, final ElkEdge elkEdge, final ConnectionEditPart connectionEditPart, final double scale) {
    if (connectionEditPart.getSource() != null && connectionEditPart.getTarget() != null) {
        // create source terminal identifier
        INodeEditPart sourceEditPart = (INodeEditPart) connectionEditPart.getSource();
        ConnectionAnchor sourceAnchor;
        if (sourceEditPart instanceof ConnectionEditPart) {
            // if the edge source is a connection, don't consider the source point
            sourceAnchor = new SlidableAnchor(sourceEditPart.getFigure());
        } else {
            KVector sourceRel = getRelativeSourcePoint(elkEdge);
            sourceAnchor = new SlidableAnchor(sourceEditPart.getFigure(), new PrecisionPoint(sourceRel.x, sourceRel.y));
        }
        String sourceTerminal = sourceEditPart.mapConnectionAnchorToTerminal(sourceAnchor);
        // create target terminal identifier
        INodeEditPart targetEditPart = (INodeEditPart) connectionEditPart.getTarget();
        ConnectionAnchor targetAnchor;
        if (targetEditPart instanceof ConnectionEditPart) {
            // if the edge target is a connection, don't consider the target point
            targetAnchor = new SlidableAnchor(targetEditPart.getFigure());
        } else {
            KVector targetRel = getRelativeTargetPoint(elkEdge);
            targetAnchor = new SlidableAnchor(targetEditPart.getFigure(), new PrecisionPoint(targetRel.x, targetRel.y));
        }
        String targetTerminal = targetEditPart.mapConnectionAnchorToTerminal(targetAnchor);
        PointList bendPoints = getBendPoints(elkEdge, connectionEditPart.getFigure(), scale);
        // check whether the connection is a note attachment to an edge, then remove bend points
        if (sourceEditPart instanceof ConnectionEditPart || targetEditPart instanceof ConnectionEditPart) {
            while (bendPoints.size() > 2) {
                bendPoints.removePoint(1);
            }
        }
        // retrieve junction points and transform them to absolute coordinates
        KVectorChain junctionPoints = elkEdge.getProperty(CoreOptions.JUNCTION_POINTS);
        String serializedJP = null;
        if (junctionPoints != null) {
            for (KVector point : junctionPoints) {
                ElkUtil.toAbsolute(point, elkEdge.getContainingNode());
            }
            serializedJP = junctionPoints.toString();
        }
        command.addEdgeLayout((Edge) connectionEditPart.getModel(), bendPoints, sourceTerminal, targetTerminal, serializedJP);
    }
}
Also used : PointList(org.eclipse.draw2d.geometry.PointList) ConnectionAnchor(org.eclipse.draw2d.ConnectionAnchor) ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) INodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.INodeEditPart) KVectorChain(org.eclipse.elk.core.math.KVectorChain) SlidableAnchor(org.eclipse.gmf.runtime.gef.ui.figures.SlidableAnchor) KVector(org.eclipse.elk.core.math.KVector) PrecisionPoint(org.eclipse.draw2d.geometry.PrecisionPoint)

Example 5 with ConnectionEditPart

use of org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart in project elk by eclipse.

the class GmfDiagramLayoutConnector method buildLayoutGraph.

@Override
public LayoutMapping buildLayoutGraph(final IWorkbenchPart workbenchPart, final Object diagramPart) {
    // get the diagram editor part
    DiagramEditor diagramEditor = getDiagramEditor(workbenchPart);
    // choose the layout root edit part
    IGraphicalEditPart layoutRootPart = null;
    List<ShapeNodeEditPart> selectedParts = null;
    if (diagramPart instanceof ShapeNodeEditPart || diagramPart instanceof DiagramEditPart) {
        layoutRootPart = (IGraphicalEditPart) diagramPart;
    } else if (diagramPart instanceof IGraphicalEditPart) {
        EditPart tgEditPart = ((IGraphicalEditPart) diagramPart).getTopGraphicEditPart();
        if (tgEditPart instanceof ShapeNodeEditPart) {
            layoutRootPart = (IGraphicalEditPart) tgEditPart;
        }
    } else if (diagramPart instanceof Collection) {
        Collection<?> selection = (Collection<?>) diagramPart;
        // determine the layout root part from the selection
        for (Object object : selection) {
            if (object instanceof IGraphicalEditPart) {
                if (layoutRootPart != null) {
                    EditPart parent = commonParent(layoutRootPart, (EditPart) object);
                    if (parent != null && !(parent instanceof RootEditPart)) {
                        layoutRootPart = (IGraphicalEditPart) parent;
                    }
                } else if (!(object instanceof ConnectionEditPart)) {
                    layoutRootPart = (IGraphicalEditPart) object;
                }
            }
        }
        // build a list of edit parts that shall be layouted completely
        if (layoutRootPart != null) {
            selectedParts = new ArrayList<ShapeNodeEditPart>(selection.size());
            for (Object object : selection) {
                if (object instanceof IGraphicalEditPart) {
                    EditPart editPart = (EditPart) object;
                    while (editPart != null && editPart.getParent() != layoutRootPart) {
                        editPart = editPart.getParent();
                    }
                    if (editPart instanceof ShapeNodeEditPart && editPartFilter.filter(editPart) && !selectedParts.contains(editPart)) {
                        selectedParts.add((ShapeNodeEditPart) editPart);
                    }
                }
            }
        }
    }
    if (layoutRootPart == null && diagramEditor != null) {
        layoutRootPart = diagramEditor.getDiagramEditPart();
    }
    if (layoutRootPart == null) {
        throw new IllegalArgumentException("Not supported by this layout connector: Workbench part " + workbenchPart + ", Edit part " + diagramPart);
    }
    // create the mapping
    LayoutMapping mapping = buildLayoutGraph(layoutRootPart, selectedParts, workbenchPart);
    return mapping;
}
Also used : IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) DiagramEditor(org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor) ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) ResizableCompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ResizableCompartmentEditPart) ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) RootEditPart(org.eclipse.gef.RootEditPart) CompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart) RenderedDiagramRootEditPart(org.eclipse.gmf.runtime.diagram.ui.render.editparts.RenderedDiagramRootEditPart) EditPart(org.eclipse.gef.EditPart) IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) AbstractBorderItemEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart) LabelEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart) ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) DiagramEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart) DiagramEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart) Collection(java.util.Collection) EObject(org.eclipse.emf.ecore.EObject) LayoutMapping(org.eclipse.elk.core.service.LayoutMapping) RootEditPart(org.eclipse.gef.RootEditPart) RenderedDiagramRootEditPart(org.eclipse.gmf.runtime.diagram.ui.render.editparts.RenderedDiagramRootEditPart)

Aggregations

ConnectionEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart)6 EObject (org.eclipse.emf.ecore.EObject)4 KVector (org.eclipse.elk.core.math.KVector)3 ElkNode (org.eclipse.elk.graph.ElkNode)3 DiagramEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart)3 LabelEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart)3 ShapeNodeEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart)3 PointList (org.eclipse.draw2d.geometry.PointList)2 PrecisionPoint (org.eclipse.draw2d.geometry.PrecisionPoint)2 Rectangle (org.eclipse.draw2d.geometry.Rectangle)2 EdgeLabelPlacement (org.eclipse.elk.core.options.EdgeLabelPlacement)2 LayoutMapping (org.eclipse.elk.core.service.LayoutMapping)2 ElkEdge (org.eclipse.elk.graph.ElkEdge)2 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)2 ElkPort (org.eclipse.elk.graph.ElkPort)2 EReference (org.eclipse.emf.ecore.EReference)2 EditPart (org.eclipse.gef.EditPart)2 RootEditPart (org.eclipse.gef.RootEditPart)2 AbstractBorderItemEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart)2 CompartmentEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart)2