1 19 20 package org.netbeans.core; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.io.ObjectInput ; 25 import java.io.IOException ; 26 import java.util.*; 27 import java.util.List ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import java.util.prefs.PreferenceChangeEvent ; 31 import java.util.prefs.PreferenceChangeListener ; 32 33 import javax.swing.*; 34 import javax.swing.event.*; 35 import org.openide.explorer.*; 36 import org.openide.explorer.view.BeanTreeView; 37 import org.openide.explorer.view.TreeView; 38 import org.openide.nodes.Node; 39 import org.openide.nodes.NodeListener; 40 import org.openide.util.Exceptions; 41 import org.openide.util.HelpCtx; 42 import org.openide.util.NbBundle; 43 import org.openide.util.Mutex; 44 import org.openide.util.WeakListeners; 45 import org.openide.windows.CloneableTopComponent; 46 import org.openide.windows.Mode; 47 import org.openide.windows.TopComponent; 48 import org.openide.windows.WindowManager; 49 50 57 public final class NbMainExplorer extends CloneableTopComponent { 58 59 static final long serialVersionUID=6021472310669753679L; 60 62 63 private List <Node> prevRoots; 64 65 67 private Map<Node, ExplorerTab> rootsToTCs; 68 69 70 private transient RootsListener rootsListener; 71 72 73 public static final int MIN_HEIGHT = 150; 74 75 public static final int DEFAULT_WIDTH = 350; 76 77 78 private static Map<Node, ModuleTab> moduleTabs; 79 80 81 public NbMainExplorer () { 82 rootsListener = new RootsListener(); 85 NbPlaces p = NbPlaces.getDefault(); 86 p.addChangeListener(WeakListeners.change (rootsListener, p)); 87 88 refreshRoots(); 89 } 90 91 public HelpCtx getHelpCtx () { 92 return ExplorerUtils.getHelpCtx (getActivatedNodes (), 93 new HelpCtx (NbMainExplorer.class)); 94 } 95 96 99 private static synchronized ModuleTab findModuleTab (Node root, ModuleTab tc) { 100 System.out.println("NbMainExplorer.findModuleTab "+root); 101 if (moduleTabs == null) { 102 moduleTabs = new WeakHashMap<Node, ModuleTab>(5); 103 } 104 ModuleTab tab = moduleTabs.get(root); 105 if (tab != null) { 106 return tab; 107 } else { 108 if (tc != null) { 109 moduleTabs.put(root, tc); 110 return tc; 111 } else { 112 ModuleTab newTC = new ModuleTab(); 113 moduleTabs.put(root, newTC); 114 return newTC; 115 } 116 } 117 } 118 119 123 @SuppressWarnings ("deprecation") 124 public void open (org.openide.windows.Workspace workspace) { 125 doOpen(workspace); 126 } 127 128 @SuppressWarnings ("deprecation") 129 private void doOpen(org.openide.windows.Workspace workspace) { 130 if (workspace == null) { 131 refreshRoots (); 133 } else { 134 super.open(workspace); 136 close(workspace); 137 NbMainExplorer singleton = NbMainExplorer.getExplorer(); 139 singleton.openRoots(workspace); 140 } 141 } 142 143 144 @SuppressWarnings ("deprecation") 145 public void openRoots () { 146 openRoots(WindowManager.getDefault().getCurrentWorkspace()); 147 } 148 149 150 @SuppressWarnings ("deprecation") 151 public void openRoots (org.openide.windows.Workspace workspace) { 152 ExplorerTab toBeActivated = MainTab.lastActivated; 154 refreshRoots(); 156 Node[] rootsArray = getRoots().toArray(new Node[0]); 157 TopComponent tc = null; 158 for (int i = 0; i < rootsArray.length; i++) { 159 tc = getRootPanel(rootsArray[i]); 160 if (tc != null) { 161 tc.open(workspace); 162 } 163 } 164 if (toBeActivated == null) { 166 toBeActivated = getRootPanel(rootsArray[0]); 167 } 168 169 185 186 if (SwingUtilities.isEventDispatchThread()) { 191 if (toBeActivated != null) { 192 Mode mode = workspace.findMode(toBeActivated); 193 if (mode != null) { 194 toBeActivated.requestActive(); 195 } 196 } 197 } else { 198 if (toBeActivated != null) { 199 final ExplorerTab localActivated = toBeActivated; 200 final org.openide.windows.Workspace localWorkspace = workspace; 201 SwingUtilities.invokeLater(new Runnable () { 202 public void run () { 203 Mode mode = localWorkspace.findMode(localActivated); 204 if (mode != null) { 205 localActivated.requestActive(); 206 } 207 } 208 }); 209 } 210 } 211 } 214 215 218 @SuppressWarnings ("deprecation") final void refreshRoots () { 219 List <Node> curRoots = getRoots (); 220 if (prevRoots != null) { 223 HashSet<Node> toRemove = new HashSet<Node>(prevRoots); 224 toRemove.removeAll(curRoots); 225 for (Map.Entry<Node, ExplorerTab> me: rootsToTCs.entrySet()) { 227 Node r = me.getKey(); 228 if (toRemove.contains(r)) { 229 closeEverywhere(me.getValue()); 232 } 233 } 234 } else { 235 prevRoots(); 237 } 238 239 List workspaces = whereOpened( 241 rootsToTCs().values().toArray(new ExplorerTab[0]) 242 ); 243 for (Iterator iter = curRoots.iterator(); iter.hasNext(); ) { 244 Node r = (Node)iter.next(); 245 ExplorerTab tc = getRootPanel(r); 246 if (tc == null) { 247 tc = createTC(r, false); 251 252 for (Iterator iter2 = workspaces.iterator(); iter2.hasNext(); ) { 253 tc.open((org.openide.windows.Workspace)iter2.next()); 254 } 255 } 256 } 257 258 prevRoots = curRoots; 260 } 261 262 264 @SuppressWarnings ("deprecation") 265 private static void closeEverywhere (TopComponent tc) { 266 org.openide.windows.Workspace[] workspaces = WindowManager.getDefault().getWorkspaces(); 267 for (int i = 0; i < workspaces.length; i++) { 268 if (tc.isOpened(workspaces[i])) { 269 tc.close(workspaces[i]); 270 } 271 } 272 } 273 274 276 @SuppressWarnings ("deprecation") 277 private static List <org.openide.windows.Workspace> whereOpened (TopComponent[] tcs) { 278 org.openide.windows.Workspace[] workspaces = WindowManager.getDefault().getWorkspaces(); 279 ArrayList<org.openide.windows.Workspace> result = new ArrayList<org.openide.windows.Workspace>(workspaces.length); 280 for (int i = 0; i < workspaces.length; i++) { 281 for (int j = 0; j < tcs.length; j++) { 282 if (tcs[j].isOpened(workspaces[i])) { 283 result.add(workspaces[i]); 284 break; 285 } 286 } 287 } 288 return result; 289 } 290 291 296 299 public static List <Node> getRoots () { 300 NbPlaces places = NbPlaces.getDefault(); 301 LinkedList<Node> result = new LinkedList<Node>(); 303 304 310 311 result.addAll(Arrays.asList(places.roots())); 313 result.add(places.environment()); 315 316 return result; 317 } 318 319 321 private ExplorerTab createTC (Node rc, boolean deserialize) { 322 MainTab panel = null; 324 NbPlaces places = NbPlaces.getDefault(); 325 326 if (rc.equals(places.environment())) { 327 if (deserialize) { 329 TopComponent tc = WindowManager.getDefault().findTopComponent("runtime"); if (tc != null) { 331 if (tc instanceof MainTab) { 332 panel = (MainTab) tc; 333 } else { 334 IllegalStateException exc = new IllegalStateException 336 ("Incorrect settings file. Unexpected class returned." + " Expected:" + MainTab.class.getName() + " Returned:" + tc.getClass().getName()); Logger.getLogger(NbMainExplorer.class.getName()).log(Level.WARNING, null, exc); 340 panel = MainTab.getDefaultMainTab(); 341 } 342 } else { 343 panel = MainTab.getDefaultMainTab(); 344 } 345 } else { 346 panel = MainTab.getDefaultMainTab(); 347 } 348 panel.setRootContext(rc, false); 349 } else { 350 panel = NbMainExplorer.findModuleTab(rc, null); 354 panel.setRootContext(rc); 355 } 356 357 358 rootsToTCs().put(rc, panel); 359 return panel; 360 } 361 362 363 private Map<Node,ExplorerTab> rootsToTCs () { 364 if (rootsToTCs == null) { 365 rootsToTCs = new HashMap<Node,ExplorerTab>(7); 366 } 367 return rootsToTCs; 368 } 369 370 371 private List <Node> prevRoots () { 372 if (prevRoots == null) { 373 prevRoots = new LinkedList<Node>(); 374 } 375 return prevRoots; 376 } 377 378 381 public void readExternal (ObjectInput in) 382 throws IOException , ClassNotFoundException { 383 super.readExternal(in); 384 int cnt = in.readInt (); 387 for (int i = 0; i < cnt; i++) { 388 in.readObject(); 389 } 390 in.readObject(); 391 in.readBoolean (); 393 in.readBoolean (); 394 in.readInt(); 395 in.readInt(); 396 } 397 398 403 406 public final ExplorerTab getRootPanel (Node root) { 407 return rootsToTCs().get(root); 408 } 409 410 411 414 417 public static NbMainExplorer getExplorer () { 418 if (explorer == null) { 419 explorer = new NbMainExplorer (); 420 } 421 return explorer; 422 } 423 424 426 @SuppressWarnings ("deprecation") 427 private static Mode explorerMode (org.openide.windows.Workspace workspace) { 428 Mode result = workspace.findMode("explorer"); if (result == null) { 430 String displayName = NbBundle.getBundle(NbMainExplorer.class). 432 getString("CTL_ExplorerTitle"); 433 result = workspace.createMode( 434 "explorer", displayName, 436 NbMainExplorer.class.getResource( 437 "/org/netbeans/core/resources/frames/explorer.gif" ) 439 ); 440 } 441 return result; 442 } 443 444 445 private static NbMainExplorer explorer; 446 447 448 450 public static class ExplorerTab extends org.netbeans.beaninfo.ExplorerPanel 451 implements TopComponent.Cloneable { 452 static final long serialVersionUID =-8202452314155464024L; 453 454 protected TreeView view; 455 456 private PropertyChangeListener weakRcL; 457 private NodeListener weakNRcL; 458 private IDESettings ideSettings; 459 460 private NodeListener rcListener; 461 462 private boolean valid = true; 463 private boolean rootVis = true; 464 465 467 protected int persistenceType = TopComponent.PERSISTENCE_ALWAYS; 468 469 public ExplorerTab () { 470 super(); 471 ideSettings = IDESettings.getInstance(); 473 474 getActionMap().put("delete", ExplorerUtils.actionDelete(getExplorerManager(), ideSettings.getConfirmDelete ())); 475 476 IDESettings.getPreferences().addPreferenceChangeListener(new PreferenceChangeListener () { 477 public void preferenceChange(PreferenceChangeEvent evt) { 478 if (IDESettings.PROP_CONFIRM_DELETE.equals(evt.getKey())) { 479 getActionMap().put("delete", ExplorerUtils.actionDelete(getExplorerManager(), ideSettings.getConfirmDelete ())); 480 } 481 } 482 }); 483 } 484 485 488 public int getPersistenceType() { 489 return TopComponent.PERSISTENCE_ALWAYS; 490 } 491 492 493 protected void componentShowing () { 494 super.componentShowing (); 495 496 if (view == null) { 497 view = initGui (); 498 view.setRootVisible(rootVis); 499 500 view.getAccessibleContext().setAccessibleName(NbBundle.getBundle(NbMainExplorer.class).getString("ACSN_ExplorerBeanTree")); 501 view.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(NbMainExplorer.class).getString("ACSD_ExplorerBeanTree")); 502 } 503 } 504 505 509 public void addNotify () { 510 super.addNotify(); 511 if (WindowManager.getDefault().findMode(this) != null) { 512 return; 513 } 514 componentShowing(); 515 } 516 517 518 @SuppressWarnings ("deprecation") public void requestFocus () { 519 super.requestFocus(); 520 if (view != null) { 521 view.requestFocus(); 522 } 523 } 524 525 526 @SuppressWarnings ("deprecation") public boolean requestFocusInWindow () { 527 super.requestFocusInWindow(); 528 if (view != null) { 529 return view.requestFocusInWindow(); 530 } else { 531 return false; 532 } 533 } 534 535 539 protected TreeView initGui () { 540 TreeView view = new BeanTreeView(); 541 view.setDragSource (true); 542 setLayout(new BorderLayout()); 543 add (view); 544 return view; 545 } 546 547 548 @SuppressWarnings ("deprecation") public void open (org.openide.windows.Workspace workspace) { 549 setValidRootContext(); 550 551 super.open(workspace); 552 } 553 554 556 public void setRootContext (Node rc) { 557 Node oldRC = getExplorerManager().getRootContext(); 558 if (weakRcL != null) { 560 oldRC.removePropertyChangeListener(weakRcL); 561 } 562 if (weakNRcL != null) { 563 oldRC.removeNodeListener(weakNRcL); 564 } 565 getExplorerManager().setRootContext(rc); 566 initializeWithRootContext(rc); 567 } 568 569 public void setRootContext(Node rc, boolean rootVisible) { 570 rootVis = rootVisible; 571 if (view != null) { 572 view.setRootVisible(rootVisible); 573 } 574 setRootContext(rc); 575 } 576 577 581 public void adjustComponentPersistence() { 582 Node.Handle handle = getExplorerManager().getRootContext().getHandle(); 583 if(handle == null) { 584 persistenceType = TopComponent.PERSISTENCE_NEVER; 586 } else { 587 persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED; 589 } 590 } 591 592 public Node getRootContext () { 593 return getExplorerManager().getRootContext(); 594 } 595 596 598 public Object readResolve() throws java.io.ObjectStreamException { 599 scheduleValidation(); 606 return this; 607 } 608 609 private void setValidRootContext() { 610 if (!valid) { 611 valid = true; 612 validateRootContext(); 613 } 614 } 615 616 623 protected void validateRootContext () { 624 initializeWithRootContext(getExplorerManager().getRootContext()); 625 } 626 627 633 protected void updateTitle () { 634 setName(getExplorerManager ().getRootContext().getDisplayName()); 636 } 637 638 private NodeListener rcListener () { 639 if (rcListener == null) { 640 rcListener = new RootContextListener(); 641 } 642 return rcListener; 643 } 644 645 647 private void initializeWithRootContext (Node rc) { 648 setIcon(rc.getIcon(BeanInfo.ICON_COLOR_16x16)); 650 setToolTipText(rc.getShortDescription()); 651 setName(rc.getDisplayName()); 653 updateTitle(); 654 655 if (weakRcL == null) { 656 weakRcL = WeakListeners.propertyChange(rcListener(), rc); 657 } 658 else { 659 rc.removePropertyChangeListener(weakRcL); 660 } 661 rc.addPropertyChangeListener(weakRcL); 662 663 if (weakNRcL == null) { 664 weakNRcL = org.openide.nodes.NodeOp.weakNodeListener (rcListener(), rc); 665 } 666 else { 667 rc.removeNodeListener(weakNRcL); 668 } 669 rc.addNodeListener(weakNRcL); 670 } 671 672 protected final void scheduleValidation() { 677 valid = false; 678 setValidRootContext(); 679 } 680 681 682 public void setName(String name) { 683 super.setName(name); 684 if (view != null) { 685 view.getAccessibleContext().setAccessibleName(name); 686 } 687 } 688 689 690 public void setToolTipText(String text) { 691 super.setToolTipText(text); 692 if (view != null) { 693 view.getAccessibleContext().setAccessibleDescription(text); 694 } 695 } 696 697 public TopComponent cloneComponent() { 698 ExplorerTab nue = new ExplorerTab(); 699 nue.getExplorerManager().setRootContext(getExplorerManager().getRootContext()); 700 try { 701 nue.getExplorerManager().setSelectedNodes(getExplorerManager().getSelectedNodes()); 702 } catch (PropertyVetoException pve) { 703 Exceptions.printStackTrace(pve); 704 } 705 return nue; 706 } 707 708 711 private final class RootContextListener extends Object implements NodeListener { 712 713 RootContextListener() {} 714 715 public void propertyChange (PropertyChangeEvent evt) { 716 String propName = evt.getPropertyName(); 717 Object source = evt.getSource(); 718 final Node n = (Node)source; 720 if (Node.PROP_DISPLAY_NAME.equals(propName) || 721 Node.PROP_NAME.equals(propName)) { 722 Mutex.EVENT.readAccess(new Runnable () { 724 public void run() { 725 setName(n.getDisplayName()); 726 } 727 }); 728 } else if (Node.PROP_ICON.equals(propName)) { 730 Mutex.EVENT.readAccess(new Runnable () { 732 public void run() { 733 setIcon(n.getIcon(BeanInfo.ICON_COLOR_16x16)); 734 } 735 }); 736 } else if (Node.PROP_SHORT_DESCRIPTION.equals(propName)) { 738 setToolTipText(n.getShortDescription()); 739 } 740 } 741 742 @SuppressWarnings ("deprecation") public void nodeDestroyed(org.openide.nodes.NodeEvent nodeEvent) { 743 ExplorerTab.this.setCloseOperation(TopComponent.CLOSE_EACH); 744 ExplorerTab.this.close(); 745 } 746 747 public void childrenRemoved(org.openide.nodes.NodeMemberEvent e) {} 748 public void childrenReordered(org.openide.nodes.NodeReorderEvent e) {} 749 public void childrenAdded(org.openide.nodes.NodeMemberEvent e) {} 750 751 } 753 } 755 758 public static class MainTab extends ExplorerTab { 759 static final long serialVersionUID =4233454980309064344L; 760 761 765 private static MainTab lastActivated; 766 767 private static MainTab DEFAULT; 768 769 public static synchronized MainTab getDefaultMainTab() { 770 if (DEFAULT == null) { 771 DEFAULT = new MainTab(); 772 DEFAULT.scheduleValidation(); 777 } 778 779 return DEFAULT; 780 } 781 782 785 public static MainTab findEnvironmentTab () { 786 return (MainTab)getExplorer().createTC( 787 NbPlaces.getDefault().environment(), true 788 ); 789 } 790 791 795 public static MainTab createEnvironmentTab () { 796 return (MainTab)getExplorer().createTC( 797 NbPlaces.getDefault().environment(), false 798 ); 799 } 800 801 803 public int getPersistenceType() { 804 return TopComponent.PERSISTENCE_ALWAYS; 805 } 806 807 protected String preferredID () { 808 return "runtime"; } 810 811 public HelpCtx getHelpCtx () { 812 return ExplorerUtils.getHelpCtx (getExplorerManager ().getSelectedNodes (), 813 new HelpCtx (EnvironmentNode.class)); 814 } 815 816 817 public Object readResolve() throws java.io.ObjectStreamException { 818 if (DEFAULT == null) { 819 DEFAULT = this; 820 } 821 getDefaultMainTab().scheduleValidation(); 822 return getDefaultMainTab(); 823 } 824 825 @SuppressWarnings ("deprecation") public void open (org.openide.windows.Workspace workspace) { 826 org.openide.windows.Workspace realWorkspace = (workspace == null) 827 ? WindowManager.getDefault().getCurrentWorkspace() 828 : workspace; 829 Mode ourMode = realWorkspace.findMode(this); 830 if (ourMode == null) { 831 explorerMode(realWorkspace).dockInto(this); 832 } 833 super.open(workspace); 834 } 835 836 839 protected void updateTitle () { 840 } 842 843 845 protected void componentActivated () { 846 super.componentActivated(); 847 lastActivated = this; 848 } 849 850 852 protected void validateRootContext () { 853 super.validateRootContext(); 854 registerRootContext(getExplorerManager().getRootContext()); 855 } 856 857 859 protected void registerRootContext (Node rc) { 860 NbMainExplorer explorer = NbMainExplorer.getExplorer(); 861 explorer.prevRoots().add(rc); 862 explorer.rootsToTCs().put(rc, this); 863 } 864 865 } 867 868 public static class ModuleTab extends MainTab { 869 static final long serialVersionUID =8089827754534653731L; 870 871 public ModuleTab() { 872 } 874 875 876 public void setRootContext(Node root) { 877 super.setRootContext(root); 878 adjustComponentPersistence(); 879 } 880 881 883 public int getPersistenceType() { 884 return persistenceType; 885 } 886 887 891 protected void validateRootContext () { 892 Class nodeClass = getExplorerManager().getRootContext().getClass(); 894 Node[] roots = NbPlaces.getDefault().roots(); 895 for (int i = 0; i < roots.length; i++) { 896 if (nodeClass.equals(roots[i].getClass())) { 897 setRootContext(roots[i]); 898 registerRootContext(roots[i]); 899 break; 900 } 901 } 902 } 903 904 905 public Object readResolve() throws java.io.ObjectStreamException { 906 Node root = getExplorerManager().getRootContext(); 907 908 ModuleTab tc = NbMainExplorer.findModuleTab(root, this); 909 if(tc == null) { 910 throw new java.io.InvalidObjectException ( 911 "Cannot deserialize ModuleTab for node " + root); } 913 914 tc.scheduleValidation(); 915 return tc; 916 } 917 918 } 920 921 private static final class RootsListener extends Object implements ChangeListener { 922 923 RootsListener() {} 924 925 public void stateChanged(ChangeEvent e) { 926 NbMainExplorer.getExplorer().doOpen(null); 927 } 928 } 930 public static void main (String [] args) throws Exception { 931 NbMainExplorer e = new NbMainExplorer (); 932 e.open (); 933 } 934 } 935 | Popular Tags |