1 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 ; 28 import java.text.Format ; 29 import java.util.List ; 30 import java.util.Iterator ; 31 32 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 [] EMPTY_ARRAY = new String [0]; 51 52 private String name; 53 private String formattedName; 54 private int modifiers; 55 private int effectiveAccess; 56 private ImageIcon icon; 57 private String declaringClassName; 58 private String [] paramNames = EMPTY_ARRAY; 59 private String [] throwFQNames = EMPTY_ARRAY; 60 private String typeFQName; 61 private final AutoCommenter.Element element; 62 private final String identity; 63 64 65 public ElementDescriptor(AutoCommenter.Element el, Format 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 getName() { 89 return this.name; 90 } 91 92 public final String getFormattedName() { 93 return formattedName; 94 } 95 96 100 public final String 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 [] getParameterNames() { 113 return paramNames; 114 } 115 116 public final String [] getThrowFQNames() { 117 return throwFQNames; 118 } 119 120 public final String getTypeFQName() { 121 return typeFQName; 122 } 123 124 public final Icon getIcon() { 125 assert this.icon != null; 126 return icon; 127 } 128 129 public String getIdentity() { 130 return identity; 131 } 132 133 136 public final void recomputeIcon() { 137 if (this.icon == null) { 138 this.icon = new ImageIcon(); 139 } 140 this.icon.setImage(getMergedImage()); 141 } 142 143 145 private static String [] getParameterNames(CallableFeature cf) { 146 List params = cf.getParameters(); 147 String [] names = new String [params.size()]; 148 int i = 0; 149 for (Iterator 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 [] getThrowFQNames(CallableFeature cf) { 157 List exs = cf.getExceptions(); 158 String [] names = new String [exs.size()]; 159 int i = 0; 160 for (Iterator 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 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"); 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"); 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"); 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"); case iconConstant: 232 return Utilities.loadImage("org/netbeans/modules/java/resources/constant.gif"); case iconField + offsetPublic: 234 return Utilities.loadImage("org/openide/src/resources/variablePublic.gif"); case iconField + offsetPackage: 236 return Utilities.loadImage("org/openide/src/resources/variablePackage.gif"); case iconField + offsetProtected: 238 return Utilities.loadImage("org/openide/src/resources/variableProtected.gif"); case iconField + offsetPrivate: 240 return Utilities.loadImage("org/openide/src/resources/variablePrivate.gif"); case iconConstructor + offsetPublic: 242 return Utilities.loadImage("org/openide/src/resources/constructorPublic.gif"); case iconConstructor + offsetPackage: 244 return Utilities.loadImage("org/openide/src/resources/constructorPackage.gif"); case iconConstructor + offsetProtected: 246 return Utilities.loadImage("org/openide/src/resources/constructorProtected.gif"); case iconConstructor + offsetPrivate: 248 return Utilities.loadImage("org/openide/src/resources/constructorPrivate.gif"); case iconMethod + offsetPublic: 250 return Utilities.loadImage("org/openide/src/resources/methodPublic.gif"); case iconMethod + offsetPackage: 252 return Utilities.loadImage("org/openide/src/resources/methodPackage.gif"); case iconMethod + offsetProtected: 254 return Utilities.loadImage("org/openide/src/resources/methodProtected.gif"); case iconMethod + offsetPrivate: 256 return Utilities.loadImage("org/openide/src/resources/methodPrivate.gif"); default: 258 return null; 259 } 260 } 261 262 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"); case 2: 271 return Utilities.loadImage("org/netbeans/modules/javadoc/comments/resources/missing.gif"); case 4: 273 return Utilities.loadImage("org/netbeans/modules/javadoc/comments/resources/error.gif"); 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 return access; 287 if (decl.isInterface()) 288 access = Modifier.PUBLIC; 290 291 int parentAccess = getEffectiveAccess(decl); 292 switch (parentAccess) { 293 case Modifier.PRIVATE: 294 case 0: 295 return parentAccess; 298 case Modifier.PROTECTED: 299 return access == 0 ? 0 : parentAccess; 301 case Modifier.PUBLIC: 302 default: 303 return access; 304 } 305 } 306 307 308 } 309 | Popular Tags |