skip to main content
Defense > Programmer's documentation > Developing with the JViews Diagrammer SDK > Using and writing data models > NonJavaBeans example: basic model variant
 
NonJavaBeans example: basic model variant
Describes how to display a set of Java™ objects that are not JavaBeans™ by subclassing the basic class IlvBasicSDMModel.
*The second Tree Model example
*Presents another example that adapts the Swing JTree model to an SDM model.
*The TreeSDMModel2 class
*Describes the TreeSDMModel2 class, which transforms the tree data into an SDM model.
*The TreeLink class
*Describes the TreeLink class, which holds references to the parent and child tree nodes.
*Loading the data model
*Shows how to load the data model for this example.
The second Tree Model example
This section makes use of the same example as for the IlvAbstractSDMModel variant, which involves adapting a Swing JTree model to an SDM model. Therefore, for a description of the common parts, see the subsections of that example as follows:
*Example description, see The Tree Model example
*Style sheet description, see The style sheet
*How to load the data model and style sheet, see Loading the data model and style sheet into the diagram component
The remaining subsections that follow describe the data model implementation for this variant.
The example is supplied with Rogue Wave® JViews Diagrammer in the directory <installdir>/jviews-diagrammer/codefragments/datamodel/treemodel2.
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 the following code example.
 
 
public class TreeSDMModel2 extends IlvBasicSDMModel
{
...
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 the following code example.
 
 
private TreeModel treeModel;
  private ArrayList links = new ArrayList();
 
  public TreeSDMModel2(TreeModel treeModel)
  {
    this.treeModel = treeModel;
 
    createLinks(treeModel.getRoot());
  }
Method for creating links
The createLinks method creates the parent-child links and stores them in a list, as shown in the following code example.
 
 
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);
    }
  }
Method for retrieving all nodes and links
The getObjects method (shown in the following code example) 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.
Retrieving nodes and links
 
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);
    }
  }
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 the following code example.
Implementing the SDM model interface
 
  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];
  }
The TreeLink class
The links are represented by instances of a very simple class that just holds references to the parent and child tree nodes, see the following code example.
 
 
public class TreeLink
{
  private Object parentNode;
  private Object childNode;
 
  public TreeLink(Object parentNode, Object childNode)
  {
    this.parentNode = parentNode;
    this.childNode = childNode;
  }
 
  public Object getParentNode()
  {
    return parentNode;
  }
 
  public Object getChildNode()
  {
    return childNode;
  }
}
Loading the data model
Most of the code in the sample is the same as in the first variant; the only difference is the way the model is created, see the following code example.
 
 
// Create the Tree -> SDM model adapter.
//
   TreeSDMModel2 sdmModel = new TreeSDMModel2(treeModel);

Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.