Eclipse 3M7
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / JavaAnnotationImageProvider.java
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.phpeclipse.phpeditor;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.widgets.Display;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.resource.ImageRegistry;
19
20 import org.eclipse.jface.text.source.Annotation;
21
22 import org.eclipse.ui.ISharedImages;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.texteditor.IAnnotationImageProvider;
25
26 import net.sourceforge.phpdt.ui.PreferenceConstants;
27
28 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
29 //import net.sourceforge.phpdt.internal.ui.text.correction.JavaCorrectionProcessor;
30
31 /**
32  * Image provider for annotations based on Java problem markers.
33  * 
34  * @since 3.0
35  */
36 public class JavaAnnotationImageProvider implements IAnnotationImageProvider {
37         
38         private final static int NO_IMAGE= 0;
39         private final static int GRAY_IMAGE= 1;
40         private final static int OVERLAY_IMAGE= 2;
41         private final static int QUICKFIX_IMAGE= 3;
42         private final static int QUICKFIX_ERROR_IMAGE= 4;
43         
44         
45         private static Image fgQuickFixImage;
46         private static Image fgQuickFixErrorImage;
47         private static ImageRegistry fgImageRegistry;
48         
49         private boolean fShowQuickFixIcon;
50         private int fCachedImageType;
51         private Image fCachedImage;
52         
53         
54         public JavaAnnotationImageProvider() {
55                 fShowQuickFixIcon= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
56         }
57         
58         /*
59          * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getManagedImage(org.eclipse.jface.text.source.Annotation)
60          */
61         public Image getManagedImage(Annotation annotation) {
62                 if (annotation instanceof IJavaAnnotation) {
63                         IJavaAnnotation javaAnnotation= (IJavaAnnotation) annotation;
64                         int imageType= getImageType(javaAnnotation);
65                         return getImage(javaAnnotation, imageType, Display.getCurrent());
66                 }
67                 return null;
68         }
69         
70         /*
71          * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getImageDescriptorId(org.eclipse.jface.text.source.Annotation)
72          */
73         public String getImageDescriptorId(Annotation annotation) {
74                 // unmanaged images are not supported
75                 return null;
76         }
77
78         /*
79          * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getImageDescriptor(java.lang.String)
80          */
81         public ImageDescriptor getImageDescriptor(String symbolicName) {
82                 // unmanaged images are not supported
83                 return null;
84         }
85         
86         
87         private boolean showQuickFix(IJavaAnnotation annotation) {
88         //      return fShowQuickFixIcon && annotation.isProblem() && JavaCorrectionProcessor.hasCorrections(annotation);
89           return false;
90         }
91         
92         private Image getQuickFixImage() {
93                 if (fgQuickFixImage == null)
94                         fgQuickFixImage= PHPUiImages.get(PHPUiImages.IMG_OBJS_FIXABLE_PROBLEM);
95                 return fgQuickFixImage;
96         }
97
98         private Image getQuickFixErrorImage() {
99                 if (fgQuickFixErrorImage == null)
100                         fgQuickFixErrorImage= PHPUiImages.get(PHPUiImages.IMG_OBJS_FIXABLE_ERROR);
101                 return fgQuickFixErrorImage;
102         }
103         
104         private ImageRegistry getImageRegistry(Display display) {
105                 if (fgImageRegistry == null)
106                         fgImageRegistry= new ImageRegistry(display);
107                 return fgImageRegistry;
108         }
109         
110         private int getImageType(IJavaAnnotation annotation) {
111                 int imageType= NO_IMAGE;
112                 if (annotation.hasOverlay())
113                         imageType= OVERLAY_IMAGE;
114                 else if (!annotation.isMarkedDeleted()) {
115                         if (showQuickFix(annotation))
116                                 imageType= JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(annotation.getType()) ? QUICKFIX_ERROR_IMAGE : QUICKFIX_IMAGE; 
117                 } else {
118                         imageType= GRAY_IMAGE;
119                 }
120                 return imageType;
121         }
122
123         private Image getImage(IJavaAnnotation annotation, int imageType, Display display) {
124                 if (fCachedImageType == imageType)
125                         return fCachedImage;
126                 
127                 Image image= null;
128                 switch (imageType) {
129                         case OVERLAY_IMAGE:
130                                 IJavaAnnotation overlay= annotation.getOverlay();
131                                 image= overlay.getImage(display);
132                                 break;
133                         case QUICKFIX_IMAGE:
134                                 image= getQuickFixImage();
135                                 break;
136                         case QUICKFIX_ERROR_IMAGE:
137                                 image= getQuickFixErrorImage();
138                                 break;
139                         case GRAY_IMAGE: {
140                                 ISharedImages sharedImages= PlatformUI.getWorkbench().getSharedImages();
141                                 String annotationType= annotation.getType();
142                                 if (JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(annotationType)) {
143                                         image= sharedImages.getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
144                                 } else if (JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE.equals(annotationType)) {
145                                         image= sharedImages.getImage(ISharedImages.IMG_OBJS_WARN_TSK);
146                                 } else if (JavaMarkerAnnotation.INFO_ANNOTATION_TYPE.equals(annotationType)) {
147                                         image= sharedImages.getImage(ISharedImages.IMG_OBJS_INFO_TSK);
148                                 }
149                                 if (image != null) {
150                                         ImageRegistry registry= getImageRegistry(display);
151                                         String key= Integer.toString(image.hashCode());
152                                         Image grayImage= registry.get(key);
153                                         if (grayImage == null) {
154                                                 grayImage= new Image(display, image, SWT.IMAGE_GRAY);
155                                                 registry.put(key, grayImage);
156                                         }
157                                         image= grayImage;
158                                 }
159                                 break;
160                         }
161                 }
162                 
163                 fCachedImageType= imageType;
164                 fCachedImage= image;
165                 return fCachedImage;
166         }
167 }