KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javadoc > comments > ElementDescriptor


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 package org.netbeans.modules.javadoc.comments;
20
21 import org.netbeans.jmi.javamodel.*;
22 import org.openide.util.Utilities;
23
24 import javax.jmi.reflect.JmiException;
25 import javax.swing.*;
26 import java.awt.*;
27 import java.lang.reflect.Modifier JavaDoc;
28 import java.text.Format JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 /**
33  * Describes jmi element. It is inteded to minimize access to MDR.
34  */

35 final class ElementDescriptor {
36     private static final int offsetPublic = 0;
37     private static final int offsetPackage = 4;
38     private static final int offsetProtected = 8;
39     private static final int offsetPrivate = 12;
40     private static final int iconNothing = 0;
41     private static final int iconClass = 1;
42     private static final int iconInterface = 2;
43     private static final int iconField = 3;
44     private static final int iconConstructor = iconField + offsetPrivate + 1;
45     private static final int iconMethod = iconConstructor + offsetPrivate + 1;
46     private static final int iconEnum = iconMethod + offsetPrivate + 1;
47     private static final int iconAnnType = iconEnum + offsetPrivate + 1;
48     private static final int iconConstant = iconAnnType + offsetPrivate + 1;
49     
50     private static String JavaDoc[] EMPTY_ARRAY = new String JavaDoc[0];
51     
52     private String JavaDoc name;
53     private String JavaDoc formattedName;
54     private int modifiers;
55     private int effectiveAccess;
56     private ImageIcon icon;
57     private String JavaDoc declaringClassName;
58     private String JavaDoc[] paramNames = EMPTY_ARRAY;
59     private String JavaDoc[] throwFQNames = EMPTY_ARRAY;
60     private String JavaDoc typeFQName;
61     private final AutoCommenter.Element element;
62     private final String JavaDoc identity;
63
64     /** needs to be enclosed in mdr transaction */
65     public ElementDescriptor(AutoCommenter.Element el, Format JavaDoc nameFormat) throws JmiException {
66         this.element = el;
67         ClassMember cm = el.getSrcElement();
68         this.name = cm.getName();
69         this.formattedName = nameFormat.format(cm);
70         this.modifiers = cm.getModifiers();
71         ClassDefinition cd = cm.getDeclaringClass();
72         if (cd != null) this.declaringClassName = cd.getName();
73         
74         if (cm instanceof CallableFeature) {
75             CallableFeature cf = (CallableFeature) cm;
76             this.paramNames = getParameterNames(cf);
77             this.throwFQNames = getThrowFQNames(cf);
78         }
79         
80         if (cm instanceof TypedElement) {
81             this.typeFQName = ((TypedElement) cm).getType().getName();
82         }
83         
84         this.effectiveAccess = getEffectiveAccess(cm);
85         this.identity = cm.refMofId();
86     }
87     
88     public final String JavaDoc getName() {
89         return this.name;
90     }
91     
92     public final String JavaDoc getFormattedName() {
93         return formattedName;
94     }
95     
96     /**
97      * name of the declaring class of the element
98      * @return name or <code>null</code>
99      */

100     public final String JavaDoc getDeclaringClassName() {
101         return declaringClassName;
102     }
103         
104     public final int getModifiers() {
105         return modifiers;
106     }
107     
108     public final int getEffectiveAccess() {
109         return effectiveAccess;
110     }
111     
112     public final String JavaDoc[] getParameterNames() {
113         return paramNames;
114     }
115     
116     public final String JavaDoc[] getThrowFQNames() {
117         return throwFQNames;
118     }
119     
120     public final String JavaDoc getTypeFQName() {
121         return typeFQName;
122     }
123         
124     public final Icon getIcon() {
125         assert this.icon != null;
126         return icon;
127     }
128
129     public String JavaDoc getIdentity() {
130         return identity;
131     }
132
133     /**
134      * recompute image; needs MDR transaction
135      */

136     public final void recomputeIcon() {
137         if (this.icon == null) {
138             this.icon = new ImageIcon();
139         }
140         this.icon.setImage(getMergedImage());
141     }
142
143     // private impl
144

145     private static String JavaDoc[] getParameterNames(CallableFeature cf) {
146         List JavaDoc params = cf.getParameters();
147         String JavaDoc[] names = new String JavaDoc[params.size()];
148         int i = 0;
149         for (Iterator JavaDoc it = params.iterator(); it.hasNext(); i++) {
150             Parameter param = (Parameter) it.next();
151             names[i] = param.getName();
152         }
153         return names;
154     }
155     
156     private static String JavaDoc[] getThrowFQNames(CallableFeature cf) {
157         List JavaDoc exs = cf.getExceptions();
158         String JavaDoc[] names = new String JavaDoc[exs.size()];
159         int i = 0;
160         for (Iterator JavaDoc it = exs.iterator(); it.hasNext(); i++) {
161             JavaClass ex = (JavaClass) it.next();
162             names[i] = ex.getName();
163         }
164         return names;
165     }
166
167     private Image getMergedImage() {
168         int error = this.element.getErrorNumber();
169         int type = resolveIconIndex();
170
171         Image im1 = getImage(error);
172         Image im2 = getMemberImage(type);
173         return Utilities.mergeImages(im1, im2, 18, 0);
174     }
175
176     private int resolveIconIndex() throws JmiException {
177         ClassMember me = this.element.getSrcElement();
178         int offset;
179         if ((Modifier.PUBLIC & modifiers) != 0)
180             offset = offsetPublic;
181         else if ((Modifier.PRIVATE & modifiers) != 0)
182             offset = offsetPrivate;
183         else if ((Modifier.PROTECTED & modifiers) != 0)
184             offset = offsetProtected;
185         else
186             offset = offsetPackage;
187
188         if (me instanceof JavaEnum)
189             return offset + iconEnum;
190         else if (me instanceof AnnotationType)
191             return offset + iconAnnType;
192         else if (me instanceof JavaClass)
193             return offset + (Modifier.isInterface(modifiers) ? iconInterface : iconClass);
194         else if (me instanceof Method || me instanceof Attribute)
195             return offset + iconMethod;
196         else if (me instanceof Constructor)
197             return offset + iconConstructor;
198         else if (me instanceof EnumConstant)
199             return iconConstant;
200         else if (me instanceof Field)
201             return offset + iconField;
202         else
203             return iconNothing;
204     }
205
206     /**
207      * @param index
208      * @return */

209     private static Image getMemberImage(int index) {
210         switch (index) {
211             case iconClass + offsetPackage:
212             case iconClass + offsetProtected:
213             case iconClass + offsetPrivate:
214             case iconClass + offsetPublic:
215                 return Utilities.loadImage("org/openide/src/resources/class.gif"); // NOI18N
216
case iconInterface + offsetPackage:
217             case iconInterface + offsetProtected:
218             case iconInterface + offsetPrivate:
219             case iconInterface + offsetPublic:
220                 return Utilities.loadImage("org/openide/src/resources/interface.gif"); // NOI18N
221
case iconEnum + offsetPackage:
222             case iconEnum + offsetProtected:
223             case iconEnum + offsetPrivate:
224             case iconEnum + offsetPublic:
225                 return Utilities.loadImage("org/netbeans/modules/java/resources/enum.gif"); // NOI18N
226
case iconAnnType + offsetPackage:
227             case iconAnnType + offsetProtected:
228             case iconAnnType + offsetPrivate:
229             case iconAnnType + offsetPublic:
230                 return Utilities.loadImage("org/netbeans/modules/java/resources/annotation_type.gif"); // NOI18N
231
case iconConstant:
232                 return Utilities.loadImage("org/netbeans/modules/java/resources/constant.gif"); // NOI18N
233
case iconField + offsetPublic:
234                 return Utilities.loadImage("org/openide/src/resources/variablePublic.gif"); // NOI18N
235
case iconField + offsetPackage:
236                 return Utilities.loadImage("org/openide/src/resources/variablePackage.gif"); // NOI18N
237
case iconField + offsetProtected:
238                 return Utilities.loadImage("org/openide/src/resources/variableProtected.gif"); // NOI18N
239
case iconField + offsetPrivate:
240                 return Utilities.loadImage("org/openide/src/resources/variablePrivate.gif"); // NOI18N
241
case iconConstructor + offsetPublic:
242                 return Utilities.loadImage("org/openide/src/resources/constructorPublic.gif"); // NOI18N
243
case iconConstructor + offsetPackage:
244                 return Utilities.loadImage("org/openide/src/resources/constructorPackage.gif"); // NOI18N
245
case iconConstructor + offsetProtected:
246                 return Utilities.loadImage("org/openide/src/resources/constructorProtected.gif"); // NOI18N
247
case iconConstructor + offsetPrivate:
248                 return Utilities.loadImage("org/openide/src/resources/constructorPrivate.gif"); // NOI18N
249
case iconMethod + offsetPublic:
250                 return Utilities.loadImage("org/openide/src/resources/methodPublic.gif"); // NOI18N
251
case iconMethod + offsetPackage:
252                 return Utilities.loadImage("org/openide/src/resources/methodPackage.gif"); // NOI18N
253
case iconMethod + offsetProtected:
254                 return Utilities.loadImage("org/openide/src/resources/methodProtected.gif"); // NOI18N
255
case iconMethod + offsetPrivate:
256                 return Utilities.loadImage("org/openide/src/resources/methodPrivate.gif"); // NOI18N
257
default:
258                 return null;
259         }
260     }
261
262     /** Used to returnn image for given index, not cached here, by Utilities
263      * @param index of Image
264      * @return Image for index
265      */

266     private static Image getImage(int index) {
267         switch (index) {
268             case 1:
269                 return Utilities.loadImage("org/netbeans/modules/javadoc/comments/resources/ok.gif"); // NOI18N
270
case 2:
271                 return Utilities.loadImage("org/netbeans/modules/javadoc/comments/resources/missing.gif"); // NOI18N
272
case 4:
273                 return Utilities.loadImage("org/netbeans/modules/javadoc/comments/resources/error.gif"); // NOI18N
274
default:
275                 return null;
276         }
277     }
278
279     private static int getEffectiveAccess(ClassMember el) {
280         int access = el.getModifiers() & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE);
281         if (access == Modifier.PRIVATE)
282             return access;
283         JavaClass decl = (JavaClass) el.getDeclaringClass();
284         if (decl == null)
285             // it is a top-level class
286
return access;
287         if (decl.isInterface())
288             // interface members must be public
289
access = Modifier.PUBLIC;
290         
291         int parentAccess = getEffectiveAccess(decl);
292         switch (parentAccess) {
293             case Modifier.PRIVATE:
294             case 0:
295                 // private members were handled above, everything else is
296
// constrained by parent's privacy
297
return parentAccess;
298             case Modifier.PROTECTED:
299                 // member can be restricted to only the package
300
return access == 0 ? 0 : parentAccess;
301             case Modifier.PUBLIC:
302             default:
303                 return access;
304         }
305     }
306
307
308 }
309
Popular Tags