KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.tree;
14
15 import org.apache.log4j.Logger;
16
17 /**
18  * Created on 10.12.2002
19  *
20  * @author av
21  */

22 public class TestTreeModel extends AbstractTreeModel {
23   private static final Logger logger = Logger.getLogger(TestTreeModel.class);
24   private int[] childCount;
25   Node[] roots;
26
27   public TestTreeModel() {
28     this(new int[] { 3, 4, 4, 4, 4, 4});
29   }
30
31   public TestTreeModel(int[] childCount) {
32     this.childCount = childCount;
33
34     char c = 'A';
35     roots = new Node[childCount[0]];
36     for (int i = 0; i < roots.length; i++) {
37       roots[i] = new Node(null, "" + (char) (c + i), 1);
38     }
39   }
40
41   public class Node {
42
43     Node parent;
44     String JavaDoc name;
45     int level;
46     Node[] children;
47
48     public Node(Node parent, String JavaDoc name, int level) {
49       this.parent = parent;
50       this.name = name;
51       this.level = level;
52       if (level < childCount.length) {
53         children = new Node[childCount[level]];
54         for (int i = 0; i < children.length; i++) {
55           children[i] = new Node(this, name + "[" + i + "]", level + 1);
56         }
57       }
58     }
59
60     boolean hasChildren() {
61       return children != null;
62     }
63
64     Object JavaDoc[] getChildren() {
65       return children;
66     }
67
68     public int getLevel() {
69       return level;
70     }
71
72     public String JavaDoc getName() {
73       return name;
74     }
75
76     public void setLevel(int level) {
77       this.level = level;
78     }
79
80     public void setName(String JavaDoc name) {
81       this.name = name;
82     }
83
84     public Node getParent() {
85       return parent;
86     }
87
88     public void setParent(Node parent) {
89       this.parent = parent;
90     }
91
92     public String JavaDoc toString() {
93       return name;
94     }
95   }
96
97   public Object JavaDoc[] getRoots() {
98     logger.info("getRoots");
99     return roots;
100   }
101
102   public boolean hasChildren(Object JavaDoc node) {
103     logger.info("hasChildren: " + node);
104     return ((Node) node).hasChildren();
105   }
106
107   public Object JavaDoc[] getChildren(Object JavaDoc node) {
108     logger.info("getChildren: " + node);
109     return ((Node) node).getChildren();
110   }
111
112   public Object JavaDoc getParent(Object JavaDoc node) {
113     logger.info("getParent: " + node);
114     return ((Node) node).getParent();
115   }
116
117   public DeleteNodeModel getDeleteNodeModel() {
118     NodeFilter nf = new NodeFilter() {
119       public boolean accept(Object JavaDoc node) {
120         return ((Node) node).getName().startsWith("A");
121       }
122     };
123     DefaultDeleteNodeModel ddnm = new DefaultDeleteNodeModel();
124     ddnm.setDeletableFilter(nf);
125     return ddnm;
126   }
127 }
Popular Tags