1 19 20 package org.netbeans.modules.tasklist.usertasks.treetable; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Comparator ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import javax.swing.SwingUtilities ; 28 29 34 public abstract class AdvancedTreeTableNode<T> 35 extends AbstractTreeTableNode { 36 protected FilterIntf filter; 37 protected Comparator <AdvancedTreeTableNode> comparator; 38 protected DefaultTreeTableModel model; 39 protected T object; 40 41 48 public AdvancedTreeTableNode(DefaultTreeTableModel model, 49 TreeTableNode parent, T object) { 50 super(parent); 51 this.model = model; 52 this.object = object; 53 } 54 55 60 public T getObject() { 61 return object; 62 } 63 64 70 public int getIndexOfObject(Object obj) { 71 AdvancedTreeTableNode[] ch = (AdvancedTreeTableNode[]) getChildren(); 72 for (int i = 0; i < ch.length; i++) { 73 if (ch[i].getObject() == obj) 74 return i; 75 } 76 return -1; 77 } 78 79 84 public void setComparator(Comparator <AdvancedTreeTableNode> comparator) { 85 if (this.comparator == comparator) 86 return; 87 88 this.comparator = comparator; 89 90 if (this.children != null) { 91 if (this.comparator != null) { 92 Arrays.sort((AdvancedTreeTableNode[]) children, 93 this.comparator); 94 } else { 95 AdvancedTreeTableNode[] newch = 96 new AdvancedTreeTableNode[children.length]; 97 Iterator <?> it = this.getChildrenObjectsIterator(); 98 int i = 0; 99 while (it.hasNext()) { 100 int index = getIndexOfObject(it.next()); 101 if (index >= 0) 102 newch[i++] = (AdvancedTreeTableNode) children[index]; 103 } 104 children = newch; 105 } 106 model.fireTreeStructureChanged(model, getPathToRoot()); 107 for (int i = 0; i < children.length; i++) { 108 ((AdvancedTreeTableNode<?>) children[i]). 109 setComparator(comparator); 110 } 111 } 112 } 113 114 119 public Comparator <AdvancedTreeTableNode> getComparator() { 120 return comparator; 121 } 122 123 129 public FilterIntf getFilter() { 130 return filter; 131 } 132 133 138 public void setFilter(FilterIntf filter) { 139 this.filter = filter; 140 this.children = null; 141 } 142 143 149 public abstract Iterator <?> getChildrenObjectsIterator(); 150 151 156 public abstract AdvancedTreeTableNode<?> createChildNode(Object child); 157 158 163 public boolean accept(Object child) { 164 if (getFilter() != null) 165 return getFilter().accept(child); 166 else 167 return true; 168 } 169 170 protected void loadChildren() { 171 List <Object > ch = new ArrayList <Object >(); 172 Iterator <?> it = getChildrenObjectsIterator(); 173 while (it.hasNext()) { 174 ch.add(it.next()); 175 } 176 177 if (getFilter() != null) { 179 it = ch.iterator(); 180 while (it.hasNext()) { 181 if (!accept(it.next())) 182 it.remove(); 183 } 184 } 185 186 children = new AdvancedTreeTableNode[ch.size()]; 187 for (int i = 0; i < ch.size(); i++) { 188 children[i] = createChildNode(ch.get(i)); 189 } 190 191 if (getComparator() != null) 193 Arrays.sort((AdvancedTreeTableNode[]) children, getComparator()); 194 } 195 196 public void refreshChildren() { 197 if (children != null) { 198 for (int i = 0; i < children.length; i++) { 199 ((AdvancedTreeTableNode) children[i]).destroy(); 200 } 201 this.children = null; 202 } 203 model.fireTreeStructureChanged(model, this.getPathToRoot()); 204 } 205 206 209 public void destroy() { 210 this.parent = null; 211 if (children != null) { 212 for (int i = 0; i < children.length; i++) { 213 ((AdvancedTreeTableNode) children[i]).destroy(); 214 } 215 } 216 } 217 218 222 protected void fireChildObjectsReordered() { 223 if (children != null) { 224 if (this.comparator == null) { 225 AdvancedTreeTableNode[] newch = 226 new AdvancedTreeTableNode[children.length]; 227 Iterator <?> it = this.getChildrenObjectsIterator(); 228 int i = 0; 229 while (it.hasNext()) { 230 int index = getIndexOfObject(it.next()); 231 if (index >= 0) 232 newch[i++] = (AdvancedTreeTableNode) children[index]; 233 } 234 children = newch; 235 model.fireTreeStructureChanged(model, getPathToRoot()); 236 } 237 } 238 } 239 240 245 protected void fireChildObjectRemoved(Object obj) { 246 if (children != null) { 247 int ind = getIndexOfObject(obj); 248 if (ind >= 0) { 249 AdvancedTreeTableNode rem = 250 (AdvancedTreeTableNode) children[ind]; 251 AdvancedTreeTableNode[] newChildren = 252 new AdvancedTreeTableNode[children.length - 1]; 253 System.arraycopy(children, 0, newChildren, 0, ind); 254 System.arraycopy(children, ind + 1, newChildren, 255 ind, children.length - ind - 1); 256 rem.destroy(); 257 children = newChildren; 258 model.fireTreeNodesRemoved(model, 259 getPathToRoot(), 260 new int[] {ind}, new Object [] {rem}); 261 } 262 } 263 } 264 265 270 protected void fireChildObjectAdded(Object obj) { 271 if (children != null) { 272 if (!accept(obj)) 273 return; 274 275 AdvancedTreeTableNode cn = createChildNode(obj); 276 277 int index; 278 if (getComparator() != null) { 279 index = Arrays.binarySearch( 280 (AdvancedTreeTableNode[]) children, cn, getComparator()); 281 if (index < 0) 282 index = -(index + 1); 283 } else { 284 index = -1; 285 Iterator <?> it = getChildrenObjectsIterator(); 286 while (it.hasNext()) { 287 Object next = it.next(); 288 if (!accept(next)) 289 continue; 290 index++; 291 if (next == obj) 292 break; 293 } 294 } 295 296 AdvancedTreeTableNode[] newch = 297 new AdvancedTreeTableNode[children.length + 1]; 298 System.arraycopy(children, 0, newch, 0, index); 299 newch[index] = cn; 300 System.arraycopy(children, index, newch, index + 1, 301 children.length - index); 302 this.children = newch; 303 model.fireTreeNodesInserted(model, getPathToRoot(), 304 new int[] {index}, new Object [] {cn}); 305 } 306 } 307 308 312 @SuppressWarnings ("unchecked") 313 protected void fireObjectChanged() { 314 AdvancedTreeTableNode parent = 316 (AdvancedTreeTableNode) getParent(); 317 TreeTableNode[] path = parent.getPathToRoot(); 318 319 assert parent.getIndex(this) != -1 : "parent=" + parent + " this=" + this + " parent.getChildCount=" + parent.getChildCount() + " parent.getChild(0)=" + parent.getChildAt(0); model.fireTreeNodesChanged(model, path, 324 new int[] {parent.getIndex(this)}, new Object [] {this}); 325 326 parent.childNodeChanged(this); 327 } 328 329 336 protected void childNodeChanged(AdvancedTreeTableNode<?> child) { 337 if (getFilter() != null) { 338 if (!accept(child.getObject())) { 339 fireChildObjectRemoved(child.getObject()); 343 } 344 } 345 } 346 347 public boolean isLeaf() { 348 return getChildren().length == 0; 349 } 350 } 351 | Popular Tags |