Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / JavaMarkerAnnotation.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpeclipse.phpeditor;
13
14
15 import java.util.Iterator;
16
17 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
18 import net.sourceforge.phpdt.ui.PreferenceConstants;
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20
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;
34
35
36
37 public class JavaMarkerAnnotation extends MarkerAnnotation implements IJavaAnnotation {
38         
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;
46
47         private static Image fgQuickFixImage;
48         private static Image fgQuickFixErrorImage;
49         private static ImageRegistry fgGrayMarkersImageRegistry;
50         
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;
57         
58         
59         public JavaMarkerAnnotation(IMarker marker) {
60                 super(marker);
61         }
62         
63         /*
64          * @see MarkerAnnotation#getUnknownImageName(IMarker)
65          */
66         protected String getUnknownImageName(IMarker marker) {
67                 return PHPUiImages.IMG_OBJS_GHOST;
68         }
69         
70         /**
71          * Initializes the annotation's icon representation and its drawing layer
72          * based upon the properties of the underlying marker.
73          */
74         protected void initialize() {
75                 fQuickFixIconEnabled= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
76                 fImageType= NO_IMAGE;
77                 IMarker marker= getMarker();
78                 
79                 if (MarkerUtilities.isMarkerType(marker, IBreakpoint.BREAKPOINT_MARKER)) {
80                         
81                         if (fPresentation == null) 
82                                 fPresentation= DebugUITools.newDebugModelPresentation();
83
84                         setImage(null); // see bug 32469
85                         setLayer(4);
86                         fImageType= BREAKPOINT_IMAGE;                                   
87                         
88                         fType= AnnotationType.UNKNOWN;
89                         
90                 } else {
91                         
92                         fType= AnnotationType.UNKNOWN;
93                         if (marker.exists()) {
94                                 try {
95                                         
96                                         if (marker.isSubtypeOf(IMarker.PROBLEM)) {
97                                                 int severity= marker.getAttribute(IMarker.SEVERITY, -1);
98                                                 switch (severity) {
99                                                         case IMarker.SEVERITY_ERROR:
100                                                                 fType= AnnotationType.ERROR;
101                                                                 break;
102                                                         case IMarker.SEVERITY_WARNING:
103                                                                 fType= AnnotationType.WARNING;
104                                                                 break;
105                                                 }
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;
112                                                 
113                                 } catch(CoreException e) {
114                                         PHPeclipsePlugin.log(e);
115                                 }
116                         }
117                         super.initialize();
118                 }
119         }
120         
121 //      private boolean mustShowQuickFixIcon() {
122 //              return fQuickFixIconEnabled && JavaCorrectionProcessor.hasCorrections(getMarker());
123 //      }
124 //      
125 //      private Image getQuickFixImage() {
126 //              if (fgQuickFixImage == null)
127 //                      fgQuickFixImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_PROBLEM);
128 //              return fgQuickFixImage;
129 //      }
130 //
131 //      private Image getQuickFixErrorImage() {
132 //              if (fgQuickFixErrorImage == null)
133 //                      fgQuickFixErrorImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_ERROR);
134 //              return fgQuickFixErrorImage;
135 //      }
136
137         /*
138          * @see IJavaAnnotation#getMessage()
139          */
140         public String getMessage() {
141                 IMarker marker= getMarker();
142                 if (marker == null || !marker.exists())
143                         return ""; //$NON-NLS-1$
144                 else
145                         return marker.getAttribute(IMarker.MESSAGE, ""); //$NON-NLS-1$
146         }
147
148         /*
149          * @see IJavaAnnotation#isTemporary()
150          */
151         public boolean isTemporary() {
152                 return false;
153         }
154         
155         /*
156          * @see IJavaAnnotation#getArguments()
157          */
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$
162                 return null;
163         }
164
165         /*
166          * @see IJavaAnnotation#getId()
167          */
168         public int getId() {
169                 IMarker marker= getMarker();
170 //              if (marker != null  && marker.exists() && isProblem())
171 //                      return marker.getAttribute(IJavaModelMarker.ID, -1);
172                 return -1;
173         }
174         
175         /*
176          * @see IJavaAnnotation#isProblem()
177          */
178         public boolean isProblem() {
179                 return fType == AnnotationType.WARNING || fType == AnnotationType.ERROR;
180         }
181         
182         /*
183          * @see IJavaAnnotation#isRelevant()
184          */
185         public boolean isRelevant() {
186                 return !fNotRelevant;
187         }
188
189         /**
190          * Overlays this annotation with the given javaAnnotation.
191          * 
192          * @param javaAnnotation annotation that is overlaid by this annotation
193          */
194         public void setOverlay(IJavaAnnotation javaAnnotation) {
195                 if (fOverlay != null)
196                         fOverlay.removeOverlaid(this);
197                         
198                 fOverlay= javaAnnotation;
199                 fNotRelevant= (fNotRelevant || fOverlay != null);
200                 
201                 if (javaAnnotation != null)
202                         javaAnnotation.addOverlaid(this);
203         }
204         
205         /*
206          * @see IJavaAnnotation#hasOverlay()
207          */
208         public boolean hasOverlay() {
209                 return fOverlay != null;
210         }
211         
212         /*
213          * @see MarkerAnnotation#getImage(Display)
214          */
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());
222                                         setImage(result);
223                                 }
224                         }                                       
225                         return result;
226                 }
227
228                 int newImageType= NO_IMAGE;
229
230                 if (hasOverlay())
231                         newImageType= OVERLAY_IMAGE;
232                 else if (isRelevant()) {
233 //                      if (mustShowQuickFixIcon()) {
234 //                              if (fType == AnnotationType.ERROR)
235 //                                      newImageType= QUICKFIX_ERROR_IMAGE;
236 //                              else
237 //                                      newImageType= QUICKFIX_IMAGE; 
238 //                      } else
239                                 newImageType= ORIGINAL_MARKER_IMAGE; 
240                 } else
241                         newImageType= GRAY_IMAGE;
242
243                 if (fImageType == newImageType && newImageType != OVERLAY_IMAGE)
244                         // Nothing changed - simply return the current image
245                         return super.getImage(display);
246
247                 Image newImage= null;
248                 switch (newImageType) {
249                         case ORIGINAL_MARKER_IMAGE:
250                                 newImage= null;
251                                 break;
252                         case OVERLAY_IMAGE:
253                                 newImage= fOverlay.getImage(display);
254                                 break;
255 //                      case QUICKFIX_IMAGE:
256 //                              newImage= getQuickFixImage();
257 //                              break;
258 //                      case QUICKFIX_ERROR_IMAGE:
259 //                              newImage= getQuickFixErrorImage();
260 //                              break;
261                         case GRAY_IMAGE:
262                                 if (fImageType != ORIGINAL_MARKER_IMAGE)
263                                         setImage(null);
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);
273                                                 }
274                                                 newImage= grayImage;
275                                         }
276                                 }
277                                 break;
278                         default:
279                                 Assert.isLegal(false);
280                 }
281
282                 fImageType= newImageType;
283                 setImage(newImage);
284                 return super.getImage(display);
285         }
286         
287         private ImageRegistry getGrayMarkerImageRegistry(Display display) {
288                 if (fgGrayMarkersImageRegistry == null)
289                         fgGrayMarkersImageRegistry= new ImageRegistry(display);
290                 return fgGrayMarkersImageRegistry;
291         }
292         
293         /*
294          * @see IJavaAnnotation#addOverlaid(IJavaAnnotation)
295          */
296         public void addOverlaid(IJavaAnnotation annotation) {
297                 // not supported
298         }
299
300         /*
301          * @see IJavaAnnotation#removeOverlaid(IJavaAnnotation)
302          */
303         public void removeOverlaid(IJavaAnnotation annotation) {
304                 // not supported
305         }
306         
307         /*
308          * @see IJavaAnnotation#getOverlaidIterator()
309          */
310         public Iterator getOverlaidIterator() {
311                 // not supported
312                 return null;
313         }
314         
315         /*
316          * @see org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation#getAnnotationType()
317          */
318         public AnnotationType getAnnotationType() {
319                 return fType;
320         }
321 }