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

To display the data model of a tree in a diagram component, you must write a data model that transforms the tree data into an SDM model. In this example, the model is implemented by a subclass of IlvBasicSDMModel , as shown in Code Sample 2.36.

public class TreeSDMModel2 extends IlvBasicSDMModel
{
...

Code Sample 2.36 A Data Model Based on the Basic SDM Model

This SDM model will use the tree nodes taken from the JTree model directly, instead of implementing a new class to represent the nodes of the graph. This approach has the advantage of saving one object allocation for each node.

However, a new class is needed for the links, because there is no object that represents a parent-child relationship in a JTree model.

Reference to the Tree Model

The TreeSDMModel2 class keeps a reference to the tree model, as shown in Code Sample 2.37.

private TreeModel treeModel;
  private ArrayList links = new ArrayList();

  public TreeSDMModel2(TreeModel treeModel)
  {
    this.treeModel = treeModel;
    
    createLinks(treeModel.getRoot());
  }

Code Sample 2.37 Keeping Track of the Tree Model

Method for Creating Links

The createLinks method creates the parent-child links and stores them in a list, as shown in Code Sample 2.38.

private void createLinks(Object treeNode)
  {
    for(int i = 0; i < treeModel.getChildCount(treeNode); i++){
      Object childNode = treeModel.getChild(treeNode, i);
      links.add(new TreeLink(treeNode, childNode));
      
      createLinks(childNode);
    }
  }

Code Sample 2.38 Creating the Parent-Child Links between Nodes

Method for Retrieving All Nodes and Links

The getObjects method (shown in Code Sample 2.39) returns all the nodes of the tree recursively, and also returns the links that have been created in the constructor. Note that, in this variant, the data model is flat, that is, the tree hierarchy does not translate into subgraphs, so all nodes and links are at the same level in the SDM model.

public Enumeration getObjects()
  {
    Vector v = new Vector();
    
    getTreeNodes(treeModel.getRoot(), v);
    
    for (int i = 0; i < links.size(); i++) {
      v.addElement(links.get(i));
    }
    
    return v.elements();
  }
  
  private void getTreeNodes(Object parentNode, Vector v)
  {
    v.addElement(parentNode);
    for(int i = 0; i < treeModel.getChildCount(parentNode); i++){
      getTreeNodes(treeModel.getChild(parentNode, i), v);
    }
  }

Code Sample 2.39 Retrieving Nodes and Links

Methods of the SDM Model Interface

The methods of the SDM model interface are implemented directly in the subclass of IlvBasicSDMModel, instead of being implemented in the node and link classes, see Code Sample 2.40.

  public String getTag(Object obj)
  {
    if(isLink(obj))
      return "treelink";
    else
      return "treenode";
  }

  public boolean isLink(Object obj)
  {
    return obj instanceof TreeLink;
  }

  public Object getFrom(Object link)
  {
    return ((TreeLink)link).getParentNode();
  }

  public Object getTo(Object link)
  {
    return ((TreeLink)link).getChildNode();
  }

  public Object getObjectProperty(Object object, String property)
  {
    if(object instanceof DefaultMutableTreeNode){
      if(property.equals("userObject")){
        return ((DefaultMutableTreeNode)object).getUserObject();
      }
      if(property.equals("CSSclass") &&        
         ((DefaultMutableTreeNode)object).getParent() != null){
        return  
        ((DefaultMutableTreeNode)((DefaultMutableTreeNode)object).
             getParent()).getUserObject();
      }
    }
    return null;
  }
  
  public String[] getObjectPropertyNames(Object object)
  {
    if(object instanceof DefaultMutableTreeNode)
      return new String[] { "userObject", "CSSclass" };
    else
      return new String[0];
  }

Code Sample 2.40 Implementing the SDM Model Interface