1 13 package com.tonbeller.wcf.tree; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 24 public class NodeIterator implements Iterator { 25 26 private boolean preorder; 27 28 private TreeModel tm; 29 private List list = new ArrayList (); 30 private Iterator iter; 31 32 37 public NodeIterator(TreeModel tm, boolean preorder) { 38 this.tm = tm; 39 this.preorder = preorder; 40 recurse(tm.getRoots()); 41 iter = list.iterator(); 42 } 43 44 50 public NodeIterator(TreeModel tm, Object root, boolean preorder) { 51 this.tm = tm; 52 this.preorder = preorder; 53 recurse(new Object []{root}); 54 iter = list.iterator(); 55 } 56 57 private void recurse(Object [] nodes) { 58 for (int i = 0; i < nodes.length; i++) { 59 Object node = nodes[i]; 60 if (preorder) 61 list.add(node); 62 if (tm.hasChildren(node)) 63 recurse(tm.getChildren(node)); 64 if (!preorder) 65 list.add(node); 66 } 67 } 68 69 public boolean hasNext() { 70 return iter.hasNext(); 71 } 72 73 public Object next() { 74 return iter.next(); 75 } 76 77 public void remove() { 78 iter.remove(); 79 } 80 81 public void rewind() { 82 iter = list.iterator(); 83 } 84 85 } 86 | Popular Tags |