Developing with the SDK > Using and Writing Data Models > NonJavaBeans Example: Abstract Model Variant > The TreeSDMNode Class

The class TreeSDMNode represents the nodes of the graph. Its definition is as shown in Code Sample 2.17.

public class TreeSDMNode implements IlvSDMNode
{

Code Sample 2.17 A Tree Node Based on the SDM Node

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.

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();
 }

Code Sample 2.18 Keeping Track of the Data: Current Node, Parent, Children, and Model

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.

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);
  }
}

Code Sample 2.19 Creating the Child Nodes and Links (Graphic Objects)

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.

  public String getTag()

  {
    return "treenode";
  }

Code Sample 2.20 Retrieving the Node Type: the getTag Method

The getID method returns the identifier of the node. The identifier of a node is its hash code.

  public String getID()
{
    return String.valueOf(hashCode());
  }

Code Sample 2.22 Retrieving the Node ID: the getID Method

The getChildren method returns the children of the node, which are stored in the children data member.

public Enumeration getChildren()

  {
    return children.elements();
  }

Code Sample 2.23 Retrieving the Child Objects: the getChildren Method

The getParent method returns the parent node of the current node.

  public IlvSDMNode getParent()
  {
    return parent;
  }

Code Sample 2.24 Retrieving the Parent Object: the getParent Method

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:

  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;
  }

Code Sample 2.25 Retrieving a Property: the getProperty Method

The getPropertyNames method retrieves the two property names, see Code Sample 2.26.

  public String[] getPropertyNames()
  {
    if(treeNode instanceof DefaultMutableTreeNode)
      return new String[] { "userObject", "CSSclass" };
    else
      return new String[0];
  }

Code Sample 2.26 Retrieving Property Names: the getPropertyNames Method