1 16 package org.apache.myfaces.custom.tree.model; 17 18 import org.apache.myfaces.custom.tree.DefaultMutableTreeNode; 19 import org.apache.myfaces.custom.tree.MutableTreeNode; 20 import org.apache.myfaces.custom.tree.TreeNode; 21 22 import java.util.Iterator ; 23 import java.util.Collection ; 24 import java.util.LinkedList ; 25 26 27 53 public class DefaultTreeModel 54 implements TreeModel 55 { 56 57 private TreeNode root; 58 private LinkedList listeners = new LinkedList (); 59 60 61 public DefaultTreeModel() 62 { 63 this(new DefaultMutableTreeNode("Root")); 64 } 65 66 67 public DefaultTreeModel(TreeNode root) 68 { 69 this.root = root; 70 } 71 72 73 public Object getRoot() 74 { 75 return root; 76 } 77 78 79 public Object getChild(Object parent, int index) 80 { 81 return ((TreeNode)parent).getChildAt(index); 82 } 83 84 85 public int getChildCount(Object parent) 86 { 87 return ((TreeNode)parent).getChildCount(); 88 } 89 90 91 public boolean isLeaf(Object node) 92 { 93 return ((TreeNode)node).isLeaf(); 94 } 95 96 97 public void valueForPathChanged(TreePath path, Object newValue) 98 { 99 MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent(); 100 101 node.setUserObject(newValue); 102 } 103 104 105 public int getIndexOfChild(Object parent, Object child) 106 { 107 return ((TreeNode)parent).getIndex((TreeNode)child); 108 } 109 110 111 public Collection getTreeModelListeners() 112 { 113 return listeners; 114 } 115 116 117 121 public void nodeChanged(TreeNode node) 122 { 123 if (listeners.isEmpty()) 124 { 125 return; 127 } 128 129 if (node != null) 130 { 131 TreeNode parent = node.getParent(); 132 133 if (parent != null) 134 { 135 int index = parent.getIndex(node); 136 if (index != -1) 137 { 138 int[] childIndices = new int[1]; 139 140 childIndices[0] = index; 141 nodesChanged(parent, childIndices); 142 } 143 } 144 else if (node == getRoot()) 145 { 146 nodesChanged(node, null); 147 } 148 } 149 } 150 151 152 156 public void nodesChanged(TreeNode node, int[] childIndices) 157 { 158 if (listeners.isEmpty()) 159 { 160 return; 162 } 163 164 if (node != null) 165 { 166 if (childIndices != null) 167 { 168 int count = childIndices.length; 169 170 if (count > 0) 171 { 172 Object [] children = new Object [count]; 173 174 for (int i = 0; i < count; i++) 175 { 176 children[i] = node.getChildAt(childIndices[i]); 177 } 178 fireTreeNodesChanged(this, getPathToRoot(node), childIndices, children); 179 } 180 } 181 else if (node == root) 182 { 183 fireTreeNodesChanged(this, getPathToRoot(node), null, null); 184 } 185 } 186 } 187 188 189 194 public void nodeStructureChanged(TreeNode node) 195 { 196 if (listeners.isEmpty()) 197 { 198 return; 200 } 201 202 if (node != null) 203 { 204 fireTreeStructureChanged(this, getPathToRoot(node), null, null); 205 } 206 } 207 208 209 214 public void nodesWereInserted(TreeNode node, int[] childIndices) 215 { 216 if (listeners.isEmpty()) 217 { 218 return; 220 } 221 if (node != null && childIndices != null && childIndices.length > 0) 222 { 223 int cCount = childIndices.length; 224 Object [] newChildren = new Object [cCount]; 225 226 for (int counter = 0; counter < cCount; counter++) 227 { 228 newChildren[counter] = node.getChildAt(childIndices[counter]); 229 } 230 fireTreeNodesInserted(this, getPathToRoot(node), childIndices, 231 newChildren); 232 } 233 } 234 235 236 242 public void nodesWereRemoved(TreeNode node, int[] childIndices, Object [] removedChildren) 243 { 244 if (listeners.isEmpty()) 245 { 246 return; 248 } 249 if (node != null && childIndices != null) 250 { 251 fireTreeNodesRemoved(this, getPathToRoot(node), childIndices, removedChildren); 252 } 253 } 254 255 256 261 public TreeNode[] getPathToRoot(TreeNode node) 262 { 263 return getPathToRoot(node, 0); 264 } 265 266 267 274 protected TreeNode[] getPathToRoot(TreeNode node, int depth) 275 { 276 TreeNode[] answer; 277 278 if (node == null) 279 { 280 if (depth == 0) 281 { 282 return null; 284 } 285 else 286 { 287 answer = new TreeNode[depth]; 289 } 290 } 291 else 292 { 293 depth++; 295 296 if (node == root) 297 { 298 answer = new TreeNode[depth]; 300 } 301 else 302 { 303 answer = getPathToRoot(node.getParent(), depth); 304 } 305 answer[answer.length - depth] = node; 306 } 307 return answer; 308 } 309 310 311 319 protected void fireTreeNodesChanged(Object source, Object [] path, int[] childIndices, Object [] children) 320 { 321 TreeModelEvent event = null; 322 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) 323 { 324 TreeModelListener listener = (TreeModelListener)iterator.next(); 325 if (event == null) 326 { 327 event = new TreeModelEvent(source, path, childIndices, children); 328 } 329 listener.treeNodesChanged(event); 330 } 331 } 332 333 334 342 protected void fireTreeNodesInserted(Object source, Object [] path, int[] childIndices, Object [] children) 343 { 344 TreeModelEvent event = null; 345 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) 346 { 347 TreeModelListener listener = (TreeModelListener)iterator.next(); 348 if (event == null) 349 { 350 event = new TreeModelEvent(source, path, childIndices, children); 351 } 352 listener.treeNodesInserted(event); 353 } 354 } 355 356 357 365 protected void fireTreeNodesRemoved(Object source, Object [] path, int[] childIndices, Object [] children) 366 { 367 TreeModelEvent event = null; 368 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) 369 { 370 TreeModelListener listener = (TreeModelListener)iterator.next(); 371 if (event == null) 372 { 373 event = new TreeModelEvent(source, path, childIndices, children); 374 } 375 listener.treeNodesRemoved(event); 376 } 377 } 378 379 380 388 protected void fireTreeStructureChanged(Object source, Object [] path, int[] childIndices, Object [] children) 389 { 390 TreeModelEvent event = null; 391 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) 392 { 393 TreeModelListener listener = (TreeModelListener)iterator.next(); 394 if (event == null) 395 { 396 event = new TreeModelEvent(source, path, childIndices, children); 397 } 398 listener.treeStructureChanged(event); 399 } 400 } 401 402 403 409 protected void fireTreeStructureChanged(Object source, TreePath path) 410 { 411 TreeModelEvent event = null; 412 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) 413 { 414 TreeModelListener listener = (TreeModelListener)iterator.next(); 415 if (event == null) 416 { 417 event = new TreeModelEvent(source, path); 418 } 419 listener.treeStructureChanged(event); 420 } 421 } 422 423 } 424 | Popular Tags |