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