1 11 package org.eclipse.jdt.internal.ui.viewsupport; 12 13 import org.eclipse.swt.graphics.Image; 14 import org.eclipse.swt.graphics.Point; 15 16 import org.eclipse.jface.resource.ImageDescriptor; 17 import org.eclipse.jface.viewers.LabelProvider; 18 19 import org.eclipse.jdt.core.dom.IBinding; 20 import org.eclipse.jdt.core.dom.IMethodBinding; 21 import org.eclipse.jdt.core.dom.IPackageBinding; 22 import org.eclipse.jdt.core.dom.ITypeBinding; 23 import org.eclipse.jdt.core.dom.IVariableBinding; 24 import org.eclipse.jdt.core.dom.Modifier; 25 26 import org.eclipse.jdt.internal.corext.util.Messages; 27 28 import org.eclipse.jdt.ui.JavaElementImageDescriptor; 29 import org.eclipse.jdt.ui.JavaElementLabels; 30 31 import org.eclipse.jdt.internal.ui.JavaPlugin; 32 import org.eclipse.jdt.internal.ui.JavaPluginImages; 33 import org.eclipse.jdt.internal.ui.JavaUIMessages; 34 35 40 public class BindingLabelProvider extends LabelProvider { 41 42 43 private static int getAdornmentFlags(IBinding binding, int flags) { 44 int adornments= 0; 45 if (binding instanceof IMethodBinding && ((IMethodBinding) binding).isConstructor()) 46 adornments|= JavaElementImageDescriptor.CONSTRUCTOR; 47 final int modifiers= binding.getModifiers(); 48 if (Modifier.isAbstract(modifiers)) 49 adornments|= JavaElementImageDescriptor.ABSTRACT; 50 if (Modifier.isFinal(modifiers)) 51 adornments|= JavaElementImageDescriptor.FINAL; 52 if (Modifier.isSynchronized(modifiers)) 53 adornments|= JavaElementImageDescriptor.SYNCHRONIZED; 54 if (Modifier.isStatic(modifiers)) 55 adornments|= JavaElementImageDescriptor.STATIC; 56 if (binding.isDeprecated()) 57 adornments|= JavaElementImageDescriptor.DEPRECATED; 58 if (binding instanceof IVariableBinding && ((IVariableBinding) binding).isField()) { 59 if (Modifier.isTransient(modifiers)) 60 adornments|= JavaElementImageDescriptor.TRANSIENT; 61 if (Modifier.isVolatile(modifiers)) 62 adornments|= JavaElementImageDescriptor.VOLATILE; 63 } 64 return adornments; 65 } 66 67 private static ImageDescriptor getBaseImageDescriptor(IBinding binding, int flags) { 68 if (binding instanceof ITypeBinding) { 69 ITypeBinding typeBinding= (ITypeBinding) binding; 70 if (typeBinding.isArray()) { 71 typeBinding= typeBinding.getElementType(); 72 } 73 if (typeBinding.isCapture()) { 74 typeBinding.getWildcard(); 75 } 76 return getTypeImageDescriptor(typeBinding.getDeclaringClass() != null, typeBinding, flags); 77 } else if (binding instanceof IMethodBinding) { 78 ITypeBinding type= ((IMethodBinding) binding).getDeclaringClass(); 79 int modifiers= binding.getModifiers(); 80 if (type.isEnum() && (!Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers)) && ((IMethodBinding) binding).isConstructor()) 81 return JavaPluginImages.DESC_MISC_PRIVATE; 82 return getMethodImageDescriptor(binding.getModifiers()); 83 } else if (binding instanceof IVariableBinding) 84 return getFieldImageDescriptor((IVariableBinding) binding); 85 return JavaPluginImages.DESC_OBJS_UNKNOWN; 86 } 87 88 private static ImageDescriptor getClassImageDescriptor(int modifiers) { 89 if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers) || Modifier.isPrivate(modifiers)) 90 return JavaPluginImages.DESC_OBJS_CLASS; 91 else 92 return JavaPluginImages.DESC_OBJS_CLASS_DEFAULT; 93 } 94 95 private static ImageDescriptor getFieldImageDescriptor(IVariableBinding binding) { 96 final int modifiers= binding.getModifiers(); 97 if (Modifier.isPublic(modifiers) || binding.isEnumConstant()) 98 return JavaPluginImages.DESC_FIELD_PUBLIC; 99 if (Modifier.isProtected(modifiers)) 100 return JavaPluginImages.DESC_FIELD_PROTECTED; 101 if (Modifier.isPrivate(modifiers)) 102 return JavaPluginImages.DESC_FIELD_PRIVATE; 103 104 return JavaPluginImages.DESC_FIELD_DEFAULT; 105 } 106 107 private static void getFieldLabel(IVariableBinding binding, long flags, StringBuffer buffer) { 108 if (((flags & JavaElementLabels.F_PRE_TYPE_SIGNATURE) != 0) && !binding.isEnumConstant()) { 109 getTypeLabel(binding.getType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 110 buffer.append(' '); 111 } 112 114 if ((flags & JavaElementLabels.F_FULLY_QUALIFIED) != 0) { 115 ITypeBinding declaringClass= binding.getDeclaringClass(); 116 if (declaringClass != null) { getTypeLabel(declaringClass, JavaElementLabels.T_FULLY_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer); 118 buffer.append('.'); 119 } 120 } 121 buffer.append(binding.getName()); 122 if (((flags & JavaElementLabels.F_APP_TYPE_SIGNATURE) != 0) && !binding.isEnumConstant()) { 123 buffer.append(JavaElementLabels.DECL_STRING); 124 getTypeLabel(binding.getType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 125 } 126 if ((flags & JavaElementLabels.F_POST_QUALIFIED) != 0) { 128 ITypeBinding declaringClass= binding.getDeclaringClass(); 129 if (declaringClass != null) { buffer.append(JavaElementLabels.CONCAT_STRING); 131 getTypeLabel(declaringClass, JavaElementLabels.T_FULLY_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer); 132 } 133 } 134 } 135 136 private static void getLocalVariableLabel(IVariableBinding binding, long flags, StringBuffer buffer) { 137 if (((flags & JavaElementLabels.F_PRE_TYPE_SIGNATURE) != 0)) { 138 getTypeLabel(binding.getType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 139 buffer.append(' '); 140 } 141 if (((flags & JavaElementLabels.F_FULLY_QUALIFIED) != 0)) { 142 IMethodBinding declaringMethod= binding.getDeclaringMethod(); 143 if (declaringMethod != null) { 144 getMethodLabel(declaringMethod, flags, buffer); 145 buffer.append('.'); 146 } 147 } 148 buffer.append(binding.getName()); 149 if (((flags & JavaElementLabels.F_APP_TYPE_SIGNATURE) != 0)) { 150 buffer.append(JavaElementLabels.DECL_STRING); 151 getTypeLabel(binding.getType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 152 } 153 } 154 155 private static ImageDescriptor getInnerClassImageDescriptor(int modifiers) { 156 if (Modifier.isPublic(modifiers)) 157 return JavaPluginImages.DESC_OBJS_INNER_CLASS_PUBLIC; 158 else if (Modifier.isPrivate(modifiers)) 159 return JavaPluginImages.DESC_OBJS_INNER_CLASS_PRIVATE; 160 else if (Modifier.isProtected(modifiers)) 161 return JavaPluginImages.DESC_OBJS_INNER_CLASS_PROTECTED; 162 else 163 return JavaPluginImages.DESC_OBJS_INNER_CLASS_DEFAULT; 164 } 165 166 private static ImageDescriptor getInnerInterfaceImageDescriptor(int modifiers) { 167 if (Modifier.isPublic(modifiers)) 168 return JavaPluginImages.DESC_OBJS_INNER_INTERFACE_PUBLIC; 169 else if (Modifier.isPrivate(modifiers)) 170 return JavaPluginImages.DESC_OBJS_INNER_INTERFACE_PRIVATE; 171 else if (Modifier.isProtected(modifiers)) 172 return JavaPluginImages.DESC_OBJS_INNER_INTERFACE_PROTECTED; 173 else 174 return JavaPluginImages.DESC_OBJS_INTERFACE_DEFAULT; 175 } 176 177 private static ImageDescriptor getInterfaceImageDescriptor(int modifiers) { 178 if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers) || Modifier.isPrivate(modifiers)) 179 return JavaPluginImages.DESC_OBJS_INTERFACE; 180 else 181 return JavaPluginImages.DESC_OBJS_INTERFACE_DEFAULT; 182 } 183 184 private static ImageDescriptor getMethodImageDescriptor(int modifiers) { 185 if (Modifier.isPublic(modifiers)) 186 return JavaPluginImages.DESC_MISC_PUBLIC; 187 if (Modifier.isProtected(modifiers)) 188 return JavaPluginImages.DESC_MISC_PROTECTED; 189 if (Modifier.isPrivate(modifiers)) 190 return JavaPluginImages.DESC_MISC_PRIVATE; 191 192 return JavaPluginImages.DESC_MISC_DEFAULT; 193 } 194 195 private static void appendDimensions(int dim, StringBuffer buffer) { 196 for (int i=0 ; i < dim; i++) { 197 buffer.append('[').append(']'); 198 } 199 } 200 201 202 private static void getMethodLabel(IMethodBinding binding, long flags, StringBuffer buffer) { 203 if ((flags & JavaElementLabels.M_PRE_TYPE_PARAMETERS) != 0) { 205 if (binding.isGenericMethod()) { 206 ITypeBinding[] typeParameters= binding.getTypeParameters(); 207 if (typeParameters.length > 0) { 208 getTypeParametersLabel(typeParameters, (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 209 buffer.append(' '); 210 } 211 } 212 } 213 if (((flags & JavaElementLabels.M_PRE_RETURNTYPE) != 0) && !binding.isConstructor()) { 215 getTypeLabel(binding.getReturnType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 216 buffer.append(' '); 217 } 218 if ((flags & JavaElementLabels.M_FULLY_QUALIFIED) != 0) { 220 getTypeLabel(binding.getDeclaringClass(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer); 221 buffer.append('.'); 222 } 223 buffer.append(binding.getName()); 224 if ((flags & JavaElementLabels.M_APP_TYPE_PARAMETERS) != 0) { 225 if (binding.isParameterizedMethod()) { 226 ITypeBinding[] typeArguments= binding.getTypeArguments(); 227 if (typeArguments.length > 0) { 228 buffer.append(' '); 229 getTypeArgumentsLabel(typeArguments, (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 230 } 231 } 232 } 233 234 235 buffer.append('('); 237 if ((flags & JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES) != 0) { 238 ITypeBinding[] parameters= ((flags & JavaElementLabels.M_PARAMETER_TYPES) != 0) ? binding.getParameterTypes() : null; 239 if (parameters != null) { 240 for (int index= 0; index < parameters.length; index++) { 241 if (index > 0) { 242 buffer.append(JavaElementLabels.COMMA_STRING); 243 } 244 ITypeBinding paramType= parameters[index]; 245 if (binding.isVarargs() && (index == parameters.length - 1)) { 246 getTypeLabel(paramType.getElementType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 247 appendDimensions(paramType.getDimensions() - 1, buffer); 248 buffer.append(JavaElementLabels.ELLIPSIS_STRING); 249 } else { 250 getTypeLabel(paramType, (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 251 } 252 } 253 } 254 } else { 255 if (binding.getParameterTypes().length > 0) { 256 buffer.append(JavaElementLabels.ELLIPSIS_STRING); 257 } 258 } 259 buffer.append(')'); 260 261 if ((flags & JavaElementLabels.M_EXCEPTIONS) != 0) { 262 ITypeBinding[] exceptions= binding.getExceptionTypes(); 263 if (exceptions.length > 0) { 264 buffer.append(" throws "); for (int index= 0; index < exceptions.length; index++) { 266 if (index > 0) { 267 buffer.append(JavaElementLabels.COMMA_STRING); 268 } 269 getTypeLabel(exceptions[index], (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 270 } 271 } 272 } 273 if ((flags & JavaElementLabels.M_APP_TYPE_PARAMETERS) != 0) { 274 if (binding.isGenericMethod()) { 275 ITypeBinding[] typeParameters= binding.getTypeParameters(); 276 if (typeParameters.length > 0) { 277 buffer.append(' '); 278 getTypeParametersLabel(typeParameters, (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 279 } 280 } 281 } 282 if (((flags & JavaElementLabels.M_APP_RETURNTYPE) != 0) && !binding.isConstructor()) { 283 buffer.append(JavaElementLabels.DECL_STRING); 284 getTypeLabel(binding.getReturnType(), (flags & JavaElementLabels.T_TYPE_PARAMETERS), buffer); 285 } 286 if ((flags & JavaElementLabels.M_POST_QUALIFIED) != 0) { 288 buffer.append(JavaElementLabels.CONCAT_STRING); 289 getTypeLabel(binding.getDeclaringClass(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer); 290 } 291 } 292 293 private static ImageDescriptor getTypeImageDescriptor(boolean inner, ITypeBinding binding, int flags) { 294 if (binding.isEnum()) 295 return JavaPluginImages.DESC_OBJS_ENUM; 296 else if (binding.isAnnotation()) 297 return JavaPluginImages.DESC_OBJS_ANNOTATION; 298 else if (binding.isInterface()) { 299 if ((flags & JavaElementImageProvider.LIGHT_TYPE_ICONS) != 0) 300 return JavaPluginImages.DESC_OBJS_INTERFACEALT; 301 if (inner) 302 return getInnerInterfaceImageDescriptor(binding.getModifiers()); 303 return getInterfaceImageDescriptor(binding.getModifiers()); 304 } else if (binding.isClass()) { 305 if ((flags & JavaElementImageProvider.LIGHT_TYPE_ICONS) != 0) 306 return JavaPluginImages.DESC_OBJS_CLASSALT; 307 if (inner) 308 return getInnerClassImageDescriptor(binding.getModifiers()); 309 return getClassImageDescriptor(binding.getModifiers()); 310 } else if (binding.isTypeVariable()) { 311 return JavaPluginImages.DESC_OBJS_TYPEVARIABLE; 312 } 313 return null; 315 } 316 317 318 private static void getTypeLabel(ITypeBinding binding, long flags, StringBuffer buffer) { 319 if ((flags & JavaElementLabels.T_FULLY_QUALIFIED) != 0) { 320 final IPackageBinding pack= binding.getPackage(); 321 if (pack != null && !pack.isUnnamed()) { 322 buffer.append(pack.getName()); 323 buffer.append('.'); 324 } 325 } 326 if ((flags & (JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.T_CONTAINER_QUALIFIED)) != 0) { 327 final ITypeBinding declaring= binding.getDeclaringClass(); 328 if (declaring != null) { 329 getTypeLabel(declaring, JavaElementLabels.T_CONTAINER_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer); 330 buffer.append('.'); 331 } 332 final IMethodBinding declaringMethod= binding.getDeclaringMethod(); 333 if (declaringMethod != null) { 334 getMethodLabel(declaringMethod, 0, buffer); 335 buffer.append('.'); 336 } 337 } 338 339 if (binding.isCapture()) { 340 getTypeLabel(binding.getWildcard(), flags & JavaElementLabels.T_TYPE_PARAMETERS, buffer); 341 } else if (binding.isWildcardType()) { 342 buffer.append('?'); 343 ITypeBinding bound= binding.getBound(); 344 if (bound != null) { 345 if (binding.isUpperbound()) { 346 buffer.append(" extends "); } else { 348 buffer.append(" super "); } 350 getTypeLabel(bound, flags & JavaElementLabels.T_TYPE_PARAMETERS, buffer); 351 } 352 } else if (binding.isArray()) { 353 getTypeLabel(binding.getElementType(), flags & JavaElementLabels.T_TYPE_PARAMETERS, buffer); 354 appendDimensions(binding.getDimensions(), buffer); 355 } else { String name= binding.getTypeDeclaration().getName(); 357 if (name.length() == 0) { 358 if (binding.isEnum()) { 359 buffer.append('{' + JavaElementLabels.ELLIPSIS_STRING + '}'); 360 } else if (binding.isAnonymous()) { 361 ITypeBinding[] superInterfaces= binding.getInterfaces(); 362 ITypeBinding baseType; 363 if (superInterfaces.length > 0) { 364 baseType= superInterfaces[0]; 365 } else { 366 baseType= binding.getSuperclass(); 367 } 368 if (baseType != null) { 369 StringBuffer anonymBaseType= new StringBuffer (); 370 getTypeLabel(baseType, flags & JavaElementLabels.T_TYPE_PARAMETERS, anonymBaseType); 371 buffer.append(Messages.format(JavaUIMessages.JavaElementLabels_anonym_type, anonymBaseType.toString())); 372 } else { 373 buffer.append(JavaUIMessages.JavaElementLabels_anonym); 374 } 375 } else { 376 buffer.append("UNKNOWN"); } 378 } else { 379 buffer.append(name); 380 } 381 382 if ((flags & JavaElementLabels.T_TYPE_PARAMETERS) != 0) { 383 if (binding.isGenericType()) { 384 getTypeParametersLabel(binding.getTypeParameters(), flags, buffer); 385 } else if (binding.isParameterizedType()) { 386 getTypeArgumentsLabel(binding.getTypeArguments(), flags, buffer); 387 } 388 } 389 } 390 391 392 if ((flags & JavaElementLabels.T_POST_QUALIFIED) != 0) { 393 final IMethodBinding declaringMethod= binding.getDeclaringMethod(); 394 final ITypeBinding declaringType= binding.getDeclaringClass(); 395 if (declaringMethod != null) { 396 buffer.append(JavaElementLabels.CONCAT_STRING); 397 getMethodLabel(declaringMethod, JavaElementLabels.T_FULLY_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer); 398 } else if (declaringType != null) { 399 buffer.append(JavaElementLabels.CONCAT_STRING); 400 getTypeLabel(declaringType, JavaElementLabels.T_FULLY_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer); 401 } else { 402 final IPackageBinding pack= binding.getPackage(); 403 if (pack != null && !pack.isUnnamed()) { 404 buffer.append(JavaElementLabels.CONCAT_STRING); 405 buffer.append(pack.getName()); 406 } 407 } 408 } 409 } 410 411 private static void getTypeArgumentsLabel(ITypeBinding[] typeArgs, long flags, StringBuffer buf) { 412 if (typeArgs.length > 0) { 413 buf.append('<'); 414 for (int i = 0; i < typeArgs.length; i++) { 415 if (i > 0) { 416 buf.append(JavaElementLabels.COMMA_STRING); 417 } 418 getTypeLabel(typeArgs[i], flags & JavaElementLabels.T_TYPE_PARAMETERS, buf); 419 } 420 buf.append('>'); 421 } 422 } 423 424 425 private static void getTypeParametersLabel(ITypeBinding[] typeParameters, long flags, StringBuffer buffer) { 426 if (typeParameters.length > 0) { 427 buffer.append('<'); 428 for (int index= 0; index < typeParameters.length; index++) { 429 if (index > 0) { 430 buffer.append(JavaElementLabels.COMMA_STRING); 431 } 432 buffer.append(typeParameters[index].getName()); 433 } 434 buffer.append('>'); 435 } 436 } 437 438 444 public static String getBindingLabel(IBinding binding, long flags) { 445 StringBuffer buffer= new StringBuffer (60); 446 if (binding instanceof ITypeBinding) { 447 getTypeLabel(((ITypeBinding) binding), flags, buffer); 448 } else if (binding instanceof IMethodBinding) { 449 getMethodLabel(((IMethodBinding) binding), flags, buffer); 450 } else if (binding instanceof IVariableBinding) { 451 final IVariableBinding variable= (IVariableBinding) binding; 452 if (variable.isField()) 453 getFieldLabel(variable, flags, buffer); 454 else 455 getLocalVariableLabel(variable, flags, buffer); 456 } 457 return buffer.toString(); 458 } 459 460 466 public static ImageDescriptor getBindingImageDescriptor(IBinding binding, int imageFlags) { 467 ImageDescriptor baseImage= getBaseImageDescriptor(binding, imageFlags); 468 if (baseImage != null) { 469 int adornmentFlags= getAdornmentFlags(binding, imageFlags); 470 Point size= ((imageFlags & JavaElementImageProvider.SMALL_ICONS) != 0) ? JavaElementImageProvider.SMALL_SIZE : JavaElementImageProvider.BIG_SIZE; 471 return new JavaElementImageDescriptor(baseImage, adornmentFlags, size); 472 } 473 return null; 474 } 475 476 477 public static final long DEFAULT_TEXTFLAGS= JavaElementLabels.ALL_DEFAULT; 478 public static final int DEFAULT_IMAGEFLAGS= JavaElementImageProvider.OVERLAY_ICONS; 479 480 481 final private long fTextFlags; 482 final private int fImageFlags; 483 484 private ImageDescriptorRegistry fRegistry; 485 486 489 public BindingLabelProvider() { 490 this(DEFAULT_TEXTFLAGS, DEFAULT_IMAGEFLAGS); 491 } 492 493 497 public BindingLabelProvider(final long textFlags, final int imageFlags) { 498 fImageFlags= imageFlags; 499 fTextFlags= textFlags; 500 fRegistry= null; 501 } 502 503 506 public Image getImage(Object element) { 507 if (element instanceof IBinding) { 508 ImageDescriptor baseImage= getBindingImageDescriptor((IBinding) element, fImageFlags); 509 if (baseImage != null) { 510 return getRegistry().get(baseImage); 511 } 512 } 513 return super.getImage(element); 514 } 515 516 private ImageDescriptorRegistry getRegistry() { 517 if (fRegistry == null) 518 fRegistry= JavaPlugin.getImageDescriptorRegistry(); 519 return fRegistry; 520 } 521 522 525 public String getText(Object element) { 526 if (element instanceof IBinding) { 527 return getBindingLabel((IBinding) element, fTextFlags); 528 } 529 return super.getText(element); 530 } 531 } 532 | Popular Tags |