1 7 8 package javax.swing.tree; 9 10 import java.util.*; 11 import java.awt.*; 12 import java.io.*; 13 import javax.swing.event.*; 14 15 35 public class DefaultTreeModel implements Serializable, TreeModel { 36 37 protected TreeNode root; 38 39 protected EventListenerList listenerList = new EventListenerList(); 40 56 protected boolean asksAllowsChildren; 57 58 59 65 public DefaultTreeModel(TreeNode root) { 66 this(root, false); 67 } 68 69 79 public DefaultTreeModel(TreeNode root, boolean asksAllowsChildren) { 80 super(); 81 this.root = root; 82 this.asksAllowsChildren = asksAllowsChildren; 83 } 84 85 90 public void setAsksAllowsChildren(boolean newValue) { 91 asksAllowsChildren = newValue; 92 } 93 94 102 public boolean asksAllowsChildren() { 103 return asksAllowsChildren; 104 } 105 106 110 public void setRoot(TreeNode root) { 111 Object oldRoot = this.root; 112 this.root = root; 113 if (root == null && oldRoot != null) { 114 fireTreeStructureChanged(this, null); 115 } 116 else { 117 nodeStructureChanged(root); 118 } 119 } 120 121 127 public Object getRoot() { 128 return root; 129 } 130 131 139 public int getIndexOfChild(Object parent, Object child) { 140 if(parent == null || child == null) 141 return -1; 142 return ((TreeNode )parent).getIndex((TreeNode )child); 143 } 144 145 155 public Object getChild(Object parent, int index) { 156 return ((TreeNode )parent).getChildAt(index); 157 } 158 159 167 public int getChildCount(Object parent) { 168 return ((TreeNode )parent).getChildCount(); 169 } 170 171 182 public boolean isLeaf(Object node) { 183 if(asksAllowsChildren) 184 return !((TreeNode )node).getAllowsChildren(); 185 return ((TreeNode )node).isLeaf(); 186 } 187 188 193 public void reload() { 194 reload(root); 195 } 196 197 203 public void valueForPathChanged(TreePath path, Object newValue) { 204 MutableTreeNode aNode = (MutableTreeNode )path.getLastPathComponent(); 205 206 aNode.setUserObject(newValue); 207 nodeChanged(aNode); 208 } 209 210 216 public void insertNodeInto(MutableTreeNode newChild, 217 MutableTreeNode parent, int index){ 218 parent.insert(newChild, index); 219 220 int[] newIndexs = new int[1]; 221 222 newIndexs[0] = index; 223 nodesWereInserted(parent, newIndexs); 224 } 225 226 232 public void removeNodeFromParent(MutableTreeNode node) { 233 MutableTreeNode parent = (MutableTreeNode )node.getParent(); 234 235 if(parent == null) 236 throw new IllegalArgumentException ("node does not have a parent."); 237 238 int[] childIndex = new int[1]; 239 Object [] removedArray = new Object [1]; 240 241 childIndex[0] = parent.getIndex(node); 242 parent.remove(childIndex[0]); 243 removedArray[0] = node; 244 nodesWereRemoved(parent, childIndex, removedArray); 245 } 246 247 251 public void nodeChanged(TreeNode node) { 252 if(listenerList != null && node != null) { 253 TreeNode parent = node.getParent(); 254 255 if(parent != null) { 256 int anIndex = parent.getIndex(node); 257 if(anIndex != -1) { 258 int[] cIndexs = new int[1]; 259 260 cIndexs[0] = anIndex; 261 nodesChanged(parent, cIndexs); 262 } 263 } 264 else if (node == getRoot()) { 265 nodesChanged(node, null); 266 } 267 } 268 } 269 270 275 public void reload(TreeNode node) { 276 if(node != null) { 277 fireTreeStructureChanged(this, getPathToRoot(node), null, null); 278 } 279 } 280 281 286 public void nodesWereInserted(TreeNode node, int[] childIndices) { 287 if(listenerList != null && node != null && childIndices != null 288 && childIndices.length > 0) { 289 int cCount = childIndices.length; 290 Object [] newChildren = new Object [cCount]; 291 292 for(int counter = 0; counter < cCount; counter++) 293 newChildren[counter] = node.getChildAt(childIndices[counter]); 294 fireTreeNodesInserted(this, getPathToRoot(node), childIndices, 295 newChildren); 296 } 297 } 298 299 305 public void nodesWereRemoved(TreeNode node, int[] childIndices, 306 Object [] removedChildren) { 307 if(node != null && childIndices != null) { 308 fireTreeNodesRemoved(this, getPathToRoot(node), childIndices, 309 removedChildren); 310 } 311 } 312 313 317 public void nodesChanged(TreeNode node, int[] childIndices) { 318 if(node != null) { 319 if (childIndices != null) { 320 int cCount = childIndices.length; 321 322 if(cCount > 0) { 323 Object [] cChildren = new Object [cCount]; 324 325 for(int counter = 0; counter < cCount; counter++) 326 cChildren[counter] = node.getChildAt 327 (childIndices[counter]); 328 fireTreeNodesChanged(this, getPathToRoot(node), 329 childIndices, cChildren); 330 } 331 } 332 else if (node == getRoot()) { 333 fireTreeNodesChanged(this, getPathToRoot(node), null, null); 334 } 335 } 336 } 337 338 343 public void nodeStructureChanged(TreeNode node) { 344 if(node != null) { 345 fireTreeStructureChanged(this, getPathToRoot(node), null, null); 346 } 347 } 348 349 357 public TreeNode [] getPathToRoot(TreeNode aNode) { 358 return getPathToRoot(aNode, 0); 359 } 360 361 373 protected TreeNode [] getPathToRoot(TreeNode aNode, int depth) { 374 TreeNode [] retNodes; 375 379 381 if(aNode == null) { 382 if(depth == 0) 383 return null; 384 else 385 retNodes = new TreeNode [depth]; 386 } 387 else { 388 depth++; 389 if(aNode == root) 390 retNodes = new TreeNode [depth]; 391 else 392 retNodes = getPathToRoot(aNode.getParent(), depth); 393 retNodes[retNodes.length - depth] = aNode; 394 } 395 return retNodes; 396 } 397 398 402 408 public void addTreeModelListener(TreeModelListener l) { 409 listenerList.add(TreeModelListener.class, l); 410 } 411 412 418 public void removeTreeModelListener(TreeModelListener l) { 419 listenerList.remove(TreeModelListener.class, l); 420 } 421 422 435 public TreeModelListener[] getTreeModelListeners() { 436 return (TreeModelListener[])listenerList.getListeners( 437 TreeModelListener.class); 438 } 439 440 452 protected void fireTreeNodesChanged(Object source, Object [] path, 453 int[] childIndices, 454 Object [] children) { 455 Object [] listeners = listenerList.getListenerList(); 457 TreeModelEvent e = null; 458 for (int i = listeners.length-2; i>=0; i-=2) { 461 if (listeners[i]==TreeModelListener.class) { 462 if (e == null) 464 e = new TreeModelEvent(source, path, 465 childIndices, children); 466 ((TreeModelListener)listeners[i+1]).treeNodesChanged(e); 467 } 468 } 469 } 470 471 483 protected void fireTreeNodesInserted(Object source, Object [] path, 484 int[] childIndices, 485 Object [] children) { 486 Object [] listeners = listenerList.getListenerList(); 488 TreeModelEvent e = null; 489 for (int i = listeners.length-2; i>=0; i-=2) { 492 if (listeners[i]==TreeModelListener.class) { 493 if (e == null) 495 e = new TreeModelEvent(source, path, 496 childIndices, children); 497 ((TreeModelListener)listeners[i+1]).treeNodesInserted(e); 498 } 499 } 500 } 501 502 514 protected void fireTreeNodesRemoved(Object source, Object [] path, 515 int[] childIndices, 516 Object [] children) { 517 Object [] listeners = listenerList.getListenerList(); 519 TreeModelEvent e = null; 520 for (int i = listeners.length-2; i>=0; i-=2) { 523 if (listeners[i]==TreeModelListener.class) { 524 if (e == null) 526 e = new TreeModelEvent(source, path, 527 childIndices, children); 528 ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e); 529 } 530 } 531 } 532 533 545 protected void fireTreeStructureChanged(Object source, Object [] path, 546 int[] childIndices, 547 Object [] children) { 548 Object [] listeners = listenerList.getListenerList(); 550 TreeModelEvent e = null; 551 for (int i = listeners.length-2; i>=0; i-=2) { 554 if (listeners[i]==TreeModelListener.class) { 555 if (e == null) 557 e = new TreeModelEvent(source, path, 558 childIndices, children); 559 ((TreeModelListener)listeners[i+1]).treeStructureChanged(e); 560 } 561 } 562 } 563 564 574 private void fireTreeStructureChanged(Object source, TreePath path) { 575 Object [] listeners = listenerList.getListenerList(); 577 TreeModelEvent e = null; 578 for (int i = listeners.length-2; i>=0; i-=2) { 581 if (listeners[i]==TreeModelListener.class) { 582 if (e == null) 584 e = new TreeModelEvent(source, path); 585 ((TreeModelListener)listeners[i+1]).treeStructureChanged(e); 586 } 587 } 588 } 589 590 626 public <T extends EventListener> T[] getListeners(Class <T> listenerType) { 627 return listenerList.getListeners(listenerType); 628 } 629 630 private void writeObject(ObjectOutputStream s) throws IOException { 632 Vector values = new Vector(); 633 634 s.defaultWriteObject(); 635 if(root != null && root instanceof Serializable) { 637 values.addElement("root"); 638 values.addElement(root); 639 } 640 s.writeObject(values); 641 } 642 643 private void readObject(ObjectInputStream s) 644 throws IOException, ClassNotFoundException { 645 s.defaultReadObject(); 646 647 Vector values = (Vector)s.readObject(); 648 int indexCounter = 0; 649 int maxCounter = values.size(); 650 651 if(indexCounter < maxCounter && values.elementAt(indexCounter). 652 equals("root")) { 653 root = (TreeNode )values.elementAt(++indexCounter); 654 indexCounter++; 655 } 656 } 657 658 659 } 661 662 | Popular Tags |