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;
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.resources.IProject;
29 import org.eclipse.core.runtime.IAdaptable;
30 import org.eclipse.jface.resource.ImageDescriptor;
31 import org.eclipse.jface.util.Assert;
32 import org.eclipse.swt.graphics.Image;
33 import org.eclipse.swt.graphics.Point;
34 import org.eclipse.ui.ISharedImages;
35 import org.eclipse.ui.model.IWorkbenchAdapter;
38 * Default strategy of the Java plugin for the construction of Java element icons.
40 public class JavaElementImageProvider {
43 * Flags for the JavaImageLabelProvider:
44 * Generate images with overlays.
46 public final static int OVERLAY_ICONS = 0x1;
49 * Generate small sized images.
51 public final static int SMALL_ICONS = 0x2;
54 * Use the 'light' style for rendering types.
56 public final static int LIGHT_TYPE_ICONS = 0x4;
58 public static final Point SMALL_SIZE = new Point(16, 16);
59 public static final Point BIG_SIZE = new Point(22, 16);
61 private static ImageDescriptor DESC_OBJ_PROJECT_CLOSED;
62 private static ImageDescriptor DESC_OBJ_PROJECT;
63 private static ImageDescriptor DESC_OBJ_FOLDER;
65 ISharedImages images = PHPeclipsePlugin.getDefault().getWorkbench().getSharedImages();
66 DESC_OBJ_PROJECT_CLOSED = images.getImageDescriptor(ISharedImages.IMG_OBJ_PROJECT_CLOSED);
67 DESC_OBJ_PROJECT = images.getImageDescriptor(ISharedImages.IMG_OBJ_PROJECT);
68 DESC_OBJ_FOLDER = images.getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
71 private ImageDescriptorRegistry fRegistry;
73 public JavaElementImageProvider() {
74 fRegistry = null; // lazy initialization
78 * Returns the icon for a given element. The icon depends on the element type
79 * and element properties. If configured, overlay icons are constructed for
80 * <code>ISourceReference</code>s.
81 * @param flags Flags as defined by the JavaImageLabelProvider
83 public Image getImageLabel(Object element, int flags) {
84 return getImageLabel(computeDescriptor(element, flags));
87 private Image getImageLabel(ImageDescriptor descriptor) {
88 if (descriptor == null)
90 return getRegistry().get(descriptor);
93 private ImageDescriptorRegistry getRegistry() {
94 if (fRegistry == null) {
95 fRegistry = PHPeclipsePlugin.getImageDescriptorRegistry();
100 private ImageDescriptor computeDescriptor(Object element, int flags) {
101 if (element instanceof IJavaElement) {
102 return getJavaImageDescriptor((IJavaElement) element, flags);
103 } else if (element instanceof IFile) {
104 IFile file = (IFile) element;
105 if ("java".equals(file.getFileExtension())) { //$NON-NLS-1$
106 return getCUResourceImageDescriptor(file, flags); // image for a CU not on the build path
108 return getWorkbenchImageDescriptor(file, flags);
109 } else if (element instanceof IAdaptable) {
110 return getWorkbenchImageDescriptor((IAdaptable) element, flags);
115 private static boolean showOverlayIcons(int flags) {
116 return (flags & OVERLAY_ICONS) != 0;
119 private static boolean useSmallSize(int flags) {
120 return (flags & SMALL_ICONS) != 0;
123 private static boolean useLightIcons(int flags) {
124 return (flags & LIGHT_TYPE_ICONS) != 0;
128 * Returns an image descriptor for a compilatio unit not on the class path.
129 * The descriptor includes overlays, if specified.
131 public ImageDescriptor getCUResourceImageDescriptor(IFile file, int flags) {
132 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
133 return new JavaElementImageDescriptor(PHPUiImages.DESC_OBJS_CUNIT_RESOURCE, 0, size);
137 * Returns an image descriptor for a java element. The descriptor includes overlays, if specified.
139 public ImageDescriptor getJavaImageDescriptor(IJavaElement element, int flags) {
140 int adornmentFlags = computeJavaAdornmentFlags(element, flags);
141 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
142 return new JavaElementImageDescriptor(getBaseImageDescriptor(element, flags), adornmentFlags, size);
146 * Returns an image descriptor for a IAdaptable. The descriptor includes overlays, if specified (only error ticks apply).
147 * Returns <code>null</code> if no image could be found.
149 public ImageDescriptor getWorkbenchImageDescriptor(IAdaptable adaptable, int flags) {
150 IWorkbenchAdapter wbAdapter = (IWorkbenchAdapter) adaptable.getAdapter(IWorkbenchAdapter.class);
151 if (wbAdapter == null) {
154 ImageDescriptor descriptor = wbAdapter.getImageDescriptor(adaptable);
155 if (descriptor == null) {
159 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
160 return new JavaElementImageDescriptor(descriptor, 0, size);
163 // ---- Computation of base image key -------------------------------------------------
166 * Returns an image descriptor for a java element. This is the base image, no overlays.
168 public ImageDescriptor getBaseImageDescriptor(IJavaElement element, int renderFlags) {
170 boolean isInterface = false;
172 switch (element.getElementType()) {
173 case IJavaElement.INITIALIZER :
174 return PHPUiImages.DESC_MISC_PRIVATE; // 23479
175 case IJavaElement.METHOD :
176 IMember member = (IMember) element;
177 type = member.getDeclaringType();
178 isInterface = (type != null) && member.getDeclaringType().isInterface();
179 return getMethodImageDescriptor(isInterface, member.getFlags());
180 case IJavaElement.FIELD :
181 IField field = (IField) element;
182 return getFieldImageDescriptor(field.getDeclaringType().isInterface(), field.getFlags());
184 case IJavaElement.PACKAGE_DECLARATION :
185 return PHPUiImages.DESC_OBJS_PACKDECL;
187 case IJavaElement.IMPORT_DECLARATION :
188 return PHPUiImages.DESC_OBJS_IMPDECL;
190 case IJavaElement.IMPORT_CONTAINER :
191 return PHPUiImages.DESC_OBJS_IMPCONT;
193 case IJavaElement.TYPE :
195 type = (IType) element;
196 isInterface = type.isInterface();
198 if (useLightIcons(renderFlags)) {
199 return isInterface ? PHPUiImages.DESC_OBJS_INTERFACEALT : PHPUiImages.DESC_OBJS_CLASSALT;
201 boolean isInner = type.getDeclaringType() != null;
202 return getTypeImageDescriptor(isInterface, isInner, type.getFlags());
205 case IJavaElement.PACKAGE_FRAGMENT_ROOT :
207 IPackageFragmentRoot root = (IPackageFragmentRoot) element;
208 // if (root.isArchive()) {
209 // IPath attach= root.getSourceAttachmentPath();
210 // if (root.isExternal()) {
211 // if (attach == null) {
212 // return PHPUiImages.DESC_OBJS_EXTJAR;
214 // return PHPUiImages.DESC_OBJS_EXTJAR_WSRC;
217 // if (attach == null) {
218 // return PHPUiImages.DESC_OBJS_JAR;
220 // return PHPUiImages.DESC_OBJS_JAR_WSRC;
224 return PHPUiImages.DESC_OBJS_PACKFRAG_ROOT;
228 case IJavaElement.PACKAGE_FRAGMENT :
229 return getPackageFragmentIcon(element, renderFlags);
231 case IJavaElement.COMPILATION_UNIT :
232 return PHPUiImages.DESC_OBJS_CUNIT;
234 case IJavaElement.CLASS_FILE :
235 /* this is too expensive for large packages
237 IClassFile cfile= (IClassFile)element;
239 return PHPUiImages.IMG_OBJS_CFILECLASS;
240 return PHPUiImages.IMG_OBJS_CFILEINT;
241 } catch(JavaModelException e) {
244 return PHPUiImages.DESC_OBJS_CFILE;
246 case IJavaElement.JAVA_PROJECT :
247 IJavaProject jp = (IJavaProject) element;
248 if (jp.getProject().isOpen()) {
249 IProject project = jp.getProject();
250 IWorkbenchAdapter adapter = (IWorkbenchAdapter) project.getAdapter(IWorkbenchAdapter.class);
251 if (adapter != null) {
252 ImageDescriptor result = adapter.getImageDescriptor(project);
256 return DESC_OBJ_PROJECT;
258 return DESC_OBJ_PROJECT_CLOSED;
260 case IJavaElement.JAVA_MODEL :
261 return PHPUiImages.DESC_OBJS_JAVA_MODEL;
264 Assert.isTrue(false, PHPUIMessages.getString("JavaImageLabelprovider.assert.wrongImage")); //$NON-NLS-1$
265 return null; //$NON-NLS-1$
267 } catch (JavaModelException e) {
268 if (e.isDoesNotExist())
269 return PHPUiImages.DESC_OBJS_UNKNOWN;
270 PHPeclipsePlugin.log(e);
271 return PHPUiImages.DESC_OBJS_GHOST;
275 protected ImageDescriptor getPackageFragmentIcon(IJavaElement element, int renderFlags) throws JavaModelException {
276 // IPackageFragment fragment= (IPackageFragment)element;
277 // boolean containsJavaElements= false;
279 // containsJavaElements= fragment.hasChildren();
280 // } catch(JavaModelException e) {
281 // // assuming no children;
283 // if(!containsJavaElements && (fragment.getNonJavaResources().length > 0))
284 // return PHPUiImages.DESC_OBJS_EMPTY_PACKAGE_RESOURCES;
285 // else if (!containsJavaElements)
286 // return PHPUiImages.DESC_OBJS_EMPTY_PACKAGE;
287 return PHPUiImages.DESC_OBJS_PACKAGE;
290 public void dispose() {
293 // ---- Methods to compute the adornments flags ---------------------------------
295 private int computeJavaAdornmentFlags(IJavaElement element, int renderFlags) {
297 if (showOverlayIcons(renderFlags) && element instanceof IMember) {
299 IMember member = (IMember) element;
301 if (element.getElementType() == IJavaElement.METHOD && ((IMethod) element).isConstructor())
302 flags |= JavaElementImageDescriptor.CONSTRUCTOR;
304 int modifiers = member.getFlags();
305 if (Flags.isAbstract(modifiers) && confirmAbstract(member))
306 flags |= JavaElementImageDescriptor.ABSTRACT;
307 if (Flags.isFinal(modifiers) || isInterfaceField(member))
308 flags |= JavaElementImageDescriptor.FINAL;
309 // if (Flags.isSynchronized(modifiers) && confirmSynchronized(member))
310 // flags |= JavaElementImageDescriptor.SYNCHRONIZED;
311 if (Flags.isStatic(modifiers) || isInterfaceField(member))
312 flags |= JavaElementImageDescriptor.STATIC;
314 // if (member.getElementType() == IJavaElement.TYPE) {
315 // if (JavaModelUtil.hasMainMethod((IType) member)) {
316 // flags |= JavaElementImageDescriptor.RUNNABLE;
319 } catch (JavaModelException e) {
320 // do nothing. Can't compute runnable adornment or get flags
326 private static boolean confirmAbstract(IMember element) throws JavaModelException {
327 // never show the abstract symbol on interfaces or members in interfaces
328 if (element.getElementType() == IJavaElement.TYPE) {
329 return ((IType) element).isClass();
331 return element.getDeclaringType().isClass();
334 private static boolean isInterfaceField(IMember element) throws JavaModelException {
335 // always show the final && static symbol on interface fields
336 if (element.getElementType() == IJavaElement.FIELD) {
337 return element.getDeclaringType().isInterface();
342 private static boolean confirmSynchronized(IJavaElement member) {
343 // Synchronized types are allowed but meaningless.
344 return member.getElementType() != IJavaElement.TYPE;
347 public static ImageDescriptor getMethodImageDescriptor(boolean isInInterface, int flags) {
348 if (Flags.isPublic(flags) || isInInterface)
349 return PHPUiImages.DESC_MISC_PUBLIC;
350 if (Flags.isProtected(flags))
351 return PHPUiImages.DESC_MISC_PROTECTED;
352 if (Flags.isPrivate(flags))
353 return PHPUiImages.DESC_MISC_PRIVATE;
355 return PHPUiImages.DESC_MISC_DEFAULT;
358 public static ImageDescriptor getFieldImageDescriptor(boolean isInInterface, int flags) {
359 if (Flags.isPublic(flags) || isInInterface)
360 return PHPUiImages.DESC_FIELD_PUBLIC;
361 if (Flags.isProtected(flags))
362 return PHPUiImages.DESC_FIELD_PROTECTED;
363 if (Flags.isPrivate(flags))
364 return PHPUiImages.DESC_FIELD_PRIVATE;
366 return PHPUiImages.DESC_FIELD_DEFAULT;
369 public static ImageDescriptor getTypeImageDescriptor(boolean isInterface, boolean isInner, int flags) {
372 return getInnerInterfaceImageDescriptor(flags);
374 return getInnerClassImageDescriptor(flags);
378 return getInterfaceImageDescriptor(flags);
380 return getClassImageDescriptor(flags);
385 private static ImageDescriptor getClassImageDescriptor(int flags) {
386 if (Flags.isPublic(flags) || Flags.isProtected(flags) || Flags.isPrivate(flags))
387 return PHPUiImages.DESC_OBJS_CLASS;
389 return PHPUiImages.DESC_OBJS_CLASS_DEFAULT;
392 private static ImageDescriptor getInnerClassImageDescriptor(int flags) {
393 if (Flags.isPublic(flags))
394 return PHPUiImages.DESC_OBJS_INNER_CLASS_PUBLIC;
395 else if (Flags.isPrivate(flags))
396 return PHPUiImages.DESC_OBJS_INNER_CLASS_PRIVATE;
397 else if (Flags.isProtected(flags))
398 return PHPUiImages.DESC_OBJS_INNER_CLASS_PROTECTED;
400 return PHPUiImages.DESC_OBJS_INNER_CLASS_DEFAULT;
403 private static ImageDescriptor getInterfaceImageDescriptor(int flags) {
404 if (Flags.isPublic(flags) || Flags.isProtected(flags) || Flags.isPrivate(flags))
405 return PHPUiImages.DESC_OBJS_INTERFACE;
407 return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;
410 private static ImageDescriptor getInnerInterfaceImageDescriptor(int flags) {
411 if (Flags.isPublic(flags))
412 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PUBLIC;
413 else if (Flags.isPrivate(flags))
414 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PRIVATE;
415 else if (Flags.isProtected(flags))
416 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PROTECTED;
418 return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;