use of org.eclipse.elk.graph.ElkEdge in project sirius-components by eclipse-sirius.
the class DiagramConverterTests method testDiagramOneEdgeBetweenTwoBorderNodes.
@Test
public void testDiagramOneEdgeBetweenTwoBorderNodes() {
ImageSizeProvider imageSizeProvider = new ImageSizeProvider();
ELKDiagramConverter diagramConverter = new ELKDiagramConverter(this.textBoundsService, imageSizeProvider);
// @formatter:off
TestDiagramBuilder diagramBuilder = new TestDiagramBuilder();
Node firstBorderNode = Node.newNode(diagramBuilder.getNode(FIRST_NODE_ID)).style(diagramBuilder.getRectangularNodeStyle()).build();
Node secondBorderNode = Node.newNode(diagramBuilder.getNode(SECOND_NODE_ID)).style(diagramBuilder.getRectangularNodeStyle()).build();
Node node = Node.newNode(diagramBuilder.getNode(THIRD_NODE_ID)).style(diagramBuilder.getImageNodeStyle()).borderNodes(List.of(firstBorderNode, secondBorderNode)).build();
Edge edge = Edge.newEdge(diagramBuilder.getEdge(FIRST_EDGE_ID, firstBorderNode.getId(), secondBorderNode.getId())).build();
Diagram diagram = Diagram.newDiagram(diagramBuilder.getDiagram(DIAGRAM_ID)).nodes(List.of(node)).edges(List.of(edge)).build();
// @formatter:on
ELKConvertedDiagram convertedDiagram = diagramConverter.convert(diagram);
ElkNode elkDiagram = convertedDiagram.getElkDiagram();
assertThat(elkDiagram.getChildren().size()).isEqualTo(1);
assertThat(elkDiagram.getChildren().get(0)).isInstanceOf(ElkNode.class);
assertThat(elkDiagram.getContainedEdges().size()).isEqualTo(1);
assertThat(elkDiagram.getContainedEdges().get(0)).isInstanceOf(ElkEdge.class);
ElkEdge elkEdge = elkDiagram.getContainedEdges().get(0);
assertThat(elkEdge.getSources().size()).isEqualTo(1);
assertThat(elkEdge.getSources().get(0).getIdentifier()).isEqualTo(firstBorderNode.getId().toString());
assertThat(elkEdge.getTargets().size()).isEqualTo(1);
assertThat(elkEdge.getTargets().get(0).getIdentifier()).isEqualTo(secondBorderNode.getId().toString());
imageSizeProvider.dispose();
}
use of org.eclipse.elk.graph.ElkEdge in project sirius-components by eclipse-sirius.
the class DiagramConverterTests method testDiagramOneNodeAndOneEdge.
@Test
public void testDiagramOneNodeAndOneEdge() {
ImageSizeProvider imageSizeProvider = new ImageSizeProvider();
ELKDiagramConverter diagramConverter = new ELKDiagramConverter(this.textBoundsService, imageSizeProvider);
// @formatter:off
TestDiagramBuilder diagramBuilder = new TestDiagramBuilder();
Node node = Node.newNode(diagramBuilder.getNode(FIRST_NODE_ID)).style(diagramBuilder.getImageNodeStyle()).build();
Edge edge = Edge.newEdge(diagramBuilder.getEdge(FIRST_EDGE_ID, node.getId(), node.getId())).build();
Diagram diagram = Diagram.newDiagram(diagramBuilder.getDiagram(DIAGRAM_ID)).nodes(List.of(node)).edges(List.of(edge)).build();
// @formatter:on
ELKConvertedDiagram convertedDiagram = diagramConverter.convert(diagram);
ElkNode elkDiagram = convertedDiagram.getElkDiagram();
assertThat(elkDiagram.getContainedEdges().size()).isEqualTo(1);
ElkEdge elkEdge = elkDiagram.getContainedEdges().get(0);
assertThat(elkEdge.getIdentifier()).isEqualTo(edge.getId().toString());
imageSizeProvider.dispose();
}
use of org.eclipse.elk.graph.ElkEdge in project elk by eclipse.
the class NGraph method writeDebugGraph.
/**
* Converts this {@link NGraph} to a KGraph and writes it to the specified filed.
*
* @param filePath a path to a file on the filesystem
*/
public void writeDebugGraph(final String filePath) {
ElkNode elkGraph = ElkGraphUtil.createGraph();
elkGraph.setProperty(CoreOptions.DIRECTION, Direction.DOWN);
Map<NNode, ElkNode> nodeMap = Maps.newHashMap();
for (NNode nNode : nodes) {
ElkNode elkNode = ElkGraphUtil.createNode(elkGraph);
nodeMap.put(nNode, elkNode);
ElkGraphUtil.createLabel(nNode.type + " " + nNode.layer, elkNode);
}
for (NNode nNode : nodes) {
for (NEdge nEdge : nNode.getOutgoingEdges()) {
ElkEdge elkEdge = ElkGraphUtil.createSimpleEdge(nodeMap.get(nEdge.source), nodeMap.get(nEdge.target));
ElkGraphUtil.createLabel(nEdge.weight + " " + nEdge.delta, elkEdge);
}
}
ResourceSet rs = new ResourceSetImpl();
Resource r = rs.createResource(URI.createFileURI(filePath));
r.getContents().add(elkGraph);
try {
r.save(Collections.emptyMap());
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.eclipse.elk.graph.ElkEdge 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.elk.graph.ElkEdge in project elk by eclipse.
the class StraightLineEdgeRouter method routeEdges.
/**
* Route edges from node center to node center. Then clip it, to not cross the node.
*/
public void routeEdges(final ElkNode node) {
for (ElkEdge edge : ElkGraphUtil.allOutgoingEdges(node)) {
if (!(edge.getSources().get(0) instanceof ElkPort)) {
ElkNode target = ElkGraphUtil.connectableShapeToNode(edge.getTargets().get(0));
if (!edge.isHierarchical()) {
double sourceX = node.getX() + node.getWidth() / 2;
double sourceY = node.getY() + node.getHeight() / 2;
double targetX = target.getX() + target.getWidth() / 2;
double targetY = target.getY() + target.getHeight() / 2;
// Clipping
KVector vector = new KVector();
vector.x = targetX - sourceX;
vector.y = targetY - sourceY;
KVector sourceClip = new KVector(vector.x, vector.y);
ElkMath.clipVector(sourceClip, node.getWidth(), node.getHeight());
vector.x -= sourceClip.x;
vector.y -= sourceClip.y;
sourceX = targetX - vector.x;
sourceY = targetY - vector.y;
KVector targetClip = new KVector(vector.x, vector.y);
ElkMath.clipVector(targetClip, target.getWidth(), target.getHeight());
vector.x -= targetClip.x;
vector.y -= targetClip.y;
targetX = sourceX + vector.x;
targetY = sourceY + vector.y;
ElkEdgeSection section = ElkGraphUtil.firstEdgeSection(edge, true, true);
section.setStartLocation(sourceX, sourceY);
section.setEndLocation(targetX, targetY);
routeEdges(target);
}
}
}
}
Aggregations