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