1 12 package org.eclipse.jdt.internal.ui.viewsupport; 13 14 import org.eclipse.core.runtime.IAdaptable; 15 import org.eclipse.core.runtime.IPath; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IProject; 19 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.swt.graphics.Point; 22 23 import org.eclipse.jface.resource.ImageDescriptor; 24 25 import org.eclipse.ui.ISharedImages; 26 import org.eclipse.ui.model.IWorkbenchAdapter; 27 28 import org.eclipse.ui.ide.IDE; 29 30 import org.eclipse.jdt.core.Flags; 31 import org.eclipse.jdt.core.IJavaElement; 32 import org.eclipse.jdt.core.IJavaProject; 33 import org.eclipse.jdt.core.IMember; 34 import org.eclipse.jdt.core.IMethod; 35 import org.eclipse.jdt.core.IPackageFragment; 36 import org.eclipse.jdt.core.IPackageFragmentRoot; 37 import org.eclipse.jdt.core.IType; 38 import org.eclipse.jdt.core.JavaCore; 39 import org.eclipse.jdt.core.JavaModelException; 40 41 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 42 43 import org.eclipse.jdt.ui.JavaElementImageDescriptor; 44 45 import org.eclipse.jdt.internal.ui.JavaPlugin; 46 import org.eclipse.jdt.internal.ui.JavaPluginImages; 47 import org.eclipse.jdt.internal.ui.JavaWorkbenchAdapter; 48 49 52 public class JavaElementImageProvider { 53 54 58 public final static int OVERLAY_ICONS= 0x1; 59 60 63 public final static int SMALL_ICONS= 0x2; 64 65 68 public final static int LIGHT_TYPE_ICONS= 0x4; 69 70 71 public static final Point SMALL_SIZE= new Point(16, 16); 72 public static final Point BIG_SIZE= new Point(22, 16); 73 74 private static ImageDescriptor DESC_OBJ_PROJECT_CLOSED; 75 private static ImageDescriptor DESC_OBJ_PROJECT; 76 { 77 ISharedImages images= JavaPlugin.getDefault().getWorkbench().getSharedImages(); 78 DESC_OBJ_PROJECT_CLOSED= images.getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT_CLOSED); 79 DESC_OBJ_PROJECT= images.getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT); 80 } 81 82 private ImageDescriptorRegistry fRegistry; 83 84 public JavaElementImageProvider() { 85 fRegistry= null; } 87 88 94 public Image getImageLabel(Object element, int flags) { 95 return getImageLabel(computeDescriptor(element, flags)); 96 } 97 98 private Image getImageLabel(ImageDescriptor descriptor){ 99 if (descriptor == null) 100 return null; 101 return getRegistry().get(descriptor); 102 } 103 104 private ImageDescriptorRegistry getRegistry() { 105 if (fRegistry == null) { 106 fRegistry= JavaPlugin.getImageDescriptorRegistry(); 107 } 108 return fRegistry; 109 } 110 111 112 private ImageDescriptor computeDescriptor(Object element, int flags){ 113 if (element instanceof IJavaElement) { 114 return getJavaImageDescriptor((IJavaElement) element, flags); 115 } else if (element instanceof IFile) { 116 IFile file= (IFile) element; 117 if (JavaCore.isJavaLikeFileName(file.getName())) { 118 return getCUResourceImageDescriptor(file, flags); } 120 return getWorkbenchImageDescriptor(file, flags); 121 } else if (element instanceof IAdaptable) { 122 return getWorkbenchImageDescriptor((IAdaptable) element, flags); 123 } 124 return null; 125 } 126 127 private static boolean showOverlayIcons(int flags) { 128 return (flags & OVERLAY_ICONS) != 0; 129 } 130 131 private static boolean useSmallSize(int flags) { 132 return (flags & SMALL_ICONS) != 0; 133 } 134 135 private static boolean useLightIcons(int flags) { 136 return (flags & LIGHT_TYPE_ICONS) != 0; 137 } 138 139 143 public ImageDescriptor getCUResourceImageDescriptor(IFile file, int flags) { 144 Point size= useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE; 145 return new JavaElementImageDescriptor(JavaPluginImages.DESC_OBJS_CUNIT_RESOURCE, 0, size); 146 } 147 148 151 public ImageDescriptor getJavaImageDescriptor(IJavaElement element, int flags) { 152 Point size= useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE; 153 154 ImageDescriptor baseDesc= getBaseImageDescriptor(element, flags); 155 if (baseDesc != null) { 156 int adornmentFlags= computeJavaAdornmentFlags(element, flags); 157 return new JavaElementImageDescriptor(baseDesc, adornmentFlags, size); 158 } 159 return new JavaElementImageDescriptor(JavaPluginImages.DESC_OBJS_GHOST, 0, size); 160 } 161 162 166 public ImageDescriptor getWorkbenchImageDescriptor(IAdaptable adaptable, int flags) { 167 IWorkbenchAdapter wbAdapter= (IWorkbenchAdapter) adaptable.getAdapter(IWorkbenchAdapter.class); 168 if (wbAdapter == null) { 169 return null; 170 } 171 ImageDescriptor descriptor= wbAdapter.getImageDescriptor(adaptable); 172 if (descriptor == null) { 173 return null; 174 } 175 176 Point size= useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE; 177 return new JavaElementImageDescriptor(descriptor, 0, size); 178 } 179 180 182 185 public ImageDescriptor getBaseImageDescriptor(IJavaElement element, int renderFlags) { 186 187 try { 188 switch (element.getElementType()) { 189 case IJavaElement.INITIALIZER: 190 return JavaPluginImages.DESC_MISC_PRIVATE; case IJavaElement.METHOD: { 192 IMethod method= (IMethod) element; 193 IType declType= method.getDeclaringType(); 194 int flags= method.getFlags(); 195 if (declType.isEnum() && isDefaultFlag(flags) && method.isConstructor()) 196 return JavaPluginImages.DESC_MISC_PRIVATE; 197 return getMethodImageDescriptor(JavaModelUtil.isInterfaceOrAnnotation(declType), flags); 198 } 199 case IJavaElement.FIELD: { 200 IMember member= (IMember) element; 201 IType declType= member.getDeclaringType(); 202 return getFieldImageDescriptor(JavaModelUtil.isInterfaceOrAnnotation(declType), member.getFlags()); 203 } 204 case IJavaElement.LOCAL_VARIABLE: 205 return JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE; 206 207 case IJavaElement.PACKAGE_DECLARATION: 208 return JavaPluginImages.DESC_OBJS_PACKDECL; 209 210 case IJavaElement.IMPORT_DECLARATION: 211 return JavaPluginImages.DESC_OBJS_IMPDECL; 212 213 case IJavaElement.IMPORT_CONTAINER: 214 return JavaPluginImages.DESC_OBJS_IMPCONT; 215 216 case IJavaElement.TYPE: { 217 IType type= (IType) element; 218 219 IType declType= type.getDeclaringType(); 220 boolean isInner= declType != null; 221 boolean isInInterfaceOrAnnotation= isInner && JavaModelUtil.isInterfaceOrAnnotation(declType); 222 return getTypeImageDescriptor(isInner, isInInterfaceOrAnnotation, type.getFlags(), useLightIcons(renderFlags)); 223 } 224 225 case IJavaElement.PACKAGE_FRAGMENT_ROOT: { 226 IPackageFragmentRoot root= (IPackageFragmentRoot) element; 227 if (root.isArchive()) { 228 IPath attach= root.getSourceAttachmentPath(); 229 if (root.isExternal()) { 230 if (attach == null) { 231 return JavaPluginImages.DESC_OBJS_EXTJAR; 232 } else { 233 return JavaPluginImages.DESC_OBJS_EXTJAR_WSRC; 234 } 235 } else { 236 if (attach == null) { 237 return JavaPluginImages.DESC_OBJS_JAR; 238 } else { 239 return JavaPluginImages.DESC_OBJS_JAR_WSRC; 240 } 241 } 242 } else { 243 return JavaPluginImages.DESC_OBJS_PACKFRAG_ROOT; 244 } 245 } 246 247 case IJavaElement.PACKAGE_FRAGMENT: 248 return getPackageFragmentIcon(element, renderFlags); 249 250 251 case IJavaElement.COMPILATION_UNIT: 252 return JavaPluginImages.DESC_OBJS_CUNIT; 253 254 case IJavaElement.CLASS_FILE: 255 264 return JavaPluginImages.DESC_OBJS_CFILE; 265 266 case IJavaElement.JAVA_PROJECT: 267 IJavaProject jp= (IJavaProject)element; 268 if (jp.getProject().isOpen()) { 269 IProject project= jp.getProject(); 270 IWorkbenchAdapter adapter= (IWorkbenchAdapter)project.getAdapter(IWorkbenchAdapter.class); 271 if (adapter != null) { 272 ImageDescriptor result= adapter.getImageDescriptor(project); 273 if (result != null) 274 return result; 275 } 276 return DESC_OBJ_PROJECT; 277 } 278 return DESC_OBJ_PROJECT_CLOSED; 279 280 case IJavaElement.JAVA_MODEL: 281 return JavaPluginImages.DESC_OBJS_JAVA_MODEL; 282 283 case IJavaElement.TYPE_PARAMETER: 284 return JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE; 285 286 default: 287 IWorkbenchAdapter wbAdapter= (IWorkbenchAdapter) element.getAdapter(IWorkbenchAdapter.class); 290 if (wbAdapter != null && !(wbAdapter instanceof JavaWorkbenchAdapter)) { ImageDescriptor imageDescriptor= wbAdapter.getImageDescriptor(element); 292 if (imageDescriptor != null) { 293 return imageDescriptor; 294 } 295 } 296 return JavaPluginImages.DESC_OBJS_GHOST; 297 } 298 299 } catch (JavaModelException e) { 300 if (e.isDoesNotExist()) 301 return JavaPluginImages.DESC_OBJS_UNKNOWN; 302 JavaPlugin.log(e); 303 return JavaPluginImages.DESC_OBJS_GHOST; 304 } 305 } 306 307 private static boolean isDefaultFlag(int flags) { 308 return !Flags.isPublic(flags) && !Flags.isProtected(flags) && !Flags.isPrivate(flags); 309 } 310 311 protected ImageDescriptor getPackageFragmentIcon(IJavaElement element, int renderFlags) throws JavaModelException { 312 IPackageFragment fragment= (IPackageFragment)element; 313 boolean containsJavaElements= false; 314 try { 315 containsJavaElements= fragment.hasChildren(); 316 } catch(JavaModelException e) { 317 } 319 if(!containsJavaElements && (fragment.getNonJavaResources().length > 0)) 320 return JavaPluginImages.DESC_OBJS_EMPTY_PACKAGE_RESOURCES; 321 else if (!containsJavaElements) 322 return JavaPluginImages.DESC_OBJS_EMPTY_PACKAGE; 323 return JavaPluginImages.DESC_OBJS_PACKAGE; 324 } 325 326 public void dispose() { 327 } 328 329 331 private int computeJavaAdornmentFlags(IJavaElement element, int renderFlags) { 332 int flags= 0; 333 if (showOverlayIcons(renderFlags) && element instanceof IMember) { 334 try { 335 IMember member= (IMember) element; 336 337 if (element.getElementType() == IJavaElement.METHOD && ((IMethod)element).isConstructor()) 338 flags |= JavaElementImageDescriptor.CONSTRUCTOR; 339 340 int modifiers= member.getFlags(); 341 if (Flags.isAbstract(modifiers) && confirmAbstract(member)) 342 flags |= JavaElementImageDescriptor.ABSTRACT; 343 if (Flags.isFinal(modifiers) || isInterfaceOrAnnotationField(member) || isEnumConstant(member, modifiers)) 344 flags |= JavaElementImageDescriptor.FINAL; 345 if (Flags.isSynchronized(modifiers) && confirmSynchronized(member)) 346 flags |= JavaElementImageDescriptor.SYNCHRONIZED; 347 if (Flags.isStatic(modifiers) || isInterfaceOrAnnotationFieldOrType(member) || isEnumConstant(member, modifiers)) 348 flags |= JavaElementImageDescriptor.STATIC; 349 350 if (Flags.isDeprecated(modifiers)) 351 flags |= JavaElementImageDescriptor.DEPRECATED; 352 if (member.getElementType() == IJavaElement.TYPE) { 353 if (JavaModelUtil.hasMainMethod((IType) member)) { 354 flags |= JavaElementImageDescriptor.RUNNABLE; 355 } 356 } 357 if (member.getElementType() == IJavaElement.FIELD) { 358 if (Flags.isVolatile(modifiers)) 359 flags |= JavaElementImageDescriptor.VOLATILE; 360 if (Flags.isTransient(modifiers)) 361 flags |= JavaElementImageDescriptor.TRANSIENT; 362 } 363 364 365 } catch (JavaModelException e) { 366 } 368 } 369 return flags; 370 } 371 372 private static boolean confirmAbstract(IMember element) throws JavaModelException { 373 if (element.getElementType() == IJavaElement.TYPE) { 375 return ! JavaModelUtil.isInterfaceOrAnnotation((IType) element); 376 } 377 return ! JavaModelUtil.isInterfaceOrAnnotation(element.getDeclaringType()); 378 } 379 380 private static boolean isInterfaceOrAnnotationField(IMember element) throws JavaModelException { 381 if (element.getElementType() == IJavaElement.FIELD) { 383 return JavaModelUtil.isInterfaceOrAnnotation(element.getDeclaringType()); 384 } 385 return false; 386 } 387 388 private static boolean isInterfaceOrAnnotationFieldOrType(IMember element) throws JavaModelException { 389 if (element.getElementType() == IJavaElement.FIELD) { 391 return JavaModelUtil.isInterfaceOrAnnotation(element.getDeclaringType()); 392 } else if (element.getElementType() == IJavaElement.TYPE && element.getDeclaringType() != null) { 393 return JavaModelUtil.isInterfaceOrAnnotation(element.getDeclaringType()); 394 } 395 return false; 396 } 397 398 private static boolean isEnumConstant(IMember element, int modifiers) throws JavaModelException { 399 if (element.getElementType() == IJavaElement.FIELD) { 400 return Flags.isEnum(modifiers); 401 } 402 return false; 403 } 404 405 private static boolean confirmSynchronized(IJavaElement member) { 406 return member.getElementType() != IJavaElement.TYPE; 408 } 409 410 411 public static ImageDescriptor getMethodImageDescriptor(boolean isInInterfaceOrAnnotation, int flags) { 412 if (Flags.isPublic(flags) || isInInterfaceOrAnnotation) 413 return JavaPluginImages.DESC_MISC_PUBLIC; 414 if (Flags.isProtected(flags)) 415 return JavaPluginImages.DESC_MISC_PROTECTED; 416 if (Flags.isPrivate(flags)) 417 return JavaPluginImages.DESC_MISC_PRIVATE; 418 419 return JavaPluginImages.DESC_MISC_DEFAULT; 420 } 421 422 public static ImageDescriptor getFieldImageDescriptor(boolean isInInterfaceOrAnnotation, int flags) { 423 if (Flags.isPublic(flags) || isInInterfaceOrAnnotation || Flags.isEnum(flags)) 424 return JavaPluginImages.DESC_FIELD_PUBLIC; 425 if (Flags.isProtected(flags)) 426 return JavaPluginImages.DESC_FIELD_PROTECTED; 427 if (Flags.isPrivate(flags)) 428 return JavaPluginImages.DESC_FIELD_PRIVATE; 429 430 return JavaPluginImages.DESC_FIELD_DEFAULT; 431 } 432 433 436 public static ImageDescriptor getTypeImageDescriptor(boolean isInterface, boolean isInner, boolean isInInterface, int flags) { 437 return getTypeImageDescriptor(isInner, isInInterface, isInterface ? flags | Flags.AccInterface : flags, false); 438 } 439 440 public static ImageDescriptor getTypeImageDescriptor(boolean isInner, boolean isInInterfaceOrAnnotation, int flags, boolean useLightIcons) { 441 if (Flags.isEnum(flags)) { 442 if (useLightIcons) { 443 return JavaPluginImages.DESC_OBJS_ENUM_ALT; 444 } 445 if (isInner) { 446 return getInnerEnumImageDescriptor(isInInterfaceOrAnnotation, flags); 447 } 448 return getEnumImageDescriptor(flags); 449 } else if (Flags.isAnnotation(flags)) { 450 if (useLightIcons) { 451 return JavaPluginImages.DESC_OBJS_ANNOTATION_ALT; 452 } 453 if (isInner) { 454 return getInnerAnnotationImageDescriptor(isInInterfaceOrAnnotation, flags); 455 } 456 return getAnnotationImageDescriptor(flags); 457 } else if (Flags.isInterface(flags)) { 458 if (useLightIcons) { 459 return JavaPluginImages.DESC_OBJS_INTERFACEALT; 460 } 461 if (isInner) { 462 return getInnerInterfaceImageDescriptor(isInInterfaceOrAnnotation, flags); 463 } 464 return getInterfaceImageDescriptor(flags); 465 } else { 466 if (useLightIcons) { 467 return JavaPluginImages.DESC_OBJS_CLASSALT; 468 } 469 if (isInner) { 470 return getInnerClassImageDescriptor(isInInterfaceOrAnnotation, flags); 471 } 472 return getClassImageDescriptor(flags); 473 } 474 } 475 476 477 public static Image getDecoratedImage(ImageDescriptor baseImage, int adornments, Point size) { 478 return JavaPlugin.getImageDescriptorRegistry().get(new JavaElementImageDescriptor(baseImage, adornments, size)); 479 } 480 481 482 private static ImageDescriptor getClassImageDescriptor(int flags) { 483 if (Flags.isPublic(flags) || Flags.isProtected(flags) || Flags.isPrivate(flags)) 484 return JavaPluginImages.DESC_OBJS_CLASS; 485 else 486 return JavaPluginImages.DESC_OBJS_CLASS_DEFAULT; 487 } 488 489 private static ImageDescriptor getInnerClassImageDescriptor(boolean isInInterfaceOrAnnotation, int flags) { 490 if (Flags.isPublic(flags) || isInInterfaceOrAnnotation) 491 return JavaPluginImages.DESC_OBJS_INNER_CLASS_PUBLIC; 492 else if (Flags.isPrivate(flags)) 493 return JavaPluginImages.DESC_OBJS_INNER_CLASS_PRIVATE; 494 else if (Flags.isProtected(flags)) 495 return JavaPluginImages.DESC_OBJS_INNER_CLASS_PROTECTED; 496 else 497 return JavaPluginImages.DESC_OBJS_INNER_CLASS_DEFAULT; 498 } 499 500 private static ImageDescriptor getEnumImageDescriptor(int flags) { 501 if (Flags.isPublic(flags) || Flags.isProtected(flags) || Flags.isPrivate(flags)) 502 return JavaPluginImages.DESC_OBJS_ENUM; 503 else 504 return JavaPluginImages.DESC_OBJS_ENUM_DEFAULT; 505 } 506 507 private static ImageDescriptor getInnerEnumImageDescriptor(boolean isInInterfaceOrAnnotation, int flags) { 508 if (Flags.isPublic(flags) || isInInterfaceOrAnnotation) 509 return JavaPluginImages.DESC_OBJS_ENUM; 510 else if (Flags.isPrivate(flags)) 511 return JavaPluginImages.DESC_OBJS_ENUM_PRIVATE; 512 else if (Flags.isProtected(flags)) 513 return JavaPluginImages.DESC_OBJS_ENUM_PROTECTED; 514 else 515 return JavaPluginImages.DESC_OBJS_ENUM_DEFAULT; 516 } 517 518 private static ImageDescriptor getAnnotationImageDescriptor(int flags) { 519 if (Flags.isPublic(flags) || Flags.isProtected(flags) || Flags.isPrivate(flags)) 520 return JavaPluginImages.DESC_OBJS_ANNOTATION; 521 else 522 return JavaPluginImages.DESC_OBJS_ANNOTATION_DEFAULT; 523 } 524 525 private static ImageDescriptor getInnerAnnotationImageDescriptor(boolean isInInterfaceOrAnnotation, int flags) { 526 if (Flags.isPublic(flags) || isInInterfaceOrAnnotation) 527 return JavaPluginImages.DESC_OBJS_ANNOTATION; 528 else if (Flags.isPrivate(flags)) 529 return JavaPluginImages.DESC_OBJS_ANNOTATION_PRIVATE; 530 else if (Flags.isProtected(flags)) 531 return JavaPluginImages.DESC_OBJS_ANNOTATION_PROTECTED; 532 else 533 return JavaPluginImages.DESC_OBJS_ANNOTATION_DEFAULT; 534 } 535 536 private static ImageDescriptor getInterfaceImageDescriptor(int flags) { 537 if (Flags.isPublic(flags) || Flags.isProtected(flags) || Flags.isPrivate(flags)) 538 return JavaPluginImages.DESC_OBJS_INTERFACE; 539 else 540 return JavaPluginImages.DESC_OBJS_INTERFACE_DEFAULT; 541 } 542 543 private static ImageDescriptor getInnerInterfaceImageDescriptor(boolean isInInterfaceOrAnnotation, int flags) { 544 if (Flags.isPublic(flags) || isInInterfaceOrAnnotation) 545 return JavaPluginImages.DESC_OBJS_INNER_INTERFACE_PUBLIC; 546 else if (Flags.isPrivate(flags)) 547 return JavaPluginImages.DESC_OBJS_INNER_INTERFACE_PRIVATE; 548 else if (Flags.isProtected(flags)) 549 return JavaPluginImages.DESC_OBJS_INNER_INTERFACE_PROTECTED; 550 else 551 return JavaPluginImages.DESC_OBJS_INTERFACE_DEFAULT; 552 } 553 } 554 | Popular Tags |