KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > nodes > DefaultFactory


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.openide.src.nodes;
21
22 import java.beans.*;
23 import java.util.Collection JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.openide.actions.*;
27 import org.openide.nodes.*;
28 import org.openide.src.*;
29 import org.openide.src.nodes.*;
30 import org.openide.util.actions.SystemAction;
31 import org.openide.util.datatransfer.NewType;
32
33 /** The default implementation of the hierarchy nodes factory.
34 * Uses the standard node implementations in this package.
35 * @author Petr Hamernik
36 */

37 public class DefaultFactory extends Object JavaDoc implements ElementNodeFactory, IconStrings {
38     /** Default instance of the factory with read-write properties. */
39     public static final DefaultFactory READ_WRITE = new DefaultFactory(true);
40
41     /** Default instance of the factory with read-only properties. */
42     public static final DefaultFactory READ_ONLY = new DefaultFactory(false);
43
44     /**
45      * Tag for fields category node.
46      */

47     private static final Object JavaDoc CATEGORY_FIELDS = new Object JavaDoc();
48     
49     /**
50      * Tag for "Method" category node.
51      */

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

57     private static final Object JavaDoc CATEGORY_CONSTRUCTORS = new Object JavaDoc();
58     
59     private static final Collection JavaDoc CLASS_CATEGORIES;
60
61     private static final Collection JavaDoc INTERFACE_CATEGORIES;
62     
63     private static final int FILTER_CATEGORIES = 0x1000;
64
65     static {
66         CLASS_CATEGORIES = new Vector JavaDoc(3, 0);
67         CLASS_CATEGORIES.add(CATEGORY_FIELDS);
68         CLASS_CATEGORIES.add(CATEGORY_CONSTRUCTORS);
69         CLASS_CATEGORIES.add(CATEGORY_METHODS);
70         
71         INTERFACE_CATEGORIES = new Vector JavaDoc(2, 0);
72         INTERFACE_CATEGORIES.add(CATEGORY_FIELDS);
73         INTERFACE_CATEGORIES.add(CATEGORY_METHODS);
74     }
75
76     /** Should be the element nodes read-only or writeable
77     * (properties, clipboard operations,...)
78     */

79     private boolean writeable;
80
81     /** Create a new factory.
82     * @param writeable <code>true</code> if the produced nodes
83     * should have writable properties
84     * @see ElementNode#writeable
85     */

86     public DefaultFactory(boolean writeable) {
87         this.writeable = writeable;
88     }
89
90     /* Test whether this factory produces writeable nodes.
91     * @return <code>true</code> if so
92     */

93     public boolean isWriteable() {
94         return writeable;
95     }
96
97     /* Returns the node asociated with specified element.
98     * @return ElementNode
99     */

100     public Node createMethodNode (final MethodElement element) {
101         return new MethodElementNode(element, writeable);
102     }
103
104     /* Returns the node asociated with specified element.
105     * @return ElementNode
106     */

107     public Node createConstructorNode (final ConstructorElement element) {
108         return new ConstructorElementNode(element, writeable);
109     }
110
111     /* Returns the node asociated with specified element.
112     * @return ElementNode
113     */

114     public Node createFieldNode (final FieldElement element) {
115         return new FieldElementNode(element, writeable);
116     }
117
118     /* Returns the node asociated with specified element.
119     * @return ElementNode
120     */

121     public Node createInitializerNode (final InitializerElement element) {
122         return new InitializerElementNode(element, writeable);
123     }
124
125     /* Returns the node asociated with specified element.
126     * @return ElementNode
127     */

128     public Node createClassNode (ClassElement element) {
129         return new ClassElementNode(element, createClassChildren(element), writeable);
130     }
131
132     /** Create children for a class node.
133     * Could be subclassed to customize, e.g., the ordering of children.
134     * The default implementation used {@link ClassChildren}.
135     * @param element a class element
136     * @return children for the class element
137     */

138     protected Children createClassChildren(ClassElement element) {
139         return createClassChildren( element, writeable ? READ_WRITE : READ_ONLY );
140     }
141
142     /** Create children for a class node, with specified factory.
143     * The default implementation used {@link ClassChildren}.
144     * @param element a class element
145     * @param factory the factory which will be used to create children
146     * @return children for the class element
147     */

148     final protected Children createClassChildren(ClassElement element, ElementNodeFactory factory ) {
149
150         if (ElementNode.sourceOptions.getCategoriesUsage()) {
151             ClassChildren children = new CategorizingChildren(factory, element, writeable);
152             ClassElementFilter filter = new ClassElementFilter();
153             filter.setOrder(new int[] {
154                     SourceElementFilter.CLASS,
155                     SourceElementFilter.INTERFACE,
156                     FILTER_CATEGORIES
157             });
158             children.setFilter(filter);
159             return children;
160         }
161         else {
162             return new ClassChildren(factory, element);
163         }
164     }
165
166     /* Creates and returns the instance of the node
167     * representing the status 'WAIT' of the DataNode.
168     * It is used when it spent more time to create elements hierarchy.
169     * @return the wait node.
170     */

171     public Node createWaitNode () {
172         AbstractNode n = new AbstractNode(Children.LEAF);
173         n.setName(ElementNode.bundle.getString("Wait"));
174         n.setIconBase(WAIT);
175         return n;
176     }
177
178     /* Creates and returns the instance of the node
179     * representing the status 'ERROR' of the DataNode
180     * @return the error node.
181     */

182     public Node createErrorNode () {
183         AbstractNode n = new AbstractNode(Children.LEAF);
184         n.setName(ElementNode.bundle.getString("Error")); // NO18N
185
n.setIconBase(ERROR);
186         return n;
187     }
188
189     /** Array of the actions of the category nodes. */
190     private static final SystemAction[] CATEGORY_ACTIONS = new SystemAction[] {
191         /*
192                 SystemAction.get(CopyAction.class),
193         */

194                 SystemAction.get(PasteAction.class),
195                 null,
196                 SystemAction.get(NewAction.class),
197                 null,
198                 SystemAction.get(ToolsAction.class),
199                 SystemAction.get(PropertiesAction.class)
200             };
201
202     /** Filters under each category node */
203     static final int[][] FILTERS = new int[][] {
204                                        { ClassElementFilter.FIELD },
205                                        { ClassElementFilter.CONSTRUCTOR },
206                                        { ClassElementFilter.METHOD },
207                                    };
208
209     /** The names of the category nodes */
210     static final String JavaDoc[] NAMES = new String JavaDoc[] {
211                                       ElementNode.bundle.getString("Fields"), // NO18N
212
ElementNode.bundle.getString("Constructors"), // NO18N
213
ElementNode.bundle.getString("Methods"), // NO18N
214
};
215
216     /** The short descriptions of the category nodes */
217     static final String JavaDoc[] SHORTDESCRS = new String JavaDoc[] {
218                                             ElementNode.bundle.getString("Fields_HINT"), // NO18N
219
ElementNode.bundle.getString("Constructors_HINT"), // NO18N
220
ElementNode.bundle.getString("Methods_HINT"), // NO18N
221
};
222
223     /** Array of the icons used for category nodes */
224     static final String JavaDoc[] CATEGORY_ICONS = new String JavaDoc[] {
225                                                FIELDS_CATEGORY, CONSTRUCTORS_CATEGORY, METHODS_CATEGORY
226                                            };
227
228     /*
229      * Simple descendant of ClassChildren that distributes nodes from the class to various
230      * categories. Since - when categories are used - only innerclass, inner interface
231      * and categories can be displayed under
232      */

233     static class CategorizingChildren extends ClassChildren {
234         boolean writeable;
235         
236         static {
237             ClassChildren.propToFilter.put(ElementProperties.PROP_CLASS_OR_INTERFACE,
238             new Integer JavaDoc(FILTER_CATEGORIES));
239         }
240         
241         CategorizingChildren(ElementNodeFactory factory, ClassElement data, boolean wr) {
242             super(factory, data);
243             writeable = wr;
244         }
245         
246         protected Node[] createNodes(Object JavaDoc key) {
247             if (key == CATEGORY_FIELDS) {
248                 return new Node[] {
249                         new ElementCategoryNode(0, factory, element, writeable)
250                 };
251             } else if (key == CATEGORY_METHODS) {
252                 return new Node[] {
253                         new ElementCategoryNode(2, factory, element, writeable)
254                 };
255             } else if (key == CATEGORY_CONSTRUCTORS) {
256                 return new Node[] {
257                     new ElementCategoryNode(1, factory, element, writeable)
258                 };
259             }
260             return super.createNodes(key);
261         }
262         
263         protected Collection JavaDoc getKeysOfType(int type) {
264             if (type != FILTER_CATEGORIES)
265                 return super.getKeysOfType(type);
266             if (element.isClassOrInterface()) {
267                 return CLASS_CATEGORIES;
268             } else {
269                 return INTERFACE_CATEGORIES;
270             }
271         }
272     }
273     
274     /**
275     * Category node - represents one section under class element node - fields,
276     * constructors, methods.
277     */

278     static class ElementCategoryNode extends AbstractNode {
279
280         /** The class element for this node */
281         ClassElement element;
282
283         /** The type of the category node - for new types. */
284         int newTypeIndex;
285         
286         /** Create new element category node for the specific category.
287         * @param index The index of type (0=fields, 1=constructors, 2=methods)
288         * @param factory The factory which is passed down to the class children object
289         * @param element the class element which this node is created for
290         */

291         ElementCategoryNode(int index, ElementNodeFactory factory, ClassElement element, boolean writeable) {
292             this(index, new ClassChildren(factory, element));
293             this.element = element;
294             newTypeIndex = writeable ? index : -1;
295             switch (index) {
296             case 0: setName("Fields"); break; // NOI18N
297
case 1: setName("Constructors"); break; // NOI18N
298
case 2: setName("Methods"); break; // NOI18N
299
}
300         }
301
302         /** Create new element node.
303         * @param index The index of type (0=fields, 1=constructors, 2=methods)
304         * @param children the class children of this node
305         */

306         private ElementCategoryNode(int index, ClassChildren children) {
307             super(children);
308             setDisplayName(NAMES[index]);
309             setShortDescription (SHORTDESCRS[index]);
310             ClassElementFilter filter = new ClassElementFilter();
311             filter.setOrder(FILTERS[index]);
312             children.setFilter(filter);
313             systemActions = CATEGORY_ACTIONS;
314             setIconBase(CATEGORY_ICONS[index]);
315         }
316
317     /** Disables copy for the whole category. Sub-elements need to be selected individually.
318      */

319     public boolean canCopy() {
320         return false;
321     }
322
323         /* Get the new types that can be created in this node.
324         * @return array of new type operations that are allowed
325         */

326         public NewType[] getNewTypes() {
327             if (!SourceEditSupport.isWriteable(element)) {
328                 return new NewType[0];
329             }
330             switch (newTypeIndex) {
331             case 0:
332                 return new NewType[] {
333                            new SourceEditSupport.ElementNewType(element, (byte) 1)
334                        };
335             case 1:
336                 return new NewType[] {
337                            new SourceEditSupport.ElementNewType(element, (byte) 0),
338                            new SourceEditSupport.ElementNewType(element, (byte) 2)
339                        };
340             case 2:
341                 return new NewType[] {
342                            new SourceEditSupport.ElementNewType(element, (byte) 3)
343                        };
344             default:
345                 return super.getNewTypes();
346             }
347         }
348
349         public void createPasteTypes(java.awt.datatransfer.Transferable JavaDoc t, java.util.List JavaDoc s) {
350             Node n = getParentNode();
351             if (n == null || !(n instanceof ClassElementNode)) {
352                 return;
353             }
354             ((ClassElementNode)n).createPasteTypes(t, s);
355         }
356     }
357
358 }
359
Popular Tags