1 13 package com.tonbeller.wcf.tree; 14 15 import java.util.ArrayList ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 26 public class NodeSorter { 27 28 TreeModel model; 29 Set expanded = new HashSet (); 30 31 32 private NodeSorter(TreeModel model) { 33 this.model = model; 34 } 35 36 37 public static List preOrder(Set nodes, TreeModel model) { 38 NodeSorter ns = new NodeSorter(model); 39 return ns.order(nodes, true); 40 } 41 42 43 public static List postOrder(Set nodes, TreeModel model) { 44 NodeSorter ns = new NodeSorter(model); 45 return ns.order(nodes, false); 46 } 47 48 49 List order(Set nodes, boolean preOrder) { 50 expandParents(nodes); 51 List list = new ArrayList (); 52 Object [] roots = model.getRoots(); 53 for (int i = 0; i < roots.length; i++) 54 order(roots[i], list, nodes, preOrder); 55 return list; 56 } 57 58 59 void order(Object node, List list, Set nodes, boolean preOrder) { 60 if (preOrder && nodes.contains(node)) 61 list.add(node); 62 if (expanded.contains(node)) { 63 Object [] children = model.getChildren(node); 64 for (int i = 0; i < children.length; i++) 65 order(children[i], list, nodes, preOrder); 66 } 67 if (!preOrder && nodes.contains(node)) 68 list.add(node); 69 } 70 71 72 void expandParents(Set nodes) { 73 expanded.clear(); 74 for (Iterator it = nodes.iterator(); it.hasNext();) { 75 Object node = it.next(); 76 node = model.getParent(node); 77 while (node != null) { 78 expanded.add(node); 79 node = model.getParent(node); 80 } 81 } 82 } 83 84 } 85 | Popular Tags |