1 19 package org.openide.nodes; 20 21 import java.util.*; 22 23 import javax.swing.event.ChangeEvent ; 24 import javax.swing.event.ChangeListener ; 25 26 27 32 public interface Index extends Node.Cookie { 33 36 public int getNodesCount(); 37 38 41 public Node[] getNodes(); 42 43 47 public int indexOf(final Node node); 48 49 51 public void reorder(); 52 53 60 public void reorder(int[] perm); 61 62 69 public void move(int x, int y); 70 71 76 public void exchange(int x, int y); 77 78 82 public void moveUp(int x); 83 84 88 public void moveDown(int x); 89 90 95 public void addChangeListener(final ChangeListener chl); 96 97 101 public void removeChangeListener(final ChangeListener chl); 102 103 104 107 public static abstract class Support implements Index { 108 109 private HashSet<ChangeListener > listeners; 110 111 112 public Support() { 113 } 114 115 122 public void move(final int x, final int y) { 123 int[] perm = new int[getNodesCount()]; 124 125 if (x == y) { 127 return; 128 } 129 130 for (int i = 0; i < perm.length; i++) { 131 if (((i < x) && (i < y)) || ((i > x) && (i > y))) { 132 perm[i] = i; 134 } else { 135 if ((i > x) && (i < y)) { 136 perm[i] = i - 1; 138 } else { 139 perm[i] = i + 1; 141 } 142 } 143 } 144 145 perm[x] = y; 147 148 if (x < y) { 149 perm[y] = y - 1; 150 } else { 151 perm[y] = y + 1; 152 } 153 154 reorder(perm); 155 } 156 157 162 public void exchange(final int x, final int y) { 163 int[] perm = new int[getNodesCount()]; 164 165 for (int i = 0; i < perm.length; i++) { 166 perm[i] = i; 167 } 168 169 perm[x] = y; 170 perm[y] = x; 171 172 reorder(perm); 173 } 174 175 179 public void moveUp(final int x) { 180 exchange(x, x - 1); 181 } 182 183 187 public void moveDown(final int x) { 188 exchange(x, x + 1); 189 } 190 191 196 public void addChangeListener(final ChangeListener chl) { 197 if (listeners == null) { 198 listeners = new HashSet<ChangeListener >(); 199 } 200 201 listeners.add(chl); 202 } 203 204 209 public void removeChangeListener(final ChangeListener chl) { 210 if (listeners == null) { 211 return; 212 } 213 214 listeners.remove(chl); 215 } 216 217 222 protected void fireChangeEvent(ChangeEvent che) { 223 if (listeners == null) { 224 return; 225 } 226 227 HashSet cloned; 228 229 synchronized (this) { 231 cloned = (HashSet) listeners.clone(); 232 } 233 234 for (Iterator iter = cloned.iterator(); iter.hasNext();) { 236 ((ChangeListener ) iter.next()).stateChanged(che); 237 } 238 } 239 240 243 public abstract Node[] getNodes(); 244 245 249 public int indexOf(final Node node) { 250 Node[] arr = getNodes(); 251 252 for (int i = 0; i < arr.length; i++) { 253 if (node.equals(arr[i])) { 254 return i; 255 } 256 } 257 258 return -1; 259 } 260 261 263 public void reorder() { 264 showIndexedCustomizer(this); 265 } 266 267 273 public static void showIndexedCustomizer(Index idx) { 274 TMUtil.showIndexedCustomizer(idx); 275 } 276 277 280 public abstract int getNodesCount(); 281 282 285 public abstract void reorder(int[] perm); 286 } 287 289 291 public static class ArrayChildren extends Children.Array implements Index { 292 293 protected Index support; 294 295 297 public ArrayChildren() { 298 this(null); 299 } 300 301 304 private ArrayChildren(List<Node> ar) { 305 super(ar); 306 307 support = new Support() { 309 public Node[] getNodes() { 310 return ArrayChildren.this.getNodes(); 311 } 312 313 public int getNodesCount() { 314 return ArrayChildren.this.getNodesCount(); 315 } 316 317 public void reorder(int[] perm) { 318 ArrayChildren.this.reorder(perm); 319 fireChangeEvent(new ChangeEvent (ArrayChildren.this)); 320 } 321 }; 322 } 323 324 332 @Override 333 protected java.util.List <Node> initCollection() { 334 return new ArrayList<Node>(); 335 } 336 337 341 public void reorder(final int[] perm) { 342 try { 343 PR.enterWriteAccess(); 344 345 Node[] n = nodes.toArray(new Node[nodes.size()]); 346 List<Node> l = (List<Node>) nodes; 347 348 for (int i = 0; i < n.length; i++) { 349 l.set(perm[i], n[i]); 350 } 351 352 refresh(); 353 } finally { 354 PR.exitWriteAccess(); 355 } 356 } 357 358 360 public void reorder() { 361 try { 362 PR.enterReadAccess(); 363 Support.showIndexedCustomizer(this); 364 } finally { 365 PR.exitReadAccess(); 366 } 367 } 368 369 377 public void move(final int x, final int y) { 378 support.move(x, y); 379 } 380 381 387 public void exchange(final int x, final int y) { 388 support.exchange(x, y); 389 } 390 391 396 public void moveUp(final int x) { 397 support.exchange(x, x - 1); 398 } 399 400 405 public void moveDown(final int x) { 406 support.exchange(x, x + 1); 407 } 408 409 413 public int indexOf(final Node node) { 414 try { 415 PR.enterReadAccess(); 416 417 return ((List<Node>) nodes).indexOf(node); 418 } finally { 419 PR.exitReadAccess(); 420 } 421 } 422 423 428 public void addChangeListener(final ChangeListener chl) { 429 support.addChangeListener(chl); 430 } 431 432 437 public void removeChangeListener(final ChangeListener chl) { 438 support.removeChangeListener(chl); 439 } 440 } 441 443 446 public abstract class KeysChildren<T> extends Children.Keys<T> { 447 448 private Index support; 450 451 protected final List<T> list; 452 453 456 public KeysChildren(List<T> ar) { 457 list = ar; 458 update(); 459 } 460 461 464 public Index getIndex() { 465 synchronized (this) { 466 if (support == null) { 467 support = createIndex(); 468 } 469 470 return support; 471 } 472 } 473 474 477 protected Index createIndex() { 478 return new Support() { 480 482 public Node[] getNodes() { 483 List<Node> l = Arrays.asList(KeysChildren.this.getNodes()); 484 485 if (KeysChildren.this.nodes != null) { 486 l.removeAll(KeysChildren.this.nodes); 487 } 488 489 return l.toArray(new Node[l.size()]); 490 } 491 492 public int getNodesCount() { 493 return list.size(); 494 } 495 496 public void reorder(int[] perm) { 497 KeysChildren.this.reorder(perm); 498 update(); 499 fireChangeEvent(new ChangeEvent (this)); 500 } 501 }; 502 } 503 504 508 protected void reorder(final int[] perm) { 509 synchronized (lock()) { 510 List<T> n = new ArrayList<T>(list); 511 512 for (int i = 0; i < n.size(); i++) { 513 list.set(perm[i], n.get(i)); 514 } 515 } 516 } 517 518 524 protected Object lock() { 525 return list; 526 } 527 528 530 public final void update() { 531 Collection<T> keys; 532 533 synchronized (lock()) { 534 keys = new ArrayList<T>(list); 535 } 536 537 super.setKeys(keys); 538 } 539 } 540 } 541 | Popular Tags |