001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.Map;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.graphics.GC;
018: import org.eclipse.swt.graphics.Image;
019: import org.eclipse.swt.graphics.Rectangle;
020: import org.eclipse.swt.widgets.Canvas;
021: import org.eclipse.swt.widgets.Display;
022:
023: import org.eclipse.core.runtime.Assert;
024:
025: import org.eclipse.core.resources.IMarker;
026:
027: import org.eclipse.jface.resource.ImageDescriptor;
028:
029: import org.eclipse.jface.text.quickassist.IQuickFixableAnnotation;
030: import org.eclipse.jface.text.source.IAnnotationAccessExtension;
031: import org.eclipse.jface.text.source.ImageUtilities;
032:
033: import org.eclipse.ui.ISharedImages;
034: import org.eclipse.ui.PlatformUI;
035: import org.eclipse.ui.ide.IDE;
036: import org.eclipse.ui.internal.editors.text.EditorsPlugin;
037: import org.eclipse.ui.model.IWorkbenchAdapter;
038:
039: /**
040: * Annotation representing a marker on a resource in the workspace.
041: * This class may be instantiated or be subclassed.
042: *
043: * @see org.eclipse.core.resources.IMarker
044: */
045: public class MarkerAnnotation extends SimpleMarkerAnnotation implements
046: IQuickFixableAnnotation {
047:
048: /**
049: * The layer in which markers representing problem are located.
050: *
051: * @since 2.0
052: * @deprecated As of 3.0, replaced by {@link IAnnotationAccessExtension}
053:
054: */
055: public final static int PROBLEM_LAYER = 5;
056:
057: /** Internal image registry. */
058: private static Map fgImageRegistry;
059:
060: /**
061: * Returns an image for the given display as specified by the given image
062: * descriptor.
063: *
064: * @param display the display
065: * @param descriptor the image descriptor
066: * @return an image for the display as specified by the descriptor
067: * @deprecated As of 3.0, visual presentation is no longer supported,
068: * annotation with a visible presentation should implement
069: * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
070: */
071: protected static Image getImage(Display display,
072: ImageDescriptor descriptor) {
073: Map map = getImageRegistry(display);
074: Image image = (Image) map.get(descriptor);
075: if (image == null) {
076: image = descriptor.createImage();
077: map.put(descriptor, image);
078: }
079: return image;
080: }
081:
082: /**
083: * Returns an image registry for the given display. If no such registry
084: * exists the registry is created.
085: *
086: * @param display the display
087: * @return the image registry for the given display
088: * @deprecated As of 3.0, visual presentation is no longer supported,
089: * annotation with a visible presentation should implement
090: * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
091: */
092: protected static Map getImageRegistry(Display display) {
093: if (fgImageRegistry == null) {
094: fgImageRegistry = new HashMap();
095: display.disposeExec(new Runnable() {
096: public void run() {
097: if (fgImageRegistry != null) {
098: Map map = fgImageRegistry;
099: fgImageRegistry = null;
100: Iterator e = map.values().iterator();
101: while (e.hasNext()) {
102: Image image = (Image) e.next();
103: if (!image.isDisposed())
104: image.dispose();
105: }
106: }
107: }
108: });
109: }
110: return fgImageRegistry;
111: }
112:
113: /** The image, i.e., visual presentation of this annotation. */
114: private Image fImage;
115: /** The image name. */
116: private String fImageName;
117: /** The presentation layer. */
118: private int fPresentationLayer = -1;
119:
120: /**
121: * Tells whether this annotation is quick fixable.
122: * @since 3.4
123: */
124: private boolean fIsQuickFixable;
125: /**
126: * Tells whether the quick fixable state (<code>fIsQuickFixable</code> has been computed.
127: * @since 3.4
128: */
129: private boolean fIsQuickFixableStateSet;
130:
131: /**
132: * Creates a new annotation for the given marker.
133: *
134: * @param marker the marker
135: */
136: public MarkerAnnotation(IMarker marker) {
137: super (marker);
138: }
139:
140: /**
141: * Creates a new annotation of the given type for the given marker.
142: *
143: * @param annotationType the annotation type
144: * @param marker the marker
145: * @since 3.0
146: */
147: public MarkerAnnotation(String annotationType, IMarker marker) {
148: super (annotationType, marker);
149: initialize();
150: }
151:
152: /**
153: * Sets the marker image to the given image.
154: *
155: * @param image the new marker image
156: * @deprecated As of 3.0, visual presentation is no longer supported,
157: * annotation with a visible presentation should implement
158: * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
159: */
160: protected void setImage(Image image) {
161: fImage = image;
162: }
163:
164: /**
165: * Initializes the annotation's icon representation and its drawing layer
166: * based upon the properties of the underlying marker.
167: *
168: * @deprecated As of 3.0, visual presentation is no longer supported,
169: * annotation with a visible presentation should implement
170: * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
171: */
172: protected void initialize() {
173: IMarker marker = getMarker();
174: String name = getUnknownImageName(marker);
175:
176: if (MarkerUtilities.isMarkerType(marker, IMarker.TASK)) {
177: name = IDE.SharedImages.IMG_OBJS_TASK_TSK;
178: } else if (MarkerUtilities.isMarkerType(marker,
179: IMarker.BOOKMARK)) {
180: name = IDE.SharedImages.IMG_OBJS_BKMRK_TSK;
181: } else if (MarkerUtilities
182: .isMarkerType(marker, IMarker.PROBLEM)) {
183: switch (MarkerUtilities.getSeverity(marker)) {
184: case IMarker.SEVERITY_INFO:
185: name = ISharedImages.IMG_OBJS_INFO_TSK;
186: break;
187: case IMarker.SEVERITY_WARNING:
188: name = ISharedImages.IMG_OBJS_WARN_TSK;
189: break;
190: case IMarker.SEVERITY_ERROR:
191: name = ISharedImages.IMG_OBJS_ERROR_TSK;
192: break;
193: }
194: }
195:
196: fImage = null;
197: fImageName = name;
198: }
199:
200: /**
201: * Returns the annotations drawing layer.
202: * <p>
203: * Note: This is only for backward compatibility.
204: * </p>
205: * @return the annotations drawing layer
206: * @deprecated As of 3.0, replaced by {@link org.eclipse.jface.text.source.IAnnotationAccessExtension#getLayer(org.eclipse.jface.text.source.Annotation)}
207: * @since 3.0
208: */
209: public int getLayer() {
210: if (fPresentationLayer != -1)
211: // Backward compatibility
212: return fPresentationLayer;
213:
214: AnnotationPreference preference = EditorsPlugin.getDefault()
215: .getAnnotationPreferenceLookup()
216: .getAnnotationPreference(this );
217: if (preference != null)
218: return preference.getPresentationLayer();
219: return IAnnotationAccessExtension.DEFAULT_LAYER;
220: }
221:
222: /**
223: * Sets the layer of this annotation.
224: * <p>
225: * Note: This is only for backward compatibility.
226: * </p>
227: * @param layer the layer of this annotation
228: * @deprecated As of 3.0, annotation with a visible presentation should implement
229: * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
230: *
231: * @since 3.0
232: */
233: protected void setLayer(int layer) {
234: fPresentationLayer = layer;
235: }
236:
237: /**
238: * Implement this method to draw a graphical representation of this
239: * annotation within the given bounds. This default implementation does
240: * nothing.
241: * <p>
242: * Note: This is only for backward compatibility.
243: * </p>
244: * @param gc the drawing GC
245: * @param canvas the canvas to draw on
246: * @param r the bounds inside the canvas to draw on
247: * @deprecated As of 3.0 replaced by {@link org.eclipse.jface.text.source.IAnnotationAccessExtension#paint(org.eclipse.jface.text.source.Annotation, GC, Canvas, Rectangle)}
248: * @since 3.0
249: */
250: public void paint(GC gc, Canvas canvas, Rectangle r) {
251: Image image = getImage(canvas.getDisplay());
252: if (image != null)
253: ImageUtilities.drawImage(image, gc, canvas, r, SWT.CENTER,
254: SWT.TOP);
255: }
256:
257: /**
258: * Informs this annotation about changes applied to its underlying marker
259: * and adapts to those changes.
260: */
261: public void update() {
262: super .update();
263: initialize();
264: }
265:
266: /**
267: * Returns the name of an image used to visually represent markers of
268: * unknown type. This implementation returns <code>null</code>.
269: * Subclasses may replace this method.
270: *
271: * @param marker the marker of unknown type
272: * @return the name of an image for markers of unknown type.
273: * @deprecated As of 3.0, visual presentation is no longer supported,
274: * annotation with a visible presentation should implement
275: * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
276: */
277: protected String getUnknownImageName(IMarker marker) {
278: return null;
279: }
280:
281: /**
282: * Returns the image of the given name. Subclasses may extend this method.
283: * If so, subclasses must assume responsibility for disposing the images
284: * they create.
285: *
286: * @param name the name of the requested image
287: * @return the image or <code>null</code> if there is no such image
288: * @deprecated As of 3.0, visual presentation is no longer supported,
289: * annotation with a visible presentation should implement
290: * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
291: */
292: protected Image getImage(String name) {
293: if (name != null)
294: return PlatformUI.getWorkbench().getSharedImages()
295: .getImage(name);
296: return null;
297: }
298:
299: /**
300: * Returns an image for this annotation. It first consults the workbench
301: * adapter for this annotation's marker. If none is defined, it tries to
302: * find an image for the image name of this annotation.
303: *
304: * @param display the display for which the image is requested
305: * @return the image for this annotation
306: * @deprecated As of 3.0, visual presentation is no longer supported,
307: * annotation with a visible presentation should implement
308: * {@link org.eclipse.jface.text.source.IAnnotationPresentation}
309: */
310: protected Image getImage(Display display) {
311: if (fImage == null) {
312:
313: IMarker marker = getMarker();
314: if (marker.exists()) {
315: IWorkbenchAdapter adapter = (IWorkbenchAdapter) marker
316: .getAdapter(IWorkbenchAdapter.class);
317: if (adapter != null) {
318: ImageDescriptor descriptor = adapter
319: .getImageDescriptor(marker);
320: if (descriptor != null)
321: fImage = getImage(display, descriptor);
322: }
323: }
324:
325: if (fImage == null)
326: fImage = getImage(fImageName);
327:
328: }
329: return fImage;
330: }
331:
332: /*
333: * @see org.eclipse.jface.text.quickassist.IQuickFixableAnnotation#setQuickFixable(boolean)
334: * @since 3.4
335: */
336: public void setQuickFixable(boolean state) {
337: fIsQuickFixable = state;
338: fIsQuickFixableStateSet = true;
339: }
340:
341: /*
342: * @see org.eclipse.jface.text.quickassist.IQuickFixableAnnotation#isQuickFixableStateSet()
343: * @since 3.4
344: */
345: public boolean isQuickFixableStateSet() {
346: return fIsQuickFixableStateSet;
347: }
348:
349: /*
350: * @see org.eclipse.jface.text.quickassist.IQuickFixableAnnotation#isQuickFixable()
351: * @since 3.4
352: */
353: public boolean isQuickFixable() {
354: Assert.isTrue(isQuickFixableStateSet());
355: return fIsQuickFixable;
356: }
357:
358: }
|