KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.lang.reflect.InvocationTargetException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.GC;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.Rectangle;
22 import org.eclipse.swt.widgets.Canvas;
23
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.resource.ImageRegistry;
26
27 import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
28 import org.eclipse.jface.text.quickassist.IQuickFixableAnnotation;
29 import org.eclipse.jface.text.source.Annotation;
30 import org.eclipse.jface.text.source.IAnnotationAccess;
31 import org.eclipse.jface.text.source.IAnnotationAccessExtension;
32 import org.eclipse.jface.text.source.IAnnotationAccessExtension2;
33 import org.eclipse.jface.text.source.IAnnotationPresentation;
34 import org.eclipse.jface.text.source.ImageUtilities;
35
36 import org.eclipse.ui.ISharedImages;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.ide.IDE;
39 import org.eclipse.ui.internal.editors.text.EditorsPlugin;
40 import org.eclipse.ui.internal.texteditor.AnnotationType;
41 import org.eclipse.ui.internal.texteditor.AnnotationTypeHierarchy;
42
43
44 /**
45  * Default class for accessing marker annotation properties.
46  *
47  * @since 2.1
48  */

49 public class DefaultMarkerAnnotationAccess implements IAnnotationAccess, IAnnotationAccessExtension, IAnnotationAccessExtension2 {
50
51     /**
52      * Constant for the unknown marker type.
53      *
54      * @deprecated As of 3.0, replaced by Annotation.TYPE_UNKNOWN
55      */

56     public static final String JavaDoc UNKNOWN= Annotation.TYPE_UNKNOWN;
57
58     /**
59      * Constant for the error system image.
60      * Value: <code>error</code>
61      *
62      * @since 3.0
63      */

64     public static final String JavaDoc ERROR_SYSTEM_IMAGE= "error"; //$NON-NLS-1$
65
/**
66      * Constant for the warning system image.
67      * Value: <code>warning</code>
68      *
69      * @since 3.0
70      */

71     public static final String JavaDoc WARNING_SYSTEM_IMAGE= "warning"; //$NON-NLS-1$
72
/**
73      * Constant for the info system image.
74      * Value: <code>info</code>
75      *
76      * @since 3.0
77      */

78     public static final String JavaDoc INFO_SYSTEM_IMAGE= "info"; //$NON-NLS-1$
79
/**
80      * Constant for the task system image.
81      * Value: <code>task</code>
82      *
83      * @since 3.0
84      */

85     public static final String JavaDoc TASK_SYSTEM_IMAGE= "task"; //$NON-NLS-1$
86
/**
87      * Constant for the bookmark system image.
88      * Value: <code>bookmark</code>
89      *
90      * @since 3.0
91      */

92     public static final String JavaDoc BOOKMARK_SYSTEM_IMAGE= "bookmark"; //$NON-NLS-1$
93

94     /**
95      * The mapping between external and internal symbolic system image names.
96      *
97      * @since 3.0
98      */

99     private final static Map JavaDoc MAPPING;
100
101     static {
102         MAPPING= new HashMap JavaDoc();
103         MAPPING.put(ERROR_SYSTEM_IMAGE, ISharedImages.IMG_OBJS_ERROR_TSK);
104         MAPPING.put(WARNING_SYSTEM_IMAGE, ISharedImages.IMG_OBJS_WARN_TSK);
105         MAPPING.put(INFO_SYSTEM_IMAGE, ISharedImages.IMG_OBJS_INFO_TSK);
106         MAPPING.put(TASK_SYSTEM_IMAGE, IDE.SharedImages.IMG_OBJS_TASK_TSK);
107         MAPPING.put(BOOKMARK_SYSTEM_IMAGE, IDE.SharedImages.IMG_OBJS_BKMRK_TSK);
108     }
109
110
111     /**
112      * The marker annotation preferences.
113      *
114      * @deprecated As of 3.0, no replacement
115      */

116     protected MarkerAnnotationPreferences fMarkerAnnotationPreferences;
117
118     /**
119      * An optional quick assist processor.
120      *
121      * @since 3.2
122      */

123     private IQuickAssistAssistant fQuickAssistAssistant;
124
125     /**
126      * Returns a new default marker annotation access with the given preferences.
127      *
128      * @param markerAnnotationPreferences
129      * @deprecated As of 3.0, replaced by {@link org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess#DefaultMarkerAnnotationAccess()}
130      */

131     public DefaultMarkerAnnotationAccess(MarkerAnnotationPreferences markerAnnotationPreferences) {
132         fMarkerAnnotationPreferences= markerAnnotationPreferences;
133     }
134
135     /**
136      * Creates a new default marker annotation access using the standard
137      * preference lookup strategy which is the one provided by the enclosing
138      * plug-in.
139      *
140      * @since 3.0
141      */

142     public DefaultMarkerAnnotationAccess() {
143     }
144
145     /*
146      * @see org.eclipse.jface.text.source.IAnnotationAccessExtension2#setQuickAssistAssistant(org.eclipse.jface.text.quickassist.IQuickAssistAssistant)
147      * @since 3.2
148      */

149     public void setQuickAssistAssistant(IQuickAssistAssistant assistant) {
150         fQuickAssistAssistant= assistant;
151     }
152
153     /**
154      * Returns the annotation preference for the given annotation.
155      *
156      * @param annotation the annotation
157      * @return the annotation preference for the given annotation or <code>null</code>
158      */

159     private AnnotationPreference getAnnotationPreference(Annotation annotation) {
160         AnnotationPreferenceLookup lookup= getAnnotationPreferenceLookup();
161         if (lookup != null)
162             return lookup.getAnnotationPreference(annotation);
163         return null;
164     }
165
166     /**
167      * Returns the annotation preference lookup used by this annotation access.
168      *
169      * @return the annotation preference lookup
170      * @since 3.0
171      */

172     protected AnnotationPreferenceLookup getAnnotationPreferenceLookup() {
173         return EditorsPlugin.getDefault().getAnnotationPreferenceLookup();
174     }
175
176     /*
177      * @see org.eclipse.jface.text.source.IAnnotationAccess#getType(org.eclipse.jface.text.source.Annotation)
178      */

179     public Object JavaDoc getType(Annotation annotation) {
180         return annotation.getType();
181     }
182
183     /*
184      * @see org.eclipse.jface.text.source.IAnnotationAccess#isMultiLine(org.eclipse.jface.text.source.Annotation)
185      */

186     public boolean isMultiLine(Annotation annotation) {
187         return true;
188     }
189
190     /*
191      * @see org.eclipse.jface.text.source.IAnnotationAccess#isTemporary(org.eclipse.jface.text.source.Annotation)
192      */

193     public boolean isTemporary(Annotation annotation) {
194         return !annotation.isPersistent();
195     }
196
197     /*
198      * @see org.eclipse.jface.text.source.IAnnotationAccessExtension#getLabel(org.eclipse.jface.text.source.Annotation)
199      * @since 3.0
200      */

201     public String JavaDoc getTypeLabel(Annotation annotation) {
202         AnnotationPreference preference= getAnnotationPreference(annotation);
203         return preference != null ? preference.getPreferenceLabel() : null;
204     }
205
206     /*
207      * @see org.eclipse.jface.text.source.IAnnotationAccessExtension#getLayer(org.eclipse.jface.text.source.Annotation)
208      * @since 3.0
209      */

210     public int getLayer(Annotation annotation) {
211         if (annotation instanceof IAnnotationPresentation) {
212             IAnnotationPresentation presentation= (IAnnotationPresentation) annotation;
213             return presentation.getLayer();
214         }
215         
216         AnnotationPreference preference= getAnnotationPreference(annotation);
217         if (preference != null)
218             return preference.getPresentationLayer();
219         
220         // backward compatibility, ignore exceptions, just return default layer
221
try {
222
223             Method JavaDoc method= annotation.getClass().getMethod("getLayer", null); //$NON-NLS-1$
224
Integer JavaDoc result= (Integer JavaDoc) method.invoke(annotation, null);
225             return result.intValue();
226
227         } catch (SecurityException JavaDoc x) {
228         } catch (IllegalArgumentException JavaDoc x) {
229         } catch (NoSuchMethodException JavaDoc x) {
230         } catch (IllegalAccessException JavaDoc x) {
231         } catch (InvocationTargetException JavaDoc x) {
232         }
233
234         return IAnnotationAccessExtension.DEFAULT_LAYER;
235     }
236
237     /*
238      * @see org.eclipse.jface.text.source.IAnnotationAccessExtension#paint(org.eclipse.jface.text.source.Annotation, org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
239      * @since 3.0
240      */

241     public void paint(Annotation annotation, GC gc, Canvas canvas, Rectangle bounds) {
242
243         if (annotation instanceof IAnnotationPresentation) {
244             IAnnotationPresentation presentation= (IAnnotationPresentation) annotation;
245             presentation.paint(gc, canvas, bounds);
246             return;
247         }
248
249         AnnotationPreference preference= getAnnotationPreference(annotation);
250         if (preference != null) {
251             Object JavaDoc type= getType(annotation);
252             String JavaDoc annotationType= (type == null ? null : type.toString());
253             Image image= getImage(annotation, preference, annotationType);
254             if (image != null) {
255                 ImageUtilities.drawImage(image, gc, canvas, bounds, SWT.CENTER, SWT.TOP);
256                 return;
257             }
258         }
259
260         // backward compatibility, ignore exceptions, just don't paint
261
try {
262
263             Method JavaDoc method= annotation.getClass().getMethod("paint", new Class JavaDoc[] { GC.class, Canvas.class, Rectangle.class }); //$NON-NLS-1$
264
method.invoke(annotation, new Object JavaDoc[] {gc, canvas, bounds });
265
266         } catch (SecurityException JavaDoc x) {
267         } catch (IllegalArgumentException JavaDoc x) {
268         } catch (NoSuchMethodException JavaDoc x) {
269         } catch (IllegalAccessException JavaDoc x) {
270         } catch (InvocationTargetException JavaDoc x) {
271         }
272     }
273
274     /*
275      * @see org.eclipse.jface.text.source.IAnnotationAccessExtension#isPaintable(org.eclipse.jface.text.source.Annotation)
276      * @since 3.0
277      */

278     public boolean isPaintable(Annotation annotation) {
279         if (annotation instanceof IAnnotationPresentation)
280             return true;
281
282         AnnotationPreference preference= getAnnotationPreference(annotation);
283         if (preference == null)
284             return false;
285
286         Object JavaDoc type= getType(annotation);
287         String JavaDoc annotationType= (type == null ? null : type.toString());
288         Image image= getImage(annotation, preference, annotationType);
289         return image != null;
290     }
291
292     /*
293      * @see org.eclipse.jface.text.source.IAnnotationAccessExtension#isSubtype(java.lang.Object, java.lang.Object)
294      */

295     public boolean isSubtype(Object JavaDoc annotationType, Object JavaDoc potentialSupertype) {
296         AnnotationTypeHierarchy hierarchy= getAnnotationTypeHierarchy();
297         return hierarchy.isSubtype(potentialSupertype.toString(), annotationType.toString());
298     }
299
300     /*
301      * @see org.eclipse.jface.text.source.IAnnotationAccessExtension#getSupertypes(java.lang.Object)
302      */

303     public Object JavaDoc[] getSupertypes(Object JavaDoc annotationType) {
304         AnnotationTypeHierarchy hierarchy= getAnnotationTypeHierarchy();
305         AnnotationType type= hierarchy.getAnnotationType(annotationType.toString());
306         return type.getSuperTypes();
307     }
308
309     /**
310      * Returns the annotation type hierarchy used by this annotation access.
311      *
312      * @return the annotation type hierarchy
313      * @since 3.0
314      */

315     protected AnnotationTypeHierarchy getAnnotationTypeHierarchy() {
316         return EditorsPlugin.getDefault().getAnnotationTypeHierarchy();
317     }
318
319     /**
320      * Translates the given symbolic image name into the according symbolic image name
321      * the {@link org.eclipse.ui.ISharedImages} understands.
322      *
323      * @param symbolicImageName the symbolic system image name to be translated
324      * @return the shared image name
325      * @since 3.0
326      */

327     private String JavaDoc translateSymbolicImageName(String JavaDoc symbolicImageName) {
328         return (String JavaDoc) MAPPING.get(symbolicImageName);
329     }
330
331     /**
332      * Returns the image for the given annotation and the given annotation preferences or
333      * <code>null</code> if there is no such image.
334      *
335      * @param annotation the annotation
336      * @param preference the annotation preference
337      * @param annotationType the annotation type
338      * @return the image or <code>null</code>
339      * @since 3.0
340      */

341     private Image getImage(Annotation annotation, AnnotationPreference preference, String JavaDoc annotationType) {
342
343         ImageRegistry registry= EditorsPlugin.getDefault().getImageRegistry();
344
345         IAnnotationImageProvider annotationImageProvider = preference.getAnnotationImageProvider();
346         if (annotationImageProvider != null) {
347
348             Image image= annotationImageProvider.getManagedImage(annotation);
349             if (image != null)
350                 return image;
351
352             String JavaDoc id= annotationImageProvider.getImageDescriptorId(annotation);
353             if (id != null) {
354                 image= registry.get(id);
355                 if (image == null) {
356                     ImageDescriptor descriptor= annotationImageProvider.getImageDescriptor(id);
357                     registry.put(id, descriptor);
358                     image= registry.get(id);
359                 }
360                 return image;
361             }
362         }
363
364         if (annotationType == null)
365             return null;
366
367         if (hasQuickFix(annotation)) {
368             ImageDescriptor quickFixImageDesc= preference.getQuickFixImageDescriptor();
369             if (quickFixImageDesc != null) {
370                 Image image= registry.get(quickFixImageDesc.toString());
371                 if (image == null) {
372                     registry.put(quickFixImageDesc.toString(), quickFixImageDesc);
373                     image= registry.get(quickFixImageDesc.toString());
374                 }
375                 if (image != null)
376                     return image;
377             }
378         }
379
380         Image image= registry.get(annotationType);
381         if (image == null) {
382             ImageDescriptor descriptor= preference.getImageDescriptor();
383             if (descriptor != null) {
384                 registry.put(annotationType, descriptor);
385                 image= registry.get(annotationType);
386             } else {
387                 String JavaDoc key= translateSymbolicImageName(preference.getSymbolicImageName());
388                 if (key != null) {
389                     ISharedImages sharedImages= PlatformUI.getWorkbench().getSharedImages();
390                     image= sharedImages.getImage(key);
391                 }
392             }
393         }
394         return image;
395     }
396
397     /**
398      * Checks whether there's a quick assist assistant and if so,
399      * whether the assistant has a possible fix for the given
400      * annotation.
401      *
402      * @param annotation the annotation
403      * @return <code>true</code> if there is quick fix
404      * @since 3.2
405      */

406     protected boolean hasQuickFix(Annotation annotation) {
407         if (annotation instanceof IQuickFixableAnnotation) {
408             IQuickFixableAnnotation quickFixableAnnotation= (IQuickFixableAnnotation)annotation;
409             if (!quickFixableAnnotation.isQuickFixableStateSet())
410                 quickFixableAnnotation.setQuickFixable(fQuickAssistAssistant != null && fQuickAssistAssistant.canFix(annotation));
411             return quickFixableAnnotation.isQuickFixable();
412         }
413         return false;
414     }
415
416 }
417
Popular Tags