1 13 package com.tonbeller.wcf.tree; 14 15 import java.util.ArrayList ; 16 import java.util.Comparator ; 17 import java.util.HashSet ; 18 import java.util.List ; 19 import java.util.Set ; 20 import java.util.TreeSet ; 21 22 23 40 41 public class OptimizingTreeModelDecorator extends TreeModelDecorator { 42 NodeFilter filter; 43 44 Object [] roots; 45 Set significant; 46 boolean dirty = true; 47 48 boolean optimizeRoot = true; 49 boolean optimizeLeafs = false; 50 51 56 public OptimizingTreeModelDecorator(NodeFilter filter, TreeModel decoree) { 57 super(decoree); 58 this.filter = filter; 59 this.significant = new HashSet (); 60 installChangeListener(decoree); 61 } 62 63 68 public OptimizingTreeModelDecorator(NodeFilter filter, TreeModel decoree, Comparator nodeComparator) { 69 super(decoree); 71 this.filter = filter; 72 this.significant = new TreeSet (nodeComparator); 73 installChangeListener(decoree); 74 } 75 76 private void installChangeListener(TreeModel decoree) { 77 decoree.addTreeModelChangeListener(new TreeModelChangeListener() { 78 public void treeModelChanged(TreeModelChangeEvent event) { 79 dirty = true; 80 } 81 }); 82 } 83 84 85 public Object [] getRoots() { 86 if (dirty) 87 initialize(); 88 return roots; 89 } 90 91 void initialize() { 92 dirty = false; 93 significant.clear(); 94 recurseFindSignificant(super.getRoots()); 95 96 roots = optimize(super.getRoots()); 97 if (optimizeRoot) { 98 while (roots.length == 1 && !filter.accept(roots[0])) 99 roots = optimize(getChildren(roots[0])); 100 } 101 } 102 103 boolean isSignificant(Object node) { 104 return significant.contains(node); 105 } 106 107 void recurseFindSignificant(Object [] nodes) { 108 for (int i = 0; i < nodes.length; i++) { 109 Object node = nodes[i]; 110 if (super.hasChildren(node)) { 111 if (filter.accept(node)) 112 addSignificant(node); 113 recurseFindSignificant(super.getChildren(node)); 114 } else if (filter.accept(node)) 115 addSignificant(node); 116 else if (!optimizeLeafs) 117 significant.add(node); } 119 } 120 121 private void addSignificant(Object node) { 122 Object n = node; 123 while (n != null) { 124 significant.add(n); 125 n = super.getParent(n); 126 } 127 } 128 129 private Object [] optimize(Object [] nodes) { 130 List optimized = new ArrayList (); 131 for (int i = 0; i < nodes.length; i++) { 132 if (significant.contains(nodes[i])) 133 optimized.add(nodes[i]); 134 } 135 return optimized.toArray(); 136 } 137 138 public boolean hasChildren(Object node) { 139 if (super.hasChildren(node)) 140 return getChildren(node).length > 0; 141 return false; 142 } 143 144 public Object [] getChildren(Object node) { 145 if (dirty) 146 initialize(); 147 return optimize(super.getChildren(node)); 148 } 149 150 public Object getParent(Object node) { 151 return super.getParent(node); 152 } 153 154 157 public void setOptimizeLeafs(boolean b) { 158 optimizeLeafs = b; 159 dirty = true; 160 fireModelChanged(false); 161 } 162 163 public boolean isOptimizeLeafs() { 164 return optimizeLeafs; 165 } 166 167 170 public void setOptimizeRoot(boolean b) { 171 optimizeRoot = b; 172 dirty = true; 173 fireModelChanged(false); 174 } 175 176 public boolean isOptimizeRoot() { 177 return optimizeRoot; 178 } 179 180 } 181 | Popular Tags |