1 16 package org.apache.cocoon.forms.formmodel.tree; 17 18 import java.util.Collections ; 19 import java.util.Iterator ; 20 21 import org.apache.commons.collections.ArrayStack; 22 23 41 public class TreeWalker implements Iterator { 42 ArrayStack stack = new ArrayStack(); 43 Tree tree; 44 Object node; 45 TreePath path; 46 Iterator iter; 47 48 public TreeWalker(Tree tree) { 49 this.iter = Collections.EMPTY_LIST.iterator(); 51 this.tree = tree; 52 this.node = tree.getModel().getRoot(); 53 this.path = TreePath.ROOT_PATH; 54 55 stack.push(this.iter); 56 stack.push(this.node); 57 } 58 59 68 public TreeWalker enterChildren() { 69 Iterator newIter; 70 if (isLeaf()) { 71 newIter = Collections.EMPTY_LIST.iterator(); 72 } else { 73 newIter = tree.getModel().getChildren(node).iterator(); 74 } 75 this.stack.push(this.iter); 76 this.stack.push(this.path); 77 this.stack.push(this.node); 78 this.iter = newIter; 79 this.node = null; 80 this.path = null; 81 82 return this; 83 } 84 85 88 public void leave() { 89 this.node = this.stack.pop(); 90 this.path = (TreePath)this.stack.pop(); 91 this.iter = (Iterator )this.stack.pop(); 92 this.path = this.path.getParentPath(); 93 } 94 95 98 public boolean hasNext() { 99 return this.iter.hasNext(); 100 } 101 102 105 public Object next() { 106 this.node = iter.next(); 107 108 this.path = new TreePath( 109 (TreePath)this.stack.peek(1), 110 tree.getModel().getChildKey(stack.peek(), this.node)); 111 return this.node; 112 } 113 114 119 public void remove() { 120 throw new UnsupportedOperationException (); 121 } 122 123 130 public int getDepth() { 131 return path.getPathCount() - (this.tree.isRootVisible() ? 1 : 2); 132 } 133 134 140 public Object getNode() { 141 return this.node; 142 } 143 144 149 public TreePath getPath() { 150 return this.path; 151 } 152 153 156 public boolean isLeaf() { 157 return this.tree.getModel().isLeaf(this.node); 158 } 159 160 163 public boolean isExpanded() { 164 return this.tree.isExpanded(this.path); 165 } 166 167 170 public boolean isCollapsed() { 171 return this.tree.isCollapsed(this.path); 172 } 173 174 177 public boolean isVisible() { 178 return this.tree.isVisible(this.path); 179 } 180 181 184 public boolean isSelected() { 185 return this.tree.isPathSelected(this.path); 186 } 187 188 199 public String getIconType() { 200 if (isLeaf()) { 201 return "leaf"; 202 } else if (isExpanded()) { 203 return "expanded"; 204 } else { 205 return "collapsed"; 206 } 207 } 208 209 219 public String getSelectionType() { 220 return this.tree.isPathSelected(this.path) ? "selected" : "unselected"; 221 } 222 } 223 | Popular Tags |