1 13 package com.tonbeller.wcf.tree; 14 15 22 23 public class SimpleOptimizingTreeModelDecorator extends TreeModelDecorator { 24 NodeFilter filter; 25 26 Object [] roots; 27 boolean dirty = true; 28 29 34 public SimpleOptimizingTreeModelDecorator(NodeFilter filter, TreeModel decoree) { 35 super(decoree); 36 this.filter = filter; 37 decoree.addTreeModelChangeListener(new TreeModelChangeListener() { 38 public void treeModelChanged(TreeModelChangeEvent event) { 39 dirty = true; 40 } 41 }); 42 } 43 44 public Object [] getRoots() { 45 if (dirty) 46 initialize(); 47 return roots; 48 } 49 50 public Object getParent(Object node) { 51 if (dirty) 52 initialize(); 53 54 for (int i = 0; i < roots.length; i++) 56 if (roots[i].equals(node)) 57 return null; 58 return super.getParent(node); 60 } 61 62 void initialize() { 63 dirty = false; 64 roots = super.getRoots(); 65 while (roots.length == 1 && !filter.accept(roots[0])) 66 roots = getChildren(roots[0]); 67 } 68 69 } 70 | Popular Tags |