KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > JavaElementNodeFactory


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;
21
22 import java.beans.*;
23 import java.util.Collection JavaDoc;
24 import java.util.LinkedList JavaDoc;
25
26 import org.openide.actions.*;
27 import org.openide.cookies.OpenCookie;
28 import org.openide.cookies.FilterCookie;
29 import org.openide.nodes.*;
30 import org.openide.loaders.DataObject;
31 import org.openide.src.*;
32 import org.openide.src.nodes.*;
33 import org.openide.util.NbBundle;
34 import org.openide.util.actions.SystemAction;
35 import org.openide.*;
36
37 import org.netbeans.modules.java.tools.*;
38 import org.netbeans.modules.java.parser.JavaParser;
39
40 /** The implementation of hierarchy nodes factory for the java loader.
41 *
42 * @author Petr Hamernik
43 */

44 class JavaElementNodeFactory extends DefaultFactory {
45     /** Default instance of this factory. */
46     public static final DefaultFactory DEFAULT = new JavaElementNodeFactory();
47
48     /** Array of the actions of the java methods, constructors and fields. */
49     private static final SystemAction[] DEFAULT_ACTIONS = new SystemAction[] {
50                 SystemAction.get(OpenAction.class),
51                 null,
52                 SystemAction.get(CutAction.class),
53                 SystemAction.get(CopyAction.class),
54                 null,
55                 SystemAction.get(DeleteAction.class),
56                 SystemAction.get(RenameAction.class),
57                 null,
58                 SystemAction.get(ToolsAction.class),
59                 SystemAction.get(PropertiesAction.class)
60             };
61
62     /** Array of the actions of the java intializers. */
63     private static final SystemAction[] INITIALIZER_ACTIONS = new SystemAction[] {
64                 SystemAction.get(OpenAction.class),
65                 null,
66                 SystemAction.get(CutAction.class),
67                 SystemAction.get(CopyAction.class),
68                 null,
69                 SystemAction.get(DeleteAction.class),
70                 null,
71                 SystemAction.get(ToolsAction.class),
72                 SystemAction.get(PropertiesAction.class)
73             };
74
75     /** Array of the actions of the java classes. */
76     private static final SystemAction[] CLASS_ACTIONS = new SystemAction[] {
77                 SystemAction.get(OpenAction.class),
78                 null,
79                 SystemAction.get(CutAction.class),
80                 SystemAction.get(CopyAction.class),
81                 SystemAction.get(PasteAction.class),
82                 null,
83                 SystemAction.get(DeleteAction.class),
84                 SystemAction.get(RenameAction.class),
85                 null,
86                 SystemAction.get(NewAction.class),
87                 null,
88                 SystemAction.get(ToolsAction.class),
89                 SystemAction.get(PropertiesAction.class)
90             };
91
92     /** This node can return current element factory as cookie */
93     private final Node FACTORY_GETTER_NODE = new FactoryGetterNode();
94
95
96     /** Create nodes for tree */
97     private boolean tree = false;
98
99     /** Creates new factory. */
100     public JavaElementNodeFactory() {
101         super(true);
102     }
103
104     /** If true generate nodes for tree.
105     */

106     public void setGenerateForTree (boolean tree) {
107         this.tree = tree;
108     }
109
110     /** Returns true if generate nodes for tree.
111     * @returns true if generate nodes for tree.
112     */

113     public boolean getGenerateForTree () {
114         return tree;
115     }
116
117     /** Returns the node asociated with specified element.
118     * @return ElementNode
119     */

120     public Node createMethodNode(MethodElement element) {
121         MethodElementNode n = new MethodElementNode(element, true);
122         n.setDefaultAction(SystemAction.get(OpenAction.class));
123         n.setActions(DEFAULT_ACTIONS);
124         return n;
125     }
126
127     /** Returns the node asociated with specified element.
128     * @return ElementNode
129     */

130     public Node createConstructorNode(ConstructorElement element) {
131         ConstructorElementNode n = new ConstructorElementNode(element, true);
132         n.setDefaultAction(SystemAction.get(OpenAction.class));
133         n.setActions(DEFAULT_ACTIONS);
134         return n;
135     }
136
137     /** Returns the node asociated with specified element.
138     * @return ElementNode
139     */

140     public Node createFieldNode(FieldElement element) {
141         FieldElementNode n = new FieldElementNode(element, true);
142         n.setDefaultAction(SystemAction.get(OpenAction.class));
143         n.setActions(DEFAULT_ACTIONS);
144         return n;
145     }
146
147     /** Returns the node asociated with specified element.
148     * @return ElementNode
149     */

150     public Node createInitializerNode(InitializerElement element) {
151         InitializerElementNode n = new InitializerElementNode(element, true);
152         n.setDefaultAction(SystemAction.get(OpenAction.class));
153         n.setActions(INITIALIZER_ACTIONS);
154         return n;
155     }
156
157     /** Returns the node asociated with specified element.
158     * @return ElementNode
159     */

160     public Node createClassNode(final ClassElement element) {
161
162         if ( element == null ) {
163             return FACTORY_GETTER_NODE;
164         }
165
166
167         ClassElementNode n;
168         if (tree) {
169             ClassChildren children = new ClassChildren( JavaDataObject.getBrowserFactory(), element);
170             ClassElementFilter filter = new ClassElementFilter();
171             n = new ClassElementNode(element, children ,true) {
172                 {
173                     getCookieSet().add((FilterCookie) getChildren ());
174                 }
175             };
176
177             n.setElementFormat(new ElementFormat (
178                                    NbBundle.getBundle (JavaElementNodeFactory.class).getString("CTL_Class_name_format")
179                                ));
180
181             // filter out inner classes
182
filter.setOrder (new int[] {
183                                  ClassElementFilter.FIELD,
184                                  ClassElementFilter.CONSTRUCTOR,
185                                  ClassElementFilter.METHOD,
186                              });
187             children.setFilter (filter);
188         }
189         else {
190             n = (ClassElementNode) super.createClassNode(element);
191         }
192         n.setDefaultAction(SystemAction.get(OpenAction.class));
193         n.setActions(CLASS_ACTIONS);
194         return n;
195     }
196
197     protected Children createClassChildren( ClassElement element ) {
198         return createClassChildren( element, JavaDataObject.getExplorerFactory() );
199     }
200
201     /**
202      * This method will try to extract more information than the ordinary Error message.
203      */

204     public Node createErrorNode() {
205         final Node n = super.createErrorNode();
206
207         n.addNodeListener(new NodeListener() {
208             public void propertyChange(PropertyChangeEvent e) {
209                 Node parent = n.getParentNode();
210                 DataObject d = null;
211                
212                 if (parent == null)
213                     return;
214                 n.removeNodeListener(this);
215                 do {
216                     d = (DataObject)parent.getCookie(DataObject.class);
217                     parent = parent.getParentNode();
218                 } while (parent != null && d == null);
219                 if (d == null)
220                     return;
221                 setErrorDescription(n, (JavaParser)d.getCookie(JavaParser.class));
222             }
223             
224             public void childrenAdded(NodeMemberEvent e) {}
225             public void childrenRemoved(NodeMemberEvent e) {}
226             public void childrenReordered(NodeReorderEvent e) {}
227             public void nodeDestroyed(NodeEvent e) {}
228         });
229         return n;
230     }
231
232     private void setErrorDescription(Node n, JavaParser p) {
233         if (p == null)
234             return;
235         SourceException e = p.getErrorCause();
236         String JavaDoc msg = findErrorMessage(e);
237         if (msg == null)
238             return;
239         
240         if (e instanceof SourceException.IO) {
241             n.setDisplayName(Util.getString("MSG_PARSE_ERROR_IO"));
242         }
243         n.setShortDescription(msg);
244     }
245
246     private String JavaDoc findErrorMessage(Throwable JavaDoc t) {
247         if (t == null) {
248             return null;
249         }
250         
251         ErrorManager.Annotation[] ann = ErrorManager.getDefault().findAnnotations(t);
252         if (ann == null)
253             return t.getLocalizedMessage();
254         for (int i = 0; i < ann.length; i++) {
255             String JavaDoc normal = ann[i].getMessage();
256             String JavaDoc localized = ann[i].getLocalizedMessage();
257             if (localized != null)
258                 return localized;
259             Throwable JavaDoc t2 = ann[i].getStackTrace();
260             if (t2 == null)
261                 continue;
262             localized = t2.getLocalizedMessage();
263             if (localized != null) {
264                 if (!localized.equals(normal))
265                     return localized;
266             }
267         }
268         return null;
269     }
270     
271     /** This is an unusuall use of Node and FilterCookie */
272
273     private class FactoryGetterNode extends AbstractNode implements FilterCookie {
274
275         FactoryGetterNode( ) {
276             super ( Children.LEAF );
277         }
278
279         public synchronized Node.Cookie getCookie( Class JavaDoc clazz ) {
280             if ( clazz == FilterFactory.class )
281                 return this;
282             else
283                 return super.getCookie( clazz );
284         }
285
286         public Class JavaDoc getFilterClass() {
287             return null;
288         }
289
290         public void setFilter( Object JavaDoc filter ) {}
291
292         public Object JavaDoc getFilter( ) {
293             if ( tree )
294                 return JavaDataObject.getBrowserFactory();
295             else
296                 return JavaDataObject.getExplorerFactory();
297         }
298
299     }
300
301 }
302
303
Popular Tags