If the layout algorithms provided with Rogue Wave® JViews Diagrammer do not
meet your needs, you can develop your own layout algorithms by
subclassing IlvGraphLayout.
When a subclass of
IlvGraphLayout
is created, it automatically fits into the generic Rogue Wave JViews Diagrammer layout
framework and benefits from its infrastructure:
- Generic parameters: see Base class parameters and features
- Notification of progress: see Using event listeners
- Capability to lay out any graph object using the generic graph model: see Using the graph model
- Capability to apply the layout separately for the connected components of a disconnected graph: see Laying out connected components of a disconnected graph
- Capability to lay out nested graphs (see Nested layouts), and so on.
Example
To illustrate the basic ideas for defining a new layout, the
following simple example shows a possible implementation of the
simplest layout algorithm, the Random Layout. The new layout
class is called
MyRandomLayout
.
The following code shows the skeleton of
the class:
public class MyRandomLayout extends IlvGraphLayout { public MyRandomLayout() { } public MyRandomLayout(MyRandomLayout source) { super.source(source); } public IlvGraphLayout copy() { return new MyRandomLayout(this); } protected void layout(boolean redraw) { ... } }
The constructor with no arguments is
empty.
The
copy
constructor and the
copy
method are implemented, because they are used when laying out a
nested graph (see Nested layouts).
Then, the abstract method
layout(boolean)
of the base class is implemented as follows:
protected void layout(boolean redraw) { // obtain the graph model IlvGraphModel graphModel = getGraphModel(); // obtain the layout report IlvGraphLayoutReport layoutReport = getLayoutReport(); // obtain the layout region IlvRect rect = getCalcLayoutRegion(); float xMin = rect.x; float yMin = rect.y; float xMax = rect.x + rect.width; float yMax = rect.y + rect.height; // initialize the random generator Random random = (isUseSeedValueForRandomGenerator()) ? new Random(getSeedValueForRandomGenerator()) : new Random(); // browse the objects in the grapher Enumeration nodes = graphModel.getNodes(); while (nodes.hasMoreElements()) { Object node = nodes.nextElement(); // skip fixed nodes if (isPreserveFixedNodes() && isFixed(node))) continue; // compute coordinates float x = xMin + (xMax - xMin) * random.nextFloat(); float y = yMin + (yMax - yMin) * random.nextFloat(); // move the node to the computed position graphModel.moveNode(node, x, y, redraw); // notify listeners on layout events callLayoutStepPerformedIfNeeded(); } // set the layout report code layoutReport.setCode(IlvGraphLayoutReport.LAYOUT_DONE); } ...
The
layout
method is
protected
, which is the access type of the method in the base class.
This does not prevent a user outside the package that contains
the class from performing the layout, because it is started by
using the public method
performLayout
.