1 19 20 24 package org.netbeans.modules.tasklist.usertasks.treetable; 25 26 import javax.swing.event.EventListenerList ; 27 import javax.swing.event.TreeModelEvent ; 28 import javax.swing.event.TreeModelListener ; 29 import javax.swing.tree.TreePath ; 30 31 37 public abstract class AbstractTreeTableModel implements TreeTableModel { 38 39 protected Object root; 40 41 42 protected EventListenerList listenerList = new EventListenerList (); 43 44 49 public AbstractTreeTableModel(Object root) { 50 this.root = root; 51 } 52 53 58 public void setRoot(Object root) { 59 if (this.root != root) { 60 this.root = root; 61 fireTreeStructureChanged(this, new Object [] {root}, null, null); 62 } 63 } 64 65 69 public Object getRoot() { 70 return root; 71 } 72 73 public boolean isLeaf(Object node) { 74 return getChildCount(node) == 0; 75 } 76 77 public void valueForPathChanged(TreePath path, Object newValue) {} 78 79 public int getIndexOfChild(Object parent, Object child) { 81 for (int i = 0; i < getChildCount(parent); i++) { 82 if (getChild(parent, i).equals(child)) { 83 return i; 84 } 85 } 86 return -1; 87 } 88 89 public void addTreeModelListener(TreeModelListener l) { 90 listenerList.add(TreeModelListener .class, l); 91 } 92 93 public void removeTreeModelListener(TreeModelListener l) { 94 listenerList.remove(TreeModelListener .class, l); 95 } 96 97 104 public void fireTreeStructureChanged(Object source, Object [] path) { 105 Object [] listeners = listenerList.getListenerList(); 107 TreeModelEvent e = null; 108 TreePath tp = path == null ? null : new TreePath (path); 109 for (int i = listeners.length-2; i>=0; i-=2) { 112 if (listeners[i]==TreeModelListener .class) { 113 if (e == null) 115 e = new TreeModelEvent (source, tp); 116 ((TreeModelListener )listeners[i+1]).treeStructureChanged(e); 117 } 118 } 119 } 120 121 128 protected void fireTreeNodesChanged(Object source, Object [] path, 129 int[] childIndices, 130 Object [] children) { 131 Object [] listeners = listenerList.getListenerList(); 133 TreeModelEvent e = null; 134 for (int i = listeners.length-2; i>=0; i-=2) { 137 if (listeners[i]==TreeModelListener .class) { 138 if (e == null) 140 e = new TreeModelEvent (source, path, 141 childIndices, children); 142 ((TreeModelListener )listeners[i+1]).treeNodesChanged(e); 143 } 144 } 145 } 146 147 154 protected void fireTreeNodesInserted(Object source, Object [] path, 155 int[] childIndices, 156 Object [] children) { 157 Object [] listeners = listenerList.getListenerList(); 159 TreeModelEvent e = null; 160 for (int i = listeners.length-2; i>=0; i-=2) { 163 if (listeners[i]==TreeModelListener .class) { 164 if (e == null) 166 e = new TreeModelEvent (source, path, 167 childIndices, children); 168 ((TreeModelListener )listeners[i+1]).treeNodesInserted(e); 169 } 170 } 171 } 172 173 180 protected void fireTreeNodesRemoved(Object source, Object [] path, 181 int[] childIndices, 182 Object [] children) { 183 Object [] listeners = listenerList.getListenerList(); 185 TreeModelEvent e = null; 186 for (int i = listeners.length-2; i>=0; i-=2) { 189 if (listeners[i]==TreeModelListener .class) { 190 if (e == null) 192 e = new TreeModelEvent (source, path, 193 childIndices, children); 194 ((TreeModelListener )listeners[i+1]).treeNodesRemoved(e); 195 } 196 } 197 } 198 199 206 public void fireTreeStructureChanged(Object source, Object [] path, 207 int[] childIndices, Object [] children) { 208 Object [] listeners = listenerList.getListenerList(); 210 TreeModelEvent e = null; 211 for (int i = listeners.length-2; i>=0; i-=2) { 214 if (listeners[i]==TreeModelListener .class) { 215 if (e == null) 217 e = new TreeModelEvent (source, path, 218 childIndices, children); 219 ((TreeModelListener )listeners[i+1]).treeStructureChanged(e); 220 } 221 } 222 } 223 224 228 public Class getColumnClass(int column) { return Object .class; } 229 230 234 public boolean isCellEditable(Object node, int column) { 235 return false; 238 } 239 240 public void setValueAt(Object aValue, Object node, int column) {} 241 242 243 245 252 } 253 | Popular Tags |