Implementing the layout method

Depending on the characteristics of the layout algorithm, some of the steps required can be different or unnecessary, or other steps can be required.
Depending on the particular implementation of your layout algorithm, other methods of the base graph layout class must be overridden. For instance, if your subclass supports some of the generic parameters of the base class, you must override the supports[ParameterName] method (see Base class parameters and features).
For further information about the class IlvGraphLayout, refer to the API reference documentation.
Note
If you want to save the layout parameters of your new layout algorithm in .ivl files, you should override the methods createLayoutGrapherProperty, createLayoutNodeProperty, createLayoutLinkProperty, and subclass IlvGraphLayoutGrapherProperty, IlvGraphLayoutNodeProperty, and IlvGraphLayoutLinkProperty. See Saving layout parameters and preferred layouts for more explanation.
To implement the layout method in the sample custom layout algorithm:
  1. Obtain the graph model ( getGraphModel() on the layout instance).
    IlvGraphModel graphModel = getGraphModel();
    
  2. Obtain the instance of the layout report that is automatically created when the performLayout method from the superclass is called ( getLayoutReport() on the layout instance, see Using a graph layout report.
    IlvGraphLayoutReport layoutReport = getLayoutReport(); 
    
  3. Obtain the layout region parameter to compute the area where the nodes are placed.
    IlvRect rect = getCalcLayoutRegion(); 
    
  4. Initialize the random generator.
    Random random = (isUseSeedValueForRandomGenerator()) ? 
            new Random(getSeedValueForRandomGenerator()) :
            new Random(); 
    
    (For information on the seed value parameter, see Random generator seed value.)
  5. Get an enumeration of the nodes ( getNodes() on the graph model instance).
    Enumeration nodes = graphModel.getNodes(); 
    
  6. Browse the nodes, skipping fixed nodes ( isFixed(node) on the layout instance) if asked by the user ( isPreserveFixedNodes() on the layout instance).
    while (nodes.hasMoreElements()) { 
       Object node = nodes.nextElement(); 
    ...
    
    (For details on fixed nodes, see Preserve fixed nodes).
  7. Move each node to the newly computed coordinates inside the layout region ( graphModel.moveNode ).
    graphModel.moveNode(node, x, y, redraw); 
    
  8. Notify the listeners on layout events that a new node was positioned ( callLayoutStepPerformedIfNeeded() on the layout instance). It allows the user to implement, for example, a progress bar if a layout event listener was registered on the layout instance.
    callLayoutStepPerformedIfNeeded();
    
    (For details on event listeners, see Using event listeners.)
  9. Finally, set the code in the layout report.
    layoutReport.setCode(IlvGraphLayoutReport.LAYOUT_DONE); 
    
Once you have implemented your own layout algorithm MyRandomLayout , you can add it to a CSS file to use it in a diagram component. Since your new layout algorithm is not one of the predefined graph layout algorithms, you need to specify it as fully qualified in CSS:
SMD {
    GraphLayout: true;
}
GraphLayout {
    graphLayout: "mypackage.MyRandomLayout";
    // ... additionally, any bean property of MyRandomLayout can
    // be specified here ...
}