78d9636136f5f27e204ce683bcdf3ee1a7dcddde
[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 //incastrix
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;
40
41 /**
42  * Default strategy of the Java plugin for the construction of Java element
43  * icons.
44  */
45 public class JavaElementImageProvider {
46
47         /**
48          * Flags for the JavaImageLabelProvider: Generate images with overlays.
49          */
50         public final static int OVERLAY_ICONS = 0x1;
51
52         /**
53          * Generate small sized images.
54          */
55         public final static int SMALL_ICONS = 0x2;
56
57         /**
58          * Use the 'light' style for rendering types.
59          */
60         public final static int LIGHT_TYPE_ICONS = 0x4;
61
62         public static final Point SMALL_SIZE = new Point(16, 16);
63
64         public static final Point BIG_SIZE = new Point(22, 16);
65
66         private static ImageDescriptor DESC_OBJ_PROJECT_CLOSED;
67
68         private static ImageDescriptor DESC_OBJ_PROJECT;
69
70         private static ImageDescriptor DESC_OBJ_FOLDER;
71         {
72                 ISharedImages images = WebUI.getDefault().getWorkbench()
73                                 .getSharedImages();
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);
80         }
81
82         private ImageDescriptorRegistry fRegistry;
83
84         public JavaElementImageProvider() {
85                 fRegistry = null; // lazy initialization
86         }
87
88         /**
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.
92          * 
93          * @param flags
94          *            Flags as defined by the JavaImageLabelProvider
95          */
96         public Image getImageLabel(Object element, int flags) {
97                 return getImageLabel(computeDescriptor(element, flags));
98         }
99
100         private Image getImageLabel(ImageDescriptor descriptor) {
101                 if (descriptor == null)
102                         return null;
103                 return getRegistry().get(descriptor);
104         }
105
106         private ImageDescriptorRegistry getRegistry() {
107                 if (fRegistry == null) {
108                         fRegistry = WebUI.getImageDescriptorRegistry();
109                 }
110                 return fRegistry;
111         }
112
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
120                                                                                                                                         // a CU not
121                                                                                                                                         // on the
122                                                                                                                                         // build
123                                                                                                                                         // path
124                         }
125                         return getWorkbenchImageDescriptor(file, flags);
126                 } else if (element instanceof IAdaptable) {
127                         return getWorkbenchImageDescriptor((IAdaptable) element, flags);
128                 }
129                 return null;
130         }
131
132         private static boolean showOverlayIcons(int flags) {
133                 return (flags & OVERLAY_ICONS) != 0;
134         }
135
136         private static boolean useSmallSize(int flags) {
137                 return (flags & SMALL_ICONS) != 0;
138         }
139
140         private static boolean useLightIcons(int flags) {
141                 return (flags & LIGHT_TYPE_ICONS) != 0;
142         }
143
144         /**
145          * Returns an image descriptor for a compilatio unit not on the class path.
146          * The descriptor includes overlays, if specified.
147          */
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);
152         }
153
154         /**
155          * Returns an image descriptor for a java element. The descriptor includes
156          * overlays, if specified.
157          */
158         public ImageDescriptor getJavaImageDescriptor(IJavaElement element,
159                         int flags) {
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);
164         }
165
166         /**
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.
170          */
171         public ImageDescriptor getWorkbenchImageDescriptor(IAdaptable adaptable,
172                         int flags) {
173                 IWorkbenchAdapter wbAdapter = (IWorkbenchAdapter) adaptable
174                                 .getAdapter(IWorkbenchAdapter.class);
175                 if (wbAdapter == null) {
176                         return null;
177                 }
178                 ImageDescriptor descriptor = wbAdapter.getImageDescriptor(adaptable);
179                 if (descriptor == null) {
180                         return null;
181                 }
182
183                 Point size = useSmallSize(flags) ? SMALL_SIZE : BIG_SIZE;
184                 return new JavaElementImageDescriptor(descriptor, 0, size);
185         }
186
187         // ---- Computation of base image key
188         // -------------------------------------------------
189
190         /**
191          * Returns an image descriptor for a java element. This is the base image,
192          * no overlays.
193          */
194         public ImageDescriptor getBaseImageDescriptor(IJavaElement element,
195                         int renderFlags) {
196                 IType type = null;
197                 boolean isInterface = false;
198                 try {
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());
212
213                         case IJavaElement.PACKAGE_DECLARATION:
214                                 return PHPUiImages.DESC_OBJS_PACKDECL;
215
216                         case IJavaElement.IMPORT_DECLARATION:
217                                 return PHPUiImages.DESC_OBJS_IMPDECL;
218
219                         case IJavaElement.IMPORT_CONTAINER:
220                                 return PHPUiImages.DESC_OBJS_IMPCONT;
221
222                         case IJavaElement.TYPE: {
223                                 type = (IType) element;
224                                 isInterface = type.isInterface();
225
226                                 if (useLightIcons(renderFlags)) {
227                                         return isInterface ? PHPUiImages.DESC_OBJS_INTERFACEALT
228                                                         : PHPUiImages.DESC_OBJS_CLASSALT;
229                                 }
230                                 boolean isInner = type.getDeclaringType() != null;
231                                 return getTypeImageDescriptor(isInterface, isInner, type
232                                                 .getFlags());
233                         }
234
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;
242                                 // } else {
243                                 // return PHPUiImages.DESC_OBJS_EXTJAR_WSRC;
244                                 // }
245                                 // } else {
246                                 // if (attach == null) {
247                                 // return PHPUiImages.DESC_OBJS_JAR;
248                                 // } else {
249                                 // return PHPUiImages.DESC_OBJS_JAR_WSRC;
250                                 // }
251                                 // }
252                                 // } else {
253                                 return PHPUiImages.DESC_OBJS_PACKFRAG_ROOT;
254                                 // }
255                         }
256
257                         case IJavaElement.PACKAGE_FRAGMENT:
258                                 return getPackageFragmentIcon(element, renderFlags);
259
260                         case IJavaElement.COMPILATION_UNIT:
261                                 return PHPUiImages.DESC_OBJS_CUNIT;
262
263                         case IJavaElement.CLASS_FILE:
264                                 /*
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) { //
269                                  * fall through; }
270                                  */
271                                 return PHPUiImages.DESC_OBJS_CFILE;
272
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);
282                                                 if (result != null)
283                                                         return result;
284                                         }
285                                         return DESC_OBJ_PROJECT;
286                                 }
287                                 return DESC_OBJ_PROJECT_CLOSED;
288
289                         case IJavaElement.JAVA_MODEL:
290                                 return PHPUiImages.DESC_OBJS_JAVA_MODEL;
291                         }
292
293                         Assert.isTrue(false, PHPUIMessages
294                                         .getString("JavaImageLabelprovider.assert.wrongImage")); //$NON-NLS-1$
295                         return null; //$NON-NLS-1$
296
297                 } catch (JavaModelException e) {
298                         if (e.isDoesNotExist())
299                                 return PHPUiImages.DESC_OBJS_UNKNOWN;
300                         PHPeclipsePlugin.log(e);
301                         return PHPUiImages.DESC_OBJS_GHOST;
302                 }
303         }
304
305         protected ImageDescriptor getPackageFragmentIcon(IJavaElement element,
306                         int renderFlags) throws JavaModelException {
307                 // IPackageFragment fragment= (IPackageFragment)element;
308                 // boolean containsJavaElements= false;
309                 // try {
310                 // containsJavaElements= fragment.hasChildren();
311                 // } catch(JavaModelException e) {
312                 // // assuming no children;
313                 // }
314                 // if(!containsJavaElements && (fragment.getNonJavaResources().length >
315                 // 0))
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;
320         }
321
322         public void dispose() {
323         }
324
325         // ---- Methods to compute the adornments flags
326         // ---------------------------------
327
328         private int computeJavaAdornmentFlags(IJavaElement element, int renderFlags) {
329                 int flags = 0;
330                 if (showOverlayIcons(renderFlags) && element instanceof IMember) {
331                         try {
332                                 IMember member = (IMember) element;
333
334                                 if (element.getElementType() == IJavaElement.METHOD
335                                                 && ((IMethod) element).isConstructor())
336                                         flags |= JavaElementImageDescriptor.CONSTRUCTOR;
337
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;
348
349                                 // if (member.getElementType() == IJavaElement.TYPE) {
350                                 // if (JavaModelUtil.hasMainMethod((IType) member)) {
351                                 // flags |= JavaElementImageDescriptor.RUNNABLE;
352                                 // }
353                                 // }
354                         } catch (JavaModelException e) {
355                                 // do nothing. Can't compute runnable adornment or get flags
356                         }
357                 }
358                 return flags;
359         }
360
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();
366                 }
367                 return element.getDeclaringType().isClass();
368         }
369
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();
375                 }
376                 return false;
377         }
378
379 //      private static boolean confirmSynchronized(IJavaElement member) {
380 //              // Synchronized types are allowed but meaningless.
381 //              return member.getElementType() != IJavaElement.TYPE;
382 //      }
383
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;
392
393                 return PHPUiImages.DESC_MISC_DEFAULT;
394         }
395
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;
404
405                 return PHPUiImages.DESC_FIELD_DEFAULT;
406         }
407
408         public static ImageDescriptor getTypeImageDescriptor(boolean isInterface,
409                         boolean isInner, int flags) {
410                 if (isInner) {
411                         if (isInterface) {
412                                 return getInnerInterfaceImageDescriptor(flags);
413                         } else {
414                                 return getInnerClassImageDescriptor(flags);
415                         }
416                 } else {
417                         if (isInterface) {
418                                 return getInterfaceImageDescriptor(flags);
419                         } else {
420                                 return getClassImageDescriptor(flags);
421                         }
422                 }
423         }
424
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;
429                 else
430                         return PHPUiImages.DESC_OBJS_CLASS_DEFAULT;
431         }
432
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;
440                 else
441                         return PHPUiImages.DESC_OBJS_INNER_CLASS_DEFAULT;
442         }
443
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;
448                 else
449                         return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;
450         }
451
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;
459                 else
460                         return PHPUiImages.DESC_OBJS_INTERFACE_DEFAULT;
461         }
462 }