Graph layout: using the API

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

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.
UML
class diagram showing the subclasses of the abstract class GraphLayout
and the relationships of some of these classes to the various layout
reports. The class diagram uses the normal UML conventions to represent
the classes and relationships. The classes that implement the graph
layout algorithms are shown on the left of the diagram. The layout
report classes are on the right.
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:
For details on ibm_ilog.graphlayout.GraphLayout and other graph layout classes, see the IBM® ILOG® Dojo Diagrammer API Reference Documentation.