1 19 20 21 package org.netbeans.core.windows.services; 22 23 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.util.Iterator ; 27 import java.util.WeakHashMap ; 28 import org.netbeans.core.NbMainExplorer; 29 import org.netbeans.core.NbSheet; 30 import org.openide.DialogDescriptor; 31 import org.openide.DialogDisplayer; 32 import org.openide.explorer.ExplorerManager; 33 import org.openide.nodes.*; 34 import org.openide.util.HelpCtx; 35 import org.openide.util.Mutex; 36 import org.openide.util.NbBundle; 37 import org.openide.util.UserCancelException; 38 import org.openide.util.WeakSet; 39 import org.openide.windows.Mode; 40 import org.openide.windows.TopComponent; 41 import org.openide.windows.WindowManager; 42 43 import javax.swing.*; 44 import java.awt.*; 45 import java.util.HashSet ; 46 import java.util.Set ; 47 import javax.swing.border.EmptyBorder ; 48 49 50 52 58 public final class NodeOperationImpl extends NodeOperation { 59 60 63 public void explore (final Node n) { 64 Mutex.EVENT.readAccess (new Runnable () { 65 public void run () { 66 NbMainExplorer.ExplorerTab et = new NonPersistentExplorerTab (); 67 et.setRootContext (n); 68 et.adjustComponentPersistence(); 69 70 Mode target = WindowManager.getDefault().findMode("explorer"); 71 if (target != null) { 72 target.dockInto(et); 73 } 74 et.open(); 75 et.requestActive(); 76 } 77 }); 78 } 79 80 88 public boolean customize (Node n) { 89 final Component customizer = n.getCustomizer (); 90 if (customizer == null) return false; 91 return Mutex.EVENT.readAccess (new Mutex.Action<Boolean > () { 92 public Boolean run () { 93 if (customizer instanceof NbPresenter) { ((NbPresenter) customizer).pack (); 95 ((NbPresenter) customizer).show (); 96 return Boolean.TRUE; 97 } 98 if (customizer instanceof Window) { 99 ((Window) customizer).pack (); 100 customizer.setVisible (true); 101 return Boolean.TRUE; 102 } 103 104 JPanel p = null; 106 if (customizer instanceof ExplorerManager.Provider) { 107 p = new ExplorerProviderFwd(customizer, (ExplorerManager.Provider)customizer); 108 } else { 109 p = new HelpFwdPanel(customizer); 110 } 111 p.setLayout(new BorderLayout()); 112 p.getAccessibleContext().setAccessibleDescription( 113 NbBundle.getMessage(NodeOperationImpl.class, "CTL_Customizer_dialog_title")); 114 115 if (customizer.getClass().getName().startsWith("org.netbeans.modules.xml.catalog")) { p.setBorder(BorderFactory.createEmptyBorder(12, 12, 0, 11)); 119 } 120 122 p.add(customizer, BorderLayout.CENTER); 123 124 DialogDescriptor dd = new DialogDescriptor 126 (p, 127 NbBundle.getMessage(NodeOperationImpl.class, "CTL_Customizer_dialog_title")); 128 dd.setOptions(new Object [] { DialogDescriptor.CLOSED_OPTION }); 129 130 Dialog dialog = org.openide.DialogDisplayer.getDefault ().createDialog(dd); 131 dialog.pack(); 132 dialog.setVisible(true); 133 return Boolean.TRUE; 134 } 135 }).booleanValue (); 136 } 137 138 141 private static class HelpFwdPanel extends JPanel implements HelpCtx.Provider { 142 private Component innerComp; 143 private boolean active = false; 144 145 146 private HelpFwdPanel (Component innerComp) { 147 this.innerComp = innerComp; 148 } 149 150 public HelpCtx getHelpCtx () { 151 try { 152 if (active) return null; 154 active = true; 155 return HelpCtx.findHelp(innerComp); 156 } finally { 157 active = false; 158 } 159 } 160 161 } 163 165 private static final class ExplorerProviderFwd extends HelpFwdPanel 166 implements ExplorerManager.Provider { 167 private ExplorerManager.Provider explProvider; 168 169 170 private ExplorerProviderFwd (Component innerComp, ExplorerManager.Provider explProvider) { 171 super(innerComp); 172 this.explProvider = explProvider; 173 } 174 175 177 public ExplorerManager getExplorerManager() { 178 return explProvider.getExplorerManager(); 179 } 180 181 } 183 186 public void showProperties (Node n) { 187 Dialog d = findCachedPropertiesDialog( n ); 188 if( null == d ) { 189 NbSheet s = new NbSheet (); 190 Node[] nds = new Node[] { n }; 191 s.setNodes (nds); 192 openProperties(s, nds); 193 } else { 194 d.setVisible( true ); 195 d.toFront(); 196 d.requestFocusInWindow(); 197 } 198 } 199 200 203 public void showProperties (Node[] nodes) { 204 Dialog d = findCachedPropertiesDialog( nodes ); 205 if( null == d ) { 206 NbSheet s = new NbSheet (); 207 s.setNodes (nodes); 208 openProperties(s, nodes); 209 } else { 210 d.setVisible( true ); 211 d.toFront(); 212 d.requestFocusInWindow(); 213 } 214 } 215 216 private static WeakSet<Node[]> nodeCache = new WeakSet<Node[]>(); 219 private static WeakHashMap <Node[], Dialog> dialogCache = new WeakHashMap <Node[], Dialog>(); 220 221 private static Dialog findCachedPropertiesDialog( Node n ) { 222 return findCachedPropertiesDialog( new Node[] { n } ); 223 } 224 225 private static Dialog findCachedPropertiesDialog( Node[] nodes ) { 226 for( Iterator <Node[]> it=nodeCache.iterator(); it.hasNext(); ) { 227 Node[] cached = it.next(); 228 if( cached.length != nodes.length ) 229 continue; 230 boolean match = true; 231 for( int i=0; i<cached.length; i++ ) { 232 if( !cached[i].equals( nodes[i] ) ) { 233 match = false; 234 break; 235 } 236 } 237 if( match ) { 238 return dialogCache.get( cached ); 239 } 240 } 241 return null; 242 } 243 244 259 public Node[] select (String title, String rootTitle, Node root, NodeAcceptor acceptor, Component top) 260 throws UserCancelException { 261 final FileSelector selector = new FileSelector(rootTitle, root, acceptor, top); 262 selector.setBorder(new EmptyBorder (12, 12, 0, 12)); 263 DialogDescriptor dd = new DialogDescriptor(selector, title, true, 264 selector.getOptions(), 265 selector.getSelectOption(), DialogDescriptor.DEFAULT_ALIGN, 266 HelpCtx.DEFAULT_HELP, null); 267 Object ret = DialogDisplayer.getDefault().notify(dd); 268 if (ret != selector.getSelectOption()) { 269 throw new UserCancelException (); 270 } 271 return selector.getNodes (); 272 } 273 274 276 private static void openProperties (final TopComponent tc, final Node[] nds) { 277 javax.swing.SwingUtilities.invokeLater(new Runnable () { 280 public void run () { 281 boolean modal; 282 if(NbPresenter.currentModalDialog == null) { 283 modal = false; 284 } else { 285 modal = true; 286 } 287 288 Dialog dlg = org.openide.DialogDisplayer.getDefault().createDialog(new DialogDescriptor ( 289 tc, 290 tc.getName(), 291 modal, 292 new Object [] {DialogDescriptor.CLOSED_OPTION}, 293 DialogDescriptor.CLOSED_OPTION, 294 DialogDescriptor.BOTTOM_ALIGN, 295 null, 296 null 297 )); 298 SheetNodesListener listener = new SheetNodesListener(dlg, tc); 300 listener.attach(nds); 301 302 nodeCache.add( nds ); 303 dialogCache.put( nds, dlg ); 304 305 dlg.setVisible(true); 306 } 307 }); 308 } 309 310 312 private static class NonPersistentExplorerTab extends NbMainExplorer.ExplorerTab { 313 public int getPersistenceType() { 314 return PERSISTENCE_NEVER; 315 } 316 317 protected String preferredID() { 318 return "NonPersistentExplorerTab"; 319 } 320 } 321 322 325 private static class SheetNodesListener extends NodeAdapter implements PropertyChangeListener { 326 327 private Dialog dialog; 328 private Set<Node> listenerSet; 329 330 private TopComponent tc; 331 332 SheetNodesListener(Dialog dialog, TopComponent tc) { 333 this.dialog = dialog; 334 this.tc = tc; 335 tc.addPropertyChangeListener(this); 336 } 337 338 public void propertyChange (PropertyChangeEvent pce) { 339 if ("name".equals(pce.getPropertyName())) { 340 dialog.setTitle((String ) pce.getNewValue()); 341 } 342 } 343 344 public void attach(Node[] nodes) { 345 listenerSet = new HashSet <Node>(nodes.length * 2); 346 for (int i = 0; i < nodes.length; i++) { 347 listenerSet.add(nodes[i]); 348 nodes[i].addNodeListener(this); 349 }; 350 } 351 352 355 public void nodeDestroyed(NodeEvent ev) { 356 Node destroyedNode = ev.getNode(); 357 destroyedNode.removeNodeListener(this); 359 listenerSet.remove(destroyedNode); 360 if (listenerSet.isEmpty()) { 362 tc.removePropertyChangeListener(this); 364 Mutex.EVENT.readAccess(new Runnable () { 365 public void run() { 366 dialog.setVisible(false); 367 dialog.dispose(); 368 dialog = null; 369 } 370 }); 371 } 372 } 373 } 374 375 } 376 | Popular Tags |