KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > nodes > JavaSourceNodeFactory


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.modules.java.ui.nodes;
21
22 import org.netbeans.modules.javacore.JMManager;
23 import org.openide.nodes.Node;
24 import org.openide.nodes.Children;
25 import org.openide.nodes.FilterNode;
26 import org.openide.nodes.AbstractNode;
27 import org.openide.util.actions.SystemAction;
28 import org.openide.util.NbBundle;
29 import org.openide.util.Lookup;
30 import org.openide.util.lookup.ProxyLookup;
31 import org.openide.actions.*;
32 import org.openide.loaders.DataObject;
33 import org.openide.cookies.OpenCookie;
34 import org.openide.cookies.SaveCookie;
35 import org.netbeans.jmi.javamodel.*;
36 import org.netbeans.modules.java.ui.nodes.elements.*;
37 import org.netbeans.modules.java.JavaDataObject;
38 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
39
40 /**
41  * Default implementation of {@link SourceNodeFactory}.
42  * <p>In case the {@link #tree} propety is true the factory supplies read-only nodes
43  * for quick view navigator. These nodes are not backward compatible.
44  * XXX clipboard and new actions are disabled yet
45  * @author Jan Pokorsky
46  */

47 public final class JavaSourceNodeFactory extends FilterSourceNodeFactory {
48     
49     private static final JavaSourceNodeFactory DEFAULT = new JavaSourceNodeFactory();
50     
51     /** Create nodes for tree
52      * see org.netbeans.modules.java.JavaElementNodeFactory
53      */

54     private boolean tree = false;
55     
56     public static SourceNodeFactory getDefault() {
57         return DEFAULT;
58     }
59     
60     public Node createFieldNode(Field element) {
61         FieldNode n = new FieldNode(element, !JavaMetamodel.getManager().isElementGuarded(element));
62         n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class));
63         if (tree) {
64             return n;
65         } else {
66             Node oldNode = getCompatibleFactory().createFieldNode(element);
67             return new CompatibleNode(n, oldNode);
68         }
69     }
70
71     public Node createConstructorNode(Constructor element) {
72         ConstructorNode n = new ConstructorNode(element, !JavaMetamodel.getManager().isElementGuarded(element));
73         n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class));
74         if (tree) {
75             return n;
76         } else {
77             Node oldNode = getCompatibleFactory().createConstructorNode(element);
78             return new CompatibleNode(n, oldNode);
79         }
80     }
81
82     public Node createMethodNode(Method element) {
83         MethodNode n = new MethodNode(element, !JavaMetamodel.getManager().isElementGuarded(element));
84         n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class));
85         if (tree) {
86             return n;
87         } else {
88             Node oldNode = getCompatibleFactory().createMethodNode(element);
89             return new CompatibleNode(n, oldNode);
90         }
91     }
92
93     public Node createInitializerNode(Initializer element) {
94         InitializerNode n = new InitializerNode(element, !JavaMetamodel.getManager().isElementGuarded(element));
95         n.setActions(INITIALIZER_ACTIONS, SystemAction.get(OpenAction.class));
96         if (tree) {
97             return n;
98         } else {
99             Node oldNode = getCompatibleFactory().createInitializerNode(element);
100             return new CompatibleNode(n, oldNode);
101         }
102     }
103
104     public Node createEnumNode(JavaEnum element) {
105         SourceNodeFactory snf;
106         EnumNode n;
107         if (tree) {
108             snf = SourceNodes.getBrowserFactory();
109             EnumChildren children = new EnumChildren(snf, element);
110             EnumFilter filter = new EnumFilter();
111             n = new EnumNode(element, children, !JavaMetamodel.getManager().isElementGuarded(element));
112
113             n.setElementFormat(new ElementFormat (
114                                    NbBundle.getBundle(JavaDataObject.class).getString("CTL_Class_name_format") // NOI18N
115
));
116
117             // filter out inner classes
118
filter.setOrder(new int[] {
119                                  EnumFilter.CONSTANTS,
120                                  ClassElementFilter.FIELD,
121                                  ClassElementFilter.CONSTRUCTOR,
122                                  ClassElementFilter.METHOD,
123                              });
124             children.setFilter(filter);
125         } else {
126             snf = SourceNodes.getExplorerFactory();
127             n = new EnumNode(element, Categories.createEnumChildren(element, snf, true), !JavaMetamodel.getManager().isElementGuarded(element));
128         }
129         n.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class));
130         return n;
131     }
132
133     public Node createEnumConstantNode(EnumConstant element) {
134         EnumConstantNode n = new EnumConstantNode(element, Children.LEAF, !JavaMetamodel.getManager().isElementGuarded(element));
135         n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class));
136         return n;
137     }
138
139     public Node createAnnotationTypeNode(AnnotationType element) {
140         SourceNodeFactory snf;
141         AnnotationTypeNode n;
142         
143         if (tree) {
144             snf = SourceNodes.getBrowserFactory();
145             AnnotationTypeChildren children = new AnnotationTypeChildren(snf, element);
146             AnnotationTypeFilter filter = new AnnotationTypeFilter();
147             n = new AnnotationTypeNode(element, children, !JavaMetamodel.getManager().isElementGuarded(element));
148             n.setElementFormat(new ElementFormat(
149                     NbBundle.getBundle (JavaDataObject.class).getString("CTL_Class_name_format") // NOI18N
150
));
151
152             // filter out inner classes
153
filter.setOrder(new int[] {
154                 ClassElementFilter.FIELD,
155                 ClassElementFilter.METHOD,
156             });
157         } else {
158             snf = SourceNodes.getExplorerFactory();
159             n = new AnnotationTypeNode(
160                     element, Categories.createAnnotationTypeChildren(element, snf, true),
161                     !JavaMetamodel.getManager().isElementGuarded(element));
162         }
163         n.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class));
164         return n;
165     }
166
167     public Node createAnnotationTypeMethodNode(Attribute element) {
168         ElementNode n = new AnnotationTypeMethodNode(element, !JavaMetamodel.getManager().isElementGuarded(element));
169         n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class));
170         return n;
171     }
172
173     public Node createClassNode(JavaClass element) {
174         if (element == null) {
175             // XXX hack for beans module; see JavaElementNodeFactory.FACTORY_GETTER_NODE and org.netbeans.modules.beans.PatternsBrowserFactory.createClassNode
176
return super.createClassNode(element);
177         }
178         
179         Node n;
180         if (tree) {
181             ClassNode cn ;
182             SourceNodeFactory snf = SourceNodes.getBrowserFactory();
183             ClassChildren children = new ClassChildren(snf, element);
184             ClassElementFilter filter = new ClassElementFilter();
185             cn = new ClassNode(element, children, !JavaMetamodel.getManager().isElementGuarded(element));
186
187             cn.setElementFormat(new ElementFormat (
188                                    NbBundle.getBundle (JavaDataObject.class).getString("CTL_Class_name_format") // NOI18N
189
));
190
191             // filter out inner classes
192
filter.setOrder (new int[] {
193                                  ClassElementFilter.FIELD,
194                                  ClassElementFilter.CONSTRUCTOR,
195                                  ClassElementFilter.METHOD,
196                              });
197             children.setFilter (filter);
198             cn.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class));
199             n = cn;
200         } else {
201             ClassNode cn ;
202             SourceNodeFactory snf = SourceNodes.getExplorerFactory();
203             cn = new ClassNode(element, Categories.createClassChildren(element, snf, true), !JavaMetamodel.getManager().isElementGuarded(element));
204             cn.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class));
205             
206             Node oldNode = getCompatibleFactory().createClassNode(element);
207             n = new CompatibleNode(cn, oldNode);
208         }
209         
210         return n;
211         
212     }
213
214     public Node createErrorNode() {
215         AbstractNode n = new AbstractNode(Children.LEAF);
216         n.setName(NbBundle.getMessage(ElementNode.class, "Error")); // NOI18N
217
n.setIconBase(IconStrings.ERROR);
218         return n;
219     }
220
221     public Node createWaitNode() {
222         AbstractNode n = new AbstractNode(Children.LEAF);
223         n.setName(NbBundle.getMessage(ElementNode.class, JavaMetamodel.getManager().isScanInProgress()? "WaitScan": "Wait")); // NOI18N
224
n.setIconBase(IconStrings.WAIT);
225         return n;
226     }
227
228     /** If true generate nodes for tree.
229     */

230     public void setGenerateForTree (boolean tree) {
231         this.tree = tree;
232     }
233
234     /** Returns true if generate nodes for tree.
235     * @return true if generate nodes for tree.
236     */

237     public boolean getGenerateForTree () {
238         return tree;
239     }
240
241
242     /** Array of the actions of the java methods, constructors and fields. */
243     private static final SystemAction[] DEFAULT_ACTIONS = new SystemAction[] {
244                 SystemAction.get(OpenAction.class),
245                 null,
246 // SystemAction.get(CutAction.class),
247
// SystemAction.get(CopyAction.class),
248
null,
249                 SystemAction.get(DeleteAction.class),
250 // SystemAction.get(RenameAction.class),
251
null,
252                 SystemAction.get(ToolsAction.class),
253                 SystemAction.get(PropertiesAction.class)
254             };
255
256     /** Array of the actions of the java intializers. */
257     private static final SystemAction[] INITIALIZER_ACTIONS = new SystemAction[] {
258                 SystemAction.get(OpenAction.class),
259                 null,
260 // SystemAction.get(CutAction.class),
261
// SystemAction.get(CopyAction.class),
262
null,
263                 SystemAction.get(DeleteAction.class),
264                 null,
265                 SystemAction.get(ToolsAction.class),
266                 SystemAction.get(PropertiesAction.class)
267             };
268
269     /** Array of the actions of the java classes. */
270     private static final SystemAction[] CLASS_ACTIONS = new SystemAction[] {
271                 SystemAction.get(OpenAction.class),
272                 null,
273 // SystemAction.get(CutAction.class),
274
// SystemAction.get(CopyAction.class),
275
// SystemAction.get(PasteAction.class),
276
// null,
277
SystemAction.get(DeleteAction.class),
278 // SystemAction.get(RenameAction.class),
279
null,
280                 SystemAction.get(NewAction.class),
281                 null,
282                 SystemAction.get(ToolsAction.class),
283                 SystemAction.get(PropertiesAction.class)
284             };
285     
286     private static SourceNodeFactory COMPATIBLE_FACTORY;
287     
288     /** gets the node facory of the old source hierarchy (java/srcmodel) */
289     private static SourceNodeFactory getCompatibleFactory() {
290         if (COMPATIBLE_FACTORY == null) {
291             COMPATIBLE_FACTORY = SourceNodes.createSourceNodeFactory(
292                     new ExFilterFactory(org.openide.src.nodes.DefaultFactory.READ_ONLY)
293             );
294         }
295         return COMPATIBLE_FACTORY;
296     }
297     
298     /** supplies cookies to ensure proper functionality of code that uses the old source hierarchy api */
299     private static final class CompatibleNode extends FilterNode {
300         public CompatibleNode(Node current, Node srcModelNode) {
301             super(current,
302                     null,
303                     new ProxyLookup(new Lookup[] {
304                         current.getLookup(),
305                         new OldElementNodeWithoutDO(srcModelNode).getLookup()
306                     })
307             );
308         }
309     }
310     
311     /** this filter the data object instance out of the node's lookup
312      * since ElementNode supplies own DO instance (issue #50644).
313      * @see CompatibleNode
314      */

315     private static final class OldElementNodeWithoutDO extends FilterNode {
316         public OldElementNodeWithoutDO(Node original) {
317             super(original);
318         }
319
320         public Node.Cookie getCookie(Class JavaDoc type) {
321             Node.Cookie nc = null;
322             if (!(DataObject.class.isAssignableFrom(type)
323                     || OpenCookie.class.isAssignableFrom(type)
324                     || SaveCookie.class.isAssignableFrom(type))) {
325                 
326                 nc = super.getCookie(type);
327             }
328             return nc;
329         }
330     }
331 }
332
Popular Tags