KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > JavaAnnotationImageProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.javaeditor;
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 org.eclipse.jdt.ui.PreferenceConstants;
27
28 import org.eclipse.jdt.internal.ui.JavaPluginImages;
29 import org.eclipse.jdt.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 JavaDoc 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 JavaDoc 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) annotation);
89     }
90
91     private Image getQuickFixImage() {
92         if (fgQuickFixImage == null)
93             fgQuickFixImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_PROBLEM);
94         return fgQuickFixImage;
95     }
96
97     private Image getQuickFixErrorImage() {
98         if (fgQuickFixErrorImage == null)
99             fgQuickFixErrorImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_ERROR);
100         return fgQuickFixErrorImage;
101     }
102
103     private ImageRegistry getImageRegistry(Display display) {
104         if (fgImageRegistry == null)
105             fgImageRegistry= new ImageRegistry(display);
106         return fgImageRegistry;
107     }
108
109     private int getImageType(IJavaAnnotation annotation) {
110         int imageType= NO_IMAGE;
111         if (annotation.hasOverlay())
112             imageType= OVERLAY_IMAGE;
113         else if (!annotation.isMarkedDeleted()) {
114             if (showQuickFix(annotation))
115                 imageType= JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(annotation.getType()) ? QUICKFIX_ERROR_IMAGE : QUICKFIX_IMAGE;
116         } else {
117             imageType= GRAY_IMAGE;
118         }
119         return imageType;
120     }
121
122     private Image getImage(IJavaAnnotation annotation, int imageType, Display display) {
123         if ((imageType == QUICKFIX_IMAGE || imageType == QUICKFIX_ERROR_IMAGE) && fCachedImageType == imageType)
124             return fCachedImage;
125
126         Image image= null;
127         switch (imageType) {
128             case OVERLAY_IMAGE:
129                 IJavaAnnotation overlay= annotation.getOverlay();
130                 image= getManagedImage((Annotation) overlay);
131                 fCachedImageType= -1;
132                 break;
133             case QUICKFIX_IMAGE:
134                 image= getQuickFixImage();
135                 fCachedImageType= imageType;
136                 fCachedImage= image;
137                 break;
138             case QUICKFIX_ERROR_IMAGE:
139                 image= getQuickFixErrorImage();
140                 fCachedImageType= imageType;
141                 fCachedImage= image;
142                 break;
143             case GRAY_IMAGE: {
144                 ISharedImages sharedImages= PlatformUI.getWorkbench().getSharedImages();
145                 String JavaDoc annotationType= annotation.getType();
146                 if (JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(annotationType)) {
147                     image= sharedImages.getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
148                 } else if (JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE.equals(annotationType)) {
149                     image= sharedImages.getImage(ISharedImages.IMG_OBJS_WARN_TSK);
150                 } else if (JavaMarkerAnnotation.INFO_ANNOTATION_TYPE.equals(annotationType)) {
151                     image= sharedImages.getImage(ISharedImages.IMG_OBJS_INFO_TSK);
152                 }
153                 if (image != null) {
154                     ImageRegistry registry= getImageRegistry(display);
155                     String JavaDoc key= Integer.toString(image.hashCode());
156                     Image grayImage= registry.get(key);
157                     if (grayImage == null) {
158                         grayImage= new Image(display, image, SWT.IMAGE_GRAY);
159                         registry.put(key, grayImage);
160                     }
161                     image= grayImage;
162                 }
163                 fCachedImageType= -1;
164                 break;
165             }
166         }
167
168         return image;
169     }
170 }
171
Popular Tags