1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.viewsupport;
13 import net.sourceforge.phpdt.core.Flags;
14 import net.sourceforge.phpdt.core.IField;
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IJavaProject;
17 import net.sourceforge.phpdt.core.IMember;
18 import net.sourceforge.phpdt.core.IMethod;
19 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
20 import net.sourceforge.phpdt.core.IType;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
23 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
24 import net.sourceforge.phpdt.ui.JavaElementImageDescriptor;
25 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
26 import net.sourceforge.phpeclipse.ui.WebUI;
28 import org.eclipse.core.resources.IFile;
29 import org.eclipse.core.resources.IProject;
30 import org.eclipse.core.runtime.IAdaptable;
31 import org.eclipse.jface.resource.ImageDescriptor;
32 import org.eclipse.jface.util.Assert;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.ui.ISharedImages;
36 import org.eclipse.ui.ide.IDE.SharedImages;
37 import org.eclipse.ui.model.IWorkbenchAdapter;
40 * Default strategy of the Java plugin for the construction of Java element
43 public class JavaElementImageProvider {
46 * Flags for the JavaImageLabelProvider: Generate images with overlays.
48 public final static int OVERLAY_ICONS = 0x1;
51 * Generate small sized images.
53 public final static int SMALL_ICONS = 0x2;
56 * Use the 'light' style for rendering types.
58 public final static int LIGHT_TYPE_ICONS = 0x4;
60 public static final Point SMALL_SIZE = new Point(16, 16);
62 public static final Point BIG_SIZE = new Point(22, 16);
64 private static ImageDescriptor DESC_OBJ_PROJECT_CLOSED;
66 private static ImageDescriptor DESC_OBJ_PROJECT;
68 private static ImageDescriptor DESC_OBJ_FOLDER;
70 ISharedImages images = WebUI.getDefault().getWorkbench()
72 DESC_OBJ_PROJECT_CLOSED = images
73 .getImageDescriptor(SharedImages.IMG_OBJ_PROJECT_CLOSED);
74 DESC_OBJ_PROJECT = images
75 .getImageDescriptor(SharedImages.IMG_OBJ_PROJECT);
76 DESC_OBJ_FOLDER = images
77 .getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
80 private ImageDescriptorRegistry fRegistry;
82 public JavaElementImageProvider() {
83 fRegistry = null; // lazy initialization
87 * Returns the icon for a given element. The icon depends on the element
88 * type and element properties. If configured, overlay icons are constructed
89 * for <code>ISourceReference</code>s.
92 * Flags as defined by the JavaImageLabelProvider
94 public Image getImageLabel(Object element, int flags) {
95 return getImageLabel(computeDescriptor(element, flags));
98 private Image getImageLabel(ImageDescriptor descriptor) {
99 if (descriptor == null)
101 return getRegistry().get(descriptor);
104 private ImageDescriptorRegistry getRegistry() {
105 if (fRegistry == null) {
106 fRegistry = WebUI.getImageDescriptorRegistry();
111 private ImageDescriptor computeDescriptor(Object element, int flags) {
112 if (element instanceof IJavaElement) {
113 return getJavaImageDescriptor((IJavaElement) element, flags);
114 } else if (element instanceof IFile) {
115 IFile file = (IFile) element;
116 if ("java".equals(file.getFileExtension())) { //$NON-NLS-1$
117 return getCUResourceImageDescriptor(file, flags); // image for
123 return getWorkbenchImageDescriptor(file, flags);
124 } else if (element instanceof IAdaptable) {
125 return getWorkbenchImageDescriptor((IAdaptable) element, flags);
130 private static boolean showOverlayIcons(int flags) {
131 return (flags & OVERLAY_ICONS) != 0;
134 private static boolean useSmallSize(int flags) {
135 return (flags & SMALL_ICONS) != 0;
138 private static boolean useLightIcons(int flags) {
139 return (flags & LIGHT_TYPE_ICONS) != 0;
143 * Returns an image descriptor for a compilatio unit not on the class path.
144 * The descriptor includes overlays, if specified.
146 public ImageDescriptor getCUResourceImageDescriptor(IFile file, int flags) {
147 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
148 return new JavaElementImageDescriptor(
149 PHPUiImages.DESC_OBJS_CUNIT_RESOURCE, 0, size);
153 * Returns an image descriptor for a java element. The descriptor includes
154 * overlays, if specified.
156 public ImageDescriptor getJavaImageDescriptor(IJavaElement element,
158 int adornmentFlags = computeJavaAdornmentFlags(element, flags);
159 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
160 return new JavaElementImageDescriptor(getBaseImageDescriptor(element,
161 flags), adornmentFlags, size);
165 * Returns an image descriptor for a IAdaptable. The descriptor includes
166 * overlays, if specified (only error ticks apply). Returns
167 * <code>null</code> if no image could be found.
169 public ImageDescriptor getWorkbenchImageDescriptor(IAdaptable adaptable,
171 IWorkbenchAdapter wbAdapter = (IWorkbenchAdapter) adaptable
172 .getAdapter(IWorkbenchAdapter.class);
173 if (wbAdapter == null) {
176 ImageDescriptor descriptor = wbAdapter.getImageDescriptor(adaptable);
177 if (descriptor == null) {
181 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
182 return new JavaElementImageDescriptor(descriptor, 0, size);
185 // ---- Computation of base image key
186 // -------------------------------------------------
189 * Returns an image descriptor for a java element. This is the base image,
192 public ImageDescriptor getBaseImageDescriptor(IJavaElement element,
195 boolean isInterface = false;
197 switch (element.getElementType()) {
198 case IJavaElement.INITIALIZER:
199 return PHPUiImages.DESC_MISC_PRIVATE; // 23479
200 case IJavaElement.METHOD:
201 IMember member = (IMember) element;
202 type = member.getDeclaringType();
203 isInterface = (type != null)
204 && member.getDeclaringType().isInterface();
205 return getMethodImageDescriptor(isInterface, member.getFlags());
206 case IJavaElement.FIELD:
207 IField field = (IField) element;
208 return getFieldImageDescriptor(field.getDeclaringType()
209 .isInterface(), field.getFlags());
211 case IJavaElement.PACKAGE_DECLARATION:
212 return PHPUiImages.DESC_OBJS_PACKDECL;
214 case IJavaElement.IMPORT_DECLARATION:
215 return PHPUiImages.DESC_OBJS_IMPDECL;
217 case IJavaElement.IMPORT_CONTAINER:
218 return PHPUiImages.DESC_OBJS_IMPCONT;
220 case IJavaElement.TYPE: {
221 type = (IType) element;
222 isInterface = type.isInterface();
224 if (useLightIcons(renderFlags)) {
225 return isInterface ? PHPUiImages.DESC_OBJS_INTERFACEALT
226 : PHPUiImages.DESC_OBJS_CLASSALT;
228 boolean isInner = type.getDeclaringType() != null;
229 return getTypeImageDescriptor(isInterface, isInner, type
233 case IJavaElement.PACKAGE_FRAGMENT_ROOT: {
234 IPackageFragmentRoot root = (IPackageFragmentRoot) element;
235 // if (root.isArchive()) {
236 // IPath attach= root.getSourceAttachmentPath();
237 // if (root.isExternal()) {
238 // if (attach == null) {
239 // return PHPUiImages.DESC_OBJS_EXTJAR;
241 // return PHPUiImages.DESC_OBJS_EXTJAR_WSRC;
244 // if (attach == null) {
245 // return PHPUiImages.DESC_OBJS_JAR;
247 // return PHPUiImages.DESC_OBJS_JAR_WSRC;
251 return PHPUiImages.DESC_OBJS_PACKFRAG_ROOT;
255 case IJavaElement.PACKAGE_FRAGMENT:
256 return getPackageFragmentIcon(element, renderFlags);
258 case IJavaElement.COMPILATION_UNIT:
259 return PHPUiImages.DESC_OBJS_CUNIT;
261 case IJavaElement.CLASS_FILE:
263 * this is too expensive for large packages try { IClassFile
264 * cfile= (IClassFile)element; if (cfile.isClass()) return
265 * PHPUiImages.IMG_OBJS_CFILECLASS; return
266 * PHPUiImages.IMG_OBJS_CFILEINT; } catch(JavaModelException e) { //
269 return PHPUiImages.DESC_OBJS_CFILE;
271 case IJavaElement.JAVA_PROJECT:
272 IJavaProject jp = (IJavaProject) element;
273 if (jp.getProject().isOpen()) {
274 IProject project = jp.getProject();
275 IWorkbenchAdapter adapter = (IWorkbenchAdapter) project
276 .getAdapter(IWorkbenchAdapter.class);
277 if (adapter != null) {
278 ImageDescriptor result = adapter
279 .getImageDescriptor(project);
283 return DESC_OBJ_PROJECT;
285 return DESC_OBJ_PROJECT_CLOSED;
287 case IJavaElement.JAVA_MODEL:
288 return PHPUiImages.DESC_OBJS_JAVA_MODEL;
291 Assert.isTrue(false, PHPUIMessages
292 .getString("JavaImageLabelprovider.assert.wrongImage")); //$NON-NLS-1$
293 return null; //$NON-NLS-1$
295 } catch (JavaModelException e) {
296 if (e.isDoesNotExist())
297 return PHPUiImages.DESC_OBJS_UNKNOWN;
298 PHPeclipsePlugin.log(e);
299 return PHPUiImages.DESC_OBJS_GHOST;
303 protected ImageDescriptor getPackageFragmentIcon(IJavaElement element,
304 int renderFlags) throws JavaModelException {
305 // IPackageFragment fragment= (IPackageFragment)element;
306 // boolean containsJavaElements= false;
308 // containsJavaElements= fragment.hasChildren();
309 // } catch(JavaModelException e) {
310 // // assuming no children;
312 // if(!containsJavaElements && (fragment.getNonJavaResources().length >
314 // return PHPUiImages.DESC_OBJS_EMPTY_PACKAGE_RESOURCES;
315 // else if (!containsJavaElements)
316 // return PHPUiImages.DESC_OBJS_EMPTY_PACKAGE;
317 return PHPUiImages.DESC_OBJS_PACKAGE;
320 public void dispose() {
323 // ---- Methods to compute the adornments flags
324 // ---------------------------------
326 private int computeJavaAdornmentFlags(IJavaElement element, int renderFlags) {
328 if (showOverlayIcons(renderFlags) && element instanceof IMember) {
330 IMember member = (IMember) element;
332 if (element.getElementType() == IJavaElement.METHOD
333 && ((IMethod) element).isConstructor())
334 flags |= JavaElementImageDescriptor.CONSTRUCTOR;
336 int modifiers = member.getFlags();
337 if (Flags.isAbstract(modifiers) && confirmAbstract(member))
338 flags |= JavaElementImageDescriptor.ABSTRACT;
339 if (Flags.isFinal(modifiers) || isInterfaceField(member))
340 flags |= JavaElementImageDescriptor.FINAL;
341 // if (Flags.isSynchronized(modifiers) &&
342 // confirmSynchronized(member))
343 // flags |= JavaElementImageDescriptor.SYNCHRONIZED;
344 if (Flags.isStatic(modifiers) || isInterfaceField(member))
345 flags |= JavaElementImageDescriptor.STATIC;
347 // if (member.getElementType() == IJavaElement.TYPE) {
348 // if (JavaModelUtil.hasMainMethod((IType) member)) {
349 // flags |= JavaElementImageDescriptor.RUNNABLE;
352 } catch (JavaModelException e) {
353 // do nothing. Can't compute runnable adornment or get flags
359 private static boolean confirmAbstract(IMember element)
360 throws JavaModelException {
361 // never show the abstract symbol on interfaces or members in interfaces
362 if (element.getElementType() == IJavaElement.TYPE) {
363 return ((IType) element).isClass();
365 return element.getDeclaringType().isClass();
368 private static boolean isInterfaceField(IMember element)
369 throws JavaModelException {
370 // always show the final && static symbol on interface fields
371 if (element.getElementType() == IJavaElement.FIELD) {
372 return element.getDeclaringType().isInterface();
377 private static boolean confirmSynchronized(IJavaElement member) {
378 // Synchronized types are allowed but meaningless.
379 return member.getElementType() != IJavaElement.TYPE;
382 public static ImageDescriptor getMethodImageDescriptor(
383 boolean isInInterface, int flags) {
384 if (Flags.isPublic(flags) || isInInterface)
385 return PHPUiImages.DESC_MISC_PUBLIC;
386 if (Flags.isProtected(flags))
387 return PHPUiImages.DESC_MISC_PROTECTED;
388 if (Flags.isPrivate(flags))
389 return PHPUiImages.DESC_MISC_PRIVATE;
391 return PHPUiImages.DESC_MISC_DEFAULT;
394 public static ImageDescriptor getFieldImageDescriptor(
395 boolean isInInterface, int flags) {
396 if (Flags.isPublic(flags) || isInInterface)
397 return PHPUiImages.DESC_FIELD_PUBLIC;
398 if (Flags.isProtected(flags))
399 return PHPUiImages.DESC_FIELD_PROTECTED;
400 if (Flags.isPrivate(flags))
401 return PHPUiImages.DESC_FIELD_PRIVATE;
403 return PHPUiImages.DESC_FIELD_DEFAULT;
406 public static ImageDescriptor getTypeImageDescriptor(boolean isInterface,
407 boolean isInner, int flags) {
410 return getInnerInterfaceImageDescriptor(flags);
412 return getInnerClassImageDescriptor(flags);
416 return getInterfaceImageDescriptor(flags);
418 return getClassImageDescriptor(flags);
423 private static ImageDescriptor getClassImageDescriptor(int flags) {
424 if (Flags.isPublic(flags) || Flags.isProtected(flags)
425 || Flags.isPrivate(flags))
426 return PHPUiImages.DESC_OBJS_CLASS;
428 return PHPUiImages.DESC_OBJS_CLASS_DEFAULT;
431 private static ImageDescriptor getInnerClassImageDescriptor(int flags) {
432 if (Flags.isPublic(flags))
433 return PHPUiImages.DESC_OBJS_INNER_CLASS_PUBLIC;
434 else if (Flags.isPrivate(flags))
435 return PHPUiImages.DESC_OBJS_INNER_CLASS_PRIVATE;
436 else if (Flags.isProtected(flags))
437 return PHPUiImages.DESC_OBJS_INNER_CLASS_PROTECTED;
439 return PHPUiImages.DESC_OBJS_INNER_CLASS_DEFAULT;
442 private static ImageDescriptor getInterfaceImageDescriptor(int flags) {
443 if (Flags.isPublic(flags) || Flags.isProtected(flags)
444 || Flags.isPrivate(flags))
445 return PHPUiImages.DESC_OBJS_INTERFACE;
447 return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;
450 private static ImageDescriptor getInnerInterfaceImageDescriptor(int flags) {
451 if (Flags.isPublic(flags))
452 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PUBLIC;
453 else if (Flags.isPrivate(flags))
454 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PRIVATE;
455 else if (Flags.isProtected(flags))
456 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PROTECTED;
458 return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;