1 package org.oddjob.designer.model; 2 3 import java.util.Enumeration ; 4 import java.util.LinkedList ; 5 import java.util.Vector ; 6 7 import javax.swing.event.TreeModelEvent ; 8 import javax.swing.event.TreeModelListener ; 9 import javax.swing.tree.TreeModel ; 10 import javax.swing.tree.TreeNode ; 11 import javax.swing.tree.TreePath ; 12 13 14 19 20 public class DesignTreeModel implements TreeModel { 21 22 23 private final Vector tmListeners = new Vector (); 24 25 26 private DesignTreeNode root; 27 28 33 public DesignTreeModel(DesignComponent node) { 34 root = new DesignTreeNode(this, null, node); 35 } 36 37 public void build() { 38 root.build(); 39 } 40 41 public void addTreeModelListener(TreeModelListener tml) { 42 synchronized (tmListeners) { 43 tmListeners.add(tml); 44 } 45 } 46 47 48 public Object getChild(Object parent, int index) { 49 return ((DesignTreeNode)parent).getChildAt(index); 50 } 51 52 public boolean isLeaf(Object node) { 53 return ((DesignTreeNode)node).isLeaf(); 54 } 55 56 public int getChildCount(Object parent) { 57 return ((DesignTreeNode)parent).getChildCount(); 58 } 59 60 public int getIndexOfChild(Object parent, Object child) { 61 return ((DesignTreeNode)parent).getIndex((DesignTreeNode)child); 62 } 63 64 public Object getRoot() { 65 return root; 66 } 67 68 public void removeTreeModelListener(TreeModelListener tml) { 69 tmListeners.remove(tml); 70 } 71 72 public void valueForPathChanged(TreePath path, Object newValue) { 73 System.err.println("valueForPathChanged"); 74 } 75 76 public void fireTreeNodesChanged(TreeNode changed) { 77 LinkedList list = new LinkedList (); 78 for (TreeNode i = changed; i != null; i = i.getParent()) { 79 list.addFirst(i); 80 } 81 TreeModelEvent event = new TreeModelEvent (changed, list.toArray()); 82 83 synchronized (tmListeners) { 84 for (Enumeration e = tmListeners.elements(); 85 e.hasMoreElements(); ) { 86 TreeModelListener tml = (TreeModelListener )e.nextElement(); 87 tml.treeNodesChanged(event); 88 } 89 } 90 } 91 92 public void fireTreeNodesInserted(TreeNode changed, DesignTreeNode child, int index) { 93 LinkedList list = new LinkedList (); 94 95 for (TreeNode i = changed; i != null; i = i.getParent()) { 96 list.addFirst(i); 97 } 98 99 int childIndecies[] = {index}; 100 Object children [] = {child}; 101 102 TreeModelEvent event = new TreeModelEvent (changed, list.toArray(), 103 childIndecies, children); 104 105 synchronized (tmListeners) { 106 for (Enumeration e = tmListeners.elements(); 107 e.hasMoreElements(); ) { 108 TreeModelListener tml = (TreeModelListener )e.nextElement(); 109 tml.treeNodesInserted(event); 110 } 111 } 112 } 113 114 public void fireTreeNodesRemoved(TreeNode changed, DesignTreeNode child, int index) { 115 LinkedList list = new LinkedList (); 116 117 for (TreeNode i = changed; i != null; i = i.getParent()) { 118 list.addFirst(i); 119 } 120 121 int childIndecies[] = {index}; 122 Object children [] = {child}; 123 124 TreeModelEvent event = new TreeModelEvent (changed, list.toArray(), 125 childIndecies, children); 126 127 synchronized (tmListeners) { 128 for (Enumeration e = tmListeners.elements(); 129 e.hasMoreElements(); ) { 130 TreeModelListener tml = (TreeModelListener )e.nextElement(); 131 tml.treeNodesRemoved(event); 132 } 133 } 134 } 135 } 136 | Popular Tags |