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;
25 //import net.sourceforge.phpdt.internal.ui.text.correction.JavaCorrectionProcessor;
28 * Image provider for annotations based on Java problem markers.
32 public class JavaAnnotationImageProvider implements IAnnotationImageProvider {
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;
41 private static Image fgQuickFixImage;
42 private static Image fgQuickFixErrorImage;
43 private static ImageRegistry fgImageRegistry;
45 private boolean fShowQuickFixIcon;
46 private int fCachedImageType;
47 private Image fCachedImage;
50 public JavaAnnotationImageProvider() {
51 fShowQuickFixIcon= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
55 * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getManagedImage(org.eclipse.jface.text.source.Annotation)
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());
67 * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getImageDescriptorId(org.eclipse.jface.text.source.Annotation)
69 public String getImageDescriptorId(Annotation annotation) {
70 // unmanaged images are not supported
75 * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getImageDescriptor(java.lang.String)
77 public ImageDescriptor getImageDescriptor(String symbolicName) {
78 // unmanaged images are not supported
83 private boolean showQuickFix(IJavaAnnotation annotation) {
84 // return fShowQuickFixIcon && annotation.isProblem() && JavaCorrectionProcessor.hasCorrections(annotation);
88 private Image getQuickFixImage() {
89 if (fgQuickFixImage == null)
90 fgQuickFixImage= PHPUiImages.get(PHPUiImages.IMG_OBJS_FIXABLE_PROBLEM);
91 return fgQuickFixImage;
94 private Image getQuickFixErrorImage() {
95 if (fgQuickFixErrorImage == null)
96 fgQuickFixErrorImage= PHPUiImages.get(PHPUiImages.IMG_OBJS_FIXABLE_ERROR);
97 return fgQuickFixErrorImage;
100 private ImageRegistry getImageRegistry(Display display) {
101 if (fgImageRegistry == null)
102 fgImageRegistry= new ImageRegistry(display);
103 return fgImageRegistry;
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;
114 imageType= GRAY_IMAGE;
119 private Image getImage(IJavaAnnotation annotation, int imageType, Display display) {
120 if (fCachedImageType == imageType)
126 IJavaAnnotation overlay= annotation.getOverlay();
127 image= overlay.getImage(display);
130 image= getQuickFixImage();
132 case QUICKFIX_ERROR_IMAGE:
133 image= getQuickFixErrorImage();
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);
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);
159 fCachedImageType= imageType;