The TreeSDMNode class

The class TreeSDMNode represents the nodes of the graph. Its definition is as shown in the following code example.
public class TreeSDMNode implements IlvSDMNode
{

Data stored

Each node in the graph (graphic object) has a reference to the corresponding node in the tree (model object). References to the parent node (graphic object) and the tree model are also needed. The children of the node are stored in a vector.
Keeping track of the data: current node, parent, children, and model
private TreeSDMNode parent;
private TreeModel treeModel;
private Object treeNode;
private Vector children;

public TreeSDMNode(TreeModel treeModel, TreeSDMNode parent, Object treeNode)
{
  this.parent = parent;
  this.treeModel = treeModel;
  this.treeNode = treeNode;
  
  createChildren();
 }

Children vector

Note that the constructor calls the method createChildren . This method traverses the tree model and creates a TreeSDMNode instance for each item in the tree. In addition, it creates an instance of TreeSDMLink to draw a link between the parent node and each child node.
Creating the child nodes and links (graphic objects)
private void createChildren()
{
  children = new Vector();
  
  // scan all the children of the root tree node, and create
  // a TreeSDMNode for each:
  //
  int count = treeModel.getChildCount(treeNode);
  for(int i = 0; i < count; i++){
    Object childTreeNode = treeModel.getChild(treeNode, i);
    
    // Create the TreeSDMNode. Note that this will recursively create
    // the SDM nodes for all sub-nodes.
    //
    TreeSDMNode childSDMNode = new TreeSDMNode(treeModel, this, childTreeNode);
    children.addElement(childSDMNode);
    
    // Create a parent/child link:
    //
    TreeSDMLink childSDMLink = new TreeSDMLink(this, childSDMNode);
    children.addElement(childSDMLink);
  }
}

Implementation of the IlvSDMNode interface

The following methods are the implementations of the methods belonging to the IlvSDMNode interface, which are inherited by TreeSDMNode .
The getTag method returns the type (or “tag”) of the node. In this example, the type is treenode .
Retrieving the node type: the getTag method
  public String getTag()

  {
    return "treenode";
  }
The getID method returns the identifier of the node. The identifier of a node is its hash code.
Retrieving the node ID: the getID method
  public String getID()
{
    return String.valueOf(hashCode());
  }
The getChildren method returns the children of the node, which are stored in the children data member.
Retrieving the child objects: the getChildren method
public Enumeration getChildren()

  {
    return children.elements();
  }
The getParent method returns the parent node of the current node.
Retrieving the parent object: the getParent method
  public IlvSDMNode getParent()
  {
    return parent;
  }
The getProperty and getPropertyNames methods must be implemented to give the diagram component access to the properties of the node. In this example, there is support for two properties, userObject and CSSclass :
  • The userObject property returns the sample food, color, and sports names.
  • The CSSclass property returns the type of item: food , color, or sport .
Retrieving a property: the getProperty method
  public Object getProperty(String property)
  {
    if(treeNode instanceof DefaultMutableTreeNode){
      if(property.equals("userObject")){
        return ((DefaultMutableTreeNode)treeNode).getUserObject();
      }
      if(property.equals("CSSclass") && ((DefaultMutableTreeNode)treeNode).getParent() != null){
        return ((DefaultMutableTreeNode)((DefaultMutableTreeNode)treeNode).getParent()).getUserObject();
      }
    }
    return null;
  }
The getPropertyNames method retrieves the two property names.
Retrieving property names: the getPropertyNames method
  public String[] getPropertyNames()
  {
    if(treeNode instanceof DefaultMutableTreeNode)
      return new String[] { "userObject", "CSSclass" };
    else
      return new String[0];
  }