KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
25 import javax.swing.JSeparator JavaDoc;
26 import org.netbeans.core.NbPlaces;
27 import org.openide.NotifyDescriptor;
28 import org.openide.actions.*;
29 import org.openide.cookies.InstanceCookie;
30 import org.openide.filesystems.FileObject;
31 import org.openide.loaders.*;
32 import org.openide.nodes.*;
33 import org.openide.nodes.FilterNode.Children;
34 import org.openide.util.*;
35 import org.openide.util.actions.SystemAction;
36 import org.openide.util.datatransfer.*;
37
38 /** The node for the menu folder representation.
39 * Delegates most of its functionality to the original data folder node.
40 * Final only for better performance, can be unfinaled.
41 *
42 * @author Dafe Simonek
43 */

44 public final class MenuFolderNode extends DataFolder.FolderNode {
45
46     /** Actions which this node supports */
47     static SystemAction[] staticActions;
48     /** Actions of this node when it is top level menu node */
49     static SystemAction[] topStaticActions;
50
51     private static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0];
52
53     private DataFolder folder;
54
55     public MenuFolderNode () {
56         this (NbPlaces.getDefault().menus ());
57     }
58
59     /** Constructs this node with given node to filter.
60     */

61     MenuFolderNode (DataFolder folder) {
62         folder.super(new MenuFolderChildren(folder));
63         this.folder = folder;
64         //JST: it displays only Menu as name! super.setDisplayName(NbBundle.getBundle (MenuFolderNode.class).getString("CTL_Menu_name"));
65
setShortDescription(NbBundle.getMessage(MenuFolderNode.class, "CTL_Menu_hint"));
66
67         setIconBaseWithExtension ("org/netbeans/core/resources/menu.gif"); // NOI18N
68
}
69
70     public HelpCtx getHelpCtx () {
71         return new HelpCtx (MenuFolderNode.class);
72     }
73
74     protected void createPasteTypes(Transferable JavaDoc t, List<PasteType> s) {
75         PasteType pType = ActionPasteType.getPasteType((DataFolder)getDataObject() , t);
76         if (pType != null) {
77             //now we know that the tranferable holds a paste-able Action
78
s.add(pType);
79         }
80     }
81     
82     /** Support for new types that can be created in this node.
83     * @return array of new type operations that are allowed
84     */

85     public NewType[] getNewTypes () {
86         NewType newMenu = new NewType() {
87             public String JavaDoc getName () {
88                 return NbBundle.getMessage(MenuFolderNode.class, "CTL_newMenuName");
89             }
90             public void create () throws java.io.IOException JavaDoc {
91                 newMenu();
92             }
93         };
94         
95         if(getParentNode() instanceof MenuFolderNode) {
96             return new NewType[] {
97                 newMenu,
98                 //Fixed bug #5610 Added support for adding new separator through popup menu
99
new NewType () {
100                     public String JavaDoc getName () {
101                         return NbBundle.getMessage(MenuFolderNode.class, "CTL_newMenuSeparator");
102                     }
103                     public void create () throws java.io.IOException JavaDoc {
104                         newSeparator();
105                     }
106                 }
107             };
108         } else {
109             // #14415. Just menu for main menu bar.
110
return new NewType[] {newMenu};
111         }
112     }
113
114     void newMenu () {
115         NotifyDescriptor.InputLine il = new NotifyDescriptor.InputLine
116                                         (NbBundle.getMessage(MenuFolderNode.class, "CTL_newMenuLabel"),
117                                          NbBundle.getMessage(MenuFolderNode.class, "CTL_newMenuDialog"));
118         il.setInputText (NbBundle.getMessage(MenuFolderNode.class, "CTL_newMenu"));
119
120         Object JavaDoc ok = org.openide.DialogDisplayer.getDefault ().notify (il);
121         if (ok == NotifyDescriptor.OK_OPTION) {
122             String JavaDoc s = il.getInputText();
123             if (!s.equals ("")) { // NOI18N
124
FileObject mnFO = folder.getPrimaryFile();
125                 try {
126                     FileObject newFO = mnFO.getFileObject(s);
127                     if (newFO == null) {
128                         String JavaDoc lastName = getLastName();
129                         
130                         newFO = mnFO.createFolder (s);
131
132                         // #13015. Set new item as last one.
133
if(lastName != null) {
134                             mnFO.setAttribute(
135                                 lastName + "/" + newFO.getNameExt(), // NOI18N
136
Boolean.TRUE
137                             );
138                         }
139                     }
140                 } catch (java.io.IOException JavaDoc e) {
141                     Exceptions.printStackTrace(e);
142                 }
143             }
144         }
145     }
146
147     //Fixed bug #5610 Added support for adding new separator through popup menu
148
void newSeparator () {
149         try {
150             InstanceDataObject instData = InstanceDataObject.find
151             (folder,null,"javax.swing.JSeparator"); // NOI18N
152

153             String JavaDoc lastName = getLastName();
154             DataObject d;
155             
156             if (instData == null) {
157                 d = InstanceDataObject.create
158                 (folder,null,"javax.swing.JSeparator"); // NOI18N
159
} else {
160                 d = instData.copy(folder);
161             }
162
163             // #13015. Set new item as last one.
164
if(lastName != null) {
165                 folder.getPrimaryFile().setAttribute(
166                     lastName + "/" + d.getPrimaryFile().getNameExt(), // NOI18N
167
Boolean.TRUE
168                 );
169             }
170         } catch (java.io.IOException JavaDoc e) {
171             Exceptions.printStackTrace(e);
172         }
173     }
174     //End
175

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

178     private String JavaDoc getLastName() {
179         String JavaDoc lastName = null;
180         Node[] ch = getChildren().getNodes();
181         if(ch.length > 0) {
182             Node last = ch[ch.length - 1];
183             DataObject d = (DataObject)last.getCookie(DataObject.class);
184             if(d != null) {
185                 lastName = d.getPrimaryFile().getNameExt();
186             }
187         }
188         
189         return lastName;
190     }
191     
192     /** Actions.
193     * @return array of actions for this node
194     */

195     protected SystemAction[] createActions () {
196         if (isTopLevel()) {
197             if (topStaticActions == null)
198                 topStaticActions = new SystemAction [] {
199                                        SystemAction.get (FileSystemAction.class),
200                                        null,
201                                        SystemAction.get(ReorderAction.class),
202                                        null,
203                                        SystemAction.get(PasteAction.class),
204                                        null,
205                                        SystemAction.get(NewAction.class),
206                                        null,
207                                        SystemAction.get(ToolsAction.class),
208                                        SystemAction.get(PropertiesAction.class),
209                                    };
210             return topStaticActions;
211         } else {
212             if (staticActions == null)
213                 staticActions = new SystemAction [] {
214                                     SystemAction.get (FileSystemAction.class),
215                                     null,
216                                     SystemAction.get(MoveUpAction.class),
217                                     SystemAction.get(MoveDownAction.class),
218                                     SystemAction.get(ReorderAction.class),
219                                     null,
220                                     SystemAction.get(CutAction.class),
221                                     SystemAction.get(CopyAction.class),
222                                     SystemAction.get(PasteAction.class),
223                                     null,
224                                     SystemAction.get(DeleteAction.class),
225                                     SystemAction.get(RenameAction.class),
226                                     null,
227                                     SystemAction.get(NewAction.class),
228                                     null,
229                                     SystemAction.get(ToolsAction.class),
230                                     SystemAction.get(PropertiesAction.class),
231                                 };
232             return staticActions;
233         }
234     }
235
236     /** Creates properties for this node */
237     public Node.PropertySet[] getPropertySets () {
238         if (isTopLevel()) {
239             return NO_PROPERTIES;
240         } else {
241             // default sheet with "properties" property set // NOI18N
242
Sheet sheet = Sheet.createDefault();
243             sheet.get(Sheet.PROPERTIES).put(
244                 new PropertySupport.Name(
245                     this,
246                     NbBundle.getMessage(MenuFolderNode.class, "PROP_MenuName"),
247                     NbBundle.getMessage(MenuFolderNode.class, "HINT_MenuName")
248                 )
249             );
250             return sheet.toArray();
251         }
252     }
253
254     /** Supports index cookie in addition to standard support.
255     *
256     * @param type the class to look for
257     * @return instance of that class or null if this class of cookie
258     * is not supported
259     *
260     public Node.Cookie getCookie (Class type) {
261         if (Index.class.isAssignableFrom(type)) {
262             // search for data object
263             DataFolder dataObj = (DataFolder)super.getCookie(DataFolder.class);
264             if (dataObj != null) {
265                 return new DataFolder.Index (dataObj, this);
266             }
267         }
268         return super.getCookie(type);
269     }
270     */

271
272     /** Utility - is this top level menu node? */
273     boolean isTopLevel () {
274         final Node n = getParentNode();
275         return (n == null) || !(n instanceof MenuFolderNode);
276     }
277
278     public boolean canDestroy () {
279         if (isTopLevel ()) return false;
280         return super.canDestroy ();
281     }
282
283     public boolean canCut () {
284         if (isTopLevel ()) return false;
285         return super.canCut ();
286     }
287
288     public boolean canRename () {
289         if (isTopLevel ()) return false;
290         return super.canRename ();
291     }
292
293     /** Children for the MenuFolderNode. Creates MenuFolderNodes or
294     * MenuItemNodes as filter subnodes...
295     */

296     static final class MenuFolderChildren extends FilterNode.Children {
297
298         /** @param or original node to take children from */
299         public MenuFolderChildren (DataFolder folder) {
300             super(folder.getNodeDelegate ());
301         }
302
303         /** Overriden, returns MenuFolderNode filters of original nodes.
304         *
305         * @param node node to create copy of
306         * @return MenuFolderNode filter of the original node
307         */

308         protected Node copyNode (Node node) {
309             DataFolder df = (DataFolder)node.getCookie(DataFolder.class);
310             if (df != null) {
311                 return new MenuFolderNode(df);
312             }
313             return new MenuItemNode(node);
314         }
315
316     }
317
318     static final class MenuItemNode extends FilterNode {
319
320         /** Actions which this node supports */
321         static SystemAction[] staticActions;
322         /** Actions which this node supports (when representing a menu separator) */
323         static SystemAction[] separatorStaticActions;
324
325         /** Constructs new filter node for menu item */
326         MenuItemNode (Node filter) {
327             super(filter, Children.LEAF);
328         }
329
330         /** Make Move Up and Move Down actions be enabled. */
331         public boolean equals (Object JavaDoc o) {
332             if (o == null) return false;
333             return this == o || getOriginal ().equals (o) || o.equals (getOriginal ());
334         }
335
336         /** Actions.
337         * @return array of actions for this node
338         */

339         public SystemAction[] getActions () {
340             InstanceCookie.Of ic = (InstanceCookie.Of)getCookie(InstanceCookie.Of.class);
341             if (ic != null && ic.instanceOf(JSeparator JavaDoc.class)) {
342                 //do not allow copy&paste for menu separators
343
if( null == separatorStaticActions ) {
344                     separatorStaticActions = new SystemAction [] {
345                                     SystemAction.get(MoveUpAction.class),
346                                     SystemAction.get(MoveDownAction.class),
347                                     null,
348                                     SystemAction.get(DeleteAction.class),
349                                     null,
350                                     SystemAction.get(ToolsAction.class),
351                                     SystemAction.get(PropertiesAction.class),
352                                 };
353                 }
354                 return separatorStaticActions;
355             }
356             
357             if (staticActions == null) {
358                 staticActions = new SystemAction [] {
359                                     SystemAction.get(MoveUpAction.class),
360                                     SystemAction.get(MoveDownAction.class),
361                                     null,
362                                     SystemAction.get(CutAction.class),
363                                     SystemAction.get(CopyAction.class),
364                                     null,
365                                     SystemAction.get(DeleteAction.class),
366                                     null,
367                                     SystemAction.get(ToolsAction.class),
368                                     SystemAction.get(PropertiesAction.class),
369                                 };
370             }
371             return staticActions;
372         }
373
374         /** Disallows renaming.
375         */

376         public boolean canRename () {
377             return false;
378         }
379
380         /** Creates properties for this node */
381         public Node.PropertySet[] getPropertySets () {
382             /*
383             // default sheet with "properties" property set // NOI18N
384             Sheet sheet = Sheet.createDefault();
385             sheet.get(Sheet.PROPERTIES).put(
386                 new PropertySupport.Name(
387                     this,
388                     bundle.getString("PROP_MenuItemName"),
389                     bundle.getString("HINT_MenuItemName")
390                 )
391             );
392             return sheet.toArray();
393              */

394             return new Node.PropertySet[] { };
395         }
396
397     } // end of MenuItemNode
398

399 }
400
Popular Tags