Explains how to invoke a layout algorithm from within a desktop application so that the GUI stays responsive
while the layout is being calculated.
| Note |
|
Starting with version 2.7 yFiles for Java Complete provides the new class Graph2DLayoutExecutor which eases performing a layout. It provides an easier way of running a layout in a separate thread than the one described below. We recommend using Graph2DLayoutExecutor, if your distribution includes it. |
Since the calculation of a graph layout may take a long time, it is desirable to invoke a layout algorithm within a separate
thread so that the GUI of the application stays responsive. Ensuring that the graph can be properly displayed
while it is being laid out requires that the layout algorithm works on a copy of the graph. The layout result needs to
be applied to the original graph in a synchronized manner.
The following code represents an action event code that invokes a layout algorithm within a separate thread.
class ThreadedLayoutAction extends AbstractAction {
boolean layoutMorphingEnabled = true;
public ThreadedLayoutAction() {
super("Threaded Layout");
}
public void actionPerformed(ActionEvent ev) {
final OrganicLayouter layouter = new OrganicLayouter();
final CopiedLayoutGraph cGraph = new CopiedLayoutGraph(view.getGraph2D());
final Thread thread = new Thread() {
public void run() {
layouter.doLayout(cGraph);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (layoutMorphingEnabled) {
GraphLayout gl = cGraph.getLayoutForOriginalGraph();
new LayoutMorpher(view, gl).execute();
}
else {
cGraph.commitLayoutToOriginalGraph();
view.fitContent();
view.updateView();
}
}
});
}
};
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
|
| Warning |
|
When applying the calculated layout to the original graph it is important that no nodes or edges have been added or removed from the graph during the layout process.
|
Attached is an executable demo program that makes use of the ThreadedLayoutAction. It extends
demo.view.ViewActionDemo and should be placed in the yFiles demo package demo.view to function properly.