1 13 package com.tonbeller.wcf.tree; 14 15 import java.util.Comparator ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 import java.util.TreeMap ; 19 20 import com.tonbeller.wcf.changeorder.ChangeOrderModel; 21 import com.tonbeller.wcf.changeorder.ChangeOrderUtils; 22 23 27 public class MutableTreeModelDecorator implements DecoratedTreeModel, MutableTreeModel, ChangeOrderModel { 28 TreeModel decoree; 29 30 Object [] roots; 31 Object [] noChildren = new Object [] {}; 32 33 34 Map childrenMap; 35 36 37 Map parentMap; 38 39 40 boolean enableChangeOrder = true; 41 42 TreeModelChangeSupport changeSupport; 43 44 public MutableTreeModelDecorator(TreeModel decoree) { 45 childrenMap = new HashMap (); 46 parentMap = new HashMap (); 47 changeSupport = new TreeModelChangeSupport(this); 48 setDecoree(decoree); 49 } 50 51 public MutableTreeModelDecorator(TreeModel decoree, Comparator comp) { 52 childrenMap = new TreeMap (comp); 53 parentMap = new TreeMap (comp); 54 changeSupport = new TreeModelChangeSupport(this); 55 setDecoree(decoree); 56 } 57 58 61 public void setDecoree(TreeModel newDecoree) { 62 if (decoree != null) 63 decoree.removeTreeModelChangeListener(changeListener); 64 decoree = newDecoree; 65 decoree.addTreeModelChangeListener(changeListener); 66 } 67 68 public TreeModel getDecoree() { 69 return decoree; 70 } 71 72 75 private TreeModelChangeListener changeListener = new TreeModelChangeListener() { 76 public void treeModelChanged(TreeModelChangeEvent event) { 77 childrenMap.clear(); 78 parentMap.clear(); 79 roots = null; 80 changeSupport.fireModelChanged(event); 81 } 82 }; 83 84 public void change(Object parent, Object [] children) { 85 if (children == null) 86 children = noChildren; 87 childrenMap.put(parent, children); 88 for (int i = 0; i < children.length; i++) 89 parentMap.put(children[i], parent); 90 changeSupport.fireModelChanged(false, parent); 91 } 92 93 public Object [] getRoots() { 94 if (roots == null) 95 roots = decoree.getRoots(); 96 return roots; 97 } 98 99 public boolean hasChildren(Object node) { 100 Object value = childrenMap.get(node); 101 if (value == null) { 102 boolean hasChildren = decoree.hasChildren(node); 104 if (hasChildren) 108 return true; 109 childrenMap.put(node, noChildren); 110 return false; 111 } 112 return value != noChildren; 113 } 114 115 public Object [] getChildren(Object node) { 116 Object [] children = (Object []) childrenMap.get(node); 117 if (children == null) { 118 children = decoree.getChildren(node); 119 change(node, children); 120 } 121 return children; 122 } 123 124 public Object getParent(Object node) { 125 Object parent = parentMap.get(node); 126 if (parent != null) 127 return parent; 128 return decoree.getParent(node); 129 } 130 131 int indexOf(Object obj, Object [] arr) { 132 for (int i = 0; i < arr.length; i++) 133 if (arr[i] == obj) 134 return i; 135 return -1; 136 } 137 138 public boolean mayMove(Object scope, Object node) { 139 return enableChangeOrder; 140 } 141 142 public void move(Object scope, Object node, int oldIndex, int newIndex) { 143 Object parent = getParent(node); 144 Object [] children; 145 if (parent == null) 146 children = getRoots(); 147 else 148 children = getChildren(parent); 149 ChangeOrderUtils.move(children, oldIndex, newIndex); 151 change(parent, children); 153 } 154 155 159 public boolean isEnableChangeOrder() { 160 return enableChangeOrder; 161 } 162 163 167 public void setEnableChangeOrder(boolean enableChangeOrder) { 168 this.enableChangeOrder = enableChangeOrder; 169 } 170 171 public void fireModelChanged(boolean identityChanged) { 172 changeSupport.fireModelChanged(identityChanged); 173 } 174 175 public void addTreeModelChangeListener(TreeModelChangeListener l) { 176 changeSupport.addTreeModelChangeListener(l); 177 } 178 179 public void removeTreeModelChangeListener(TreeModelChangeListener l) { 180 changeSupport.removeTreeModelChangeListener(l); 181 } 182 183 } 184 | Popular Tags |