1 7 8 package org.jdesktop.swing.treetable; 9 10 import javax.swing.event.TreeModelEvent ; 11 import javax.swing.event.TreeModelListener ; 12 import javax.swing.tree.TreeNode ; 13 import javax.swing.tree.TreePath ; 14 15 23 public class DefaultTreeTableModel extends AbstractTreeTableModel { 24 25 protected boolean asksAllowsChildren; 26 27 public DefaultTreeTableModel() { 28 this(null); 29 } 30 31 public DefaultTreeTableModel(TreeNode root) { 32 this(root, false); 33 } 34 35 public DefaultTreeTableModel(TreeNode root, boolean asksAllowsChildren) { 36 super(root); 37 this.asksAllowsChildren = asksAllowsChildren; 38 } 39 40 public void setRoot(TreeNode root) { 41 Object oldRoot = this.root; 42 this.root = root; 43 if (root == null && oldRoot != null) { 44 fireTreeStructureChanged(this, null); 45 } 46 else { 47 nodeStructureChanged(root); 48 } 49 } 50 51 61 private void fireTreeStructureChanged(Object source, TreePath path) { 62 Object [] listeners = listenerList.getListenerList(); 64 TreeModelEvent e = null; 65 for (int i = listeners.length - 2; i >= 0; i -= 2) { 68 if (listeners[i] == TreeModelListener .class) { 69 if (e == null) 71 e = new TreeModelEvent (source, path); 72 ((TreeModelListener ) listeners[i + 1]).treeStructureChanged(e); 73 } 74 } 75 } 76 77 public boolean asksAllowsChildren() { 78 return asksAllowsChildren; 79 } 80 81 public void setAsksAllowsChildren(boolean newValue) { 82 asksAllowsChildren = newValue; 83 } 84 85 public Object getValueAt(Object node, int column) { 86 87 return node + "@column " + column; 88 } 89 90 public void setValueAt(Object value, Object node, int column) { 91 92 } 93 94 public TreeNode [] getPathToRoot(TreeNode node) { 95 return getPathToRoot(node, 0); 96 } 97 98 protected TreeNode [] getPathToRoot(TreeNode node, int depth) { 99 TreeNode [] retNodes; 100 104 106 if (node == null) { 107 if (depth == 0) 108 return null; 109 else 110 retNodes = new TreeNode [depth]; 111 } 112 else { 113 depth++; 114 if (node == root) 115 retNodes = new TreeNode [depth]; 116 else 117 retNodes = getPathToRoot(node.getParent(), depth); 118 retNodes[retNodes.length - depth] = node; 119 } 120 return retNodes; 121 } 122 123 127 public boolean isLeaf(Object node) { 128 if (node instanceof TreeNode ) { 129 if (asksAllowsChildren) { 130 return!((TreeNode ) node).getAllowsChildren(); 131 } 132 } 133 return super.isLeaf(node); 134 } 135 136 public void reload() { 137 TreeNode treeNode; 138 try { 139 treeNode = (TreeNode ) root; 140 } 141 catch (ClassCastException ex) { 142 return; 143 } 144 145 reload(treeNode); 146 } 147 148 public void reload(TreeNode node) { 149 if (node != null) { 150 fireTreeStructureChanged(this, getPathToRoot(node), null, null); 151 } 152 } 153 154 159 public void nodesWereInserted(TreeNode node, int[] childIndices) { 160 if (listenerList != null && node != null && childIndices != null 161 && childIndices.length > 0) { 162 int cCount = childIndices.length; 163 Object [] newChildren = new Object [cCount]; 164 165 for (int counter = 0; counter < cCount; counter++) 166 newChildren[counter] = node.getChildAt(childIndices[counter]); 167 fireTreeNodesInserted(this, getPathToRoot(node), childIndices, 168 newChildren); 169 } 170 } 171 172 178 public void nodesWereRemoved(TreeNode node, int[] childIndices, 179 Object [] removedChildren) { 180 if (node != null && childIndices != null) { 181 fireTreeNodesRemoved(this, getPathToRoot(node), childIndices, 182 removedChildren); 183 } 184 } 185 186 190 public void nodesChanged(TreeNode node, int[] childIndices) { 191 if (node != null) { 192 if (childIndices != null) { 193 int cCount = childIndices.length; 194 195 if (cCount > 0) { 196 Object [] cChildren = new Object [cCount]; 197 198 for (int counter = 0; counter < cCount; counter++) 199 cChildren[counter] = node.getChildAt 200 (childIndices[counter]); 201 fireTreeNodesChanged(this, getPathToRoot(node), 202 childIndices, cChildren); 203 } 204 } 205 else if (node == getRoot()) { 206 fireTreeNodesChanged(this, getPathToRoot(node), null, null); 207 } 208 } 209 } 210 211 216 public void nodeStructureChanged(TreeNode node) { 217 if (node != null) { 218 fireTreeStructureChanged(this, getPathToRoot(node), null, null); 219 } 220 } 221 } | Popular Tags |