Using filtering to lay out a part of an IlvGrapher

Filter support

Important
To understand this topic better, read the topic Using the graph model first.
Applications sometimes need to perform a layout algorithm on a subset of the nodes and links of a graph. The support for such partial layouts is a filtering mechanism.

Built-in filtering

For applications that use IlvGrapher, a filtering feature is built into the IlvGrapherAdapter class. To do a partial layout, the IlvGrapherAdapter instance needs a way to know which nodes and links to include in the layout. This is the role of the “filter” class, IlvLayoutGraphicFilter.

Custom filtering

If the graph is not an IlvGrapher , the custom adapter should support the filtering of a graph, see Laying out a non-JViews grapher. The methods that are related to the structure of the graph ( getNodes , getLinks , getNeighbors , and so on as shown in Information on the structure of the graph) must behave just as if the graph has changed in some way. They must take into account only the nodes and links that belong to the part of the graph that must be laid out.

The graphic filter class

The IlvLayoutGraphicFilter class implements the interface IlvGraphicFilter, that is, its method:
boolean accept(IlvGraphic nodeOrLink)   
If a filter is specified, the IlvGrapherAdapter calls the accept method for each node or link whenever necessary. If the method returns true , the IlvGrapherAdapter considers the node or the link as part of the graph that needs to be laid out. Otherwise, it ignores the node or the link.
To specify a filter on an IlvGrapherAdapter , use the following method of the IlvGrapherAdapter class:
void setFilter(IlvLayoutGraphicFilter filter)   
To remove the filter, call the setFilter method with a null argument.
To obtain the filter that has been specified, use the method:
IlvLayoutGraphicFilter getFilter()   
Note
All overridden implementations of the accept method must respect the following rule: a link cannot be accepted by the filter if any of its end nodes (origin or destination nodes) are not accepted.
There are two ways to filter an IlvGrapher : by layers or by graphic objects. The two methods can be combined.

Filtering by layers

Inside an IlvGrapher , nodes and links can be managed by layers, see the IlvManagerLayerclass. Rogue Wave®  JViews allows you specify that only nodes and links belonging to certain layers have to be taken into account when performing the layout. Use the following methods of the IlvGrapherAdapter class:
void addLayer(IlvManagerLayer layer)   
boolean removeLayer(IlvManagerLayer layer)   
boolean removeAllLayers()   
boolean isLayerAdded()   
To get an enumeration of the manager layers to be taken into account during the layout, use the method:
Enumeration getLayers()  
If no layers have been specified or all the specified layers have been removed, all layers in the IlvGrapher are used. In this case, the getLayers method returns null .
When at least one layer is specified, an IlvLayoutGraphicFilter is created internally if it has not already been specified using the setFilter method. The default implementation of its accept method will automatically check whether a node or a link received as an argument belongs to one of the specified layers.

Filtering by graphic objects

To filter nodes and links individually, you need to write a custom subclass of IlvLayoutGraphicFilter.. You must embed the filtering rules in the implementation when you override the accept method. For example, an application could use user properties to “mark” nodes and links to be accepted by the filter. The filter class could then be written as follows:
public class LayoutFilter
  extends IlvLayoutGraphicFilter
{
  public LayoutFilter()
  {
  }
  public boolean accept(IlvGraphic obj)
  {
    Object prop = obj.getProperty("markedObj");
    if (prop == null)
      return false;
    // accept a link only if its two end-nodes are accepted
    if (obj instanceof IlvLinkImage) {
      IlvLinkImage link = (IlvLinkImage)obj;
      return (link.getFrom().getProperty("markedObj") != null && 
              link.getTo().getProperty("markedObj") != null);
    }
    return true;
  }
}