Using event listeners

All layout classes support two kinds of events: layout events and parameter events. The listening mechanism therefore provides:

Graph layout event listeners

The layout event listening mechanism provides a way to inform the user of what is happening during the layout. At times, a layout algorithm can take a long time to execute, especially when dealing with large graphs. In addition, an algorithm might not converge in some cases. No matter what the situation, the user must be informed of the events that occur during the layout.
It can be done by displaying appropriate information, such as the percentage of completion after each iteration or step.
To receive layout events delivered during the layout, you must connect (using dojo.connect) to the appropriate on <Event> method.
The following events are triggered:
  1. onLayoutStepPerformed(layoutStarted, layoutFinished): A layout step performed event indicates when a step in the graph layout execution is performed. It also indicates when the layout is finished or stopped.
  2. onAttached(): This event is triggered when the graph layout is attached to a graph layout model. A graph layout must be attached to a graph layout model in order to be executed.
  3. onDetached(): This event is triggered when the graph layout is detached from a graph layout model.
To listen for graph layout events, you must implement a listener function and connect it (using dojo.connect) to the appropriate method of the graph layout instance.
See Using a graph layout report. In this way, you can read information about the current state of the layout report. (For example, you can read this information after each iteration or step of the layout algorithm).
The following example shows how to implement and connect a layout event listener:
dojo.connect(layout, "onLayoutStepPerformed", function(layoutStarted, layoutFinished){
 console.log("started = " + layoutStarted + " finished = " + layoutFinished);
 console.log("percentage of completion: " + layout.getLayoutReport().getPercentageComplete());
});

Parameter event listeners

The layout parameter event listeners mechanism provides a way to inform the user that any layout parameter has changed.
To receive the layout parameter events, you must implement a listener function and register it by connecting to the onParameterChanged event.
The following events can be received:
  1. ibm_ilog.graphlayout.GraphLayout.onObjectParameterChanged(nodeOrLink, parameterName): This event is triggered whenever a local layout parameter (that is, a parameter that applies to a specific node or link) changes.
  2. ibm_ilog.graphlayout.GraphLayout.onParameterChanged(parameterName): This event is triggered whenever a global layout parameter is changed.
The following example shows how to implement a layout parameter event listener.
 dojo.connect(layout, "onParameterChanged", function(parameterName){
  console.log("parameter " + parameterName + " has changed");
 });