Skip to main content
summaryrefslogtreecommitdiffstats
blob: d4ec1e35f0fa197a7f6e0ae38a73b0eaae7c9752 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.texteditor;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;

import org.eclipse.core.runtime.Assert;

import org.eclipse.core.resources.IMarker;

import org.eclipse.jface.resource.ImageDescriptor;

import org.eclipse.jface.text.quickassist.IQuickFixableAnnotation;
import org.eclipse.jface.text.source.IAnnotationAccessExtension;
import org.eclipse.jface.text.source.ImageUtilities;

import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.editors.text.EditorsPlugin;
import org.eclipse.ui.model.IWorkbenchAdapter;


/**
 * Annotation representing a marker on a resource in the workspace.
 * This class may be instantiated or be subclassed.
 *
 * @see org.eclipse.core.resources.IMarker
 */
public class MarkerAnnotation extends SimpleMarkerAnnotation implements IQuickFixableAnnotation {

	/**
	 * The layer in which markers representing problem are located.
	 *
	 * @since 2.0
	 * @deprecated As of 3.0, replaced by {@link IAnnotationAccessExtension}

	 */
	@Deprecated
	public final static int PROBLEM_LAYER= 5;

	/** Internal image registry. */
	private static Map fgImageRegistry;

	/**
	 * Returns an image for the given display as specified by the given image
	 * descriptor.
	 *
	 * @param display the display
	 * @param descriptor the image descriptor
	 * @return an image for the display as specified by the descriptor
	 * @deprecated As of 3.0, visual presentation is no longer supported,
	 *             annotation with a visible presentation should implement
	 *             {@link org.eclipse.jface.text.source.IAnnotationPresentation}
	 */
	@Deprecated
	protected static Image getImage(Display display, ImageDescriptor descriptor) {
		Map map= getImageRegistry(display);
		Image image= (Image) map.get(descriptor);
		if (image == null) {
			image= descriptor.createImage();
			map.put(descriptor, image);
		}
		return image;
	}

	/**
	 * Returns an image registry for the given display. If no such registry
	 * exists the registry is created.
	 *
	 * @param display the display
	 * @return the image registry for the given display
	 * @deprecated As of 3.0, visual presentation is no longer supported,
	 *             annotation with a visible presentation should implement
	 *             {@link org.eclipse.jface.text.source.IAnnotationPresentation}
	 */
	@Deprecated
	protected static Map getImageRegistry(Display display) {
		if (fgImageRegistry == null) {
			fgImageRegistry= new HashMap();
			display.disposeExec(new Runnable() {
				@Override
				public void run() {
					if (fgImageRegistry != null) {
						Map map= fgImageRegistry;
						fgImageRegistry= null;
						Iterator e= map.values().iterator();
						while (e.hasNext()) {
							Image image= (Image) e.next();
							if (!image.isDisposed())
								image.dispose();
						}
					}
				}
			});
		}
		return fgImageRegistry;
	}


	/** The image, i.e., visual presentation of this annotation. */
	private Image fImage;
	/** The image name. */
	private String fImageName;
	/** The presentation layer. */
	private int fPresentationLayer= -1;

	/**
	 * Tells whether this annotation is quick fixable.
	 * @since 3.4
	 */
	private boolean fIsQuickFixable;
	/**
	 * Tells whether the quick fixable state (<code>fIsQuickFixable</code> has been computed.
	 * @since 3.4
	 */
	private boolean fIsQuickFixableStateSet;


	/**
	 * Creates a new annotation for the given marker.
	 *
	 * @param marker the marker
	 */
	public MarkerAnnotation(IMarker marker) {
		super(marker);
	}

	/**
	 * Creates a new annotation of the given type for the given marker.
	 *
	 * @param annotationType the annotation type
	 * @param marker the marker
	 * @since 3.0
	 */
	public MarkerAnnotation(String annotationType, IMarker marker) {
		super(annotationType, marker);
		initialize();
	}

	/**
	 * Sets the marker image to the given image.
	 *
	 * @param image the new marker image
	 * @deprecated As of 3.0, visual presentation is no longer supported,
	 *             annotation with a visible presentation should implement
	 *             {@link org.eclipse.jface.text.source.IAnnotationPresentation}
	 */
	@Deprecated
	protected void setImage(Image image) {
		fImage= image;
	}

	/**
	 * Initializes the annotation's icon representation and its drawing layer
	 * based upon the properties of the underlying marker.
	 *
	 * @deprecated As of 3.0, visual presentation is no longer supported,
	 *             annotation with a visible presentation should implement
	 *             {@link org.eclipse.jface.text.source.IAnnotationPresentation}
	 */
	@Deprecated
	protected void initialize() {
		IMarker marker= getMarker();
		String name= getUnknownImageName(marker);

		if (MarkerUtilities.isMarkerType(marker, IMarker.TASK)) {
			name= IDE.SharedImages.IMG_OBJS_TASK_TSK;
		} else if (MarkerUtilities.isMarkerType(marker, IMarker.BOOKMARK)) {
			name= IDE.SharedImages.IMG_OBJS_BKMRK_TSK;
		} else if (MarkerUtilities.isMarkerType(marker, IMarker.PROBLEM)) {
			switch (MarkerUtilities.getSeverity(marker)) {
				case IMarker.SEVERITY_INFO:
					name= ISharedImages.IMG_OBJS_INFO_TSK;
					break;
				case IMarker.SEVERITY_WARNING:
					name= ISharedImages.IMG_OBJS_WARN_TSK;
					break;
				case IMarker.SEVERITY_ERROR:
					name= ISharedImages.IMG_OBJS_ERROR_TSK;
					break;
			}
		}

		fImage= null;
		fImageName= name;
	}

	/**
	 * Returns the annotations drawing layer.
	 * <p>
	 * Note: This is only for backward compatibility.
	 * </p>
	 * @return the annotations drawing layer
	 * @deprecated As of 3.0, replaced by {@link org.eclipse.jface.text.source.IAnnotationAccessExtension#getLayer(org.eclipse.jface.text.source.Annotation)}
	 * @since 3.0
	 */
	@Deprecated
	public int getLayer() {
		if (fPresentationLayer != -1)
			// Backward compatibility
			return fPresentationLayer;

		AnnotationPreference preference= EditorsPlugin.getDefault().getAnnotationPreferenceLookup().getAnnotationPreference(this);
		if (preference != null)
			return preference.getPresentationLayer();
		return IAnnotationAccessExtension.DEFAULT_LAYER;
	}

	/**
	 * Sets the layer of this annotation.
	 * <p>
	 * Note: This is only for backward compatibility.
	 * </p>
	 * @param layer the layer of this annotation
	 * @deprecated As of 3.0, annotation with a visible presentation should implement
	 *             {@link org.eclipse.jface.text.source.IAnnotationPresentation}
	 *
	 * @since 3.0
	 */
	@Deprecated
	protected void setLayer(int layer) {
		fPresentationLayer= layer;
	}

	/**
	 * Implement this method to draw a graphical representation of this
	 * annotation within the given bounds. This default implementation does
	 * nothing.
	 * <p>
	 * Note: This is only for backward compatibility.
	 * </p>
	 * @param gc the drawing GC
	 * @param canvas the canvas to draw on
	 * @param r the bounds inside the canvas to draw on
	 * @deprecated As of 3.0 replaced by {@link org.eclipse.jface.text.source.IAnnotationAccessExtension#paint(org.eclipse.jface.text.source.Annotation, GC, Canvas, Rectangle)}
	 * @since 3.0
	 */
	@Deprecated
	public void paint(GC gc, Canvas canvas, Rectangle r) {
		Image image= getImage(canvas.getDisplay());
		if (image != null)
			ImageUtilities.drawImage(image, gc, canvas, r, SWT.CENTER, SWT.TOP);
	}

	/**
	 * Informs this annotation about changes applied to its underlying marker
	 * and adapts to those changes.
	 */
	@Override
	public void update() {
		super.update();
		initialize();
	}

	/**
	 * Returns the name of an image used to visually represent markers of
	 * unknown type. This implementation returns <code>null</code>.
	 * Subclasses may replace this method.
	 *
	 * @param marker the marker of unknown type
	 * @return the name of an image for markers of unknown type.
	 * @deprecated As of 3.0, visual presentation is no longer supported,
	 *             annotation with a visible presentation should implement
	 *             {@link org.eclipse.jface.text.source.IAnnotationPresentation}
	 */
	@Deprecated
	protected String getUnknownImageName(IMarker marker) {
		return null;
	}

	/**
	 * Returns the image of the given name. Subclasses may extend this method.
	 * If so, subclasses must assume responsibility for disposing the images
	 * they create.
	 *
	 * @param name the name of the requested image
	 * @return the image or <code>null</code> if there is no such image
	 * @deprecated As of 3.0, visual presentation is no longer supported,
	 *             annotation with a visible presentation should implement
	 *             {@link org.eclipse.jface.text.source.IAnnotationPresentation}
	 */
	@Deprecated
	protected Image getImage(String name) {
		if (name != null)
			return PlatformUI.getWorkbench().getSharedImages().getImage(name);
		return null;
	}

	/**
	 * Returns an image for this annotation. It first consults the workbench
	 * adapter for this annotation's marker. If none is defined, it tries to
	 * find an image for the image name of this annotation.
	 *
	 * @param display the display for which the image is requested
	 * @return the image for this annotation
	 * @deprecated As of 3.0, visual presentation is no longer supported,
	 *             annotation with a visible presentation should implement
	 *             {@link org.eclipse.jface.text.source.IAnnotationPresentation}
	 */
	@Deprecated
	protected Image getImage(Display display) {
		if (fImage == null) {

			IMarker marker= getMarker();
			if (marker.exists()) {
				IWorkbenchAdapter adapter= marker.getAdapter(IWorkbenchAdapter.class);
				if (adapter != null) {
					ImageDescriptor descriptor= adapter.getImageDescriptor(marker);
					if (descriptor != null)
						fImage= getImage(display, descriptor);
				}
			}

			if (fImage == null)
				fImage= getImage(fImageName);

		}
		return fImage;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @since 3.4
	 */
	@Override
	public void setQuickFixable(boolean state) {
		fIsQuickFixable= state;
		fIsQuickFixableStateSet= true;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @since 3.4
	 */
	@Override
	public boolean isQuickFixableStateSet() {
		return fIsQuickFixableStateSet;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @since 3.4
	 */
	@Override
	public boolean isQuickFixable() {
		Assert.isTrue(isQuickFixableStateSet());
		return fIsQuickFixable;
	}

}

Back to the top