In an application that does not work directly on graphs,
operations such as attaching or detaching a graph layout instance
must be performed explicitly.
The base class
The ibm_ilog.graphlayout.GraphLayout class is the base class for all layout algorithms.
This class is an abstract class and cannot be used directly. You must
use one of its subclasses: ibm_ilog.graphlayout.hierarchical.HierarchicalLayout, ibm_ilog.graphlayout.tree.TreeLayout, ibm_ilog.graphlayout.forcedirected.ForceDirectedLayout, ibm_ilog.graphlayout.shortlink.ShortLinkLayout, ibm_ilog.graphlayout.longlink.LongLinkLayout, ibm_ilog.graphlayout.random.RandomLayout, ibm_ilog.graphlayout.circular.CircularLayout, ibm_ilog.graphlayout.grid.GridLayout. You can also create your own subclasses to implement
other layout algorithms. See Defining your own type of layout.
Although only subclasses of
GraphLayout
are
directly used to obtain the layouts, you need to learn about this
class because it contains methods that are inherited (or overridden)
by the subclasses. You must understand it to create your own subclasses. 
The Class GraphLayout and its subclasses and relationships
to layout reports
Instantiating a subclass of the base class
The class ibm_ilog.graphlayout.GraphLayout is an abstract class. It has no constructors. You
instantiate a subclass as shown in the following example:
var layout = new ibm_ilog.graphlayout.tree.TreeLayout();
Attaching or detaching the layout to a graph or diagram
In IBM® ILOG® Dojo Diagrammer,
you must attach the layout to an ibm_ilog.diagram.widget.Diagram widget
instance or to an ibm_ilog.diagram.Graph instance.
If you use the Diagram widget,
attach the layout using the nodeLayout or linkLayout attributes
of the Diagram widget. The following
example shows how you can do it in the HTML markup of the Diagram widget:
<div jsId="linkLayout" dojoType="ibm_ilog.graphlayout.shortlink.ShortLinkLayout"/> <div id="diagram" dojoType="ibm_ilog.diagram.widget.Diagram" nodeLayout="treeLayout"/>
The nodeLayout attribute
is used to set the main graph layout algorithm that places the nodes
and links of the Diagram. You can
also use the linkLayout attribute
to set a second layout that routes only the links of the Diagram.
See the following example:
<div jsId="linkLayout" dojoType="ibm_ilog.graphlayout.shortlink.ShortLinkLayout"/> <div id="diagram" dojoType="ibm_ilog.diagram.widget.Diagram" linkLayout="linkLayout"/>
You can also set the node or link layout of a Diagram in
JavaScript:
var diagram = dijit.byId("diagram"); var treeLayout = new ibm_ilog.graphlayout.tree.TreeLayout(); var linkLayout = new ibm_ilog.graphlayout.shortlink.ShortLinkLayout(); diagram.attr("nodeLayout", treeLayout); diagram.attr("linkLayout", linkLayout);
If you work directly with the Graph object
(that is, you do not use the Diagram widget,
but you work directly with the GFX APIs), you must use the setNodeLayout or setLinkLayout methods
to attach the layout to the Graph:
var graph = surface.createGraph(); var treeLayout = new ibm_ilog.graphlayout.tree.TreeLayout(); var linkLayout = new ibm_ilog.graphlayout.shortlink.ShortLinkLayout(); graph.setNodeLayout(treeLayout); graph.setLinkLayout(linkLayout);
Performing a layout
After the layout algorithm is attached to a Diagram or Graph instance,
use the performGraphLayout method
of the Diagram or Graph to
start the layout:
diagram.performGraphLayout();
or
graph.performGraphLayout();
The protected abstract method
layout(boolean
redraw)
of the GraphLayout subclass
is then called. This means that the control is passed to the subclasses
that implement this method. The implementation computes the layout
and moves the nodes to new positions or reshapes the links, or both
moves the nodes and reshapes the links. The
getLayoutReport
method
of the graph layout returns an instance of ibm_ilog.graphlayout.GraphLayoutReport (or of a subclass) that contains information about
the behavior of the layout algorithm. It tells you whether the algorithm
performed normally, or whether a particular, predefined case occurred.
(For a more detailed description of the layout report, see Using a graph layout report.) Further information
You can find more information about the class ibm_ilog.graphlayout.GraphLayout in the following sections:
- Base class parameters and features contains the methods that are related to the customization of the layout algorithms.
- Using event listeners tells you about the layout event listener mechanism.
- Defining your own type of layout tells you how to implement new subclasses.
For details on ibm_ilog.graphlayout.GraphLayout and
other graph layout classes, see the IBM® ILOG® Dojo Diagrammer API
Reference Documentation.