KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > ui > LookupNode


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.ui;
21
22 import javax.swing.Action JavaDoc;
23 import org.openide.actions.FileSystemAction;
24 import org.openide.actions.MoveDownAction;
25 import org.openide.actions.MoveUpAction;
26 import org.openide.actions.NewTemplateAction;
27 import org.openide.actions.PasteAction;
28 import org.openide.actions.PropertiesAction;
29 import org.openide.actions.ReorderAction;
30 import org.openide.actions.ToolsAction;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileSystem;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.Repository;
35 import org.openide.loaders.DataFolder;
36 import org.openide.loaders.DataObject;
37 import org.openide.loaders.DataShadow;
38 import org.openide.loaders.TemplateWizard;
39 import org.openide.loaders.XMLDataObject;
40 import org.openide.nodes.FilterNode;
41 import org.openide.nodes.Node;
42 import org.openide.nodes.Sheet;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.actions.SystemAction;
45
46 /** Node that displays the content of Services directory and let's user
47 * customize it.
48 *
49 * @author Jaroslav Tulach
50 */

51 public class LookupNode extends DataFolder.FolderNode implements NewTemplateAction.Cookie {
52     /** extended attribute that signals that this object should not be visible to the user */
53     private static final String JavaDoc EA_HIDDEN = "hidden"; // NOI18N
54
private static final String JavaDoc EA_HELPCTX = "helpID"; // NOI18N
55
/** This is quite unsafe, but it's the only way how to test that we got uncustomized
56      * InstanceDataNode's help (which is really of no use to the user).
57      */

58     private static final HelpCtx INSTANCE_DEFAULT_HELP = new HelpCtx("org.openide.loaders.InstanceDataObject"); // NOI18N
59
/** This is quite unsafe, but it's the only way how to test that we got uncustomized
60      * DataFolder's help (which is really of no use to the user).
61      */

62     private static final HelpCtx FOLDER_DEFAULT_HELP = new HelpCtx("org.openide.loaders.DataFolder"); // NOI18N
63
private static final String JavaDoc PREFIX_SETTING_CATEGORIES = "UI"; // NOI18N
64

65     /** Constructs this node with given node to filter.
66     */

67     public LookupNode (DataFolder folder) {
68         folder.super(new Ch(folder));
69 // setShortDescription(bundle.getString("CTL_Lookup_hint"));
70
// super.setIconBase ("/org/netbeans/modules/url/Lookup"); // NOI18N
71
getCookieSet ().add (this);
72     }
73     
74     /** is this node representing a setting ui category? */
75     private boolean isUISettingCategoryNode() {
76         DataFolder df = (DataFolder) super.getCookie (DataFolder.class);
77         if (df != null) {
78             String JavaDoc name = df.getPrimaryFile ().getPath();
79             return name.startsWith(PREFIX_SETTING_CATEGORIES);
80         } else return false;
81     }
82     
83     public HelpCtx getHelpCtx () {
84         Object JavaDoc o = getDataObject().getPrimaryFile().getAttribute(EA_HELPCTX);
85         if (o != null) {
86             return new HelpCtx(o.toString());
87         }
88         // now try the original DataObject (assume it is a folder-thing)
89
HelpCtx ctx = getDataObject().getHelpCtx();
90         if (ctx != null &&
91             ctx != HelpCtx.DEFAULT_HELP &&
92             !FOLDER_DEFAULT_HELP.equals(ctx)) {
93             return ctx;
94         }
95         // try the parent node:
96
Node n = getParentNode();
97         if (n != null)
98             ctx = n.getHelpCtx();
99         return ctx;
100     }
101
102
103     public final Action JavaDoc[] getActions(boolean context) {
104         if (isUISettingCategoryNode()) {
105             return new Action JavaDoc[0];
106         } else {
107             return new Action JavaDoc[] {
108                 SystemAction.get(FileSystemAction.class),
109                 null,
110                 SystemAction.get(PasteAction.class),
111                 null,
112                 SystemAction.get(MoveUpAction.class),
113                 SystemAction.get(MoveDownAction.class),
114                 SystemAction.get(ReorderAction.class),
115                 null,
116                 SystemAction.get(NewTemplateAction.class),
117                 null,
118                 SystemAction.get(ToolsAction.class),
119                 SystemAction.get(PropertiesAction.class),
120             };
121         }
122     }
123
124     /** @return empty property sets. *
125     public PropertySet[] getPropertySets () {
126         return NO_PROPERTIES;
127     }
128      */

129
130     public final Node.Cookie getCookie (Class JavaDoc type) {
131         if (isUISettingCategoryNode()) return null;
132         return super.getCookie (type);
133     }
134
135     /** NewTemplateAction.Cookie method implementation to create the desired
136      * template wizard for this node.
137      */

138     public final TemplateWizard getTemplateWizard () {
139         TemplateWizard templateWizard = createWizard ();
140         
141         templateWizard.setTemplatesFolder (findFolder (root (), findName (), true));
142         templateWizard.setTargetFolder (findFolder (root (), findName (), false));
143         return templateWizard;
144     }
145     
146     /** Allows subclasses to create special wizard.
147      */

148     protected TemplateWizard createWizard () {
149         return new TemplateWizard ();
150     }
151
152     /** A method to allow subclasses to create different child for folder.
153     * @param folder the folder to create child for
154     */

155     protected LookupNode createChild (DataFolder folder) {
156         return new LookupNode (folder);
157     }
158     
159     /** A method to allow subclasses to create different child for any other node then folder.
160     * @param node to create child for
161     */

162     protected Node createChild (Node node) {
163         return node.cloneNode ();
164     }
165
166     /** Gets the root from children on system filesystem and in
167     * templates folder.
168     */

169     protected String JavaDoc root () {
170         return "Services"; // NOI18N
171
}
172
173     /** Finds a prefix for templates.
174     * @return prefix
175     */

176     private static String JavaDoc prefTemplates (String JavaDoc root) {
177         return "Templates/" + root; // NOI18N
178
}
179
180     /** Finds a prefix for objects.
181     */

182     private static String JavaDoc prefObjects (String JavaDoc root) {
183         return root;
184     }
185     
186     /** Finds name of the node by extracting the begin of nodes.
187      * @return the string name
188      */

189     private String JavaDoc findName () {
190         DataFolder df = (DataFolder)getCookie (DataFolder.class);
191         if (df == null) {
192             return "";
193         }
194         String JavaDoc name = df.getPrimaryFile ().getPath();
195         if (name.startsWith (prefObjects (root ()))) {
196             name = name.substring (prefObjects (root ()).length ());
197         }
198         return name;
199     }
200
201     /** Locates the right folder for given service name.
202      * @param name of the resource
203      * @param template folder for templates or for instances?
204      * @return the folder
205      */

206     static DataFolder findFolder (String JavaDoc root, String JavaDoc name, boolean template) {
207         try {
208             FileSystem fs = Repository.getDefault ().getDefaultFileSystem ();
209             if (template) {
210                 name = '/' + prefTemplates (root) + name;
211             } else {
212                 name = '/' + prefObjects (root) + name;
213             }
214             FileObject fo = fs.findResource (name);
215             
216             if (fo == null && template) {
217                 // we do not create template directories, if it is missing
218
// we use the root services template directory
219
name = prefTemplates (root);
220             }
221             
222             if (fo == null) {
223                 // if the directory is missing, create new one
224
fo = FileUtil.createFolder (fs.getRoot (), name);
225             }
226             
227             return DataFolder.findFolder (fo);
228         } catch (java.io.IOException JavaDoc ex) {
229             throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc(ex.toString()).initCause(ex);
230         }
231     }
232
233     /** Refreshes the node for given key.
234     * @param node the original node
235     */

236     public final void refreshKey (Node node) {
237         ((Ch)getChildren()).refreshKey0(node);
238     }
239     
240     public boolean canDestroy() {
241         return false;
242     }
243     
244     public boolean canCut() {
245         return false;
246     }
247     
248     public boolean canCopy() {
249         return false;
250     }
251     
252     protected Sheet createSheet() {
253         return new Sheet();
254     }
255     
256     public boolean canRename() {
257         return false;
258     }
259
260     public Node cloneNode () {
261         return new LookupNode((DataFolder)super.getCookie(DataFolder.class));
262     }
263     
264     
265     /** Misleading name: need not be a leaf at all. */
266     private static final class Leaf extends FilterNode {
267         DataObject data;
268         Node parent;
269         
270         Leaf (Node node, DataObject data, Node parent) {
271             super(node, ((data instanceof XMLDataObject) || node.isLeaf()) ? Children.LEAF : new FilterNode.Children(node));
272             this.data = data;
273             this.parent = parent;
274         }
275         
276         // #17920: Index cookie works only when equality works
277
public boolean equals(Object JavaDoc o) {
278             return this == o || getOriginal().equals(o) || (o != null && o.equals(getOriginal()));
279         }
280         public int hashCode() {
281             return getOriginal().hashCode();
282         }
283         
284         public HelpCtx getHelpCtx() {
285             Object JavaDoc o = data.getPrimaryFile().getAttribute(EA_HELPCTX);
286             if (o != null) {
287                 return new HelpCtx(o.toString());
288             }
289             // now try the original DataObject (assume it is a folder-thing)
290
HelpCtx ctx = getOriginal().getHelpCtx();
291             if (ctx != null &&
292                 ctx != HelpCtx.DEFAULT_HELP &&
293                 !INSTANCE_DEFAULT_HELP.equals(ctx)) {
294                 return ctx;
295             }
296             // try the parent node:
297
Node n = getParentNode();
298             if (n == null)
299                 n = parent;
300             if (n != null)
301                 ctx = n.getHelpCtx();
302             return ctx;
303         }
304         
305         public Action JavaDoc getPreferredAction() {
306             return null;
307         }
308
309     }
310     
311
312     /** Children for the LookupNode. Creates LookupNodes or
313     * LookupItemNodes as filter subnodes...
314     */

315     private static final class Ch extends FilterNode.Children {
316         /** @param or original node to take children from */
317         public Ch (DataFolder folder) {
318             super(folder.getNodeDelegate ());
319         }
320
321         /** Overridden to provide package-private access. */
322         void refreshKey0(Node node) {
323             refreshKey(node);
324         }
325
326         /** Overridden, returns LookupNode filters of original nodes.
327         *
328         * @param node node to create copy of
329         * @return LookupNode filter of the original node
330         */

331         @Override JavaDoc
332         protected Node[] createNodes(Node node) {
333             DataObject obj = (DataObject)node.getCookie(DataObject.class);
334             //System.err.println("obj="+obj+" node="+node+" hidden="+(obj==null?null:obj.getPrimaryFile ().getAttribute (EA_HIDDEN)));
335

336             if (
337                 obj != null && Boolean.TRUE.equals (obj.getPrimaryFile ().getAttribute (EA_HIDDEN))
338             ) {
339                 return new Node[0];
340             }
341             
342             LookupNode parent = (LookupNode)getNode ();
343             
344             if (obj != null) {
345                 if (obj instanceof DataFolder && node.equals (obj.getNodeDelegate ())) {
346                     node = parent.createChild ((DataFolder)obj);
347                     return new Node[] { node };
348                 } else if (obj instanceof DataShadow) {
349                     DataObject orig = ((DataShadow) obj).getOriginal();
350                     FileObject fo = orig.getPrimaryFile();
351                     
352                     // if folder referenced by shadow is empty do not show it
353
if (fo.isFolder() && !fo.getChildren(false).hasMoreElements()) return null;
354                     
355                     if (orig instanceof DataFolder) {
356                         return new Node[] {
357                             parent.createChild ((DataFolder) orig)
358                         };
359                     } else {
360                         obj = orig;
361                         node = orig.getNodeDelegate();
362                     }
363                 }
364                 node = new Leaf(node, obj, parent);
365             }
366             
367             node = parent.createChild (node);
368
369             return new Node[] { node };
370         }
371
372     }
373
374 }
375
Popular Tags