f0b4ca14bc34c2d65efc53a3c2a44b6ee4d0b803
[phpeclipse.git] /
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.viewsupport;
12
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;
27
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;
38
39 /**
40  * Default strategy of the Java plugin for the construction of Java element
41  * icons.
42  */
43 public class JavaElementImageProvider {
44
45         /**
46          * Flags for the JavaImageLabelProvider: Generate images with overlays.
47          */
48         public final static int OVERLAY_ICONS = 0x1;
49
50         /**
51          * Generate small sized images.
52          */
53         public final static int SMALL_ICONS = 0x2;
54
55         /**
56          * Use the 'light' style for rendering types.
57          */
58         public final static int LIGHT_TYPE_ICONS = 0x4;
59
60         public static final Point SMALL_SIZE = new Point(16, 16);
61
62         public static final Point BIG_SIZE = new Point(22, 16);
63
64         private static ImageDescriptor DESC_OBJ_PROJECT_CLOSED;
65
66         private static ImageDescriptor DESC_OBJ_PROJECT;
67
68         private static ImageDescriptor DESC_OBJ_FOLDER;
69         {
70                 ISharedImages images = WebUI.getDefault().getWorkbench()
71                                 .getSharedImages();
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);
78         }
79
80         private ImageDescriptorRegistry fRegistry;
81
82         public JavaElementImageProvider() {
83                 fRegistry = null; // lazy initialization
84         }
85
86         /**
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.
90          * 
91          * @param flags
92          *            Flags as defined by the JavaImageLabelProvider
93          */
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 = WebUI.getImageDescriptorRegistry();
107                 }
108                 return fRegistry;
109         }
110
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
118                                                                                                                                         // a CU not
119                                                                                                                                         // on the
120                                                                                                                                         // build
121                                                                                                                                         // path
122                         }
123                         return getWorkbenchImageDescriptor(file, flags);
124                 } else if (element instanceof IAdaptable) {
125                         return getWorkbenchImageDescriptor((IAdaptable) element, flags);
126                 }
127                 return null;
128         }
129
130         private static boolean showOverlayIcons(int flags) {
131                 return (flags & OVERLAY_ICONS) != 0;
132         }
133
134         private static boolean useSmallSize(int flags) {
135                 return (flags & SMALL_ICONS) != 0;
136         }
137
138         private static boolean useLightIcons(int flags) {
139                 return (flags & LIGHT_TYPE_ICONS) != 0;
140         }
141
142         /**
143          * Returns an image descriptor for a compilatio unit not on the class path.
144          * The descriptor includes overlays, if specified.
145          */
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);
150         }
151
152         /**
153          * Returns an image descriptor for a java element. The descriptor includes
154          * overlays, if specified.
155          */
156         public ImageDescriptor getJavaImageDescriptor(IJavaElement element,
157                         int flags) {
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);
162         }
163
164         /**
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.
168          */
169         public ImageDescriptor getWorkbenchImageDescriptor(IAdaptable adaptable,
170                         int flags) {
171                 IWorkbenchAdapter wbAdapter = (IWorkbenchAdapter) adaptable
172                                 .getAdapter(IWorkbenchAdapter.class);
173                 if (wbAdapter == null) {
174                         return null;
175                 }
176                 ImageDescriptor descriptor = wbAdapter.getImageDescriptor(adaptable);
177                 if (descriptor == null) {
178                         return null;
179                 }
180
181                 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
182                 return new JavaElementImageDescriptor(descriptor, 0, size);
183         }
184
185         // ---- Computation of base image key
186         // -------------------------------------------------
187
188         /**
189          * Returns an image descriptor for a java element. This is the base image,
190          * no overlays.
191          */
192         public ImageDescriptor getBaseImageDescriptor(IJavaElement element,
193                         int renderFlags) {
194                 IType type = null;
195                 boolean isInterface = false;
196                 try {
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());
210
211                         case IJavaElement.PACKAGE_DECLARATION:
212                                 return PHPUiImages.DESC_OBJS_PACKDECL;
213
214                         case IJavaElement.IMPORT_DECLARATION:
215                                 return PHPUiImages.DESC_OBJS_IMPDECL;
216
217                         case IJavaElement.IMPORT_CONTAINER:
218                                 return PHPUiImages.DESC_OBJS_IMPCONT;
219
220                         case IJavaElement.TYPE: {
221                                 type = (IType) element;
222                                 isInterface = type.isInterface();
223
224                                 if (useLightIcons(renderFlags)) {
225                                         return isInterface ? PHPUiImages.DESC_OBJS_INTERFACEALT
226                                                         : PHPUiImages.DESC_OBJS_CLASSALT;
227                                 }
228                                 boolean isInner = type.getDeclaringType() != null;
229                                 return getTypeImageDescriptor(isInterface, isInner, type
230                                                 .getFlags());
231                         }
232
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;
240                                 // } else {
241                                 // return PHPUiImages.DESC_OBJS_EXTJAR_WSRC;
242                                 // }
243                                 // } else {
244                                 // if (attach == null) {
245                                 // return PHPUiImages.DESC_OBJS_JAR;
246                                 // } else {
247                                 // return PHPUiImages.DESC_OBJS_JAR_WSRC;
248                                 // }
249                                 // }
250                                 // } else {
251                                 return PHPUiImages.DESC_OBJS_PACKFRAG_ROOT;
252                                 // }
253                         }
254
255                         case IJavaElement.PACKAGE_FRAGMENT:
256                                 return getPackageFragmentIcon(element, renderFlags);
257
258                         case IJavaElement.COMPILATION_UNIT:
259                                 return PHPUiImages.DESC_OBJS_CUNIT;
260
261                         case IJavaElement.CLASS_FILE:
262                                 /*
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) { //
267                                  * fall through; }
268                                  */
269                                 return PHPUiImages.DESC_OBJS_CFILE;
270
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);
280                                                 if (result != null)
281                                                         return result;
282                                         }
283                                         return DESC_OBJ_PROJECT;
284                                 }
285                                 return DESC_OBJ_PROJECT_CLOSED;
286
287                         case IJavaElement.JAVA_MODEL:
288                                 return PHPUiImages.DESC_OBJS_JAVA_MODEL;
289                         }
290
291                         Assert.isTrue(false, PHPUIMessages
292                                         .getString("JavaImageLabelprovider.assert.wrongImage")); //$NON-NLS-1$
293                         return null; //$NON-NLS-1$
294
295                 } catch (JavaModelException e) {
296                         if (e.isDoesNotExist())
297                                 return PHPUiImages.DESC_OBJS_UNKNOWN;
298                         PHPeclipsePlugin.log(e);
299                         return PHPUiImages.DESC_OBJS_GHOST;
300                 }
301         }
302
303         protected ImageDescriptor getPackageFragmentIcon(IJavaElement element,
304                         int renderFlags) throws JavaModelException {
305                 // IPackageFragment fragment= (IPackageFragment)element;
306                 // boolean containsJavaElements= false;
307                 // try {
308                 // containsJavaElements= fragment.hasChildren();
309                 // } catch(JavaModelException e) {
310                 // // assuming no children;
311                 // }
312                 // if(!containsJavaElements && (fragment.getNonJavaResources().length >
313                 // 0))
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;
318         }
319
320         public void dispose() {
321         }
322
323         // ---- Methods to compute the adornments flags
324         // ---------------------------------
325
326         private int computeJavaAdornmentFlags(IJavaElement element, int renderFlags) {
327                 int flags = 0;
328                 if (showOverlayIcons(renderFlags) && element instanceof IMember) {
329                         try {
330                                 IMember member = (IMember) element;
331
332                                 if (element.getElementType() == IJavaElement.METHOD
333                                                 && ((IMethod) element).isConstructor())
334                                         flags |= JavaElementImageDescriptor.CONSTRUCTOR;
335
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;
346
347                                 // if (member.getElementType() == IJavaElement.TYPE) {
348                                 // if (JavaModelUtil.hasMainMethod((IType) member)) {
349                                 // flags |= JavaElementImageDescriptor.RUNNABLE;
350                                 // }
351                                 // }
352                         } catch (JavaModelException e) {
353                                 // do nothing. Can't compute runnable adornment or get flags
354                         }
355                 }
356                 return flags;
357         }
358
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();
364                 }
365                 return element.getDeclaringType().isClass();
366         }
367
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();
373                 }
374                 return false;
375         }
376
377         private static boolean confirmSynchronized(IJavaElement member) {
378                 // Synchronized types are allowed but meaningless.
379                 return member.getElementType() != IJavaElement.TYPE;
380         }
381
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;
390
391                 return PHPUiImages.DESC_MISC_DEFAULT;
392         }
393
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;
402
403                 return PHPUiImages.DESC_FIELD_DEFAULT;
404         }
405
406         public static ImageDescriptor getTypeImageDescriptor(boolean isInterface,
407                         boolean isInner, int flags) {
408                 if (isInner) {
409                         if (isInterface) {
410                                 return getInnerInterfaceImageDescriptor(flags);
411                         } else {
412                                 return getInnerClassImageDescriptor(flags);
413                         }
414                 } else {
415                         if (isInterface) {
416                                 return getInterfaceImageDescriptor(flags);
417                         } else {
418                                 return getClassImageDescriptor(flags);
419                         }
420                 }
421         }
422
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;
427                 else
428                         return PHPUiImages.DESC_OBJS_CLASS_DEFAULT;
429         }
430
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;
438                 else
439                         return PHPUiImages.DESC_OBJS_INNER_CLASS_DEFAULT;
440         }
441
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;
446                 else
447                         return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;
448         }
449
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;
457                 else
458                         return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;
459         }
460 }