KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > tree > TestModel


1 /*
2  * Created on 17.10.2003
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package com.tonbeller.wcf.tree;
8
9 class TestModel extends AbstractTreeModel {
10   N root;
11   TestModel(N root) {
12     this.root = root;
13   }
14   public Object JavaDoc[] getRoots() {
15     return new Object JavaDoc[] { root };
16   }
17   public boolean hasChildren(Object JavaDoc node) {
18     return ((N) node).children.size() > 0;
19   }
20   public Object JavaDoc[] getChildren(Object JavaDoc node) {
21     return ((N) node).children.toArray();
22   }
23   public Object JavaDoc getParent(Object JavaDoc node) {
24     return ((N) node).parent;
25   }
26
27   public static String JavaDoc print(TreeModel tm) {
28     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
29     Object JavaDoc[] roots = tm.getRoots();
30     print(tm, sb, roots);
31     return sb.toString();
32   }
33   
34   public void fireModelChanged() {
35     super.fireModelChanged();
36   }
37
38   private static void print(TreeModel tm, StringBuffer JavaDoc sb, Object JavaDoc[] nodes) {
39     for (int i = 0; i < nodes.length; i++) {
40       N n = (N) nodes[i];
41       sb.append(n.getLabel());
42       if (tm.hasChildren(n)) {
43         sb.append("[");
44         print(tm, sb, tm.getChildren(n));
45         sb.append("]");
46       }
47       if (i < nodes.length - 1)
48         sb.append(",");
49     }
50   }
51
52   public N getRoot() {
53     return root;
54   }
55
56   public void setRoot(N n) {
57     root = n;
58     fireModelChanged();
59   }
60
61 }
Popular Tags