KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > services > ToolbarFolderNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.services;
21
22
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.beans.*;
25 import java.io.IOException JavaDoc;
26 import java.text.MessageFormat JavaDoc;
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 /** The node for the toolbar folder representation.
45 * Delegates most of its functionality to the original data folder node.
46 * Final only for better performance, can be unfinaled.
47 *
48 * @author Dafe Simonek
49 */

50 public final class ToolbarFolderNode extends DataFolder.FolderNode implements PropertyChangeListener {
51
52     /** Actions of this node when it is top level toolbar node */
53     static SystemAction[] topStaticActions;
54
55     private DataFolder folder;
56
57     public ToolbarFolderNode () {
58         this (NbPlaces.getDefault().toolbars ());
59     }
60
61     /** Constructs this node with given node to filter.
62     */

63     ToolbarFolderNode (DataFolder folder) {
64         folder.super(new ToolbarFolderChildren(folder));
65         this.folder = folder;
66         //JST: it displays only Toolbar as name! super.setDisplayName(NbBundle.getBundle (ToolbarFolderNode.class).getString("CTL_Toolbars_name"));
67
super.setShortDescription(NbBundle.getBundle (ToolbarFolderNode.class).getString("CTL_Toolbars_hint"));
68         setIconBaseWithExtension ("org/netbeans/core/resources/toolbars.gif"); // NOI18N
69

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 JavaDoc t, List<PasteType> s) {
78         PasteType pType = ActionPasteType.getPasteType((DataFolder)getDataObject() , t);
79         if (pType != null) {
80             s.add(pType);
81         }
82     }
83     
84     /** Support for new types that can be created in this node.
85     * @return array of new type operations that are allowed
86     */

87     public NewType[] getNewTypes () {
88         return new NewType[] {
89                    new NewType () {
90                        public String JavaDoc getName () {
91                            return NbBundle.getBundle (ToolbarFolderNode.class).getString ("PROP_newToolbarName");
92                        }
93                        public void create () throws IOException JavaDoc {
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 JavaDoc ok = org.openide.DialogDisplayer.getDefault ().notify (il);
108         if (ok == NotifyDescriptor.OK_OPTION) {
109             String JavaDoc s = il.getInputText();
110             if (!s.equals ("")) { // NOI18N
111
FileObject tbFO = folder.getPrimaryFile();
112                 try {
113                     FileObject newFO = tbFO.getFileObject(s);
114                     if (newFO == null) {
115                         String JavaDoc lastName = getLastName();
116                         newFO = tbFO.createFolder (s);
117                         
118                         // #13015. Set new item as last one.
119
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 JavaDoc[] { s } ) ); // NOI18N
128
org.openide.DialogDisplayer.getDefault().notify( msg );
129                     }
130                 } catch (IOException JavaDoc e) {
131                     Exceptions.printStackTrace(e);
132                 }
133             }
134         }
135     }
136
137     /** Gets name of last child.
138      * @return name of last menu or <code>null</code> if there is no one */

139     private String JavaDoc getLastName() {
140         String JavaDoc 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     /** Actions.
154     * @return array of actions for this node
155     */

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     /** Creates properties for this node */
172     public Node.PropertySet[] getPropertySets () {
173         return new Node.PropertySet[] { };
174     }
175
176     /** Supports index cookie in addition to standard support.
177     *
178     * @param type the class to look for
179     * @return instance of that class or null if this class of cookie
180     * is not supported
181     *
182     public Node.Cookie getCookie (Class type) {
183         // no index for reordering toolbars, just for toolbar items
184         if ((!isTopLevel ()) && Index.class.isAssignableFrom(type)) {
185             // search for data object
186             DataFolder dataObj = (DataFolder)super.getCookie(DataFolder.class);
187             if (dataObj != null) {
188                 return new ToolbarIndex(dataObj, (ToolbarFolderChildren)getChildren());
189             }
190         }
191         return super.getCookie(type);
192     }
193     */

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())) { // NOI18N
209
firePropertyChange("configuration", evt.getOldValue(), evt.getNewValue()); // NOI18N
210
}
211     }
212
213     /** Children for the ToolbarFolderNode. Creates ToolbarFolderNodes or
214     * ToolbarItemNodes as filter subnodes...
215     */

216     static final class ToolbarFolderChildren extends FilterNode.Children {
217
218         /** @param or original node to take children from */
219         public ToolbarFolderChildren (DataFolder folder) {
220             super(folder.getNodeDelegate ());
221         }
222
223         /** Overriden, returns ToolbarFolderNode filters of original nodes.
224         *
225         * @param node node to create copy of
226         * @return ToolbarFolderNode filter of the original node
227         */

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             // nodes in the same folder as toolbar folders are not toolbar items!
234
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 JavaDoc
242         protected Node[] createNodes(Node key) {
243             Node[] retValue;
244             
245             retValue = super.createNodes(key);
246
247             if( retValue.length == 1 ) {
248                 //hide ToolbarConfiguration nodes
249
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 JavaDoc cnfe) {
262                         } catch(IOException JavaDoc ioe) {
263                         }
264                     }
265                 }
266                 
267                 if(isConfig) {
268                     retValue = new Node[] {};
269                 }
270             }
271             return retValue;
272         }
273     }
274
275     /** This class serves as index cookie implementation for the
276     * ToolbarFolderNode object. Allows reordering of Toolbar items.
277     *
278     static final class ToolbarIndex extends DataFolder.Index {
279
280         /** The children we are working with *
281         ToolbarFolderChildren children;
282
283         ToolbarIndex (final DataFolder df, final ToolbarFolderChildren children) {
284             super(df);
285             this.children = children;
286         }
287
288         /** Overrides DataFolder.Index.getNodesCount().
289         * Returns count of the nodes from the asociated chidren.
290         *
291         public int getNodesCount () {
292             return children.getNodesCount();
293         }
294
295         /** Overrides DataFolder.Index.getNodes().
296         * Returns array of subnodes from asociated children.
297         * @return array of subnodes
298         *
299         public Node[] getNodes () {
300             return children.getNodes();
301         }
302
303     } // end of ToolbarIndex
304     */

305
306     static final class ToolbarItemNode extends FilterNode {
307
308         /** Actions which this node supports */
309         static SystemAction[] staticActions;
310         /** Actions which this node supports (when representing a toolbar separator) */
311         static SystemAction[] separatorStaticActions;
312
313         /** Constructs new filter node for Toolbar item */
314         ToolbarItemNode (Node filter) {
315             super(filter, Children.LEAF);
316         }
317
318         /** Make Move Up and Move Down actions be enabled. */
319         public boolean equals (Object JavaDoc o) {
320             if (o == null) return false;
321             return this == o || getOriginal ().equals (o) || o.equals (getOriginal ());
322         }
323
324         /** Actions.
325         * @return array of actions for this node
326         */

327         public SystemAction[] getActions () {
328             InstanceCookie.Of ic = (InstanceCookie.Of)getCookie(InstanceCookie.Of.class);
329             if (ic != null && ic.instanceOf(JSeparator.class)) {
330                 //do not allow copy&paste for toolbar separators
331
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         /** Disallows renaming.
363         */

364         public boolean canRename () {
365             return false;
366         }
367
368         /** Creates properties for this node */
369         public Node.PropertySet[] getPropertySets () {
370             /*
371             ResourceBundle bundle = NbBundle.getBundle(ToolbarFolderNode.class);
372             // default sheet with "properties" property set // NOI18N
373             Sheet sheet = Sheet.createDefault();
374             sheet.get(Sheet.PROPERTIES).put(
375                 new PropertySupport.Name(
376                     this,
377                     bundle.getString("PROP_ToolbarItemName"),
378                     bundle.getString("HINT_ToolbarItemName")
379                 )
380             );
381             return sheet.toArray();
382              */

383             return new Node.PropertySet[] { };
384         }
385     } // end of ToolbarItemNode
386

387     /** Toolbar folder node.
388      */

389     private static class ToolbarNode extends DataFolder.FolderNode implements PropertyChangeListener {
390
391         /** Actions which this node supports */
392         static SystemAction[] staticActions;
393
394         private DataFolder folder;
395
396         /** Toolbar folder node.
397         */

398         ToolbarNode(DataFolder folder) {
399             folder.super(new ToolbarFolderChildren(folder));
400             this.folder = folder;
401             //JST: it displays only Toolbar as name! super.setDisplayName(NbBundle.getBundle (ToolbarFolderNode.class).getString("CTL_Toolbars_name"));
402
super.setName( folder.getName() );
403             super.setShortDescription(NbBundle.getBundle (ToolbarFolderNode.class).getString("CTL_Toolbars_hint"));
404             setIconBaseWithExtension ("org/netbeans/core/resources/toolbars.gif"); // NOI18N
405

406             //prevent deadlock
407
SwingUtilities.invokeLater( new Runnable JavaDoc() {
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 JavaDoc t, List<PasteType> s) {
417             PasteType pType = ActionPasteType.getPasteType((DataFolder)getDataObject() , t);
418             if (pType != null) {
419                 //now we know that the tranferable holds a paste-able Action
420
s.add(pType);
421             }
422         }
423         
424         /** Support for new types that can be created in this node.
425         * @return array of new type operations that are allowed
426         */

427         public NewType[] getNewTypes () {
428             return new NewType[] {
429                        new NewType () {
430                            public String JavaDoc getName () {
431                                return NbBundle.getBundle (ToolbarFolderNode.class).getString ("PROP_newToolbarSeparator");
432                            }
433                            public void create () throws IOException JavaDoc {
434                                newToolbarSeparator();
435                            }
436                        }
437                    };
438         }
439         
440         public void setName (String JavaDoc name, boolean rename) {
441             //Bugfix #19735: Rename toolbar too, not only node
442
Toolbar tb = ToolbarPool.getDefault().findToolbar(getName());
443             if (tb != null) {
444                 tb.setName(name);
445             }
446             super.setName(name, rename);
447         }
448             
449         //Fixed bug #5610 Added support for adding new separator through popup menu
450
void newToolbarSeparator () {
451             try {
452                 InstanceDataObject instData = InstanceDataObject.find
453                 (folder,null,"javax.swing.JToolBar$Separator"); // NOI18N
454

455                 String JavaDoc lastName = getLastName();
456                 DataObject d;
457
458                 if (instData == null) {
459                     d = InstanceDataObject.create
460                         (folder,null,"javax.swing.JToolBar$Separator"); // NOI18N
461

462                 } else {
463                     d = instData.copy(folder);
464                 }
465
466                 // #13015. Set new item as last one.
467
if(lastName != null) {
468                     folder.getPrimaryFile().setAttribute(
469                         lastName + "/" + d.getPrimaryFile().getNameExt(), // NOI18N
470
Boolean.TRUE
471                     );
472                 }
473             } catch (java.io.IOException JavaDoc e) {
474                 Exceptions.printStackTrace(e);
475             }
476         }
477         //End
478

479         /** Gets name of last child.
480          * @return name of last menu or <code>null</code> if there is no one */

481         private String JavaDoc getLastName() {
482             String JavaDoc 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         /** Actions.
496         * @return array of actions for this node
497         */

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         /** Creates properties for this node */
521         public Node.PropertySet[] getPropertySets () {
522             // default sheet with "properties" property set // NOI18N
523
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 JavaDoc>(
534                     "visible", // NOI18N
535
Boolean JavaDoc.class,
536                     NbBundle.getBundle (ToolbarFolderNode.class).getString("PROP_ToolbarVisible"),
537                     NbBundle.getBundle (ToolbarFolderNode.class).getString("HINT_ToolbarVisible")
538                 ) {
539                     public void setValue(Boolean JavaDoc v) {
540                         currentConfiguration().setToolbarVisible(ToolbarPool.getDefault().findToolbar(folder.getName()), v.booleanValue());
541                     }
542
543                     public Boolean JavaDoc 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         // Configuration listener
553
private PropertyChangeListener wlpc;
554
555         public void propertyChange(PropertyChangeEvent evt) {
556             if ("configuration".equals(evt.getPropertyName())) { // NOI18N
557
ToolbarConfiguration tc = configuration((String JavaDoc)evt.getOldValue());
558                 if (tc != null && wlpc != null) {
559                     tc.removePropertyChangeListener(wlpc);
560                 }
561                 attachConfigListener();
562             } else if ("constraints".equals(evt.getPropertyName())) { // NOI18N
563
firePropertyChange("visible", evt.getOldValue(), evt.getNewValue()); // NOI18N
564
}
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         /** Returns current ToolbarConfiguration */
575         ToolbarConfiguration currentConfiguration() {
576             String JavaDoc conf = ToolbarPool.getDefault().getConfiguration();
577             return configuration(conf);
578         }
579
580         /** Returns ToolbarConfiguration found by its name or null if such config doesn't exist. */
581         ToolbarConfiguration configuration(String JavaDoc 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 JavaDoc ex) {
594                         Exceptions.printStackTrace(ex);
595                     } catch (ClassNotFoundException JavaDoc ex) {
596                         Exceptions.printStackTrace(ex);
597                     }
598                 }
599             }
600             return null;
601         }
602     }
603 }
604
Popular Tags