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 org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.resource.ImageRegistry;
20 import org.eclipse.jface.text.source.Annotation;
22 import org.eclipse.ui.ISharedImages;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.texteditor.IAnnotationImageProvider;
26 import net.sourceforge.phpdt.ui.PreferenceConstants;
28 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
29 //import net.sourceforge.phpdt.internal.ui.text.correction.JavaCorrectionProcessor;
32 * Image provider for annotations based on Java problem markers.
36 public class JavaAnnotationImageProvider implements IAnnotationImageProvider {
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;
45 private static Image fgQuickFixImage;
46 private static Image fgQuickFixErrorImage;
47 private static ImageRegistry fgImageRegistry;
49 private boolean fShowQuickFixIcon;
50 private int fCachedImageType;
51 private Image fCachedImage;
54 public JavaAnnotationImageProvider() {
55 fShowQuickFixIcon= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
59 * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getManagedImage(org.eclipse.jface.text.source.Annotation)
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());
71 * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getImageDescriptorId(org.eclipse.jface.text.source.Annotation)
73 public String getImageDescriptorId(Annotation annotation) {
74 // unmanaged images are not supported
79 * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getImageDescriptor(java.lang.String)
81 public ImageDescriptor getImageDescriptor(String symbolicName) {
82 // unmanaged images are not supported
87 private boolean showQuickFix(IJavaAnnotation annotation) {
88 // return fShowQuickFixIcon && annotation.isProblem() && JavaCorrectionProcessor.hasCorrections(annotation);
92 private Image getQuickFixImage() {
93 if (fgQuickFixImage == null)
94 fgQuickFixImage= PHPUiImages.get(PHPUiImages.IMG_OBJS_FIXABLE_PROBLEM);
95 return fgQuickFixImage;
98 private Image getQuickFixErrorImage() {
99 if (fgQuickFixErrorImage == null)
100 fgQuickFixErrorImage= PHPUiImages.get(PHPUiImages.IMG_OBJS_FIXABLE_ERROR);
101 return fgQuickFixErrorImage;
104 private ImageRegistry getImageRegistry(Display display) {
105 if (fgImageRegistry == null)
106 fgImageRegistry= new ImageRegistry(display);
107 return fgImageRegistry;
110 private int getImageType(IJavaAnnotation annotation) {
111 int imageType= NO_IMAGE;
112 if (annotation.hasOverlay())
113 imageType= OVERLAY_IMAGE;
114 else if (!annotation.isMarkedDeleted()) {
115 if (showQuickFix(annotation))
116 imageType= JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(annotation.getType()) ? QUICKFIX_ERROR_IMAGE : QUICKFIX_IMAGE;
118 imageType= GRAY_IMAGE;
123 private Image getImage(IJavaAnnotation annotation, int imageType, Display display) {
124 if (fCachedImageType == imageType)
130 IJavaAnnotation overlay= annotation.getOverlay();
131 image= overlay.getImage(display);
134 image= getQuickFixImage();
136 case QUICKFIX_ERROR_IMAGE:
137 image= getQuickFixErrorImage();
140 ISharedImages sharedImages= PlatformUI.getWorkbench().getSharedImages();
141 String annotationType= annotation.getType();
142 if (JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(annotationType)) {
143 image= sharedImages.getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
144 } else if (JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE.equals(annotationType)) {
145 image= sharedImages.getImage(ISharedImages.IMG_OBJS_WARN_TSK);
146 } else if (JavaMarkerAnnotation.INFO_ANNOTATION_TYPE.equals(annotationType)) {
147 image= sharedImages.getImage(ISharedImages.IMG_OBJS_INFO_TSK);
150 ImageRegistry registry= getImageRegistry(display);
151 String key= Integer.toString(image.hashCode());
152 Image grayImage= registry.get(key);
153 if (grayImage == null) {
154 grayImage= new Image(display, image, SWT.IMAGE_GRAY);
155 registry.put(key, grayImage);
163 fCachedImageType= imageType;