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.IPackageFragment;
20 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
21 import net.sourceforge.phpdt.core.IType;
22 import net.sourceforge.phpdt.core.JavaModelException;
23 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
24 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
25 import net.sourceforge.phpdt.ui.JavaElementImageDescriptor;
26 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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.model.IWorkbenchAdapter;
39 * Default strategy of the Java plugin for the construction of Java element icons.
41 public class JavaElementImageProvider {
44 * Flags for the JavaImageLabelProvider:
45 * Generate images with overlays.
47 public final static int OVERLAY_ICONS = 0x1;
50 * Generate small sized images.
52 public final static int SMALL_ICONS = 0x2;
55 * Use the 'light' style for rendering types.
57 public final static int LIGHT_TYPE_ICONS = 0x4;
59 public static final Point SMALL_SIZE = new Point(16, 16);
60 public static final Point BIG_SIZE = new Point(22, 16);
62 private static ImageDescriptor DESC_OBJ_PROJECT_CLOSED;
63 private static ImageDescriptor DESC_OBJ_PROJECT;
64 private static ImageDescriptor DESC_OBJ_FOLDER;
66 ISharedImages images = PHPeclipsePlugin.getDefault().getWorkbench().getSharedImages();
67 DESC_OBJ_PROJECT_CLOSED = images.getImageDescriptor(ISharedImages.IMG_OBJ_PROJECT_CLOSED);
68 DESC_OBJ_PROJECT = images.getImageDescriptor(ISharedImages.IMG_OBJ_PROJECT);
69 DESC_OBJ_FOLDER = images.getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
72 private ImageDescriptorRegistry fRegistry;
74 public JavaElementImageProvider() {
75 fRegistry = null; // lazy initialization
79 * Returns the icon for a given element. The icon depends on the element type
80 * and element properties. If configured, overlay icons are constructed for
81 * <code>ISourceReference</code>s.
82 * @param flags Flags as defined by the JavaImageLabelProvider
84 public Image getImageLabel(Object element, int flags) {
85 return getImageLabel(computeDescriptor(element, flags));
88 private Image getImageLabel(ImageDescriptor descriptor) {
89 if (descriptor == null)
91 return getRegistry().get(descriptor);
94 private ImageDescriptorRegistry getRegistry() {
95 if (fRegistry == null) {
96 fRegistry = PHPeclipsePlugin.getImageDescriptorRegistry();
101 private ImageDescriptor computeDescriptor(Object element, int flags) {
102 if (element instanceof IJavaElement) {
103 return getJavaImageDescriptor((IJavaElement) element, flags);
104 } else if (element instanceof IFile) {
105 IFile file = (IFile) element;
106 if ("java".equals(file.getFileExtension())) { //$NON-NLS-1$
107 return getCUResourceImageDescriptor(file, flags); // image for a CU not on the build path
109 return getWorkbenchImageDescriptor(file, flags);
110 } else if (element instanceof IAdaptable) {
111 return getWorkbenchImageDescriptor((IAdaptable) element, flags);
116 private static boolean showOverlayIcons(int flags) {
117 return (flags & OVERLAY_ICONS) != 0;
120 private static boolean useSmallSize(int flags) {
121 return (flags & SMALL_ICONS) != 0;
124 private static boolean useLightIcons(int flags) {
125 return (flags & LIGHT_TYPE_ICONS) != 0;
129 * Returns an image descriptor for a compilatio unit not on the class path.
130 * The descriptor includes overlays, if specified.
132 public ImageDescriptor getCUResourceImageDescriptor(IFile file, int flags) {
133 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
134 return new JavaElementImageDescriptor(PHPUiImages.DESC_OBJS_CUNIT_RESOURCE, 0, size);
138 * Returns an image descriptor for a java element. The descriptor includes overlays, if specified.
140 public ImageDescriptor getJavaImageDescriptor(IJavaElement element, int flags) {
141 int adornmentFlags = computeJavaAdornmentFlags(element, flags);
142 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
143 return new JavaElementImageDescriptor(getBaseImageDescriptor(element, flags), adornmentFlags, size);
147 * Returns an image descriptor for a IAdaptable. The descriptor includes overlays, if specified (only error ticks apply).
148 * Returns <code>null</code> if no image could be found.
150 public ImageDescriptor getWorkbenchImageDescriptor(IAdaptable adaptable, int flags) {
151 IWorkbenchAdapter wbAdapter = (IWorkbenchAdapter) adaptable.getAdapter(IWorkbenchAdapter.class);
152 if (wbAdapter == null) {
155 ImageDescriptor descriptor = wbAdapter.getImageDescriptor(adaptable);
156 if (descriptor == null) {
160 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
161 return new JavaElementImageDescriptor(descriptor, 0, size);
164 // ---- Computation of base image key -------------------------------------------------
167 * Returns an image descriptor for a java element. This is the base image, no overlays.
169 public ImageDescriptor getBaseImageDescriptor(IJavaElement element, int renderFlags) {
171 boolean isInterface = false;
173 switch (element.getElementType()) {
174 case IJavaElement.INITIALIZER :
175 return PHPUiImages.DESC_MISC_PRIVATE; // 23479
176 case IJavaElement.METHOD :
177 IMember member = (IMember) element;
178 type = member.getDeclaringType();
179 isInterface = (type != null) && member.getDeclaringType().isInterface();
180 return getMethodImageDescriptor(isInterface, member.getFlags());
181 case IJavaElement.FIELD :
182 IField field = (IField) element;
183 return getFieldImageDescriptor(field.getDeclaringType().isInterface(), field.getFlags());
185 case IJavaElement.PACKAGE_DECLARATION :
186 return PHPUiImages.DESC_OBJS_PACKDECL;
188 case IJavaElement.IMPORT_DECLARATION :
189 return PHPUiImages.DESC_OBJS_IMPDECL;
191 case IJavaElement.IMPORT_CONTAINER :
192 return PHPUiImages.DESC_OBJS_IMPCONT;
194 case IJavaElement.TYPE :
196 type = (IType) element;
197 isInterface = type.isInterface();
199 if (useLightIcons(renderFlags)) {
200 return isInterface ? PHPUiImages.DESC_OBJS_INTERFACEALT : PHPUiImages.DESC_OBJS_CLASSALT;
202 boolean isInner = type.getDeclaringType() != null;
203 return getTypeImageDescriptor(isInterface, isInner, type.getFlags());
206 case IJavaElement.PACKAGE_FRAGMENT_ROOT :
208 IPackageFragmentRoot root = (IPackageFragmentRoot) element;
209 // if (root.isArchive()) {
210 // IPath attach= root.getSourceAttachmentPath();
211 // if (root.isExternal()) {
212 // if (attach == null) {
213 // return PHPUiImages.DESC_OBJS_EXTJAR;
215 // return PHPUiImages.DESC_OBJS_EXTJAR_WSRC;
218 // if (attach == null) {
219 // return PHPUiImages.DESC_OBJS_JAR;
221 // return PHPUiImages.DESC_OBJS_JAR_WSRC;
225 return PHPUiImages.DESC_OBJS_PACKFRAG_ROOT;
229 case IJavaElement.PACKAGE_FRAGMENT :
230 return getPackageFragmentIcon(element, renderFlags);
232 case IJavaElement.COMPILATION_UNIT :
233 return PHPUiImages.DESC_OBJS_CUNIT;
235 case IJavaElement.CLASS_FILE :
236 /* this is too expensive for large packages
238 IClassFile cfile= (IClassFile)element;
240 return PHPUiImages.IMG_OBJS_CFILECLASS;
241 return PHPUiImages.IMG_OBJS_CFILEINT;
242 } catch(JavaModelException e) {
245 return PHPUiImages.DESC_OBJS_CFILE;
247 case IJavaElement.JAVA_PROJECT :
248 IJavaProject jp = (IJavaProject) element;
249 if (jp.getProject().isOpen()) {
250 IProject project = jp.getProject();
251 IWorkbenchAdapter adapter = (IWorkbenchAdapter) project.getAdapter(IWorkbenchAdapter.class);
252 if (adapter != null) {
253 ImageDescriptor result = adapter.getImageDescriptor(project);
257 return DESC_OBJ_PROJECT;
259 return DESC_OBJ_PROJECT_CLOSED;
261 case IJavaElement.JAVA_MODEL :
262 return PHPUiImages.DESC_OBJS_JAVA_MODEL;
265 Assert.isTrue(false, PHPUIMessages.getString("JavaImageLabelprovider.assert.wrongImage")); //$NON-NLS-1$
266 return null; //$NON-NLS-1$
268 } catch (JavaModelException e) {
269 if (e.isDoesNotExist())
270 return PHPUiImages.DESC_OBJS_UNKNOWN;
271 PHPeclipsePlugin.log(e);
272 return PHPUiImages.DESC_OBJS_GHOST;
276 protected ImageDescriptor getPackageFragmentIcon(IJavaElement element, int renderFlags) throws JavaModelException {
277 // IPackageFragment fragment= (IPackageFragment)element;
278 // boolean containsJavaElements= false;
280 // containsJavaElements= fragment.hasChildren();
281 // } catch(JavaModelException e) {
282 // // assuming no children;
284 // if(!containsJavaElements && (fragment.getNonJavaResources().length > 0))
285 // return PHPUiImages.DESC_OBJS_EMPTY_PACKAGE_RESOURCES;
286 // else if (!containsJavaElements)
287 // return PHPUiImages.DESC_OBJS_EMPTY_PACKAGE;
288 return PHPUiImages.DESC_OBJS_PACKAGE;
291 public void dispose() {
294 // ---- Methods to compute the adornments flags ---------------------------------
296 private int computeJavaAdornmentFlags(IJavaElement element, int renderFlags) {
298 if (showOverlayIcons(renderFlags) && element instanceof IMember) {
300 IMember member = (IMember) element;
302 if (element.getElementType() == IJavaElement.METHOD && ((IMethod) element).isConstructor())
303 flags |= JavaElementImageDescriptor.CONSTRUCTOR;
305 int modifiers = member.getFlags();
306 if (Flags.isAbstract(modifiers) && confirmAbstract(member))
307 flags |= JavaElementImageDescriptor.ABSTRACT;
308 if (Flags.isFinal(modifiers) || isInterfaceField(member))
309 flags |= JavaElementImageDescriptor.FINAL;
310 // if (Flags.isSynchronized(modifiers) && confirmSynchronized(member))
311 // flags |= JavaElementImageDescriptor.SYNCHRONIZED;
312 if (Flags.isStatic(modifiers) || isInterfaceField(member))
313 flags |= JavaElementImageDescriptor.STATIC;
315 // if (member.getElementType() == IJavaElement.TYPE) {
316 // if (JavaModelUtil.hasMainMethod((IType) member)) {
317 // flags |= JavaElementImageDescriptor.RUNNABLE;
320 } catch (JavaModelException e) {
321 // do nothing. Can't compute runnable adornment or get flags
327 private static boolean confirmAbstract(IMember element) throws JavaModelException {
328 // never show the abstract symbol on interfaces or members in interfaces
329 if (element.getElementType() == IJavaElement.TYPE) {
330 return ((IType) element).isClass();
332 return element.getDeclaringType().isClass();
335 private static boolean isInterfaceField(IMember element) throws JavaModelException {
336 // always show the final && static symbol on interface fields
337 if (element.getElementType() == IJavaElement.FIELD) {
338 return element.getDeclaringType().isInterface();
343 private static boolean confirmSynchronized(IJavaElement member) {
344 // Synchronized types are allowed but meaningless.
345 return member.getElementType() != IJavaElement.TYPE;
348 public static ImageDescriptor getMethodImageDescriptor(boolean isInInterface, int flags) {
349 if (Flags.isPublic(flags) || isInInterface)
350 return PHPUiImages.DESC_MISC_PUBLIC;
351 if (Flags.isProtected(flags))
352 return PHPUiImages.DESC_MISC_PROTECTED;
353 if (Flags.isPrivate(flags))
354 return PHPUiImages.DESC_MISC_PRIVATE;
356 return PHPUiImages.DESC_MISC_DEFAULT;
359 public static ImageDescriptor getFieldImageDescriptor(boolean isInInterface, int flags) {
360 if (Flags.isPublic(flags) || isInInterface)
361 return PHPUiImages.DESC_FIELD_PUBLIC;
362 if (Flags.isProtected(flags))
363 return PHPUiImages.DESC_FIELD_PROTECTED;
364 if (Flags.isPrivate(flags))
365 return PHPUiImages.DESC_FIELD_PRIVATE;
367 return PHPUiImages.DESC_FIELD_DEFAULT;
370 public static ImageDescriptor getTypeImageDescriptor(boolean isInterface, boolean isInner, int flags) {
373 return getInnerInterfaceImageDescriptor(flags);
375 return getInnerClassImageDescriptor(flags);
379 return getInterfaceImageDescriptor(flags);
381 return getClassImageDescriptor(flags);
386 private static ImageDescriptor getClassImageDescriptor(int flags) {
387 if (Flags.isPublic(flags) || Flags.isProtected(flags) || Flags.isPrivate(flags))
388 return PHPUiImages.DESC_OBJS_CLASS;
390 return PHPUiImages.DESC_OBJS_CLASS_DEFAULT;
393 private static ImageDescriptor getInnerClassImageDescriptor(int flags) {
394 if (Flags.isPublic(flags))
395 return PHPUiImages.DESC_OBJS_INNER_CLASS_PUBLIC;
396 else if (Flags.isPrivate(flags))
397 return PHPUiImages.DESC_OBJS_INNER_CLASS_PRIVATE;
398 else if (Flags.isProtected(flags))
399 return PHPUiImages.DESC_OBJS_INNER_CLASS_PROTECTED;
401 return PHPUiImages.DESC_OBJS_INNER_CLASS_DEFAULT;
404 private static ImageDescriptor getInterfaceImageDescriptor(int flags) {
405 if (Flags.isPublic(flags) || Flags.isProtected(flags) || Flags.isPrivate(flags))
406 return PHPUiImages.DESC_OBJS_INTERFACE;
408 return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;
411 private static ImageDescriptor getInnerInterfaceImageDescriptor(int flags) {
412 if (Flags.isPublic(flags))
413 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PUBLIC;
414 else if (Flags.isPrivate(flags))
415 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PRIVATE;
416 else if (Flags.isProtected(flags))
417 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PROTECTED;
419 return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;