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