1 19 20 package org.netbeans.core.windows.services; 21 22 23 import java.awt.datatransfer.Transferable ; 24 import java.beans.*; 25 import java.io.IOException ; 26 import java.text.MessageFormat ; 27 import java.util.*; 28 import javax.swing.*; 29 import org.netbeans.core.NbPlaces; 30 import org.netbeans.core.windows.view.ui.toolbars.ToolbarConfiguration; 31 import org.openide.NotifyDescriptor; 32 import org.openide.actions.*; 33 import org.openide.awt.*; 34 import org.openide.cookies.InstanceCookie; 35 import org.openide.filesystems.FileObject; 36 import org.openide.loaders.*; 37 import org.openide.nodes.*; 38 import org.openide.nodes.FilterNode.Children; 39 import org.openide.util.*; 40 import org.openide.util.actions.SystemAction; 41 import org.openide.util.datatransfer.*; 42 43 44 50 public final class ToolbarFolderNode extends DataFolder.FolderNode implements PropertyChangeListener { 51 52 53 static SystemAction[] topStaticActions; 54 55 private DataFolder folder; 56 57 public ToolbarFolderNode () { 58 this (NbPlaces.getDefault().toolbars ()); 59 } 60 61 63 ToolbarFolderNode (DataFolder folder) { 64 folder.super(new ToolbarFolderChildren(folder)); 65 this.folder = folder; 66 super.setShortDescription(NbBundle.getBundle (ToolbarFolderNode.class).getString("CTL_Toolbars_hint")); 68 setIconBaseWithExtension ("org/netbeans/core/resources/toolbars.gif"); 70 ToolbarPool.getDefault().addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(this, ToolbarPool.getDefault())); 71 } 72 73 public HelpCtx getHelpCtx () { 74 return new HelpCtx (ToolbarFolderNode.class); 75 } 76 77 protected void createPasteTypes(Transferable t, List<PasteType> s) { 78 PasteType pType = ActionPasteType.getPasteType((DataFolder)getDataObject() , t); 79 if (pType != null) { 80 s.add(pType); 81 } 82 } 83 84 87 public NewType[] getNewTypes () { 88 return new NewType[] { 89 new NewType () { 90 public String getName () { 91 return NbBundle.getBundle (ToolbarFolderNode.class).getString ("PROP_newToolbarName"); 92 } 93 public void create () throws IOException { 94 newToolbar(); 95 } 96 }, 97 }; 98 } 99 100 void newToolbar () { 101 ResourceBundle bundle = NbBundle.getBundle (ToolbarFolderNode.class); 102 NotifyDescriptor.InputLine il = new NotifyDescriptor.InputLine 103 (bundle.getString ("PROP_newToolbarLabel"), 104 bundle.getString ("PROP_newToolbarDialog")); 105 il.setInputText (bundle.getString ("PROP_newToolbar")); 106 107 Object ok = org.openide.DialogDisplayer.getDefault ().notify (il); 108 if (ok == NotifyDescriptor.OK_OPTION) { 109 String s = il.getInputText(); 110 if (!s.equals ("")) { FileObject tbFO = folder.getPrimaryFile(); 112 try { 113 FileObject newFO = tbFO.getFileObject(s); 114 if (newFO == null) { 115 String lastName = getLastName(); 116 newFO = tbFO.createFolder (s); 117 118 if(lastName != null) { 120 tbFO.setAttribute( 121 lastName + "/" +newFO.getNameExt(), 122 Boolean.TRUE 123 ); 124 } 125 } else { 126 NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 127 MessageFormat.format( bundle.getString("MSG_ToolbarExists"), new Object [] { s } ) ); org.openide.DialogDisplayer.getDefault().notify( msg ); 129 } 130 } catch (IOException e) { 131 Exceptions.printStackTrace(e); 132 } 133 } 134 } 135 } 136 137 139 private String getLastName() { 140 String lastName = null; 141 Node[] ch = getChildren().getNodes(); 142 if(ch.length > 0) { 143 Node last = ch[ch.length - 1]; 144 DataObject d = (DataObject)last.getCookie(DataObject.class); 145 if(d != null) { 146 lastName = d.getPrimaryFile().getNameExt(); 147 } 148 } 149 150 return lastName; 151 } 152 153 156 public Action[] getActions( boolean context ) { 157 if (topStaticActions == null) 158 topStaticActions = new SystemAction [] { 159 SystemAction.get (FileSystemAction.class), 160 null, 161 SystemAction.get(PasteAction.class), 162 null, 163 SystemAction.get(NewAction.class), 164 null, 165 SystemAction.get(ToolsAction.class), 166 SystemAction.get(PropertiesAction.class), 167 }; 168 return topStaticActions; 169 } 170 171 172 public Node.PropertySet[] getPropertySets () { 173 return new Node.PropertySet[] { }; 174 } 175 176 194 195 public boolean canDestroy () { 196 return false; 197 } 198 199 public boolean canCut () { 200 return false; 201 } 202 203 public boolean canRename () { 204 return false; 205 } 206 207 public void propertyChange(PropertyChangeEvent evt) { 208 if ("configuration".equals(evt.getPropertyName())) { firePropertyChange("configuration", evt.getOldValue(), evt.getNewValue()); } 211 } 212 213 216 static final class ToolbarFolderChildren extends FilterNode.Children { 217 218 219 public ToolbarFolderChildren (DataFolder folder) { 220 super(folder.getNodeDelegate ()); 221 } 222 223 228 protected Node copyNode (Node node) { 229 DataFolder df = (DataFolder)node.getCookie(DataFolder.class); 230 if (df != null) { 231 return new ToolbarNode(df); 232 } 233 if (node.getCookie (org.openide.loaders.InstanceDataObject.class) != null) { 235 return new ToolbarItemNode(node); 236 } else { 237 return node.cloneNode(); 238 } 239 } 240 241 @Override 242 protected Node[] createNodes(Node key) { 243 Node[] retValue; 244 245 retValue = super.createNodes(key); 246 247 if( retValue.length == 1 ) { 248 InstanceCookie ic = (InstanceCookie)retValue[0].getCookie(InstanceCookie.class); 250 251 boolean isConfig = false; 252 253 if(ic != null) { 254 if(ic instanceof InstanceCookie.Of) { 255 isConfig = ((InstanceCookie.Of)ic).instanceOf(ToolbarConfiguration.class); 256 } else { 257 try { 258 if(ic.instanceClass().equals(ToolbarConfiguration.class)) { 259 isConfig = true; 260 } 261 } catch(ClassNotFoundException cnfe) { 262 } catch(IOException ioe) { 263 } 264 } 265 } 266 267 if(isConfig) { 268 retValue = new Node[] {}; 269 } 270 } 271 return retValue; 272 } 273 } 274 275 305 306 static final class ToolbarItemNode extends FilterNode { 307 308 309 static SystemAction[] staticActions; 310 311 static SystemAction[] separatorStaticActions; 312 313 314 ToolbarItemNode (Node filter) { 315 super(filter, Children.LEAF); 316 } 317 318 319 public boolean equals (Object o) { 320 if (o == null) return false; 321 return this == o || getOriginal ().equals (o) || o.equals (getOriginal ()); 322 } 323 324 327 public SystemAction[] getActions () { 328 InstanceCookie.Of ic = (InstanceCookie.Of)getCookie(InstanceCookie.Of.class); 329 if (ic != null && ic.instanceOf(JSeparator.class)) { 330 if( null == separatorStaticActions ) { 332 separatorStaticActions = new SystemAction [] { 333 SystemAction.get(MoveUpAction.class), 334 SystemAction.get(MoveDownAction.class), 335 null, 336 SystemAction.get(DeleteAction.class), 337 null, 338 SystemAction.get(ToolsAction.class), 339 SystemAction.get(PropertiesAction.class), 340 }; 341 } 342 return separatorStaticActions; 343 } 344 345 if (staticActions == null) { 346 staticActions = new SystemAction [] { 347 SystemAction.get(MoveUpAction.class), 348 SystemAction.get(MoveDownAction.class), 349 null, 350 SystemAction.get(CutAction.class), 351 SystemAction.get(CopyAction.class), 352 null, 353 SystemAction.get(DeleteAction.class), 354 null, 355 SystemAction.get(ToolsAction.class), 356 SystemAction.get(PropertiesAction.class), 357 }; 358 } 359 return staticActions; 360 } 361 362 364 public boolean canRename () { 365 return false; 366 } 367 368 369 public Node.PropertySet[] getPropertySets () { 370 383 return new Node.PropertySet[] { }; 384 } 385 } 387 389 private static class ToolbarNode extends DataFolder.FolderNode implements PropertyChangeListener { 390 391 392 static SystemAction[] staticActions; 393 394 private DataFolder folder; 395 396 398 ToolbarNode(DataFolder folder) { 399 folder.super(new ToolbarFolderChildren(folder)); 400 this.folder = folder; 401 super.setName( folder.getName() ); 403 super.setShortDescription(NbBundle.getBundle (ToolbarFolderNode.class).getString("CTL_Toolbars_hint")); 404 setIconBaseWithExtension ("org/netbeans/core/resources/toolbars.gif"); 406 SwingUtilities.invokeLater( new Runnable () { 408 public void run() { 409 ToolbarPool pool = ToolbarPool.getDefault(); 410 pool.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(ToolbarNode.this, pool)); 411 } 412 }); 413 attachConfigListener(); 414 } 415 416 protected void createPasteTypes(Transferable t, List<PasteType> s) { 417 PasteType pType = ActionPasteType.getPasteType((DataFolder)getDataObject() , t); 418 if (pType != null) { 419 s.add(pType); 421 } 422 } 423 424 427 public NewType[] getNewTypes () { 428 return new NewType[] { 429 new NewType () { 430 public String getName () { 431 return NbBundle.getBundle (ToolbarFolderNode.class).getString ("PROP_newToolbarSeparator"); 432 } 433 public void create () throws IOException { 434 newToolbarSeparator(); 435 } 436 } 437 }; 438 } 439 440 public void setName (String name, boolean rename) { 441 Toolbar tb = ToolbarPool.getDefault().findToolbar(getName()); 443 if (tb != null) { 444 tb.setName(name); 445 } 446 super.setName(name, rename); 447 } 448 449 void newToolbarSeparator () { 451 try { 452 InstanceDataObject instData = InstanceDataObject.find 453 (folder,null,"javax.swing.JToolBar$Separator"); 455 String lastName = getLastName(); 456 DataObject d; 457 458 if (instData == null) { 459 d = InstanceDataObject.create 460 (folder,null,"javax.swing.JToolBar$Separator"); 462 } else { 463 d = instData.copy(folder); 464 } 465 466 if(lastName != null) { 468 folder.getPrimaryFile().setAttribute( 469 lastName + "/" + d.getPrimaryFile().getNameExt(), Boolean.TRUE 471 ); 472 } 473 } catch (java.io.IOException e) { 474 Exceptions.printStackTrace(e); 475 } 476 } 477 479 481 private String getLastName() { 482 String lastName = null; 483 Node[] ch = getChildren().getNodes(); 484 if(ch.length > 0) { 485 Node last = ch[ch.length - 1]; 486 DataObject d = (DataObject)last.getCookie(DataObject.class); 487 if(d != null) { 488 lastName = d.getPrimaryFile().getNameExt(); 489 } 490 } 491 492 return lastName; 493 } 494 495 498 public Action[] getActions (boolean context) { 499 if (staticActions == null) 500 staticActions = new SystemAction [] { 501 SystemAction.get (FileSystemAction.class), 502 null, 503 SystemAction.get(ReorderAction.class), 504 null, 505 SystemAction.get(CutAction.class), 506 SystemAction.get(CopyAction.class), 507 SystemAction.get(PasteAction.class), 508 null, 509 SystemAction.get(DeleteAction.class), 510 SystemAction.get(RenameAction.class), 511 null, 512 SystemAction.get(NewAction.class), 513 null, 514 SystemAction.get(ToolsAction.class), 515 SystemAction.get(PropertiesAction.class), 516 }; 517 return staticActions; 518 } 519 520 521 public Node.PropertySet[] getPropertySets () { 522 Sheet sheet = Sheet.createDefault(); 524 Sheet.Set ss = sheet.get(Sheet.PROPERTIES); 525 ss.put( 526 new PropertySupport.Name( 527 this, 528 NbBundle.getBundle (ToolbarFolderNode.class).getString("PROP_ToolbarName"), 529 NbBundle.getBundle (ToolbarFolderNode.class).getString("HINT_ToolbarName") 530 ) 531 ); 532 ss.put( 533 new PropertySupport.ReadWrite<Boolean >( 534 "visible", Boolean .class, 536 NbBundle.getBundle (ToolbarFolderNode.class).getString("PROP_ToolbarVisible"), 537 NbBundle.getBundle (ToolbarFolderNode.class).getString("HINT_ToolbarVisible") 538 ) { 539 public void setValue(Boolean v) { 540 currentConfiguration().setToolbarVisible(ToolbarPool.getDefault().findToolbar(folder.getName()), v.booleanValue()); 541 } 542 543 public Boolean getValue() { 544 return currentConfiguration().isToolbarVisible(ToolbarPool.getDefault().findToolbar(folder.getName())) 545 ? Boolean.TRUE : Boolean.FALSE; 546 } 547 } 548 ); 549 return sheet.toArray(); 550 } 551 552 private PropertyChangeListener wlpc; 554 555 public void propertyChange(PropertyChangeEvent evt) { 556 if ("configuration".equals(evt.getPropertyName())) { ToolbarConfiguration tc = configuration((String )evt.getOldValue()); 558 if (tc != null && wlpc != null) { 559 tc.removePropertyChangeListener(wlpc); 560 } 561 attachConfigListener(); 562 } else if ("constraints".equals(evt.getPropertyName())) { firePropertyChange("visible", evt.getOldValue(), evt.getNewValue()); } 565 } 566 567 private void attachConfigListener() { 568 ToolbarConfiguration tc = currentConfiguration(); 569 if (tc != null) { 570 tc.addPropertyChangeListener(wlpc = org.openide.util.WeakListeners.propertyChange(this, tc)); 571 } 572 } 573 574 575 ToolbarConfiguration currentConfiguration() { 576 String conf = ToolbarPool.getDefault().getConfiguration(); 577 return configuration(conf); 578 } 579 580 581 ToolbarConfiguration configuration(String conf) { 582 DataObject[] obj = NbPlaces.getDefault().toolbars ().getChildren(); 583 for (int i = 0; i < obj.length; i++) { 584 DataObject o = obj[i]; 585 org.openide.cookies.InstanceCookie ic = (org.openide.cookies.InstanceCookie)o.getCookie(org.openide.cookies.InstanceCookie.class); 586 if (ic != null) { 587 try { 588 if (ToolbarConfiguration.class.isAssignableFrom(ic.instanceClass())) { 589 ToolbarConfiguration tc = (ToolbarConfiguration)ic.instanceCreate(); 590 if (conf.equals(tc.getName())) 591 return tc; 592 } 593 } catch (java.io.IOException ex) { 594 Exceptions.printStackTrace(ex); 595 } catch (ClassNotFoundException ex) { 596 Exceptions.printStackTrace(ex); 597 } 598 } 599 } 600 return null; 601 } 602 } 603 } 604 | Popular Tags |