1 5 package com.tonbeller.wcf.tree; 6 7 import java.util.Collection ; 8 import java.util.Comparator ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 import java.util.TreeMap ; 13 14 22 public class EnumBoundedTreeModelDecorator extends TreeModelDecorator implements TreeBounding, 23 TreeModelChangeListener { 24 25 Map childrenMap; 26 TreeModelChangeSupport changeSupport; 27 28 public EnumBoundedTreeModelDecorator(TreeModel decoree) { 29 super(decoree); 30 childrenMap = new HashMap (); 31 changeSupport = new TreeModelChangeSupport(this); 32 setDecoree(decoree); 33 } 34 35 public EnumBoundedTreeModelDecorator(TreeModel decoree, Comparator nodeComparator) { 36 super(decoree); 37 childrenMap = new TreeMap (nodeComparator); 38 changeSupport = new TreeModelChangeSupport(this); 39 setDecoree(decoree); 40 } 41 42 public void setDecoree(TreeModel model) { 43 TreeModel decoree = getDecoree(); 44 if (decoree != null) 45 decoree.removeTreeModelChangeListener(this); 46 super.setDecoree(model); 47 decoree = getDecoree(); 48 if (decoree != null) 49 decoree.addTreeModelChangeListener(this); 50 } 51 52 public boolean isBounded(Object parent) { 53 return childrenMap.containsKey(parent); 54 } 55 56 public void unbound(Object parent) { 57 if (childrenMap.remove(parent) != null) { 58 changeSupport.fireModelChanged(false, parent); 59 } 60 } 61 62 public boolean hasChildren(Object node) { 63 Object [] children = (Object []) childrenMap.get(node); 64 if (children != null) 65 return true; 66 return super.hasChildren(node); 67 } 68 69 public Object [] getChildren(Object node) { 70 Object [] children = (Object []) childrenMap.get(node); 71 if (children != null) 72 return children; 73 return super.getChildren(node); 74 } 75 76 81 public void setVisible(Collection nodes) { 82 setVisible(getDecoree(), nodes); 83 } 84 85 93 public void setVisible(TreeModel model, Collection nodes) { 94 childrenMap.clear(); 95 for (Iterator it = nodes.iterator(); it.hasNext();) { 96 Object node = it.next(); 97 while (node != null) { 98 Object parent = model.getParent(node); 99 addVisible(parent, node); 100 node = parent; 101 } 102 } 103 } 104 105 private void addVisible(Object parent, Object child) { 106 if (parent == null || child == null) 107 return; 108 109 Object [] children = (Object []) childrenMap.get(parent); 110 if (children == null) { 111 childrenMap.put(parent, new Object [] { child}); 112 return; 113 } 114 for (int i = 0; i < children.length; i++) { 116 if (children[i].equals(child)) 117 return; 118 } 119 Object [] newChildren = new Object [children.length + 1]; 120 System.arraycopy(children, 0, newChildren, 0, children.length); 121 newChildren[children.length] = child; 122 childrenMap.put(parent, newChildren); 123 124 } 125 126 public void addTreeModelChangeListener(TreeModelChangeListener l) { 127 changeSupport.addTreeModelChangeListener(l); 128 } 129 130 public void fireModelChanged(boolean identityChanged) { 131 changeSupport.fireModelChanged(identityChanged); 132 } 133 134 public void removeTreeModelChangeListener(TreeModelChangeListener l) { 135 changeSupport.removeTreeModelChangeListener(l); 136 } 137 138 public void treeModelChanged(TreeModelChangeEvent event) { 139 changeSupport.fireModelChanged(event); 140 } 141 142 } 143 | Popular Tags |