1 19 20 package org.openide.windows; 21 22 import java.awt.Frame ; 23 import java.awt.Image ; 24 import java.awt.Rectangle ; 25 import java.awt.event.WindowAdapter ; 26 import java.awt.event.WindowEvent ; 27 import java.beans.PropertyChangeListener ; 28 import java.beans.PropertyChangeSupport ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.Arrays ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.Set ; 37 import java.util.TreeMap ; 38 import java.util.WeakHashMap ; 39 import javax.swing.Action ; 40 import javax.swing.JFrame ; 41 import javax.swing.SwingUtilities ; 42 import org.openide.nodes.Node; 43 import org.openide.util.Utilities; 44 import org.openide.util.actions.SystemAction; 45 46 55 @SuppressWarnings ("deprecation") 56 final class DummyWindowManager extends WindowManager { 57 58 private static final boolean VISIBLE = Boolean.parseBoolean(System.getProperty("org.openide.windows.DummyWindowManager.VISIBLE", "true")); 59 private static final long serialVersionUID = 1L; 60 private static Action [] DEFAULT_ACTIONS_CLONEABLE; 61 private static Action [] DEFAULT_ACTIONS_NOT_CLONEABLE; 62 private final Map <String ,Workspace> workspaces; 63 private transient Frame mw; 64 private transient PropertyChangeSupport pcs; 65 private transient R r; 66 67 public DummyWindowManager() { 68 workspaces = new TreeMap <String ,Workspace>(); 69 createWorkspace("default", null).createMode("editor", "editor", null); } 71 72 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 73 if (pcs == null) { 74 pcs = new PropertyChangeSupport (this); 75 } 76 77 pcs.addPropertyChangeListener(l); 78 } 79 80 public synchronized void removePropertyChangeListener(PropertyChangeListener l) { 81 if (pcs != null) { 82 pcs.removePropertyChangeListener(l); 83 } 84 } 85 86 protected TopComponent.Registry componentRegistry() { 87 TopComponent.Registry reg = super.componentRegistry(); 88 89 if (reg != null) { 90 return reg; 91 } else { 92 return registry(); 93 } 94 } 95 96 synchronized R registry() { 97 if (r == null) { 98 r = new R(); 99 } 100 101 return r; 102 } 103 104 protected WindowManager.Component createTopComponentManager(TopComponent c) { 105 return null; } 107 108 public synchronized Workspace createWorkspace(String name, String displayName) { 109 Workspace w = new W(name); 110 workspaces.put(name, w); 111 112 if (pcs != null) { 113 pcs.firePropertyChange(PROP_WORKSPACES, null, null); 114 pcs.firePropertyChange(PROP_CURRENT_WORKSPACE, null, null); 115 } 116 117 return w; 118 } 119 120 synchronized void delete(Workspace w) { 121 workspaces.remove(w.getName()); 122 123 if (workspaces.isEmpty()) { 124 createWorkspace("default", null); } 126 127 if (pcs != null) { 128 pcs.firePropertyChange(PROP_WORKSPACES, null, null); 129 pcs.firePropertyChange(PROP_CURRENT_WORKSPACE, null, null); 130 } 131 } 132 133 public synchronized Workspace findWorkspace(String name) { 134 return workspaces.get(name); 135 } 136 137 public synchronized Workspace getCurrentWorkspace() { 138 return workspaces.values().iterator().next(); 139 } 140 141 public synchronized Workspace[] getWorkspaces() { 142 return workspaces.values().toArray(new Workspace[0]); 143 } 144 145 public synchronized void setWorkspaces(Workspace[] ws) { 146 if (ws.length == 0) { 147 throw new IllegalArgumentException (); 148 } 149 150 workspaces.clear(); 151 152 for (int i = 0; i < ws.length; i++) { 153 workspaces.put(ws[i].getName(), ws[i]); 154 } 155 156 if (pcs != null) { 157 pcs.firePropertyChange(PROP_WORKSPACES, null, null); 158 pcs.firePropertyChange(PROP_CURRENT_WORKSPACE, null, null); 159 } 160 } 161 162 public synchronized Frame getMainWindow() { 163 if (mw == null) { 164 mw = new JFrame ("dummy"); } 166 167 return mw; 168 } 169 170 public void updateUI() { 171 } 172 173 public Set <Mode> getModes() { 175 Set <Mode> s = new HashSet <Mode>(); 176 177 for (Iterator <Workspace> it = new HashSet <Workspace>(workspaces.values()).iterator(); it.hasNext();) { 178 Workspace w = it.next(); 179 s.addAll(w.getModes()); 180 } 181 182 return s; 183 } 184 185 public Mode findMode(TopComponent tc) { 186 for (Iterator <Mode> it = getModes().iterator(); it.hasNext();) { 187 Mode m = it.next(); 188 189 if (Arrays.asList(m.getTopComponents()).contains(tc)) { 190 return m; 191 } 192 } 193 194 return null; 195 } 196 197 public Mode findMode(String name) { 198 if (name == null) { 199 return null; 200 } 201 202 for (Iterator <Mode> it = getModes().iterator(); it.hasNext();) { 203 Mode m = it.next(); 204 205 if (name.equals(m.getName())) { 206 return m; 207 } 208 } 209 210 return null; 211 } 212 213 public TopComponentGroup findTopComponentGroup(String name) { 215 return null; 216 } 217 218 public TopComponent findTopComponent(String tcID) { 220 return null; 221 } 222 223 protected String topComponentID(TopComponent tc, String preferredID) { 224 return preferredID; 225 } 226 227 protected Action [] topComponentDefaultActions(TopComponent tc) { 228 synchronized (DummyWindowManager.class) { 230 if (tc instanceof TopComponent.Cloneable) { 233 if (DEFAULT_ACTIONS_CLONEABLE == null) { 234 DEFAULT_ACTIONS_CLONEABLE = loadActions( 235 new String [] { "Save", "CloneView", null, "CloseView" } 239 ); 240 } 241 242 return DEFAULT_ACTIONS_CLONEABLE; 243 } else { 244 if (DEFAULT_ACTIONS_NOT_CLONEABLE == null) { 245 DEFAULT_ACTIONS_NOT_CLONEABLE = loadActions( 246 new String [] { "Save", null, "CloseView" } 249 ); 250 } 251 252 return DEFAULT_ACTIONS_NOT_CLONEABLE; 253 } 254 } 255 } 256 257 private static Action [] loadActions(String [] names) { 258 ArrayList <Action > arr = new ArrayList <Action >(); 259 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 260 261 for (int i = 0; i < names.length; i++) { 262 if (names[i] == null) { 263 arr.add(null); 264 265 continue; 266 } 267 268 try { 269 Class <? extends SystemAction> sa = Class.forName("org.openide.actions." + names[i] + "Action", true, loader).asSubclass(SystemAction.class); 270 arr.add(SystemAction.get(sa)); } catch (ClassNotFoundException e) { 272 } 274 } 275 276 return arr.toArray(new Action [0]); 277 } 278 279 protected boolean topComponentIsOpened(TopComponent tc) { 280 return tc.isShowing(); 281 } 282 283 protected void topComponentActivatedNodesChanged(TopComponent tc, Node[] nodes) { 284 registry().setActivatedNodes(tc, nodes); 285 } 286 287 protected void topComponentIconChanged(TopComponent tc, Image icon) { 288 JFrame f = (JFrame ) SwingUtilities.getAncestorOfClass(JFrame .class, tc); 289 290 if (f != null) { 291 f.setIconImage(icon); 292 } 293 } 294 295 protected void topComponentToolTipChanged(TopComponent tc, String tooltip) { 296 } 298 299 protected void topComponentDisplayNameChanged(TopComponent tc, String displayName) { 300 JFrame f = (JFrame ) SwingUtilities.getAncestorOfClass(JFrame .class, tc); 301 302 if (f != null) { 303 f.setTitle(displayName); 304 } 305 } 306 307 protected void topComponentHtmlDisplayNameChanged(TopComponent tc, String htmlDisplayName) { 308 } 310 311 protected void topComponentOpen(TopComponent tc) { 312 JFrame f = (JFrame ) SwingUtilities.getAncestorOfClass(JFrame .class, tc); 313 314 if (f == null) { 315 f = new JFrame (tc.getName()); 316 317 Image icon = tc.getIcon(); 318 319 if (icon != null) { 320 f.setIconImage(icon); 321 } 322 323 f.getContentPane().add(tc); 324 f.pack(); 325 326 final java.lang.ref.WeakReference <TopComponent> ref = new java.lang.ref.WeakReference <TopComponent>(tc); 327 f.addWindowListener( 328 new WindowAdapter () { 329 public void windowClosing(WindowEvent ev) { 330 TopComponent tc = ref.get(); 331 332 if (tc == null) { 333 return; 334 } 335 336 tc.close(); 337 } 338 339 public void windowActivated(WindowEvent e) { 340 TopComponent tc = ref.get(); 341 342 if (tc == null) { 343 return; 344 } 345 346 tc.requestActive(); 347 } 348 } 349 ); 350 } 351 352 if (!tc.isShowing()) { 353 componentOpenNotify(tc); 354 componentShowing(tc); 355 if (VISIBLE) { 356 f.setVisible(true); 357 } 358 registry().open(tc); 359 } 360 } 361 362 protected void topComponentClose(TopComponent tc) { 363 componentHidden(tc); 364 componentCloseNotify(tc); 365 366 JFrame f = (JFrame ) SwingUtilities.getAncestorOfClass(JFrame .class, tc); 367 368 if (f != null) { 369 if (VISIBLE) { 370 f.setVisible(false); 371 } 372 tc.getParent().remove(tc); 373 } 374 375 registry().close(tc); 376 377 java.util.Iterator it = workspaces.values().iterator(); 378 379 while (it.hasNext()) { 380 W w = (W) it.next(); 381 w.close(tc); 382 } 383 } 384 385 protected void topComponentRequestVisible(TopComponent tc) { 386 JFrame f = (JFrame ) SwingUtilities.getAncestorOfClass(JFrame .class, tc); 387 388 if (f != null) { 389 if (VISIBLE) { 390 f.setVisible(true); 391 } 392 } 393 } 394 395 protected void topComponentRequestActive(TopComponent tc) { 396 JFrame f = (JFrame ) SwingUtilities.getAncestorOfClass(JFrame .class, tc); 397 398 if (f != null) { 399 f.toFront(); 400 } 401 402 registry().setActive(tc); 403 activateComponent(tc); 404 } 405 406 protected void topComponentRequestAttention(TopComponent tc) { 407 } 409 410 protected void topComponentCancelRequestAttention(TopComponent tc) { 411 } 413 414 private final class W implements Workspace { 415 private static final long serialVersionUID = 1L; 416 private final String name; 417 private final Map <String ,Mode> modes = new HashMap <String ,Mode>(); 418 private final Map <TopComponent,Mode> modesByComponent = new WeakHashMap <TopComponent,Mode>(); 419 private transient PropertyChangeSupport pcs; 420 421 public W(String name) { 422 this.name = name; 423 } 424 425 public void activate() { 426 } 427 428 public synchronized void addPropertyChangeListener(PropertyChangeListener list) { 429 if (pcs == null) { 430 pcs = new PropertyChangeSupport (this); 431 } 432 433 pcs.addPropertyChangeListener(list); 434 } 435 436 public synchronized void removePropertyChangeListener(PropertyChangeListener list) { 437 if (pcs != null) { 438 pcs.removePropertyChangeListener(list); 439 } 440 } 441 442 public void remove() { 443 DummyWindowManager.this.delete(this); 444 } 445 446 public synchronized Mode createMode(String name, String displayName, URL icon) { 447 Mode m = new M(name); 448 modes.put(name, m); 449 450 if (pcs != null) { 451 pcs.firePropertyChange(PROP_MODES, null, null); 452 } 453 454 return m; 455 } 456 457 public synchronized Set <Mode> getModes() { 458 return new HashSet <Mode>(modes.values()); 459 } 460 461 public synchronized Mode findMode(String name) { 462 return modes.get(name); 463 } 464 465 public synchronized Mode findMode(TopComponent c) { 466 return modesByComponent.get(c); 467 } 468 469 synchronized void dock(Mode m, TopComponent c) { 470 modesByComponent.put(c, m); 471 } 472 473 public Rectangle getBounds() { 474 return Utilities.getUsableScreenBounds(); 475 } 476 477 public String getName() { 478 return name; 479 } 480 481 public String getDisplayName() { 482 return getName(); 483 } 484 485 public void close(TopComponent tc) { 486 java.util.Iterator it = modes.values().iterator(); 487 488 while (it.hasNext()) { 489 M m = (M) it.next(); 490 m.close(tc); 491 } 492 } 493 494 private final class M implements Mode { 495 private static final long serialVersionUID = 1L; 496 private final String name; 497 private final Set <TopComponent> components = new HashSet <TopComponent>(); 498 499 public M(String name) { 500 this.name = name; 501 } 502 503 public void close(TopComponent tc) { 504 components.remove(tc); 505 } 506 507 521 public void addPropertyChangeListener(PropertyChangeListener l) { 522 } 523 524 public void removePropertyChangeListener(PropertyChangeListener l) { 525 } 526 527 public boolean canDock(TopComponent tc) { 528 return true; 529 } 530 531 public synchronized boolean dockInto(TopComponent c) { 532 if (components.add(c)) { 533 Mode old = findMode(c); 534 535 if ((old != null) && (old != this) && old instanceof M) { 536 synchronized (old) { 537 ((M) old).components.remove(c); 538 } 539 } 540 541 dock(this, c); 542 } 543 544 return true; 545 } 546 547 public String getName() { 548 return name; 549 } 550 551 public String getDisplayName() { 552 return getName(); 553 } 554 555 public Image getIcon() { 556 return null; 557 } 558 559 public synchronized TopComponent[] getTopComponents() { 560 return components.toArray(new TopComponent[0]); 561 } 562 563 public Workspace getWorkspace() { 564 return W.this; 565 } 566 567 public synchronized Rectangle getBounds() { 568 return W.this.getBounds(); 569 } 570 571 public void setBounds(Rectangle s) { 572 } 573 574 public TopComponent getSelectedTopComponent() { 575 TopComponent[] tcs = components.toArray(new TopComponent[0]); 576 577 return (tcs.length > 0) ? tcs[0] : null; 578 } 579 } 580 } 581 582 private static final class R implements TopComponent.Registry { 583 private TopComponent active; 584 private final Set <TopComponent> opened; 585 private Node[] nodes; 586 private PropertyChangeSupport pcs; 587 588 public R() { 589 opened = new HashSet <TopComponent>(); 590 nodes = new Node[0]; 591 } 592 593 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 594 if (pcs == null) { 595 pcs = new PropertyChangeSupport (this); 596 } 597 598 pcs.addPropertyChangeListener(l); 599 } 600 601 public synchronized void removePropertyChangeListener(PropertyChangeListener l) { 602 if (pcs != null) { 603 pcs.removePropertyChangeListener(l); 604 } 605 } 606 607 synchronized void open(TopComponent tc) { 608 opened.add(tc); 609 610 if (pcs != null) { 611 pcs.firePropertyChange(PROP_TC_OPENED, null, tc); 612 pcs.firePropertyChange(PROP_OPENED, null, null); 613 } 614 } 615 616 synchronized void close(TopComponent tc) { 617 opened.remove(tc); 618 619 if (pcs != null) { 620 pcs.firePropertyChange(PROP_TC_CLOSED, null, tc); 621 pcs.firePropertyChange(PROP_OPENED, null, null); 622 } 623 624 if (active == tc) { 625 setActive(null); 626 } 627 } 628 629 public synchronized Set <TopComponent> getOpened() { 630 return new HashSet <TopComponent>(opened); 631 } 632 633 synchronized void setActive(TopComponent tc) { 634 active = tc; 635 636 Node[] _nodes = (tc == null) ? new Node[0] : tc.getActivatedNodes(); 637 638 if (_nodes != null) { 639 nodes = _nodes; 640 641 if (pcs != null) { 642 pcs.firePropertyChange(PROP_ACTIVATED_NODES, null, null); 643 } 644 } 645 646 if (pcs != null) { 647 pcs.firePropertyChange(PROP_ACTIVATED, null, null); 648 pcs.firePropertyChange(PROP_CURRENT_NODES, null, null); 649 } 650 } 651 652 synchronized void setActivatedNodes(TopComponent tc, Node[] _nodes) { 653 if (tc == active) { 654 if (_nodes != null) { 655 nodes = _nodes; 656 657 if (pcs != null) { 658 pcs.firePropertyChange(PROP_ACTIVATED_NODES, null, null); 659 } 660 } 661 662 if (pcs != null) { 663 pcs.firePropertyChange(PROP_CURRENT_NODES, null, null); 664 } 665 } 666 } 667 668 public TopComponent getActivated() { 669 return active; 670 } 671 672 public Node[] getActivatedNodes() { 673 return nodes; 674 } 675 676 public synchronized Node[] getCurrentNodes() { 677 if (active != null) { 678 return active.getActivatedNodes(); 679 } else { 680 return null; 681 } 682 } 683 } 684 } 685 | Popular Tags |