KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > tree > DefaultTreeModel


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.formmodel.tree;
17
18 import java.util.Collection JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 /**
24  * A default tree model, implemented with {@link org.apache.cocoon.forms.formmodel.tree.DefaultTreeModel.TreeNode}s.
25  *
26  * @version $Id: DefaultTreeModel.java 190962 2005-06-16 17:17:00Z sylvain $
27  */

28 public class DefaultTreeModel implements TreeModel {
29     
30     /**
31      * Default model that is used by a Tree when no model has been specified.
32      */

33     public static final TreeModel UNSPECIFIED_MODEL = buildUnspecifiedModel();
34     
35     private TreeModelHelper helper = new TreeModelHelper(this);
36     
37     TreeNode root;
38
39     public interface TreeNode {
40         Collection JavaDoc getChildren();
41
42         boolean isLeaf();
43
44         String JavaDoc getChildKey(Object JavaDoc child);
45
46         Object JavaDoc getChild(String JavaDoc key);
47     }
48     
49     public static class DefaultTreeNode implements TreeNode {
50         
51         private Object JavaDoc data;
52         private Map JavaDoc children;
53
54         public DefaultTreeNode(Object JavaDoc data) {
55             this.data = data;
56         }
57         
58         public DefaultTreeNode(Object JavaDoc data, Map JavaDoc children) {
59             this.data = data;
60             this.children = children;
61         }
62         
63         public Object JavaDoc getData() {
64             return this.data;
65         }
66         
67         public void add(String JavaDoc key, TreeNode node) {
68             if (this.children == null) {
69                 this.children = new HashMap JavaDoc();
70             }
71             children.put(key, node);
72         }
73
74         public Collection JavaDoc getChildren() {
75             if (this.children == null) {
76                 return null;
77             }
78             
79             return this.children.values();
80         }
81
82         public boolean isLeaf() {
83             return this.children == null;
84         }
85
86         public String JavaDoc getChildKey(Object JavaDoc child) {
87             Iterator JavaDoc iter = this.children.entrySet().iterator();
88             while(iter.hasNext()) {
89                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
90                 if (child.equals(entry.getValue())) {
91                     return (String JavaDoc)entry.getKey();
92                 }
93             }
94             return null;
95         }
96
97         public Object JavaDoc getChild(String JavaDoc key) {
98             return this.children.get(key);
99         }
100     }
101
102     public DefaultTreeModel(TreeNode root) {
103         this.root = root;
104     }
105
106     public Object JavaDoc getRoot() {
107         return this.root;
108     }
109
110     public Collection JavaDoc getChildren(Object JavaDoc parent) {
111         return ((TreeNode)parent).getChildren();
112     }
113
114     public boolean isLeaf(Object JavaDoc node) {
115         return ((TreeNode)node).isLeaf();
116     }
117
118     public String JavaDoc getChildKey(Object JavaDoc parent, Object JavaDoc child) {
119         return ((TreeNode)parent).getChildKey(child);
120     }
121
122     public Object JavaDoc getChild(Object JavaDoc parent, String JavaDoc key) {
123         return ((TreeNode)parent).getChild(key);
124     }
125
126     public Object JavaDoc getNode(TreePath path) {
127         return helper.getNode(path);
128     }
129
130     public void addTreeModelListener(TreeModelListener l) {
131         helper.addTreeModelListener(l);
132     }
133
134     public void removeTreeModelListener(TreeModelListener l) {
135         helper.removeTreeModelListener(l);
136     }
137
138     private static TreeModel buildUnspecifiedModel() {
139         DefaultTreeNode root = new DefaultTreeNode("This tree has no model.");
140         DefaultTreeNode parent = new DefaultTreeNode("Tree model should be defined...");
141         root.add("explanation", parent);
142         parent.add("1", new DefaultTreeNode("in the form definition using <fd:tree-model>"));
143         parent.add("2", new DefaultTreeNode("by the application using flowscript, event listeners, etc."));
144         return new DefaultTreeModel(root);
145     }
146
147     /**
148      * The classical Swing sample tree model, that can be used for demonstration purposes.
149      */

150     public static class Sample extends DefaultTreeModel {
151         public Sample() {
152             super(new DefaultTreeNode("root"));
153             DefaultTreeNode root = (DefaultTreeNode)getRoot();
154
155             DefaultTreeNode parent;
156             
157             parent = new DefaultTreeNode("Colors");
158             root.add("colors", parent);
159             parent.add("blue", new DefaultTreeNode("Blue"));
160             parent.add("violet", new DefaultTreeNode("Violet"));
161             parent.add("red", new DefaultTreeNode("Red"));
162             parent.add("yellow", new DefaultTreeNode("Yellow"));
163     
164             parent = new DefaultTreeNode("Sports");
165             root.add("sports", parent);
166             parent.add("basketball", new DefaultTreeNode("Basketball"));
167             parent.add("soccer", new DefaultTreeNode("Soccer"));
168             parent.add("football", new DefaultTreeNode("Football"));
169             parent.add("hockey", new DefaultTreeNode("Hockey"));
170     
171             parent = new DefaultTreeNode("Food");
172             root.add("food", parent);
173             parent.add("hotdogs", new DefaultTreeNode("Hot Dogs"));
174             parent.add("pizza", new DefaultTreeNode("Pizza"));
175             parent.add("ravioli", new DefaultTreeNode("Ravioli"));
176             parent.add("bananas", new DefaultTreeNode("Bananas"));
177         }
178     }
179 }
180
Popular Tags