Using Algorithms Directly
All layout algorithms implemented in the Eclipse Layout Kernel can be called directly. To do so, you must have an instance of our KGraph data structure that can be fed to the layout algorithm, which will then compute a layout for the graph’s elements.
Warning: This is very low-level stuff. Most people will want to use the next layer of abstraction, which also works with pure Java applications.
The Entry Point to Layout Algorithms
When using layout algorithms directly, the central most important type you will deal with is the AbstractLayoutProvider
class. A layout provider basically consists of three methods:
void initialize(String parameter);
void layout(KNode layoutGraph, IElkProgressMonitor progressMonitor);
void dispose();
Each layout algorithm provides a subclass of AbstractLayoutProvider
that is the entry point to the algorithm. Using it is a matter of doing the following:
-
Create a KGraph to be laid out (see our graph documentation for how to do so). The KGraph will be based on the diagram you want your application to display.
-
Configure how the layout algorithm should layout your graph by setting TODO layout properties on the graph’s elements. Check the documentation of the layout algorithm you want to use for which layout properties it supports and what they do, exactly.
-
Obtain an instance of the layout algorithm’s implementation of
AbstractLayoutProvider
. For our layer-based algorithm, for example, this is theLayeredLayoutProvider
. Be sure to callinitialize(...)
on it. The parameter can benull
in most cases, unless specified otherwise. -
Call the
layout(...)
method and pass it the graph to be laid out as well as a progress monitor to track progress. The progress monitor will allow users to see how long layout will take and to cancel the operation. If you do not care to provide your own progress monitor, simply useBasicProgressMonitor
, which is our default implementation. Once thelayout(...)
method returns, the result will be stored in the layout-related fields of the graph. -
Either keep your layout provider instance around to be used again for the next layout run, or call
dispose()
on it and throw it away.