KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > MarkerAnnotation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.texteditor;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.resources.IMarker;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Canvas;
24 import org.eclipse.swt.widgets.Display;
25
26 import org.eclipse.jface.resource.ImageDescriptor;
27
28 import org.eclipse.jface.text.source.IAnnotationAccessExtension;
29 import org.eclipse.jface.text.source.ImageUtilities;
30
31 import org.eclipse.ui.ISharedImages;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.ide.IDE;
34 import org.eclipse.ui.internal.editors.text.EditorsPlugin;
35 import org.eclipse.ui.model.IWorkbenchAdapter;
36
37
38 /**
39  * Annotation representing a marker on a resource in the workspace.
40  * This class may be instantiated or be subclassed.
41  *
42  * @see org.eclipse.core.resources.IMarker
43  */

44 public class MarkerAnnotation extends SimpleMarkerAnnotation {
45
46     /**
47      * The layer in which markers representing problem are located.
48      *
49      * @since 2.0
50      * @deprecated As of 3.0, replaced by {@link IAnnotationAccessExtension}
51
52      */

53     public final static int PROBLEM_LAYER= 5;
54
55     /** Internal image registry. */
56     private static Map JavaDoc fgImageRegistry;
57
58     /**
59      * Returns an image for the given display as specified by the given image
60      * descriptor.
61      *
62      * @param display the display
63      * @param descriptor the image descriptor
64      * @return an image for the display as specified by the descriptor
65      * @deprecated As of 3.0, visual presentation is no longer supported,
66      * annotation with a visible presentation should implement
67      * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
68      */

69     protected static Image getImage(Display display, ImageDescriptor descriptor) {
70         Map JavaDoc map= getImageRegistry(display);
71         Image image= (Image) map.get(descriptor);
72         if (image == null) {
73             image= descriptor.createImage();
74             map.put(descriptor, image);
75         }
76         return image;
77     }
78
79     /**
80      * Returns an image registry for the given display. If no such registry
81      * exists the registry is created.
82      *
83      * @param display the display
84      * @return the image registry for the given display
85      * @deprecated As of 3.0, visual presentation is no longer supported,
86      * annotation with a visible presentation should implement
87      * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
88      */

89     protected static Map JavaDoc getImageRegistry(Display display) {
90         if (fgImageRegistry == null) {
91             fgImageRegistry= new HashMap JavaDoc();
92             display.disposeExec(new Runnable JavaDoc() {
93                 public void run() {
94                     if (fgImageRegistry != null) {
95                         Map JavaDoc map= fgImageRegistry;
96                         fgImageRegistry= null;
97                         Iterator JavaDoc e= map.values().iterator();
98                         while (e.hasNext()) {
99                             Image image= (Image) e.next();
100                             if (!image.isDisposed())
101                                 image.dispose();
102                         }
103                     }
104                 }
105             });
106         }
107         return fgImageRegistry;
108     }
109
110
111     /** The image, i.e., visual presentation of this annotation. */
112     private Image fImage;
113     /** The image name. */
114     private String JavaDoc fImageName;
115     /** The presentation layer. */
116     private int fPresentationLayer= -1;
117
118     /**
119      * Creates a new annotation for the given marker.
120      *
121      * @param marker the marker
122      */

123     public MarkerAnnotation(IMarker marker) {
124         super(marker);
125     }
126
127     /**
128      * Creates a new annotation of the given type for the given marker.
129      *
130      * @param annotationType the annotation type
131      * @param marker the marker
132      * @since 3.0
133      */

134     public MarkerAnnotation(String JavaDoc annotationType, IMarker marker) {
135         super(annotationType, marker);
136         initialize();
137     }
138
139     /**
140      * Sets the marker image to the given image.
141      *
142      * @param image the new marker image
143      * @deprecated As of 3.0, visual presentation is no longer supported,
144      * annotation with a visible presentation should implement
145      * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
146      */

147     protected void setImage(Image image) {
148         fImage= image;
149     }
150
151     /**
152      * Initializes the annotation's icon representation and its drawing layer
153      * based upon the properties of the underlying marker.
154      *
155      * @deprecated As of 3.0, visual presentation is no longer supported,
156      * annotation with a visible presentation should implement
157      * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
158      */

159     protected void initialize() {
160         IMarker marker= getMarker();
161         String JavaDoc name= getUnknownImageName(marker);
162
163         if (MarkerUtilities.isMarkerType(marker, IMarker.TASK)) {
164             name= IDE.SharedImages.IMG_OBJS_TASK_TSK;
165         } else if (MarkerUtilities.isMarkerType(marker, IMarker.BOOKMARK)) {
166             name= IDE.SharedImages.IMG_OBJS_BKMRK_TSK;
167         } else if (MarkerUtilities.isMarkerType(marker, IMarker.PROBLEM)) {
168             switch (MarkerUtilities.getSeverity(marker)) {
169                 case IMarker.SEVERITY_INFO:
170                     name= ISharedImages.IMG_OBJS_INFO_TSK;
171                     break;
172                 case IMarker.SEVERITY_WARNING:
173                     name= ISharedImages.IMG_OBJS_WARN_TSK;
174                     break;
175                 case IMarker.SEVERITY_ERROR:
176                     name= ISharedImages.IMG_OBJS_ERROR_TSK;
177                     break;
178             }
179         }
180
181         fImage= null;
182         fImageName= name;
183     }
184
185     /**
186      * Returns the annotations drawing layer.
187      * <p>
188      * Note: This is only for backward compatibility.
189      * </p>
190      * @return the annotations drawing layer
191      * @deprecated As of 3.0, replaced by {@link org.eclipse.jface.text.source.IAnnotationAccessExtension#getLayer(org.eclipse.jface.text.source.Annotation)}
192      * @since 3.0
193      */

194     public int getLayer() {
195         if (fPresentationLayer != -1)
196             // Backward compatibility
197
return fPresentationLayer;
198
199         AnnotationPreference preference= EditorsPlugin.getDefault().getAnnotationPreferenceLookup().getAnnotationPreference(this);
200         if (preference != null)
201             return preference.getPresentationLayer();
202         return IAnnotationAccessExtension.DEFAULT_LAYER;
203     }
204
205     /**
206      * Sets the layer of this annotation.
207      * <p>
208      * Note: This is only for backward compatibility.
209      * </p>
210      * @param layer the layer of this annotation
211      * @deprecated As of 3.0, annotation with a visible presentation should implement
212      * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
213      *
214      * @since 3.0
215      */

216     protected void setLayer(int layer) {
217         fPresentationLayer= layer;
218     }
219
220     /**
221      * Implement this method to draw a graphical representation of this
222      * annotation within the given bounds. This default implementation does
223      * nothing.
224      * <p>
225      * Note: This is only for backward compatibility.
226      * </p>
227      * @param gc the drawing GC
228      * @param canvas the canvas to draw on
229      * @param r the bounds inside the canvas to draw on
230      * @deprecated As of 3.0 replaced by {@link org.eclipse.jface.text.source.IAnnotationAccessExtension#paint(org.eclipse.jface.text.source.Annotation, GC, Canvas, Rectangle)}
231      * @since 3.0
232      */

233     public void paint(GC gc, Canvas canvas, Rectangle r) {
234         Image image= getImage(canvas.getDisplay());
235         if (image != null)
236             ImageUtilities.drawImage(image, gc, canvas, r, SWT.CENTER, SWT.TOP);
237     }
238
239     /**
240      * Informs this annotation about changes applied to its underlying marker
241      * and adapts to those changes.
242      */

243     public void update() {
244         super.update();
245         initialize();
246     }
247
248     /**
249      * Returns the name of an image used to visually represent markers of
250      * unknown type. This implementation returns <code>null</code>.
251      * Subclasses may replace this method.
252      *
253      * @param marker the marker of unknown type
254      * @return the name of an image for markers of unknown type.
255      * @deprecated As of 3.0, visual presentation is no longer supported,
256      * annotation with a visible presentation should implement
257      * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
258      */

259     protected String JavaDoc getUnknownImageName(IMarker marker) {
260         return null;
261     }
262
263     /**
264      * Returns the image of the given name. Subclasses may extend this method.
265      * If so, subclasses must assume responsibility for disposing the images
266      * they create.
267      *
268      * @param name the name of the requested image
269      * @return the image or <code>null</code> if there is no such image
270      * @deprecated As of 3.0, visual presentation is no longer supported,
271      * annotation with a visible presentation should implement
272      * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
273      */

274     protected Image getImage(String JavaDoc name) {
275         if (name != null)
276             return PlatformUI.getWorkbench().getSharedImages().getImage(name);
277         return null;
278     }
279
280     /**
281      * Returns an image for this annotation. It first consults the workbench
282      * adapter for this annotation's marker. If none is defined, it tries to
283      * find an image for the image name of this annotation.
284      *
285      * @param display the display for which the image is requested
286      * @return the image for this annotation
287      * @deprecated As of 3.0, visual presentation is no longer supported,
288      * annotation with a visible presentation should implement
289      * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
290      */

291     protected Image getImage(Display display) {
292         if (fImage == null) {
293
294             IMarker marker= getMarker();
295             if (marker.exists()) {
296                 IWorkbenchAdapter adapter= (IWorkbenchAdapter) marker.getAdapter(IWorkbenchAdapter.class);
297                 if (adapter != null) {
298                     ImageDescriptor descriptor= adapter.getImageDescriptor(marker);
299                     if (descriptor != null)
300                         fImage= getImage(display, descriptor);
301                 }
302             }
303
304             if (fImage == null)
305                 fImage= getImage(fImageName);
306
307         }
308         return fImage;
309     }
310 }
311
Popular Tags