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;
33 //import org.eclipse.jface.text.Assert;
34 import org.eclipse.core.runtime.Assert;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.graphics.Point;
37 import org.eclipse.ui.ISharedImages;
38 import org.eclipse.ui.ide.IDE.SharedImages;
39 import org.eclipse.ui.model.IWorkbenchAdapter;
42 * Default strategy of the Java plugin for the construction of Java element
45 public class JavaElementImageProvider {
48 * Flags for the JavaImageLabelProvider: Generate images with overlays.
50 public final static int OVERLAY_ICONS = 0x1;
53 * Generate small sized images.
55 public final static int SMALL_ICONS = 0x2;
58 * Use the 'light' style for rendering types.
60 public final static int LIGHT_TYPE_ICONS = 0x4;
62 public static final Point SMALL_SIZE = new Point(16, 16);
64 public static final Point BIG_SIZE = new Point(22, 16);
66 private static ImageDescriptor DESC_OBJ_PROJECT_CLOSED;
68 private static ImageDescriptor DESC_OBJ_PROJECT;
70 private static ImageDescriptor DESC_OBJ_FOLDER;
72 ISharedImages images = WebUI.getDefault().getWorkbench()
74 DESC_OBJ_PROJECT_CLOSED = images
75 .getImageDescriptor(SharedImages.IMG_OBJ_PROJECT_CLOSED);
76 DESC_OBJ_PROJECT = images
77 .getImageDescriptor(SharedImages.IMG_OBJ_PROJECT);
78 DESC_OBJ_FOLDER = images
79 .getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
82 private ImageDescriptorRegistry fRegistry;
84 public JavaElementImageProvider() {
85 fRegistry = null; // lazy initialization
89 * Returns the icon for a given element. The icon depends on the element
90 * type and element properties. If configured, overlay icons are constructed
91 * for <code>ISourceReference</code>s.
94 * Flags as defined by the JavaImageLabelProvider
96 public Image getImageLabel(Object element, int flags) {
97 return getImageLabel(computeDescriptor(element, flags));
100 private Image getImageLabel(ImageDescriptor descriptor) {
101 if (descriptor == null)
103 return getRegistry().get(descriptor);
106 private ImageDescriptorRegistry getRegistry() {
107 if (fRegistry == null) {
108 fRegistry = WebUI.getImageDescriptorRegistry();
113 private ImageDescriptor computeDescriptor(Object element, int flags) {
114 if (element instanceof IJavaElement) {
115 return getJavaImageDescriptor((IJavaElement) element, flags);
116 } else if (element instanceof IFile) {
117 IFile file = (IFile) element;
118 if ("java".equals(file.getFileExtension())) { //$NON-NLS-1$
119 return getCUResourceImageDescriptor(file, flags); // image for
125 return getWorkbenchImageDescriptor(file, flags);
126 } else if (element instanceof IAdaptable) {
127 return getWorkbenchImageDescriptor((IAdaptable) element, flags);
132 private static boolean showOverlayIcons(int flags) {
133 return (flags & OVERLAY_ICONS) != 0;
136 private static boolean useSmallSize(int flags) {
137 return (flags & SMALL_ICONS) != 0;
140 private static boolean useLightIcons(int flags) {
141 return (flags & LIGHT_TYPE_ICONS) != 0;
145 * Returns an image descriptor for a compilatio unit not on the class path.
146 * The descriptor includes overlays, if specified.
148 public ImageDescriptor getCUResourceImageDescriptor(IFile file, int flags) {
149 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
150 return new JavaElementImageDescriptor(
151 PHPUiImages.DESC_OBJS_CUNIT_RESOURCE, 0, size);
155 * Returns an image descriptor for a java element. The descriptor includes
156 * overlays, if specified.
158 public ImageDescriptor getJavaImageDescriptor(IJavaElement element,
160 int adornmentFlags = computeJavaAdornmentFlags(element, flags);
161 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
162 return new JavaElementImageDescriptor(getBaseImageDescriptor(element,
163 flags), adornmentFlags, size);
167 * Returns an image descriptor for a IAdaptable. The descriptor includes
168 * overlays, if specified (only error ticks apply). Returns
169 * <code>null</code> if no image could be found.
171 public ImageDescriptor getWorkbenchImageDescriptor(IAdaptable adaptable,
173 IWorkbenchAdapter wbAdapter = (IWorkbenchAdapter) adaptable
174 .getAdapter(IWorkbenchAdapter.class);
175 if (wbAdapter == null) {
178 ImageDescriptor descriptor = wbAdapter.getImageDescriptor(adaptable);
179 if (descriptor == null) {
183 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
184 return new JavaElementImageDescriptor(descriptor, 0, size);
187 // ---- Computation of base image key
188 // -------------------------------------------------
191 * Returns an image descriptor for a java element. This is the base image,
194 public ImageDescriptor getBaseImageDescriptor(IJavaElement element,
197 boolean isInterface = false;
199 switch (element.getElementType()) {
200 case IJavaElement.INITIALIZER:
201 return PHPUiImages.DESC_MISC_PRIVATE; // 23479
202 case IJavaElement.METHOD:
203 IMember member = (IMember) element;
204 type = member.getDeclaringType();
205 isInterface = (type != null)
206 && member.getDeclaringType().isInterface();
207 return getMethodImageDescriptor(isInterface, member.getFlags());
208 case IJavaElement.FIELD:
209 IField field = (IField) element;
210 return getFieldImageDescriptor(field.getDeclaringType()
211 .isInterface(), field.getFlags());
213 case IJavaElement.PACKAGE_DECLARATION:
214 return PHPUiImages.DESC_OBJS_PACKDECL;
216 case IJavaElement.IMPORT_DECLARATION:
217 return PHPUiImages.DESC_OBJS_IMPDECL;
219 case IJavaElement.IMPORT_CONTAINER:
220 return PHPUiImages.DESC_OBJS_IMPCONT;
222 case IJavaElement.TYPE: {
223 type = (IType) element;
224 isInterface = type.isInterface();
226 if (useLightIcons(renderFlags)) {
227 return isInterface ? PHPUiImages.DESC_OBJS_INTERFACEALT
228 : PHPUiImages.DESC_OBJS_CLASSALT;
230 boolean isInner = type.getDeclaringType() != null;
231 return getTypeImageDescriptor(isInterface, isInner, type
235 case IJavaElement.PACKAGE_FRAGMENT_ROOT: {
236 IPackageFragmentRoot root = (IPackageFragmentRoot) element;
237 // if (root.isArchive()) {
238 // IPath attach= root.getSourceAttachmentPath();
239 // if (root.isExternal()) {
240 // if (attach == null) {
241 // return PHPUiImages.DESC_OBJS_EXTJAR;
243 // return PHPUiImages.DESC_OBJS_EXTJAR_WSRC;
246 // if (attach == null) {
247 // return PHPUiImages.DESC_OBJS_JAR;
249 // return PHPUiImages.DESC_OBJS_JAR_WSRC;
253 return PHPUiImages.DESC_OBJS_PACKFRAG_ROOT;
257 case IJavaElement.PACKAGE_FRAGMENT:
258 return getPackageFragmentIcon(element, renderFlags);
260 case IJavaElement.COMPILATION_UNIT:
261 return PHPUiImages.DESC_OBJS_CUNIT;
263 case IJavaElement.CLASS_FILE:
265 * this is too expensive for large packages try { IClassFile
266 * cfile= (IClassFile)element; if (cfile.isClass()) return
267 * PHPUiImages.IMG_OBJS_CFILECLASS; return
268 * PHPUiImages.IMG_OBJS_CFILEINT; } catch(JavaModelException e) { //
271 return PHPUiImages.DESC_OBJS_CFILE;
273 case IJavaElement.JAVA_PROJECT:
274 IJavaProject jp = (IJavaProject) element;
275 if (jp.getProject().isOpen()) {
276 IProject project = jp.getProject();
277 IWorkbenchAdapter adapter = (IWorkbenchAdapter) project
278 .getAdapter(IWorkbenchAdapter.class);
279 if (adapter != null) {
280 ImageDescriptor result = adapter
281 .getImageDescriptor(project);
285 return DESC_OBJ_PROJECT;
287 return DESC_OBJ_PROJECT_CLOSED;
289 case IJavaElement.JAVA_MODEL:
290 return PHPUiImages.DESC_OBJS_JAVA_MODEL;
293 Assert.isTrue(false, PHPUIMessages
294 .getString("JavaImageLabelprovider.assert.wrongImage")); //$NON-NLS-1$
295 return null; //$NON-NLS-1$
297 } catch (JavaModelException e) {
298 if (e.isDoesNotExist())
299 return PHPUiImages.DESC_OBJS_UNKNOWN;
300 PHPeclipsePlugin.log(e);
301 return PHPUiImages.DESC_OBJS_GHOST;
305 protected ImageDescriptor getPackageFragmentIcon(IJavaElement element,
306 int renderFlags) throws JavaModelException {
307 // IPackageFragment fragment= (IPackageFragment)element;
308 // boolean containsJavaElements= false;
310 // containsJavaElements= fragment.hasChildren();
311 // } catch(JavaModelException e) {
312 // // assuming no children;
314 // if(!containsJavaElements && (fragment.getNonJavaResources().length >
316 // return PHPUiImages.DESC_OBJS_EMPTY_PACKAGE_RESOURCES;
317 // else if (!containsJavaElements)
318 // return PHPUiImages.DESC_OBJS_EMPTY_PACKAGE;
319 return PHPUiImages.DESC_OBJS_PACKAGE;
322 public void dispose() {
325 // ---- Methods to compute the adornments flags
326 // ---------------------------------
328 private int computeJavaAdornmentFlags(IJavaElement element, int renderFlags) {
330 if (showOverlayIcons(renderFlags) && element instanceof IMember) {
332 IMember member = (IMember) element;
334 if (element.getElementType() == IJavaElement.METHOD
335 && ((IMethod) element).isConstructor())
336 flags |= JavaElementImageDescriptor.CONSTRUCTOR;
338 int modifiers = member.getFlags();
339 if (Flags.isAbstract(modifiers) && confirmAbstract(member))
340 flags |= JavaElementImageDescriptor.ABSTRACT;
341 if (Flags.isFinal(modifiers) || isInterfaceField(member))
342 flags |= JavaElementImageDescriptor.FINAL;
343 // if (Flags.isSynchronized(modifiers) &&
344 // confirmSynchronized(member))
345 // flags |= JavaElementImageDescriptor.SYNCHRONIZED;
346 if (Flags.isStatic(modifiers) || isInterfaceField(member))
347 flags |= JavaElementImageDescriptor.STATIC;
349 // if (member.getElementType() == IJavaElement.TYPE) {
350 // if (JavaModelUtil.hasMainMethod((IType) member)) {
351 // flags |= JavaElementImageDescriptor.RUNNABLE;
354 } catch (JavaModelException e) {
355 // do nothing. Can't compute runnable adornment or get flags
361 private static boolean confirmAbstract(IMember element)
362 throws JavaModelException {
363 // never show the abstract symbol on interfaces or members in interfaces
364 if (element.getElementType() == IJavaElement.TYPE) {
365 return ((IType) element).isClass();
367 return element.getDeclaringType().isClass();
370 private static boolean isInterfaceField(IMember element)
371 throws JavaModelException {
372 // always show the final && static symbol on interface fields
373 if (element.getElementType() == IJavaElement.FIELD) {
374 return element.getDeclaringType().isInterface();
379 private static boolean confirmSynchronized(IJavaElement member) {
380 // Synchronized types are allowed but meaningless.
381 return member.getElementType() != IJavaElement.TYPE;
384 public static ImageDescriptor getMethodImageDescriptor(
385 boolean isInInterface, int flags) {
386 if (Flags.isPublic(flags) || isInInterface)
387 return PHPUiImages.DESC_MISC_PUBLIC;
388 if (Flags.isProtected(flags))
389 return PHPUiImages.DESC_MISC_PROTECTED;
390 if (Flags.isPrivate(flags))
391 return PHPUiImages.DESC_MISC_PRIVATE;
393 return PHPUiImages.DESC_MISC_DEFAULT;
396 public static ImageDescriptor getFieldImageDescriptor(
397 boolean isInInterface, int flags) {
398 if (Flags.isPublic(flags) || isInInterface)
399 return PHPUiImages.DESC_FIELD_PUBLIC;
400 if (Flags.isProtected(flags))
401 return PHPUiImages.DESC_FIELD_PROTECTED;
402 if (Flags.isPrivate(flags))
403 return PHPUiImages.DESC_FIELD_PRIVATE;
405 return PHPUiImages.DESC_FIELD_DEFAULT;
408 public static ImageDescriptor getTypeImageDescriptor(boolean isInterface,
409 boolean isInner, int flags) {
412 return getInnerInterfaceImageDescriptor(flags);
414 return getInnerClassImageDescriptor(flags);
418 return getInterfaceImageDescriptor(flags);
420 return getClassImageDescriptor(flags);
425 private static ImageDescriptor getClassImageDescriptor(int flags) {
426 if (Flags.isPublic(flags) || Flags.isProtected(flags)
427 || Flags.isPrivate(flags))
428 return PHPUiImages.DESC_OBJS_CLASS;
430 return PHPUiImages.DESC_OBJS_CLASS_DEFAULT;
433 private static ImageDescriptor getInnerClassImageDescriptor(int flags) {
434 if (Flags.isPublic(flags))
435 return PHPUiImages.DESC_OBJS_INNER_CLASS_PUBLIC;
436 else if (Flags.isPrivate(flags))
437 return PHPUiImages.DESC_OBJS_INNER_CLASS_PRIVATE;
438 else if (Flags.isProtected(flags))
439 return PHPUiImages.DESC_OBJS_INNER_CLASS_PROTECTED;
441 return PHPUiImages.DESC_OBJS_INNER_CLASS_DEFAULT;
444 private static ImageDescriptor getInterfaceImageDescriptor(int flags) {
445 if (Flags.isPublic(flags) || Flags.isProtected(flags)
446 || Flags.isPrivate(flags))
447 return PHPUiImages.DESC_OBJS_INTERFACE;
449 return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;
452 private static ImageDescriptor getInnerInterfaceImageDescriptor(int flags) {
453 if (Flags.isPublic(flags))
454 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PUBLIC;
455 else if (Flags.isPrivate(flags))
456 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PRIVATE;
457 else if (Flags.isProtected(flags))
458 return PHPUiImages.DESC_OBJS_INNER_INTERFACE_PROTECTED;
460 return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;