1 7 8 package org.jdesktop.swing.treetable; 9 10 import java.util.EventListener ; 11 12 import javax.swing.event.EventListenerList ; 13 import javax.swing.event.TreeModelEvent ; 14 import javax.swing.event.TreeModelListener ; 15 import javax.swing.tree.TreeNode ; 16 import javax.swing.tree.TreePath ; 17 18 20 27 public abstract class AbstractTreeTableModel implements TreeTableModel { 28 32 public final static Class hierarchicalColumnClass = TreeTableModel.class; 33 34 37 protected Object root; 38 39 42 protected EventListenerList listenerList = new EventListenerList (); 43 44 47 public AbstractTreeTableModel() { 48 this(null); 49 } 50 51 57 public AbstractTreeTableModel(Object root) { 58 this.root = root; 59 } 60 61 64 public Class getColumnClass(int column) { 65 return column == 0 ? hierarchicalColumnClass : Object .class; 67 } 68 69 72 public String getColumnName(int column) { 73 return "Column " + column; } 75 76 79 public Object getRoot() { return root; 81 } 82 83 94 public Object getChild(Object parent, int index) { 95 try { 97 return ((TreeNode ) parent).getChildAt(index); 98 } 99 catch (ClassCastException ex) { return null; 101 } 102 } 103 104 110 public int getChildCount(Object parent) { 111 try { 113 return ((TreeNode ) parent).getChildCount(); 114 } 115 catch (ClassCastException ex) { return 0; 117 } 118 } 119 120 123 public int getColumnCount() { 124 return 1; } 127 128 136 public int getIndexOfChild(Object parent, Object child) { 137 if (parent == null || child == null) 138 return -1; 139 140 try { 141 return ((TreeNode ) parent).getIndex((TreeNode ) child); 142 } 143 catch (ClassCastException ex) { for (int i = 0; i < getChildCount(parent); i++) { 147 if (getChild(parent, i).equals(child)) { 148 return i; 149 } 150 } 151 return -1; 152 } 153 } 154 155 158 public boolean isCellEditable(Object node, int column) { 159 return false; 162 } 163 164 170 public boolean isLeaf(Object node) { 171 try { 172 return ((TreeNode ) node).isLeaf(); 173 } 174 catch (ClassCastException ex) { return getChildCount(node) == 0; 176 } 177 } 178 179 187 public void valueForPathChanged(TreePath path, Object newValue) { 188 189 } 190 191 public void addTreeModelListener(TreeModelListener l) { 192 listenerList.add(TreeModelListener .class, l); 193 } 194 195 public void removeTreeModelListener(TreeModelListener l) { 196 listenerList.remove(TreeModelListener .class, l); 197 } 198 199 public TreeModelListener [] getTreeModelListeners() { 200 return (TreeModelListener []) listenerList.getListeners( 201 TreeModelListener .class); 202 } 203 204 211 protected void fireTreeNodesChanged(Object source, Object [] path, 212 int[] childIndices, Object [] children) { 213 Object [] listeners = listenerList.getListenerList(); 215 TreeModelEvent e = null; 216 for (int i = listeners.length - 2; i >= 0; i -= 2) { 219 if (listeners[i] == TreeModelListener .class) { 220 if (e == null) 222 e = new TreeModelEvent (source, path, 223 childIndices, children); 224 ((TreeModelListener ) listeners[i + 1]).treeNodesChanged(e); 225 } 226 } 227 } 228 229 236 protected void fireTreeNodesInserted(Object source, Object [] path, 237 int[] childIndices, Object [] children) { 238 Object [] listeners = listenerList.getListenerList(); 240 TreeModelEvent e = null; 241 for (int i = listeners.length - 2; i >= 0; i -= 2) { 244 if (listeners[i] == TreeModelListener .class) { 245 if (e == null) 247 e = new TreeModelEvent (source, path, 248 childIndices, children); 249 ((TreeModelListener ) listeners[i + 1]).treeNodesInserted(e); 250 } 251 } 252 } 253 254 261 protected void fireTreeNodesRemoved(Object source, Object [] path, 262 int[] childIndices, Object [] children) { 263 Object [] listeners = listenerList.getListenerList(); 265 TreeModelEvent e = null; 266 for (int i = listeners.length - 2; i >= 0; i -= 2) { 269 if (listeners[i] == TreeModelListener .class) { 270 if (e == null) 272 e = new TreeModelEvent (source, path, 273 childIndices, children); 274 ((TreeModelListener ) listeners[i + 1]).treeNodesRemoved(e); 275 } 276 } 277 } 278 279 286 protected void fireTreeStructureChanged(Object source, Object [] path, 287 int[] childIndices, 288 Object [] children) { 289 Object [] listeners = listenerList.getListenerList(); 291 TreeModelEvent e = null; 292 for (int i = listeners.length - 2; i >= 0; i -= 2) { 295 if (listeners[i] == TreeModelListener .class) { 296 if (e == null) { 298 e = new TreeModelEvent (source, path, 299 childIndices, children); 300 } 301 ((TreeModelListener ) listeners[i + 1]).treeStructureChanged(e); 302 } 303 } 304 } 305 306 342 public EventListener [] getListeners(Class listenerType) { 343 return listenerList.getListeners(listenerType); 344 } 345 346 348 356 } 357 | Popular Tags |