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);
}
}
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);
}
}
}
}
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);
}
}
}
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);
}
}
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;
}
Aggregations