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 *******************************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
15 import java.util.Iterator;
17 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
18 import net.sourceforge.phpdt.ui.PreferenceConstants;
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import org.eclipse.core.resources.IMarker;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.debug.core.model.IBreakpoint;
24 import org.eclipse.debug.ui.DebugUITools;
25 import org.eclipse.debug.ui.IDebugModelPresentation;
26 import org.eclipse.jface.resource.ImageRegistry;
27 import org.eclipse.jface.text.Assert;
28 import org.eclipse.search.ui.SearchUI;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.ui.texteditor.MarkerAnnotation;
33 import org.eclipse.ui.texteditor.MarkerUtilities;
37 public class JavaMarkerAnnotation extends MarkerAnnotation implements IJavaAnnotation {
39 private static final int NO_IMAGE= 0;
40 private static final int ORIGINAL_MARKER_IMAGE= 1;
41 private static final int QUICKFIX_IMAGE= 2;
42 private static final int QUICKFIX_ERROR_IMAGE= 3;
43 private static final int OVERLAY_IMAGE= 4;
44 private static final int GRAY_IMAGE= 5;
45 private static final int BREAKPOINT_IMAGE= 6;
47 private static Image fgQuickFixImage;
48 private static Image fgQuickFixErrorImage;
49 private static ImageRegistry fgGrayMarkersImageRegistry;
51 private IDebugModelPresentation fPresentation;
52 private IJavaAnnotation fOverlay;
53 private boolean fNotRelevant= false;
54 private AnnotationType fType;
55 private int fImageType;
56 private boolean fQuickFixIconEnabled;
59 public JavaMarkerAnnotation(IMarker marker) {
64 * @see MarkerAnnotation#getUnknownImageName(IMarker)
66 protected String getUnknownImageName(IMarker marker) {
67 return PHPUiImages.IMG_OBJS_GHOST;
71 * Initializes the annotation's icon representation and its drawing layer
72 * based upon the properties of the underlying marker.
74 protected void initialize() {
75 fQuickFixIconEnabled= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
77 IMarker marker= getMarker();
79 if (MarkerUtilities.isMarkerType(marker, IBreakpoint.BREAKPOINT_MARKER)) {
81 if (fPresentation == null)
82 fPresentation= DebugUITools.newDebugModelPresentation();
84 setImage(null); // see bug 32469
86 fImageType= BREAKPOINT_IMAGE;
88 fType= AnnotationType.UNKNOWN;
92 fType= AnnotationType.UNKNOWN;
93 if (marker.exists()) {
96 if (marker.isSubtypeOf(IMarker.PROBLEM)) {
97 int severity= marker.getAttribute(IMarker.SEVERITY, -1);
99 case IMarker.SEVERITY_ERROR:
100 fType= AnnotationType.ERROR;
102 case IMarker.SEVERITY_WARNING:
103 fType= AnnotationType.WARNING;
106 } else if (marker.isSubtypeOf(IMarker.TASK))
107 fType= AnnotationType.TASK;
108 else if (marker.isSubtypeOf(SearchUI.SEARCH_MARKER))
109 fType= AnnotationType.SEARCH;
110 else if (marker.isSubtypeOf(IMarker.BOOKMARK))
111 fType= AnnotationType.BOOKMARK;
113 } catch(CoreException e) {
114 PHPeclipsePlugin.log(e);
121 // private boolean mustShowQuickFixIcon() {
122 // return fQuickFixIconEnabled && JavaCorrectionProcessor.hasCorrections(getMarker());
125 // private Image getQuickFixImage() {
126 // if (fgQuickFixImage == null)
127 // fgQuickFixImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_PROBLEM);
128 // return fgQuickFixImage;
131 // private Image getQuickFixErrorImage() {
132 // if (fgQuickFixErrorImage == null)
133 // fgQuickFixErrorImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_ERROR);
134 // return fgQuickFixErrorImage;
138 * @see IJavaAnnotation#getMessage()
140 public String getMessage() {
141 IMarker marker= getMarker();
142 if (marker == null || !marker.exists())
143 return ""; //$NON-NLS-1$
145 return marker.getAttribute(IMarker.MESSAGE, ""); //$NON-NLS-1$
149 * @see IJavaAnnotation#isTemporary()
151 public boolean isTemporary() {
156 * @see IJavaAnnotation#getArguments()
158 public String[] getArguments() {
159 IMarker marker= getMarker();
160 // if (marker != null && marker.exists() && isProblem())
161 // return Util.getProblemArgumentsFromMarker(marker.getAttribute(IJavaModelMarker.ARGUMENTS, "")); //$NON-NLS-1$
166 * @see IJavaAnnotation#getId()
169 IMarker marker= getMarker();
170 // if (marker != null && marker.exists() && isProblem())
171 // return marker.getAttribute(IJavaModelMarker.ID, -1);
176 * @see IJavaAnnotation#isProblem()
178 public boolean isProblem() {
179 return fType == AnnotationType.WARNING || fType == AnnotationType.ERROR;
183 * @see IJavaAnnotation#isRelevant()
185 public boolean isRelevant() {
186 return !fNotRelevant;
190 * Overlays this annotation with the given javaAnnotation.
192 * @param javaAnnotation annotation that is overlaid by this annotation
194 public void setOverlay(IJavaAnnotation javaAnnotation) {
195 if (fOverlay != null)
196 fOverlay.removeOverlaid(this);
198 fOverlay= javaAnnotation;
199 fNotRelevant= (fNotRelevant || fOverlay != null);
201 if (javaAnnotation != null)
202 javaAnnotation.addOverlaid(this);
206 * @see IJavaAnnotation#hasOverlay()
208 public boolean hasOverlay() {
209 return fOverlay != null;
213 * @see MarkerAnnotation#getImage(Display)
215 public Image getImage(Display display) {
216 if (fImageType == BREAKPOINT_IMAGE) {
217 Image result= super.getImage(display);
218 if (result == null) {
219 IMarker marker= getMarker();
220 if (marker != null && marker.exists()) {
221 result= fPresentation.getImage(getMarker());
228 int newImageType= NO_IMAGE;
231 newImageType= OVERLAY_IMAGE;
232 else if (isRelevant()) {
233 // if (mustShowQuickFixIcon()) {
234 // if (fType == AnnotationType.ERROR)
235 // newImageType= QUICKFIX_ERROR_IMAGE;
237 // newImageType= QUICKFIX_IMAGE;
239 newImageType= ORIGINAL_MARKER_IMAGE;
241 newImageType= GRAY_IMAGE;
243 if (fImageType == newImageType && newImageType != OVERLAY_IMAGE)
244 // Nothing changed - simply return the current image
245 return super.getImage(display);
247 Image newImage= null;
248 switch (newImageType) {
249 case ORIGINAL_MARKER_IMAGE:
253 newImage= fOverlay.getImage(display);
255 // case QUICKFIX_IMAGE:
256 // newImage= getQuickFixImage();
258 // case QUICKFIX_ERROR_IMAGE:
259 // newImage= getQuickFixErrorImage();
262 if (fImageType != ORIGINAL_MARKER_IMAGE)
264 Image originalImage= super.getImage(display);
265 if (originalImage != null) {
266 ImageRegistry imageRegistry= getGrayMarkerImageRegistry(display);
267 if (imageRegistry != null) {
268 String key= Integer.toString(originalImage.hashCode());
269 Image grayImage= imageRegistry.get(key);
270 if (grayImage == null) {
271 grayImage= new Image(display, originalImage, SWT.IMAGE_GRAY);
272 imageRegistry.put(key, grayImage);
279 Assert.isLegal(false);
282 fImageType= newImageType;
284 return super.getImage(display);
287 private ImageRegistry getGrayMarkerImageRegistry(Display display) {
288 if (fgGrayMarkersImageRegistry == null)
289 fgGrayMarkersImageRegistry= new ImageRegistry(display);
290 return fgGrayMarkersImageRegistry;
294 * @see IJavaAnnotation#addOverlaid(IJavaAnnotation)
296 public void addOverlaid(IJavaAnnotation annotation) {
301 * @see IJavaAnnotation#removeOverlaid(IJavaAnnotation)
303 public void removeOverlaid(IJavaAnnotation annotation) {
308 * @see IJavaAnnotation#getOverlaidIterator()
310 public Iterator getOverlaidIterator() {
316 * @see org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation#getAnnotationType()
318 public AnnotationType getAnnotationType() {