1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 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 44 public class MarkerAnnotation extends SimpleMarkerAnnotation { 45 46 53 public final static int PROBLEM_LAYER= 5; 54 55 56 private static Map fgImageRegistry; 57 58 69 protected static Image getImage(Display display, ImageDescriptor descriptor) { 70 Map 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 89 protected static Map getImageRegistry(Display display) { 90 if (fgImageRegistry == null) { 91 fgImageRegistry= new HashMap (); 92 display.disposeExec(new Runnable () { 93 public void run() { 94 if (fgImageRegistry != null) { 95 Map map= fgImageRegistry; 96 fgImageRegistry= null; 97 Iterator 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 112 private Image fImage; 113 114 private String fImageName; 115 116 private int fPresentationLayer= -1; 117 118 123 public MarkerAnnotation(IMarker marker) { 124 super(marker); 125 } 126 127 134 public MarkerAnnotation(String annotationType, IMarker marker) { 135 super(annotationType, marker); 136 initialize(); 137 } 138 139 147 protected void setImage(Image image) { 148 fImage= image; 149 } 150 151 159 protected void initialize() { 160 IMarker marker= getMarker(); 161 String 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 194 public int getLayer() { 195 if (fPresentationLayer != -1) 196 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 216 protected void setLayer(int layer) { 217 fPresentationLayer= layer; 218 } 219 220 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 243 public void update() { 244 super.update(); 245 initialize(); 246 } 247 248 259 protected String getUnknownImageName(IMarker marker) { 260 return null; 261 } 262 263 274 protected Image getImage(String name) { 275 if (name != null) 276 return PlatformUI.getWorkbench().getSharedImages().getImage(name); 277 return null; 278 } 279 280 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 |