001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.formmodel.tree;
018:
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: /**
025: * A default tree model, implemented with {@link org.apache.cocoon.forms.formmodel.tree.DefaultTreeModel.TreeNode}s.
026: *
027: * @version $Id: DefaultTreeModel.java 449149 2006-09-23 03:58:05Z crossley $
028: */
029: public class DefaultTreeModel implements TreeModel {
030:
031: /**
032: * Default model that is used by a Tree when no model has been specified.
033: */
034: public static final TreeModel UNSPECIFIED_MODEL = buildUnspecifiedModel();
035:
036: private TreeModelHelper helper = new TreeModelHelper(this );
037:
038: TreeNode root;
039:
040: public interface TreeNode {
041: Collection getChildren();
042:
043: boolean isLeaf();
044:
045: String getChildKey(Object child);
046:
047: Object getChild(String key);
048: }
049:
050: public static class DefaultTreeNode implements TreeNode {
051:
052: private Object data;
053: private Map children;
054:
055: public DefaultTreeNode(Object data) {
056: this .data = data;
057: }
058:
059: public DefaultTreeNode(Object data, Map children) {
060: this .data = data;
061: this .children = children;
062: }
063:
064: public Object getData() {
065: return this .data;
066: }
067:
068: public void add(String key, TreeNode node) {
069: if (this .children == null) {
070: this .children = new HashMap();
071: }
072: children.put(key, node);
073: }
074:
075: public Collection getChildren() {
076: if (this .children == null) {
077: return null;
078: }
079:
080: return this .children.values();
081: }
082:
083: public boolean isLeaf() {
084: return this .children == null;
085: }
086:
087: public String getChildKey(Object child) {
088: Iterator iter = this .children.entrySet().iterator();
089: while (iter.hasNext()) {
090: Map.Entry entry = (Map.Entry) iter.next();
091: if (child.equals(entry.getValue())) {
092: return (String) entry.getKey();
093: }
094: }
095: return null;
096: }
097:
098: public Object getChild(String key) {
099: return this .children.get(key);
100: }
101: }
102:
103: public DefaultTreeModel(TreeNode root) {
104: this .root = root;
105: }
106:
107: public Object getRoot() {
108: return this .root;
109: }
110:
111: public Collection getChildren(Object parent) {
112: return ((TreeNode) parent).getChildren();
113: }
114:
115: public boolean isLeaf(Object node) {
116: return ((TreeNode) node).isLeaf();
117: }
118:
119: public String getChildKey(Object parent, Object child) {
120: return ((TreeNode) parent).getChildKey(child);
121: }
122:
123: public Object getChild(Object parent, String key) {
124: return ((TreeNode) parent).getChild(key);
125: }
126:
127: public Object getNode(TreePath path) {
128: return helper.getNode(path);
129: }
130:
131: public void addTreeModelListener(TreeModelListener l) {
132: helper.addTreeModelListener(l);
133: }
134:
135: public void removeTreeModelListener(TreeModelListener l) {
136: helper.removeTreeModelListener(l);
137: }
138:
139: private static TreeModel buildUnspecifiedModel() {
140: DefaultTreeNode root = new DefaultTreeNode(
141: "This tree has no model.");
142: DefaultTreeNode parent = new DefaultTreeNode(
143: "Tree model should be defined...");
144: root.add("explanation", parent);
145: parent.add("1", new DefaultTreeNode(
146: "in the form definition using <fd:tree-model>"));
147: parent
148: .add(
149: "2",
150: new DefaultTreeNode(
151: "by the application using flowscript, event listeners, etc."));
152: return new DefaultTreeModel(root);
153: }
154:
155: /**
156: * The classical Swing sample tree model, that can be used for demonstration purposes.
157: */
158: public static class Sample extends DefaultTreeModel {
159: public Sample() {
160: super (new DefaultTreeNode("root"));
161: DefaultTreeNode root = (DefaultTreeNode) getRoot();
162:
163: DefaultTreeNode parent;
164:
165: parent = new DefaultTreeNode("Colors");
166: root.add("colors", parent);
167: parent.add("blue", new DefaultTreeNode("Blue"));
168: parent.add("violet", new DefaultTreeNode("Violet"));
169: parent.add("red", new DefaultTreeNode("Red"));
170: parent.add("yellow", new DefaultTreeNode("Yellow"));
171:
172: parent = new DefaultTreeNode("Sports");
173: root.add("sports", parent);
174: parent.add("basketball", new DefaultTreeNode("Basketball"));
175: parent.add("soccer", new DefaultTreeNode("Soccer"));
176: parent.add("football", new DefaultTreeNode("Football"));
177: parent.add("hockey", new DefaultTreeNode("Hockey"));
178:
179: parent = new DefaultTreeNode("Food");
180: root.add("food", parent);
181: parent.add("hotdogs", new DefaultTreeNode("Hot Dogs"));
182: parent.add("pizza", new DefaultTreeNode("Pizza"));
183: parent.add("ravioli", new DefaultTreeNode("Ravioli"));
184: parent.add("bananas", new DefaultTreeNode("Bananas"));
185: }
186: }
187: }
|