| Developing with the SDK > Using and Writing Data Models > NonJavaBeans Example: Basic Model Variant > The TreeSDMModel2 Class |
The TreeSDMModel2 Class |
INDEX
PREVIOUS
NEXT
|
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 { ... |
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.
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()); } |
The createLinks method creates the parent-child links and stores them in a list, as shown in Code Sample 2.38.
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.
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.
| Copyright © 1987-2007 ILOG S.A. All rights reserved. Documentation homepage. Legal terms. | PREVIOUS NEXT |