1 16 package org.apache.cocoon.forms.formmodel.tree; 17 18 import java.util.Collection ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 28 public class DefaultTreeModel implements TreeModel { 29 30 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 getChildren(); 41 42 boolean isLeaf(); 43 44 String getChildKey(Object child); 45 46 Object getChild(String key); 47 } 48 49 public static class DefaultTreeNode implements TreeNode { 50 51 private Object data; 52 private Map children; 53 54 public DefaultTreeNode(Object data) { 55 this.data = data; 56 } 57 58 public DefaultTreeNode(Object data, Map children) { 59 this.data = data; 60 this.children = children; 61 } 62 63 public Object getData() { 64 return this.data; 65 } 66 67 public void add(String key, TreeNode node) { 68 if (this.children == null) { 69 this.children = new HashMap (); 70 } 71 children.put(key, node); 72 } 73 74 public Collection 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 getChildKey(Object child) { 87 Iterator iter = this.children.entrySet().iterator(); 88 while(iter.hasNext()) { 89 Map.Entry entry = (Map.Entry )iter.next(); 90 if (child.equals(entry.getValue())) { 91 return (String )entry.getKey(); 92 } 93 } 94 return null; 95 } 96 97 public Object getChild(String key) { 98 return this.children.get(key); 99 } 100 } 101 102 public DefaultTreeModel(TreeNode root) { 103 this.root = root; 104 } 105 106 public Object getRoot() { 107 return this.root; 108 } 109 110 public Collection getChildren(Object parent) { 111 return ((TreeNode)parent).getChildren(); 112 } 113 114 public boolean isLeaf(Object node) { 115 return ((TreeNode)node).isLeaf(); 116 } 117 118 public String getChildKey(Object parent, Object child) { 119 return ((TreeNode)parent).getChildKey(child); 120 } 121 122 public Object getChild(Object parent, String key) { 123 return ((TreeNode)parent).getChild(key); 124 } 125 126 public Object 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 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 |