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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.phpeditor;
13 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
14 import net.sourceforge.phpdt.ui.PreferenceConstants;
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;
27 * Image provider for annotations based on Java problem markers.
31 public class JavaAnnotationImageProvider implements IAnnotationImageProvider {
33 private final static int NO_IMAGE = 0;
35 private final static int GRAY_IMAGE = 1;
37 private final static int OVERLAY_IMAGE = 2;
39 private final static int QUICKFIX_IMAGE = 3;
41 private final static int QUICKFIX_ERROR_IMAGE = 4;
43 private static Image fgQuickFixImage;
45 private static Image fgQuickFixErrorImage;
47 private static ImageRegistry fgImageRegistry;
49 private boolean fShowQuickFixIcon;
51 private int fCachedImageType;
53 private Image fCachedImage;
55 public JavaAnnotationImageProvider() {
56 fShowQuickFixIcon = PreferenceConstants.getPreferenceStore()
57 .getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
61 * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getManagedImage(org.eclipse.jface.text.source.Annotation)
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());
73 * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getImageDescriptorId(org.eclipse.jface.text.source.Annotation)
75 public String getImageDescriptorId(Annotation annotation) {
76 // unmanaged images are not supported
81 * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getImageDescriptor(java.lang.String)
83 public ImageDescriptor getImageDescriptor(String symbolicName) {
84 // unmanaged images are not supported
88 private boolean showQuickFix(IJavaAnnotation annotation) {
89 // return fShowQuickFixIcon && annotation.isProblem() &&
90 // JavaCorrectionProcessor.hasCorrections(annotation);
94 private Image getQuickFixImage() {
95 if (fgQuickFixImage == null)
96 fgQuickFixImage = PHPUiImages
97 .get(PHPUiImages.IMG_OBJS_FIXABLE_PROBLEM);
98 return fgQuickFixImage;
101 private Image getQuickFixErrorImage() {
102 if (fgQuickFixErrorImage == null)
103 fgQuickFixErrorImage = PHPUiImages
104 .get(PHPUiImages.IMG_OBJS_FIXABLE_ERROR);
105 return fgQuickFixErrorImage;
108 private ImageRegistry getImageRegistry(Display display) {
109 if (fgImageRegistry == null)
110 fgImageRegistry = new ImageRegistry(display);
111 return fgImageRegistry;
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
124 imageType = GRAY_IMAGE;
129 private Image getImage(IJavaAnnotation annotation, int imageType,
131 if (fCachedImageType == imageType)
137 IJavaAnnotation overlay = annotation.getOverlay();
138 image = overlay.getImage(display);
141 image = getQuickFixImage();
143 case QUICKFIX_ERROR_IMAGE:
144 image = getQuickFixErrorImage();
147 ISharedImages sharedImages = PlatformUI.getWorkbench()
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);
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);
174 fCachedImageType = imageType;
175 fCachedImage = image;