1 11 package org.eclipse.ui.texteditor; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.lang.reflect.Method ; 15 import java.util.HashMap ; 16 import java.util.Map ; 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 49 public class DefaultMarkerAnnotationAccess implements IAnnotationAccess, IAnnotationAccessExtension, IAnnotationAccessExtension2 { 50 51 56 public static final String UNKNOWN= Annotation.TYPE_UNKNOWN; 57 58 64 public static final String ERROR_SYSTEM_IMAGE= "error"; 71 public static final String WARNING_SYSTEM_IMAGE= "warning"; 78 public static final String INFO_SYSTEM_IMAGE= "info"; 85 public static final String TASK_SYSTEM_IMAGE= "task"; 92 public static final String BOOKMARK_SYSTEM_IMAGE= "bookmark"; 94 99 private final static Map MAPPING; 100 101 static { 102 MAPPING= new HashMap (); 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 116 protected MarkerAnnotationPreferences fMarkerAnnotationPreferences; 117 118 123 private IQuickAssistAssistant fQuickAssistAssistant; 124 125 131 public DefaultMarkerAnnotationAccess(MarkerAnnotationPreferences markerAnnotationPreferences) { 132 fMarkerAnnotationPreferences= markerAnnotationPreferences; 133 } 134 135 142 public DefaultMarkerAnnotationAccess() { 143 } 144 145 149 public void setQuickAssistAssistant(IQuickAssistAssistant assistant) { 150 fQuickAssistAssistant= assistant; 151 } 152 153 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 172 protected AnnotationPreferenceLookup getAnnotationPreferenceLookup() { 173 return EditorsPlugin.getDefault().getAnnotationPreferenceLookup(); 174 } 175 176 179 public Object getType(Annotation annotation) { 180 return annotation.getType(); 181 } 182 183 186 public boolean isMultiLine(Annotation annotation) { 187 return true; 188 } 189 190 193 public boolean isTemporary(Annotation annotation) { 194 return !annotation.isPersistent(); 195 } 196 197 201 public String getTypeLabel(Annotation annotation) { 202 AnnotationPreference preference= getAnnotationPreference(annotation); 203 return preference != null ? preference.getPreferenceLabel() : null; 204 } 205 206 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 try { 222 223 Method method= annotation.getClass().getMethod("getLayer", null); Integer result= (Integer ) method.invoke(annotation, null); 225 return result.intValue(); 226 227 } catch (SecurityException x) { 228 } catch (IllegalArgumentException x) { 229 } catch (NoSuchMethodException x) { 230 } catch (IllegalAccessException x) { 231 } catch (InvocationTargetException x) { 232 } 233 234 return IAnnotationAccessExtension.DEFAULT_LAYER; 235 } 236 237 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 type= getType(annotation); 252 String 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 try { 262 263 Method method= annotation.getClass().getMethod("paint", new Class [] { GC.class, Canvas.class, Rectangle.class }); method.invoke(annotation, new Object [] {gc, canvas, bounds }); 265 266 } catch (SecurityException x) { 267 } catch (IllegalArgumentException x) { 268 } catch (NoSuchMethodException x) { 269 } catch (IllegalAccessException x) { 270 } catch (InvocationTargetException x) { 271 } 272 } 273 274 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 type= getType(annotation); 287 String annotationType= (type == null ? null : type.toString()); 288 Image image= getImage(annotation, preference, annotationType); 289 return image != null; 290 } 291 292 295 public boolean isSubtype(Object annotationType, Object potentialSupertype) { 296 AnnotationTypeHierarchy hierarchy= getAnnotationTypeHierarchy(); 297 return hierarchy.isSubtype(potentialSupertype.toString(), annotationType.toString()); 298 } 299 300 303 public Object [] getSupertypes(Object annotationType) { 304 AnnotationTypeHierarchy hierarchy= getAnnotationTypeHierarchy(); 305 AnnotationType type= hierarchy.getAnnotationType(annotationType.toString()); 306 return type.getSuperTypes(); 307 } 308 309 315 protected AnnotationTypeHierarchy getAnnotationTypeHierarchy() { 316 return EditorsPlugin.getDefault().getAnnotationTypeHierarchy(); 317 } 318 319 327 private String translateSymbolicImageName(String symbolicImageName) { 328 return (String ) MAPPING.get(symbolicImageName); 329 } 330 331 341 private Image getImage(Annotation annotation, AnnotationPreference preference, String 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 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 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 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 |