ELK Layered with JavaFx [message #1830802] |
Tue, 04 August 2020 12:54 |
Rakesh Nidavani Messages: 25 Registered: March 2018 |
Junior Member |
|
|
Hello Team,
I am trying to use ELK layered algorithm with JavaFx.
The positions of the nodes after the ELK run is not expected.
My implementation is a below:
1) Created root node using (ElkGraphUtil.createGraph();)
2) Created three children nodes using (ElkGraphFactory.eINSTANCE.createElkNode())
3) Created two edges using (ElkGraphUtil.createSimpleEdge(sourceNode, destNode))
i) From first node to second.
ii) From first node to third.
Can you please check and assist me. I have the output diagram.
import org.eclipse.elk.alg.layered.LayeredLayoutProvider;
import org.eclipse.elk.alg.layered.options.LayeredOptions;
import org.eclipse.elk.alg.layered.options.NodePlacementStrategy;
import org.eclipse.elk.alg.layered.options.WrappingStrategy;
import org.eclipse.elk.core.data.LayoutAlgorithmData;
import org.eclipse.elk.core.data.LayoutMetaDataService;
import org.eclipse.elk.core.options.CoreOptions;
import org.eclipse.elk.core.options.Direction;
import org.eclipse.elk.core.options.EdgeRouting;
import org.eclipse.elk.core.util.BasicProgressMonitor;
import org.eclipse.elk.graph.ElkBendPoint;
import org.eclipse.elk.graph.ElkEdge;
import org.eclipse.elk.graph.ElkEdgeSection;
import org.eclipse.elk.graph.ElkGraphFactory;
import org.eclipse.elk.graph.ElkNode;
import org.eclipse.elk.graph.util.ElkGraphUtil;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polyline;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
* @author Rakesh
*/
public class TestNodes extends Application {
/**
* {@inheritDoc}
*/
@Override
public void start(final Stage primaryStage) throws Exception {
LayoutAlgorithmData data = LayoutMetaDataService.getInstance()
.getAlgorithmDataBySuffixOrDefault("org.eclipse.elk.layered", "org.eclipse.elk.layered");
Group root = new Group();
HBox hbox = new HBox(root);
ScrollPane scrollPane = new ScrollPane(hbox);
Scene scene = new Scene(scrollPane, 1200, 800);
ElkNode rootEle = ElkGraphUtil.createGraph();
rootEle.setProperty(CoreOptions.RESOLVED_ALGORITHM, data);
rootEle.setProperty(CoreOptions.EDGE_ROUTING, EdgeRouting.ORTHOGONAL);
rootEle.setProperty(CoreOptions.DIRECTION, Direction.UP);
rootEle.setProperty(CoreOptions.SPACING_EDGE_NODE, 40.0);
rootEle.setProperty(LayeredOptions.WRAPPING_STRATEGY, WrappingStrategy.SINGLE_EDGE);
rootEle.setProperty(LayeredOptions.WRAPPING_CORRECTION_FACTOR, 100.0);
rootEle.setProperty(LayeredOptions.NODE_PLACEMENT_STRATEGY, NodePlacementStrategy.NETWORK_SIMPLEX);
rootEle.setWidth(100);
createChildrens(rootEle);
LayeredLayoutProvider layeredLayoutProvider = new LayeredLayoutProvider();
layeredLayoutProvider.initialize(null);
try {
BasicProgressMonitor progressMonitor = new BasicProgressMonitor();
layeredLayoutProvider.layout(rootEle, progressMonitor);
}
finally {
layeredLayoutProvider.dispose();
}
EList<ElkNode> children = rootEle.getChildren();
for (ElkNode elkNode : children) {
Rectangle rectangle1 = new Rectangle();
rectangle1.setLayoutX(elkNode.getX());
rectangle1.setLayoutY(elkNode.getY());
rectangle1.setWidth(40);
rectangle1.setHeight(40);
rectangle1.setFill(Color.BLACK);
rectangle1.setStroke(Color.BLACK);
root.getChildren().add(rectangle1);
// edges
EList<ElkEdge> containedEdges = new BasicEList<>();
// containedEdges.addAll(elkNode.getIncomingEdges());
containedEdges.addAll(elkNode.getOutgoingEdges());
for (ElkEdge edge : containedEdges) {
EList<ElkEdgeSection> sections = edge.getSections();
final Polyline polyline = new Polyline();
for (ElkEdgeSection section : sections) {
polyline.getPoints().add(section.getStartX());
polyline.getPoints().add(section.getStartY());
EList<ElkBendPoint> bendPoints = section.getBendPoints();
for (ElkBendPoint elkBendPoint : bendPoints) {
polyline.getPoints().add(elkBendPoint.getX());
polyline.getPoints().add(elkBendPoint.getY());
}
polyline.getPoints().add(section.getEndX());
polyline.getPoints().add(section.getEndY());
}
root.getChildren().add(polyline);
}
}
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param rootELKNode
*/
private void createChildrens(final ElkNode rootELKNode) {
ElkNode one = ElkGraphFactory.eINSTANCE.createElkNode();
one.setIdentifier("One");
ElkNode two = ElkGraphFactory.eINSTANCE.createElkNode();
two.setIdentifier("Two");
ElkNode three = ElkGraphFactory.eINSTANCE.createElkNode();
three.setIdentifier("three");
rootELKNode.getChildren().add(one);
rootELKNode.getChildren().add(two);
rootELKNode.getChildren().add(three);
ElkEdge edge = ElkGraphUtil.createSimpleEdge(one, two);
ElkEdge edge1 = ElkGraphUtil.createSimpleEdge(one, three);
rootELKNode.getContainedEdges().add(edge);
rootELKNode.getContainedEdges().add(edge1);
}
/**
* @param args :
*/
public static void main(final String[] args) {
launch(args);
}
Thank you,
Rakesh
-
Attachment: ELK.png
(Size: 3.32KB, Downloaded 137 times)
[Updated on: Thu, 29 June 2023 11:38] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.25327 seconds