1 19 package org.openide.explorer.view; 20 21 import org.openide.nodes.Node; 22 import org.openide.util.*; 23 24 import java.beans.*; 25 26 import java.lang.ref.Reference ; 27 import java.lang.ref.WeakReference ; 28 29 import java.util.*; 30 31 import javax.swing.*; 32 import javax.swing.event.*; 33 import javax.swing.tree.*; 34 35 36 40 public class NodeListModel extends AbstractListModel implements ComboBoxModel { 41 static final long serialVersionUID = -1926931095895356820L; 42 43 44 private transient Listener listener; 45 46 47 private transient VisualizerNode parent; 48 49 50 private transient Object selectedObject; 51 52 53 private transient int size; 54 55 56 private int depth = 1; 57 58 61 private Map<VisualizerNode, Info> childrenCount; 62 63 65 public NodeListModel() { 66 parent = VisualizerNode.EMPTY; 67 selectedObject = VisualizerNode.EMPTY; 68 clearChildrenCount(); 69 } 70 71 74 public NodeListModel(Node root) { 75 this(); 76 setNode(root); 77 } 78 79 82 public void setNode(final Node root) { 83 Mutex.EVENT.readAccess( 84 new Runnable () { 85 public void run() { 86 VisualizerNode v = VisualizerNode.getVisualizer(null, root); 87 88 if (v == parent) { 89 return; 91 } 92 93 removeAll(); 94 parent.removeNodeModel(listener()); 95 96 parent = v; 97 selectedObject = v; 98 clearChildrenCount(); 99 100 addAll(); 101 parent.addNodeModel(listener()); 102 } 103 } 104 ); 105 } 106 107 110 public void setDepth(int depth) { 111 if (depth != this.depth) { 112 this.depth = depth; 113 clearChildrenCount(); 114 115 Mutex.EVENT.readAccess( 116 new Runnable () { 117 public void run() { 118 removeAll(); 119 addAll(); 120 } 121 } 122 ); 123 } 124 } 125 126 129 public int getDepth() { 130 return depth; 131 } 132 133 135 private Listener listener() { 136 if (listener == null) { 137 listener = new Listener(this); 138 } 139 140 return listener; 141 } 142 143 147 149 public int getSize() { 150 int s = findSize(parent, -1, depth); 151 152 return s; 153 } 154 155 157 public Object getElementAt(int i) { 158 return findElementAt(parent, i, -1, depth); 159 } 160 161 165 public int getIndex(Object o) { 166 getSize(); 167 168 Info i = childrenCount.get(o); 169 170 return (i == null) ? (-1) : i.index; 171 } 172 173 175 public void setSelectedItem(Object anObject) { 176 if (selectedObject != anObject) { 177 selectedObject = anObject; 178 fireContentsChanged(this, -1, -1); 179 } 180 } 181 182 public Object getSelectedItem() { 183 return selectedObject; 184 } 185 186 private void clearChildrenCount() { 190 childrenCount = new HashMap<VisualizerNode, Info>(17); 191 } 192 193 200 private int findSize(VisualizerNode vis, int index, int depth) { 201 Info info = childrenCount.get(vis); 202 203 if (info != null) { 204 return info.childrenCount; 205 } 206 207 int size = 0; 209 210 info = new Info(); 211 info.depth = depth; 212 info.index = index; 213 214 if (depth-- > 0) { 215 Iterator it = vis.getChildren().iterator(); 216 217 while (it.hasNext()) { 218 VisualizerNode v = (VisualizerNode) it.next(); 219 220 size++; 222 223 size += findSize(v, index + size, depth); 225 } 226 } 227 228 info.childrenCount = size; 229 childrenCount.put(vis, info); 230 231 return size; 232 } 233 234 241 private VisualizerNode findElementAt(VisualizerNode vis, int indx, int realIndx, int depth) { 242 if (--depth == 0) { 243 return (VisualizerNode) vis.getChildAt(indx); 245 } 246 247 Iterator it = vis.getChildren().iterator(); 248 249 while (it.hasNext()) { 250 VisualizerNode v = (VisualizerNode) it.next(); 251 252 if (indx-- == 0) { 253 return v; 254 } 255 256 int s = findSize(v, ++realIndx, depth); 257 258 if (indx < s) { 259 return findElementAt(v, indx, realIndx, depth); 261 } 262 263 indx -= s; 265 realIndx += s; 266 } 267 268 return vis; 269 } 270 271 276 static int findVisualizerDepth(ListModel m, VisualizerNode o) { 277 if (m instanceof NodeListModel) { 278 NodeListModel n = (NodeListModel) m; 279 Info i = n.childrenCount.get(o); 280 281 if (i != null) { 282 return n.depth - i.depth - 1; 283 } 284 } 285 286 return 0; 287 } 288 289 final void addAll() { 293 size = getSize(); 294 295 if (size > 0) { 296 fireIntervalAdded(this, 0, size - 1); 297 } 298 } 299 300 final void removeAll() { 301 if (size > 0) { 302 fireIntervalRemoved(this, 0, size - 1); 303 } 304 } 305 306 final void changeAll() { 307 size = getSize(); 308 309 if (size > 0) { 310 fireContentsChanged(this, 0, size - 1); 311 } 312 313 clearChildrenCount(); 314 } 315 316 final void added(VisualizerEvent.Added ev) { 317 VisualizerNode v = ev.getVisualizer(); 318 int[] indices = ev.getArray(); 319 320 if ((cachedDepth(v) <= 0) || (indices.length == 0)) { 323 return; 324 } 325 326 clearChildrenCount(); 327 size = getSize(); 328 329 int seg = (parent == v) ? 0 : getIndex(v); 330 fireIntervalAdded(this, indices[0] + seg, indices[indices.length - 1] + seg); 331 } 332 333 final void removed(VisualizerEvent.Removed ev) { 334 VisualizerNode v = ev.getVisualizer(); 335 int[] indices = ev.getArray(); 336 337 if ((cachedDepth(v) <= 0) || (indices.length == 0)) { 340 return; 341 } 342 343 clearChildrenCount(); 344 345 int seg = (parent == v) ? 0 : getIndex(v); 346 fireIntervalRemoved(this, indices[0] + seg, indices[indices.length - 1] + seg); 347 } 348 349 final void update(VisualizerNode v) { 350 getSize(); 352 353 Info i = childrenCount.get(v); 354 355 if (i != null) { 356 fireContentsChanged(this, i.index, i.index); 357 } 358 } 359 360 private int cachedDepth(VisualizerNode v) { 361 getSize(); 362 363 Info i = childrenCount.get(v); 364 365 if (i != null) { 366 return i.depth; 367 } 368 369 return -1; 371 } 372 373 374 private static final class Listener implements NodeModel { 375 376 private Reference <NodeListModel> model; 377 378 380 public Listener(NodeListModel m) { 381 model = new WeakReference <NodeListModel>(m); 382 } 383 384 386 private NodeListModel get(VisualizerEvent ev) { 387 NodeListModel m = model.get(); 388 389 if ((m == null) && (ev != null)) { 390 ev.getVisualizer().removeNodeModel(this); 391 392 return null; 393 } 394 395 return m; 396 } 397 398 401 public void added(VisualizerEvent.Added ev) { 402 NodeListModel m = get(ev); 403 404 if (m == null) { 405 return; 406 } 407 408 m.added(ev); 409 } 410 411 414 public void removed(VisualizerEvent.Removed ev) { 415 NodeListModel m = get(ev); 416 417 if (m == null) { 418 return; 419 } 420 421 m.removed(ev); 422 } 423 424 427 public void reordered(VisualizerEvent.Reordered ev) { 428 NodeListModel m = get(ev); 429 430 if (m == null) { 431 return; 432 } 433 434 m.changeAll(); 435 } 436 437 439 public void update(VisualizerNode v) { 440 NodeListModel m = get(null); 441 442 if (m == null) { 443 return; 444 } 445 446 m.update(v); 447 } 448 449 451 public void structuralChange(VisualizerNode v) { 452 NodeListModel m = get(null); 453 454 if (m == null) { 455 return; 456 } 457 458 m.changeAll(); 459 } 460 } 461 462 464 private static final class Info extends Object { 465 public int childrenCount; 466 public int depth; 467 public int index; 468 469 Info() { 470 } 471 472 public String toString() { 473 return "Info[childrenCount=" + childrenCount + ", depth=" + depth + ", index=" + index; } 476 } 477 } 478 | Popular Tags |