KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > nodes > elements > Categories


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.elements;
21
22 import org.openide.src.ElementProperties;
23 import org.openide.nodes.Node;
24 import org.openide.nodes.AbstractNode;
25 import org.openide.nodes.Children;
26 import org.openide.util.datatransfer.NewType;
27 import org.openide.util.actions.SystemAction;
28 import org.openide.util.NbBundle;
29 import org.openide.actions.NewAction;
30 import org.openide.actions.ToolsAction;
31 import org.openide.actions.PropertiesAction;
32 import org.netbeans.jmi.javamodel.JavaClass;
33 import org.netbeans.jmi.javamodel.JavaEnum;
34 import org.netbeans.jmi.javamodel.ClassDefinition;
35 import org.netbeans.jmi.javamodel.AnnotationType;
36 import org.netbeans.modules.java.ui.nodes.SourceNodeFactory;
37
38 import java.util.Collection JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.List JavaDoc;
41
42 /**
43  * Support for category nodes (fields, methods, ...). Use Categories.createClassChildren(...).
44  * @author Petr Hamernik, Jan Pokorsky
45  *
46  * XXX fix helpids
47  */

48 public final class Categories {
49     /**
50      * Tag for fields category node.
51      */

52     private static final Object JavaDoc CATEGORY_FIELDS = new Object JavaDoc();
53     /**
54      * Tag for "Method" category node.
55      */

56     private static final Object JavaDoc CATEGORY_METHODS = new Object JavaDoc();
57     /**
58      * Tag for "Constructors" category node.
59      */

60     private static final Object JavaDoc CATEGORY_CONSTRUCTORS = new Object JavaDoc();
61     /**
62      * Tag for "Constants" category node
63      */

64     private static final Object JavaDoc CATEGORY_CONSTANTS = new Object JavaDoc();
65     /**
66      * Tag for "Members" category node.
67      */

68     private static final Object JavaDoc CATEGORY_ANN_TYPE_METHODS = new Object JavaDoc();
69     
70     private static final List JavaDoc CLASS_CATEGORIES;
71     private static final List JavaDoc INTERFACE_CATEGORIES;
72     private static final List JavaDoc ENUM_CATEGORIES;
73     private static final List JavaDoc ANNTYPES_CATEGORIES;
74     public static final int FILTER_CATEGORIES = 0x1000;
75     /** Array of the actions of the category nodes. */
76     private static final SystemAction[] CATEGORY_ACTIONS = new SystemAction[] {
77         /*
78                 SystemAction.get(CopyAction.class),
79         */

80         // XXX paste is not supported
81
// SystemAction.get(PasteAction.class),
82
// null,
83
SystemAction.get(NewAction.class),
84                 null,
85                 SystemAction.get(ToolsAction.class),
86                 SystemAction.get(PropertiesAction.class)
87             };
88     /** Filters under each category node */
89     static final int[][] FILTERS = new int[][] {
90                                        { ClassElementFilter.FIELD },
91                                        { ClassElementFilter.CONSTRUCTOR },
92                                        { ClassElementFilter.METHOD },
93                                        { EnumFilter.CONSTANTS },
94                                        { AnnotationTypeFilter.MEMBER },
95                                    };
96     /** The names of the category nodes */
97     static final String JavaDoc[] NAMES = new String JavaDoc[] {
98                                       getString("Fields"), //NOI18N
99
getString("Constructors"), //NOI18N
100
getString("Methods"), //NOI18N
101
getString("Constants"), // NOI18N
102
getString("AnnTypeMethods") // NOI18N
103
};
104     /** The short descriptions of the category nodes */
105     static final String JavaDoc[] SHORTDESCRS = new String JavaDoc[] {
106                                             getString("Fields_HINT"), //NOI18N
107
getString("Constructors_HINT"), //NOI18N
108
getString("Methods_HINT"), //NOI18N
109
getString("Constants_HINT"), // NOI18N
110
getString("AnnTypeMethods_HINT") // NOI18N
111
};
112     /** Array of the icons used for category nodes */
113     static final String JavaDoc[] CATEGORY_ICONS = new String JavaDoc[] {
114         IconStrings.FIELDS_CATEGORY,
115         IconStrings.CONSTRUCTORS_CATEGORY,
116         IconStrings.METHODS_CATEGORY,
117         IconStrings.CONSTANTS_CATEGORY,
118         IconStrings.ANN_TYPE_METHODS_CATEGORY,
119     };
120     
121     /** Create children for a class node, with specified factory.
122     * The default implementation used {@link org.netbeans.modules.java.ui.nodes.elements.ClassChildren}.
123     * @param element a class element
124     * @param factory the factory which will be used to create children
125     * @return children for the class element
126     */

127     public static final Children createClassChildren(ClassDefinition element, SourceNodeFactory factory, boolean writable) {
128         if (ElementNode.getSourceOptions().getCategoriesUsage()) {
129             ClassChildren children = new Categories.CategorizingChildren(factory, element, writable);
130             ClassElementFilter filter = new ClassElementFilter();
131             filter.setOrder(new int[] {
132                     SourceElementFilter.CLASS,
133                     SourceElementFilter.INTERFACE,
134                     SourceElementFilter.ENUM,
135                     SourceElementFilter.ANNOTATION_TYPE,
136                     Categories.FILTER_CATEGORIES
137             });
138             children.setFilter(filter);
139             return children;
140         } else {
141             return new ClassChildren(factory, element);
142         }
143     }
144     
145     /** Create children for an enum node, with specified factory.
146     * @param element a class element
147     * @param factory the factory which will be used to create children
148     * @return children for the class element
149     */

150     public static final Children createEnumChildren(JavaEnum element, SourceNodeFactory factory, boolean writable) {
151         if (ElementNode.getSourceOptions().getCategoriesUsage()) {
152             ClassChildren children = new Categories.CategorizingChildren(factory, element, writable);
153             ClassElementFilter filter = new EnumFilter();
154             filter.setOrder(new int[] {
155                     SourceElementFilter.CLASS,
156                     SourceElementFilter.INTERFACE,
157                     SourceElementFilter.ENUM,
158                     SourceElementFilter.ANNOTATION_TYPE,
159                     Categories.FILTER_CATEGORIES
160             });
161             children.setFilter(filter);
162             return children;
163         } else {
164             return new EnumChildren(factory, element);
165         }
166     }
167     
168     /** Create children for an enum node, with specified factory.
169     * @param element a class element
170     * @param factory the factory which will be used to create children
171     * @return children for the class element
172     */

173     public static final Children createAnnotationTypeChildren(AnnotationType element, SourceNodeFactory factory, boolean writable) {
174         if (ElementNode.getSourceOptions().getCategoriesUsage()) {
175             ClassChildren children = new Categories.CategorizingChildren(factory, element, writable);
176             ClassElementFilter filter = new AnnotationTypeFilter();
177             filter.setOrder(new int[] {
178                     SourceElementFilter.CLASS,
179                     SourceElementFilter.INTERFACE,
180                     SourceElementFilter.ENUM,
181                     SourceElementFilter.ANNOTATION_TYPE,
182                     Categories.FILTER_CATEGORIES
183             });
184             children.setFilter(filter);
185             return children;
186         } else {
187             return new AnnotationTypeChildren(factory, element);
188         }
189     }
190
191     static {
192         CLASS_CATEGORIES = new ArrayList JavaDoc(4);
193         CLASS_CATEGORIES.add(CATEGORY_FIELDS);
194         CLASS_CATEGORIES.add(CATEGORY_CONSTRUCTORS);
195         CLASS_CATEGORIES.add(CATEGORY_METHODS);
196         
197         INTERFACE_CATEGORIES = new ArrayList JavaDoc(2);
198         INTERFACE_CATEGORIES.add(CATEGORY_FIELDS);
199         INTERFACE_CATEGORIES.add(CATEGORY_METHODS);
200         
201         ENUM_CATEGORIES = new ArrayList JavaDoc(4);
202         ENUM_CATEGORIES.add(CATEGORY_CONSTANTS);
203         ENUM_CATEGORIES.add(CATEGORY_FIELDS);
204         ENUM_CATEGORIES.add(CATEGORY_CONSTRUCTORS);
205         ENUM_CATEGORIES.add(CATEGORY_METHODS);
206         
207         ANNTYPES_CATEGORIES = new ArrayList JavaDoc(2);
208         ANNTYPES_CATEGORIES.add(CATEGORY_FIELDS);
209         ANNTYPES_CATEGORIES.add(CATEGORY_ANN_TYPE_METHODS);
210     }
211
212     /*
213      * Simple descendant of ClassChildren that distributes nodes from the class to various
214      * categories. Since - when categories are used - only innerclass, inner interface
215      * and categories can be displayed under
216      */

217     static final class CategorizingChildren extends ClassChildren {
218         boolean writeable;
219         
220         static {
221             ClassChildren.propToFilter.put(ElementProperties.PROP_CLASS_OR_INTERFACE,
222             new Integer JavaDoc(FILTER_CATEGORIES));
223         }
224         
225         CategorizingChildren(SourceNodeFactory factory, ClassDefinition data, boolean wr) {
226             super(factory, data);
227             writeable = wr;
228         }
229         
230         protected Node[] createNodesImpl(Object JavaDoc key) {
231             if (key == CATEGORY_FIELDS) {
232                 return new Node[] {
233                         new ElementCategoryNode(0, getFactory(), element, writeable)
234                 };
235             } else if (key == CATEGORY_METHODS) {
236                 return new Node[] {
237                         new ElementCategoryNode(2, getFactory(), element, writeable)
238                 };
239             } else if (key == CATEGORY_CONSTRUCTORS) {
240                 return new Node[] {
241                     new ElementCategoryNode(1, getFactory(), element, writeable)
242                 };
243             } else if (key == CATEGORY_CONSTANTS) {
244                 return new Node[] {
245                     new ElementCategoryNode(3, getFactory(), element, writeable)
246                 };
247             } else if (key == CATEGORY_ANN_TYPE_METHODS) {
248                 return new Node[] {
249                     new ElementCategoryNode(4, getFactory(), element, writeable)
250                 };
251             }
252             return super.createNodesImpl(key);
253         }
254         
255         protected List JavaDoc getKeysOfType(Collection JavaDoc/*<ClassMemeber>*/ elements, int type) {
256             if (type != FILTER_CATEGORIES)
257                 return super.getKeysOfType(elements, type);
258             if (element instanceof JavaEnum) {
259                 return ENUM_CATEGORIES;
260             } else if (element instanceof AnnotationType) {
261                     return ANNTYPES_CATEGORIES;
262             } else if (element instanceof JavaClass && ((JavaClass) element).isInterface()) {
263                 return INTERFACE_CATEGORIES;
264             } else {
265                 return CLASS_CATEGORIES;
266             }
267         }
268     }
269
270     /**
271     * Category node - represents one section under class element node - fields,
272     * constructors, methods.
273     */

274     static final class ElementCategoryNode extends AbstractNode {
275
276         /** The class element for this node */
277         ClassDefinition element;
278
279         /** The type of the category node - for new types. */
280         int newTypeIndex;
281         
282         private static ClassChildren createChildren(SourceNodeFactory f, ClassDefinition jc) {
283             ClassChildren cc;
284             if (jc instanceof AnnotationType) {
285                 cc = new AnnotationTypeChildren(f, (AnnotationType) jc);
286             } else if (jc instanceof JavaEnum) {
287                 cc = new EnumChildren(f, (JavaEnum) jc);
288             } else {
289                 cc = new ClassChildren(f, jc);
290             }
291             return cc;
292         }
293         
294         /** Create new element category node for the specific category.
295         * @param index The index of type (0=fields, 1=constructors, 2=methods)
296         * @param factory The factory which is passed down to the class children object
297         * @param element the class element which this node is created for
298         */

299         ElementCategoryNode(int index, SourceNodeFactory factory, ClassDefinition element, boolean writeable) {
300             this(index, createChildren(factory, element));
301             this.element = element;
302             newTypeIndex = writeable ? index : -1;
303             switch (index) {
304                 case 0: setName("Fields"); break; // NOI18N
305
case 1: setName("Constructors"); break; // NOI18N
306
case 2: setName("Methods"); break; // NOI18N
307
case 3: setName("Constants"); break; // NOI18N
308
case 4: setName("AnnTypeMethods"); break; // NOI18N
309
}
310         }
311
312         /** Create new element node.
313         * @param index The index of type (0=fields, 1=constructors, 2=methods)
314         * @param children the class children of this node
315         */

316         private ElementCategoryNode(int index, ClassChildren children) {
317             super(children);
318             setDisplayName(NAMES[index]);
319             setShortDescription(SHORTDESCRS[index]);
320             ClassElementFilter filter = new ClassElementFilter();
321             filter.setOrder(FILTERS[index]);
322             children.setFilter(filter);
323             systemActions = CATEGORY_ACTIONS;
324             setIconBase(CATEGORY_ICONS[index]);
325         }
326
327         /** Disables copy for the whole category. Sub-elements need to be selected individually.
328          */

329         public boolean canCopy() {
330             return false;
331         }
332
333         /* Get the new types that can be created in this node.
334         * @return array of new type operations that are allowed
335         */

336         public NewType[] getNewTypes() {
337             if (!SourceEditSupport.isWriteable(element) || !(element instanceof JavaClass)) {
338                 return new NewType[0];
339             }
340             
341             JavaClass jc = (JavaClass) this.element;
342             
343             switch(newTypeIndex) {
344                 case 0:
345                     return new NewType[] {
346                                new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_FIELD)
347                            };
348                 case 1:
349                     return new NewType[] {
350                                new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_INITIALIZER),
351                                new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_CONSTRUCTOR)
352                            };
353                 case 2:
354                     return new NewType[] {
355                                new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_METHOD)
356                            };
357                 case 3:
358                     return new NewType[] {
359                                new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_ENUMCONSTANT)
360                            };
361                 case 4:
362                     return new NewType[] {
363                                new SourceEditSupport.ElementNewType(jc, SourceEditSupport.NT_ANNOTATION_TYPE_METHOD)
364                            };
365                 default:
366                     return super.getNewTypes();
367             }
368         }
369
370         // XXX solve paste types
371
// public void createPasteTypes(java.awt.datatransfer.Transferable t, java.util.List s) {
372
// Node n = getParentNode();
373
// if (n == null || !(n instanceof ClassElementNode)) {
374
// return;
375
// }
376
// ((ClassElementNode)n).createPasteTypes(t, s);
377
// }
378
}
379     
380     private static String JavaDoc getString(String JavaDoc key) {
381         return NbBundle.getMessage(Categories.class, key);
382     }
383     
384 }
385
Popular Tags