Mercurial > projects > dwt-addons
annotate dwtx/jface/text/source/AnnotationPainter.d @ 158:25f1f92fa3df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 26 Aug 2008 02:46:34 +0200 |
parents | 7f75eaa8103a |
children | 7926b636c282 |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.source.AnnotationPainter; | |
14 | |
131 | 15 import dwtx.jface.text.source.ISharedTextColors; // packageimport |
16 import dwtx.jface.text.source.ILineRange; // packageimport | |
17 import dwtx.jface.text.source.IAnnotationPresentation; // packageimport | |
18 import dwtx.jface.text.source.IVerticalRulerInfoExtension; // packageimport | |
19 import dwtx.jface.text.source.ICharacterPairMatcher; // packageimport | |
20 import dwtx.jface.text.source.TextInvocationContext; // packageimport | |
21 import dwtx.jface.text.source.LineChangeHover; // packageimport | |
22 import dwtx.jface.text.source.IChangeRulerColumn; // packageimport | |
23 import dwtx.jface.text.source.IAnnotationMap; // packageimport | |
24 import dwtx.jface.text.source.IAnnotationModelListenerExtension; // packageimport | |
25 import dwtx.jface.text.source.ISourceViewerExtension2; // packageimport | |
26 import dwtx.jface.text.source.IAnnotationHover; // packageimport | |
27 import dwtx.jface.text.source.ContentAssistantFacade; // packageimport | |
28 import dwtx.jface.text.source.IAnnotationAccess; // packageimport | |
29 import dwtx.jface.text.source.IVerticalRulerExtension; // packageimport | |
30 import dwtx.jface.text.source.IVerticalRulerColumn; // packageimport | |
31 import dwtx.jface.text.source.LineNumberRulerColumn; // packageimport | |
32 import dwtx.jface.text.source.MatchingCharacterPainter; // packageimport | |
33 import dwtx.jface.text.source.IAnnotationModelExtension; // packageimport | |
34 import dwtx.jface.text.source.ILineDifferExtension; // packageimport | |
35 import dwtx.jface.text.source.DefaultCharacterPairMatcher; // packageimport | |
36 import dwtx.jface.text.source.LineNumberChangeRulerColumn; // packageimport | |
37 import dwtx.jface.text.source.IAnnotationAccessExtension; // packageimport | |
38 import dwtx.jface.text.source.ISourceViewer; // packageimport | |
39 import dwtx.jface.text.source.AnnotationModel; // packageimport | |
40 import dwtx.jface.text.source.ILineDifferExtension2; // packageimport | |
41 import dwtx.jface.text.source.IAnnotationModelListener; // packageimport | |
42 import dwtx.jface.text.source.IVerticalRuler; // packageimport | |
43 import dwtx.jface.text.source.DefaultAnnotationHover; // packageimport | |
44 import dwtx.jface.text.source.SourceViewer; // packageimport | |
45 import dwtx.jface.text.source.SourceViewerConfiguration; // packageimport | |
46 import dwtx.jface.text.source.AnnotationBarHoverManager; // packageimport | |
47 import dwtx.jface.text.source.CompositeRuler; // packageimport | |
48 import dwtx.jface.text.source.ImageUtilities; // packageimport | |
49 import dwtx.jface.text.source.VisualAnnotationModel; // packageimport | |
50 import dwtx.jface.text.source.IAnnotationModel; // packageimport | |
51 import dwtx.jface.text.source.ISourceViewerExtension3; // packageimport | |
52 import dwtx.jface.text.source.ILineDiffInfo; // packageimport | |
53 import dwtx.jface.text.source.VerticalRulerEvent; // packageimport | |
54 import dwtx.jface.text.source.ChangeRulerColumn; // packageimport | |
55 import dwtx.jface.text.source.ILineDiffer; // packageimport | |
56 import dwtx.jface.text.source.AnnotationModelEvent; // packageimport | |
57 import dwtx.jface.text.source.AnnotationColumn; // packageimport | |
58 import dwtx.jface.text.source.AnnotationRulerColumn; // packageimport | |
59 import dwtx.jface.text.source.IAnnotationHoverExtension; // packageimport | |
60 import dwtx.jface.text.source.AbstractRulerColumn; // packageimport | |
61 import dwtx.jface.text.source.ISourceViewerExtension; // packageimport | |
62 import dwtx.jface.text.source.AnnotationMap; // packageimport | |
63 import dwtx.jface.text.source.IVerticalRulerInfo; // packageimport | |
64 import dwtx.jface.text.source.IAnnotationModelExtension2; // packageimport | |
65 import dwtx.jface.text.source.LineRange; // packageimport | |
66 import dwtx.jface.text.source.IAnnotationAccessExtension2; // packageimport | |
67 import dwtx.jface.text.source.VerticalRuler; // packageimport | |
68 import dwtx.jface.text.source.JFaceTextMessages; // packageimport | |
69 import dwtx.jface.text.source.IOverviewRuler; // packageimport | |
70 import dwtx.jface.text.source.Annotation; // packageimport | |
71 import dwtx.jface.text.source.IVerticalRulerListener; // packageimport | |
72 import dwtx.jface.text.source.ISourceViewerExtension4; // packageimport | |
73 import dwtx.jface.text.source.IAnnotationHoverExtension2; // packageimport | |
74 import dwtx.jface.text.source.OverviewRuler; // packageimport | |
75 import dwtx.jface.text.source.OverviewRulerHoverManager; // packageimport | |
76 | |
77 | |
129 | 78 import dwt.dwthelper.utils; |
79 | |
80 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
81 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
82 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
83 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
84 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
85 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
86 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
87 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
88 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
89 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
90 |
129 | 91 |
92 import dwt.DWT; | |
93 import dwt.DWTException; | |
94 import dwt.custom.StyleRange; | |
95 import dwt.custom.StyledText; | |
96 import dwt.events.PaintEvent; | |
97 import dwt.events.PaintListener; | |
98 import dwt.graphics.Color; | |
99 import dwt.graphics.GC; | |
100 import dwt.graphics.Point; | |
101 import dwt.graphics.Rectangle; | |
102 import dwt.graphics.TextStyle; | |
103 import dwt.widgets.Display; | |
104 import dwtx.core.runtime.Assert; | |
105 import dwtx.core.runtime.Platform; | |
106 import dwtx.jface.text.BadLocationException; | |
107 import dwtx.jface.text.IDocument; | |
108 import dwtx.jface.text.IPaintPositionManager; | |
109 import dwtx.jface.text.IPainter; | |
110 import dwtx.jface.text.IRegion; | |
111 import dwtx.jface.text.ITextInputListener; | |
112 import dwtx.jface.text.ITextPresentationListener; | |
113 import dwtx.jface.text.ITextViewerExtension2; | |
114 import dwtx.jface.text.ITextViewerExtension5; | |
115 import dwtx.jface.text.JFaceTextUtil; | |
116 import dwtx.jface.text.Position; | |
117 import dwtx.jface.text.Region; | |
118 import dwtx.jface.text.TextPresentation; | |
119 | |
120 | |
121 /** | |
122 * A drawing strategy draws the decoration for an annotation onto the text widget. | |
123 * | |
124 * @since 3.0 | |
125 */ | |
126 public interface IDrawingStrategy { | |
127 /** | |
128 * Draws a decoration for an annotation onto the specified GC at the given text range. There | |
129 * are two different invocation modes of the <code>draw</code> method: | |
130 * <ul> | |
131 * <li><strong>drawing mode:</strong> the passed GC is the graphics context of a paint | |
132 * event occurring on the text widget. The strategy should draw the decoration onto the | |
133 * graphics context, such that the decoration appears at the given range in the text | |
134 * widget.</li> | |
135 * <li><strong>clearing mode:</strong> the passed GC is <code>null</code>. In this case | |
136 * the strategy must invalidate enough of the text widget's client area to cover any | |
137 * decoration drawn in drawing mode. This can usually be accomplished by calling | |
138 * {@linkplain StyledText#redrawRange(int, int, bool) textWidget.redrawRange(offset, length, true)}.</li> | |
139 * </ul> | |
145 | 140 * |
129 | 141 * @param annotation the annotation to be drawn |
142 * @param gc the graphics context, <code>null</code> when in clearing mode | |
143 * @param textWidget the text widget to draw on | |
144 * @param offset the offset of the line | |
145 * @param length the length of the line | |
146 * @param color the color of the line | |
147 */ | |
148 void draw(Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color); | |
149 } | |
150 | |
158 | 151 alias IDrawingStrategy AnnotationPainter_IDrawingStrategy; |
152 | |
153 /** | |
154 * Paints decorations for annotations provided by an annotation model and/or | |
155 * highlights them in the associated source viewer. | |
156 * <p> | |
157 * The annotation painter can be configured with drawing strategies. A drawing | |
158 * strategy defines the visual presentation of a particular type of annotation | |
159 * decoration.</p> | |
160 * <p> | |
161 * Clients usually instantiate and configure objects of this class.</p> | |
162 * | |
163 * @since 2.1 | |
164 */ | |
165 public class AnnotationPainter : IPainter, PaintListener, IAnnotationModelListener, IAnnotationModelListenerExtension, ITextPresentationListener { | |
166 | |
129 | 167 /** |
168 * Squiggles drawing strategy. | |
145 | 169 * |
129 | 170 * @since 3.0 |
171 * @deprecated As of 3.4, replaced by {@link AnnotationPainter.UnderlineStrategy} | |
172 */ | |
173 public static class SquigglesStrategy : IDrawingStrategy { | |
174 | |
175 /* | |
176 * @see dwtx.jface.text.source.AnnotationPainter.IDrawingStrategy#draw(dwtx.jface.text.source.Annotation, dwt.graphics.GC, dwt.custom.StyledText, int, int, dwt.graphics.Color) | |
177 * @since 3.0 | |
178 */ | |
179 public void draw(Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) { | |
180 if (gc !is null) { | |
181 | |
182 if (length < 1) | |
183 return; | |
184 | |
185 Point left= textWidget.getLocationAtOffset(offset); | |
186 Point right= textWidget.getLocationAtOffset(offset + length); | |
187 Rectangle rect= textWidget.getTextBounds(offset, offset + length - 1); | |
188 left.x= rect.x; | |
189 right.x= rect.x + rect.width; | |
190 | |
191 int[] polyline= computePolyline(left, right, textWidget.getBaseline(offset), textWidget.getLineHeight(offset)); | |
145 | 192 |
129 | 193 gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance |
194 gc.setLineStyle(DWT.LINE_SOLID); | |
195 gc.setForeground(color); | |
196 gc.drawPolyline(polyline); | |
197 | |
198 } else { | |
199 textWidget.redrawRange(offset, length, true); | |
200 } | |
201 } | |
202 | |
203 /** | |
204 * Computes an array of alternating x and y values which are the corners of the squiggly line of the | |
205 * given height between the given end points. | |
206 * | |
207 * @param left the left end point | |
208 * @param right the right end point | |
209 * @param baseline the font's baseline | |
210 * @param lineHeight the height of the line | |
211 * @return the array of alternating x and y values which are the corners of the squiggly line | |
212 */ | |
213 private int[] computePolyline(Point left, Point right, int baseline, int lineHeight) { | |
214 | |
215 final int WIDTH= 4; // must be even | |
216 final int HEIGHT= 2; // can be any number | |
217 | |
218 int peaks= (right.x - left.x) / WIDTH; | |
219 if (peaks is 0 && right.x - left.x > 2) | |
220 peaks= 1; | |
221 | |
222 int leftX= left.x; | |
223 | |
224 // compute (number of point) * 2 | |
225 int length= ((2 * peaks) + 1) * 2; | |
226 if (length < 0) | |
227 return new int[0]; | |
228 | |
229 int[] coordinates= new int[length]; | |
230 | |
231 // cache peeks' y-coordinates | |
232 int top= left.y + Math.min(baseline + 1, lineHeight - HEIGHT - 1); | |
233 int bottom= top + HEIGHT; | |
234 | |
235 // populate array with peek coordinates | |
236 for (int i= 0; i < peaks; i++) { | |
237 int index= 4 * i; | |
238 coordinates[index]= leftX + (WIDTH * i); | |
239 coordinates[index+1]= bottom; | |
240 coordinates[index+2]= coordinates[index] + WIDTH/2; | |
241 coordinates[index+3]= top; | |
242 } | |
243 | |
244 // the last down flank is missing | |
245 coordinates[length-2]= Math.min(Math.max(0, right.x - 1), left.x + (WIDTH * peaks)); | |
246 coordinates[length-1]= bottom; | |
247 | |
248 return coordinates; | |
249 } | |
250 } | |
251 | |
252 /** | |
253 * Drawing strategy that does nothing. | |
254 * | |
255 * @since 3.0 | |
256 */ | |
257 public static final class NullStrategy : IDrawingStrategy { | |
258 | |
259 /* | |
260 * @see dwtx.jface.text.source.AnnotationPainter.IDrawingStrategy#draw(dwtx.jface.text.source.Annotation, dwt.graphics.GC, dwt.custom.StyledText, int, int, dwt.graphics.Color) | |
261 * @since 3.0 | |
262 */ | |
263 public void draw(Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) { | |
264 // do nothing | |
265 } | |
266 } | |
145 | 267 |
268 | |
129 | 269 /** |
270 * A text style painting strategy draws the decoration for an annotation | |
271 * onto the text widget by applying a {@link TextStyle} on a given | |
272 * {@link StyleRange}. | |
145 | 273 * |
129 | 274 * @since 3.4 |
275 */ | |
276 public interface ITextStyleStrategy { | |
277 | |
278 /** | |
279 * Applies a text style on the given <code>StyleRange</code>. | |
145 | 280 * |
129 | 281 * @param styleRange the style range on which to apply the text style |
282 * @param annotationColor the color of the annotation | |
283 */ | |
284 void applyTextStyle(StyleRange styleRange, Color annotationColor); | |
285 } | |
145 | 286 |
129 | 287 |
288 /** | |
289 * @since 3.4 | |
290 */ | |
291 public static final class HighlightingStrategy : ITextStyleStrategy { | |
292 public void applyTextStyle(StyleRange styleRange, Color annotationColor) { | |
293 styleRange.background= annotationColor; | |
294 } | |
295 } | |
296 | |
145 | 297 |
129 | 298 /** |
299 * Underline text style strategy. | |
145 | 300 * |
129 | 301 * @since 3.4 |
302 */ | |
303 public static final class UnderlineStrategy : ITextStyleStrategy { | |
145 | 304 |
129 | 305 int fUnderlineStyle; |
306 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
307 public this(int style) { |
129 | 308 Assert.isLegal(style is DWT.UNDERLINE_SINGLE || style is DWT.UNDERLINE_DOUBLE || style is DWT.UNDERLINE_ERROR || style is DWT.UNDERLINE_SQUIGGLE); |
309 fUnderlineStyle= style; | |
310 } | |
311 | |
312 public void applyTextStyle(StyleRange styleRange, Color annotationColor) { | |
313 styleRange.underline= true; | |
314 styleRange.underlineStyle= fUnderlineStyle; | |
315 styleRange.underlineColor= annotationColor; | |
316 } | |
317 } | |
318 | |
145 | 319 |
129 | 320 /** |
321 * Box text style strategy. | |
145 | 322 * |
129 | 323 * @since 3.4 |
324 */ | |
325 public static final class BoxStrategy : ITextStyleStrategy { | |
145 | 326 |
129 | 327 int fBorderStyle; |
328 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
329 public this(int style) { |
129 | 330 Assert.isLegal(style is DWT.BORDER_DASH || style is DWT.BORDER_DASH || style is DWT.BORDER_SOLID); |
331 fBorderStyle= style; | |
332 } | |
145 | 333 |
129 | 334 public void applyTextStyle(StyleRange styleRange, Color annotationColor) { |
335 styleRange.borderStyle= fBorderStyle; | |
336 styleRange.borderColor= annotationColor; | |
337 } | |
338 } | |
145 | 339 |
129 | 340 |
341 /** | |
342 * Implementation of <code>IRegion</code> that can be reused | |
343 * by setting the offset and the length. | |
344 */ | |
345 private static class ReusableRegion : Position , IRegion {} | |
346 | |
347 /** | |
348 * Tells whether this class is in debug mode. | |
349 * @since 3.0 | |
350 */ | |
351 private static bool DEBUG= "true".equalsIgnoreCase(Platform.getDebugOption("dwtx.jface.text/debug/AnnotationPainter")); //$NON-NLS-1$//$NON-NLS-2$ | |
352 /** | |
353 * The squiggly painter strategy. | |
354 * @since 3.0 | |
355 */ | |
147 | 356 private static const IDrawingStrategy SQUIGGLES_STRATEGY= new SquigglesStrategy(); |
129 | 357 |
358 /** | |
359 * This strategy is used to mark the <code>null</code> value in the chache | |
360 * maps. | |
145 | 361 * |
129 | 362 * @since 3.4 |
363 */ | |
147 | 364 private static const IDrawingStrategy NULL_STRATEGY= new NullStrategy(); |
129 | 365 /** |
366 * The squiggles painter id. | |
367 * @since 3.0 | |
368 */ | |
147 | 369 private static const Object SQUIGGLES= new Object(); |
129 | 370 /** |
371 * The squiggly painter strategy. | |
145 | 372 * |
129 | 373 * @since 3.4 |
374 */ | |
147 | 375 private static const ITextStyleStrategy HIGHLIGHTING_STRATEGY= new HighlightingStrategy(); |
129 | 376 |
377 /** | |
378 * The highlighting text style strategy id. | |
145 | 379 * |
129 | 380 * @since 3.4 |
381 */ | |
147 | 382 private static const Object HIGHLIGHTING= new Object(); |
129 | 383 |
384 /** | |
385 * The presentation information (decoration) for an annotation. Each such | |
386 * object represents one decoration drawn on the text area, such as squiggly lines | |
387 * and underlines. | |
388 */ | |
389 private static class Decoration { | |
390 /** The position of this decoration */ | |
391 private Position fPosition; | |
392 /** The color of this decoration */ | |
393 private Color fColor; | |
394 /** | |
395 * The annotation's layer | |
396 * @since 3.0 | |
397 */ | |
398 private int fLayer; | |
399 /** | |
400 * The painting strategy for this decoration. | |
401 * @since 3.0 | |
402 */ | |
403 private Object fPaintingStrategy; | |
404 } | |
405 | |
406 | |
407 /** Indicates whether this painter is active */ | |
408 private bool fIsActive= false; | |
409 /** Indicates whether this painter is managing decorations */ | |
410 private bool fIsPainting= false; | |
411 /** Indicates whether this painter is setting its annotation model */ | |
157
7f75eaa8103a
volatile and cast Map.Entry
Frank Benoit <benoit@tionex.de>
parents:
153
diff
changeset
|
412 private /+volatile+/ bool fIsSettingModel= false; |
129 | 413 /** The associated source viewer */ |
414 private ISourceViewer fSourceViewer; | |
415 /** The cached widget of the source viewer */ | |
416 private StyledText fTextWidget; | |
417 /** The annotation model providing the annotations to be drawn */ | |
418 private IAnnotationModel fModel; | |
419 /** The annotation access */ | |
420 private IAnnotationAccess fAnnotationAccess; | |
421 /** | |
422 * The map with decorations | |
423 * @since 3.0 | |
424 */ | |
425 private Map fDecorationsMap= new HashMap(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=50767 | |
426 /** | |
427 * The map with of highlighted decorations. | |
428 * @since 3.0 | |
429 */ | |
430 private Map fHighlightedDecorationsMap= new HashMap(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=50767 | |
431 /** | |
432 * Mutex for highlighted decorations map. | |
433 * @since 3.0 | |
434 */ | |
435 private Object fDecorationMapLock= new Object(); | |
436 /** | |
437 * Mutex for for decorations map. | |
438 * @since 3.0 | |
439 */ | |
440 private Object fHighlightedDecorationsMapLock= new Object(); | |
441 /** | |
442 * Maps an annotation type to its registered color. | |
145 | 443 * |
129 | 444 * @see #setAnnotationTypeColor(Object, Color) |
445 */ | |
446 private Map fAnnotationType2Color= new HashMap(); | |
447 | |
448 /** | |
449 * Cache that maps the annotation type to its color. | |
450 * @since 3.4 | |
451 */ | |
452 private Map fCachedAnnotationType2Color= new HashMap(); | |
453 /** | |
454 * The range in which the current highlight annotations can be found. | |
455 * @since 3.0 | |
456 */ | |
457 private Position fCurrentHighlightAnnotationRange= null; | |
458 /** | |
459 * The range in which all added, removed and changed highlight | |
460 * annotations can be found since the last world change. | |
461 * @since 3.0 | |
462 */ | |
463 private Position fTotalHighlightAnnotationRange= null; | |
464 /** | |
465 * The range in which the currently drawn annotations can be found. | |
466 * @since 3.3 | |
467 */ | |
468 private Position fCurrentDrawRange= null; | |
469 /** | |
470 * The range in which all added, removed and changed drawn | |
471 * annotations can be found since the last world change. | |
472 * @since 3.3 | |
473 */ | |
474 private Position fTotalDrawRange= null; | |
475 /** | |
476 * The text input listener. | |
477 * @since 3.0 | |
478 */ | |
479 private ITextInputListener fTextInputListener; | |
480 /** | |
481 * Flag which tells that a new document input is currently being set. | |
482 * @since 3.0 | |
483 */ | |
484 private bool fInputDocumentAboutToBeChanged; | |
485 /** | |
486 * Maps annotation types to painting strategy identifiers. | |
145 | 487 * |
129 | 488 * @see #addAnnotationType(Object, Object) |
489 * @since 3.0 | |
490 */ | |
491 private Map fAnnotationType2PaintingStrategyId= new HashMap(); | |
492 /** | |
493 * Maps annotation types to painting strategy identifiers. | |
494 * @since 3.4 | |
495 */ | |
496 private Map fCachedAnnotationType2PaintingStrategy= new HashMap(); | |
497 | |
498 /** | |
499 * Maps painting strategy identifiers to painting strategies. | |
145 | 500 * |
129 | 501 * @since 3.0 |
502 */ | |
503 private Map fPaintingStrategyId2PaintingStrategy= new HashMap(); | |
504 | |
505 /** | |
506 * Reuse this region for performance reasons. | |
507 * @since 3.3 | |
508 */ | |
509 private ReusableRegion fReusableRegion= new ReusableRegion(); | |
145 | 510 |
129 | 511 /** |
512 * Creates a new annotation painter for the given source viewer and with the | |
513 * given annotation access. The painter is not initialized, i.e. no | |
514 * annotation types are configured to be painted. | |
515 * | |
516 * @param sourceViewer the source viewer for this painter | |
517 * @param access the annotation access for this painter | |
518 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
519 public this(ISourceViewer sourceViewer, IAnnotationAccess access) { |
129 | 520 fSourceViewer= sourceViewer; |
521 fAnnotationAccess= access; | |
522 fTextWidget= sourceViewer.getTextWidget(); | |
523 | |
524 // default drawing strategies: squiggles were the only decoration style before version 3.0 | |
525 fPaintingStrategyId2PaintingStrategy.put(SQUIGGLES, SQUIGGLES_STRATEGY); | |
526 fPaintingStrategyId2PaintingStrategy.put(HIGHLIGHTING, HIGHLIGHTING_STRATEGY); | |
527 } | |
528 | |
529 /** | |
530 * Returns whether this painter has to draw any squiggles. | |
531 * | |
532 * @return <code>true</code> if there are squiggles to be drawn, <code>false</code> otherwise | |
533 */ | |
534 private bool hasDecorations() { | |
535 synchronized (fDecorationMapLock) { | |
536 return !fDecorationsMap.isEmpty(); | |
537 } | |
538 } | |
539 | |
540 /** | |
541 * Enables painting. This painter registers a paint listener with the | |
542 * source viewer's widget. | |
543 */ | |
544 private void enablePainting() { | |
545 if (!fIsPainting && hasDecorations()) { | |
546 fIsPainting= true; | |
547 fTextWidget.addPaintListener(this); | |
548 handleDrawRequest(null); | |
549 } | |
550 } | |
551 | |
552 /** | |
553 * Disables painting, if is has previously been enabled. Removes | |
554 * any paint listeners registered with the source viewer's widget. | |
555 * | |
556 * @param redraw <code>true</code> if the widget should be redrawn after disabling | |
557 */ | |
558 private void disablePainting(bool redraw) { | |
559 if (fIsPainting) { | |
560 fIsPainting= false; | |
561 fTextWidget.removePaintListener(this); | |
562 if (redraw && hasDecorations()) | |
563 handleDrawRequest(null); | |
564 } | |
565 } | |
566 | |
567 /** | |
568 * Sets the annotation model for this painter. Registers this painter | |
569 * as listener of the give model, if the model is not <code>null</code>. | |
570 * | |
571 * @param model the annotation model | |
572 */ | |
573 private void setModel(IAnnotationModel model) { | |
574 if (fModel !is model) { | |
575 if (fModel !is null) | |
576 fModel.removeAnnotationModelListener(this); | |
577 fModel= model; | |
578 if (fModel !is null) { | |
579 try { | |
580 fIsSettingModel= true; | |
581 fModel.addAnnotationModelListener(this); | |
582 } finally { | |
583 fIsSettingModel= false; | |
584 } | |
585 } | |
586 } | |
587 } | |
588 | |
589 /** | |
590 * Updates the set of decorations based on the current state of | |
591 * the painter's annotation model. | |
592 * | |
593 * @param event the annotation model event | |
594 */ | |
595 private void catchupWithModel(AnnotationModelEvent event) { | |
596 | |
597 synchronized (fDecorationMapLock) { | |
598 if (fDecorationsMap is null) | |
599 return; | |
600 } | |
601 | |
602 IRegion clippingRegion= computeClippingRegion(null, true); | |
603 IDocument document= fSourceViewer.getDocument(); | |
604 | |
605 int highlightAnnotationRangeStart= Integer.MAX_VALUE; | |
606 int highlightAnnotationRangeEnd= -1; | |
145 | 607 |
129 | 608 int drawRangeStart= Integer.MAX_VALUE; |
609 int drawRangeEnd= -1; | |
610 | |
611 if (fModel !is null) { | |
612 | |
613 Map decorationsMap; | |
614 Map highlightedDecorationsMap; | |
615 | |
616 // Clone decoration maps | |
617 synchronized (fDecorationMapLock) { | |
618 decorationsMap= new HashMap(fDecorationsMap); | |
619 } | |
620 synchronized (fHighlightedDecorationsMapLock) { | |
621 highlightedDecorationsMap= new HashMap(fHighlightedDecorationsMap); | |
622 } | |
623 | |
624 bool isWorldChange= false; | |
625 | |
626 Iterator e; | |
627 if (event is null || event.isWorldChange()) { | |
628 isWorldChange= true; | |
629 | |
630 if (DEBUG && event is null) | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
631 System.out_.println("AP: INTERNAL CHANGE"); //$NON-NLS-1$ |
129 | 632 |
633 Iterator iter= decorationsMap.entrySet().iterator(); | |
634 while (iter.hasNext()) { | |
157
7f75eaa8103a
volatile and cast Map.Entry
Frank Benoit <benoit@tionex.de>
parents:
153
diff
changeset
|
635 Map.Entry entry= cast(Map.Entry)iter.next(); |
134 | 636 Annotation annotation= cast(Annotation)entry.getKey(); |
637 Decoration decoration= cast(Decoration)entry.getValue(); | |
129 | 638 drawDecoration(decoration, null, annotation, clippingRegion, document); |
639 } | |
145 | 640 |
129 | 641 decorationsMap.clear(); |
145 | 642 |
129 | 643 highlightedDecorationsMap.clear(); |
644 | |
645 e= fModel.getAnnotationIterator(); | |
646 | |
647 | |
648 } else { | |
649 | |
650 // Remove annotations | |
651 Annotation[] removedAnnotations= event.getRemovedAnnotations(); | |
652 for (int i=0, length= removedAnnotations.length; i < length; i++) { | |
653 Annotation annotation= removedAnnotations[i]; | |
134 | 654 Decoration decoration= cast(Decoration)highlightedDecorationsMap.remove(annotation); |
129 | 655 if (decoration !is null) { |
656 Position position= decoration.fPosition; | |
657 if (position !is null) { | |
658 highlightAnnotationRangeStart= Math.min(highlightAnnotationRangeStart, position.offset); | |
659 highlightAnnotationRangeEnd= Math.max(highlightAnnotationRangeEnd, position.offset + position.length); | |
660 } | |
661 } | |
134 | 662 decoration= cast(Decoration)decorationsMap.remove(annotation); |
129 | 663 if (decoration !is null) { |
664 drawDecoration(decoration, null, annotation, clippingRegion, document); | |
665 Position position= decoration.fPosition; | |
666 if (position !is null) { | |
667 drawRangeStart= Math.min(drawRangeStart, position.offset); | |
668 drawRangeEnd= Math.max(drawRangeEnd, position.offset + position.length); | |
669 } | |
670 } | |
145 | 671 |
129 | 672 } |
673 | |
674 // Update existing annotations | |
675 Annotation[] changedAnnotations= event.getChangedAnnotations(); | |
676 for (int i=0, length= changedAnnotations.length; i < length; i++) { | |
677 Annotation annotation= changedAnnotations[i]; | |
678 | |
679 bool isHighlighting= false; | |
145 | 680 |
134 | 681 Decoration decoration= cast(Decoration)highlightedDecorationsMap.get(annotation); |
129 | 682 |
683 if (decoration !is null) { | |
684 isHighlighting= true; | |
685 // The call below updates the decoration - no need to create new decoration | |
686 decoration= getDecoration(annotation, decoration); | |
687 if (decoration is null) | |
688 highlightedDecorationsMap.remove(annotation); | |
689 } else { | |
690 decoration= getDecoration(annotation, decoration); | |
145 | 691 if (decoration !is null && cast(ITextStyleStrategy)decoration.fPaintingStrategy ) { |
129 | 692 highlightedDecorationsMap.put(annotation, decoration); |
693 isHighlighting= true; | |
694 } | |
695 } | |
696 | |
697 bool usesDrawingStrategy= !isHighlighting && decoration !is null; | |
698 | |
699 Position position= null; | |
700 if (decoration is null) | |
701 position= fModel.getPosition(annotation); | |
702 else | |
703 position= decoration.fPosition; | |
704 | |
705 if (position !is null && !position.isDeleted()) { | |
706 if (isHighlighting) { | |
707 highlightAnnotationRangeStart= Math.min(highlightAnnotationRangeStart, position.offset); | |
708 highlightAnnotationRangeEnd= Math.max(highlightAnnotationRangeEnd, position.offset + position.length); | |
709 } | |
710 if (usesDrawingStrategy) { | |
711 drawRangeStart= Math.min(drawRangeStart, position.offset); | |
712 drawRangeEnd= Math.max(drawRangeEnd, position.offset + position.length); | |
713 } | |
714 } else { | |
715 highlightedDecorationsMap.remove(annotation); | |
716 } | |
717 | |
718 if (usesDrawingStrategy) { | |
134 | 719 Decoration oldDecoration= cast(Decoration)decorationsMap.get(annotation); |
129 | 720 if (oldDecoration !is null) { |
721 drawDecoration(oldDecoration, null, annotation, clippingRegion, document); | |
145 | 722 |
129 | 723 if (decoration !is null) |
724 decorationsMap.put(annotation, decoration); | |
725 else if (oldDecoration !is null) | |
726 decorationsMap.remove(annotation); | |
727 } | |
728 } | |
729 } | |
730 | |
731 e= Arrays.asList(event.getAddedAnnotations()).iterator(); | |
732 } | |
733 | |
734 // Add new annotations | |
735 while (e.hasNext()) { | |
134 | 736 Annotation annotation= cast(Annotation) e.next(); |
129 | 737 Decoration pp= getDecoration(annotation, null); |
738 if (pp !is null) { | |
145 | 739 if (cast(IDrawingStrategy)pp.fPaintingStrategy ) { |
129 | 740 decorationsMap.put(annotation, pp); |
741 drawRangeStart= Math.min(drawRangeStart, pp.fPosition.offset); | |
742 drawRangeEnd= Math.max(drawRangeEnd, pp.fPosition.offset + pp.fPosition.length); | |
145 | 743 } else if (cast(ITextStyleStrategy)pp.fPaintingStrategy ) { |
129 | 744 highlightedDecorationsMap.put(annotation, pp); |
745 highlightAnnotationRangeStart= Math.min(highlightAnnotationRangeStart, pp.fPosition.offset); | |
746 highlightAnnotationRangeEnd= Math.max(highlightAnnotationRangeEnd, pp.fPosition.offset + pp.fPosition.length); | |
747 } | |
145 | 748 |
129 | 749 } |
750 } | |
751 | |
752 synchronized (fDecorationMapLock) { | |
753 fDecorationsMap= decorationsMap; | |
754 updateDrawRanges(drawRangeStart, drawRangeEnd, isWorldChange); | |
755 } | |
756 | |
757 synchronized (fHighlightedDecorationsMapLock) { | |
758 fHighlightedDecorationsMap= highlightedDecorationsMap; | |
759 updateHighlightRanges(highlightAnnotationRangeStart, highlightAnnotationRangeEnd, isWorldChange); | |
760 } | |
761 } else { | |
762 // annotation model is null -> clear all | |
763 synchronized (fDecorationMapLock) { | |
764 fDecorationsMap.clear(); | |
765 } | |
766 synchronized (fHighlightedDecorationsMapLock) { | |
767 fHighlightedDecorationsMap.clear(); | |
768 } | |
769 } | |
770 } | |
771 | |
772 /** | |
773 * Updates the remembered highlight ranges. | |
774 * | |
775 * @param highlightAnnotationRangeStart the start of the range | |
776 * @param highlightAnnotationRangeEnd the end of the range | |
777 * @param isWorldChange tells whether the range belongs to a annotation model event reporting a world change | |
778 * @since 3.0 | |
779 */ | |
780 private void updateHighlightRanges(int highlightAnnotationRangeStart, int highlightAnnotationRangeEnd, bool isWorldChange) { | |
781 if (highlightAnnotationRangeStart !is Integer.MAX_VALUE) { | |
782 | |
783 int maxRangeStart= highlightAnnotationRangeStart; | |
784 int maxRangeEnd= highlightAnnotationRangeEnd; | |
785 | |
786 if (fTotalHighlightAnnotationRange !is null) { | |
787 maxRangeStart= Math.min(maxRangeStart, fTotalHighlightAnnotationRange.offset); | |
788 maxRangeEnd= Math.max(maxRangeEnd, fTotalHighlightAnnotationRange.offset + fTotalHighlightAnnotationRange.length); | |
789 } | |
790 | |
791 if (fTotalHighlightAnnotationRange is null) | |
792 fTotalHighlightAnnotationRange= new Position(0); | |
793 if (fCurrentHighlightAnnotationRange is null) | |
794 fCurrentHighlightAnnotationRange= new Position(0); | |
795 | |
796 if (isWorldChange) { | |
797 fTotalHighlightAnnotationRange.offset= highlightAnnotationRangeStart; | |
798 fTotalHighlightAnnotationRange.length= highlightAnnotationRangeEnd - highlightAnnotationRangeStart; | |
799 fCurrentHighlightAnnotationRange.offset= maxRangeStart; | |
800 fCurrentHighlightAnnotationRange.length= maxRangeEnd - maxRangeStart; | |
801 } else { | |
802 fTotalHighlightAnnotationRange.offset= maxRangeStart; | |
803 fTotalHighlightAnnotationRange.length= maxRangeEnd - maxRangeStart; | |
804 fCurrentHighlightAnnotationRange.offset=highlightAnnotationRangeStart; | |
805 fCurrentHighlightAnnotationRange.length= highlightAnnotationRangeEnd - highlightAnnotationRangeStart; | |
806 } | |
807 } else { | |
808 if (isWorldChange) { | |
809 fCurrentHighlightAnnotationRange= fTotalHighlightAnnotationRange; | |
810 fTotalHighlightAnnotationRange= null; | |
811 } else { | |
812 fCurrentHighlightAnnotationRange= null; | |
813 } | |
814 } | |
815 | |
816 adaptToDocumentLength(fCurrentHighlightAnnotationRange); | |
817 adaptToDocumentLength(fTotalHighlightAnnotationRange); | |
818 } | |
145 | 819 |
129 | 820 /** |
821 * Updates the remembered decoration ranges. | |
822 * | |
823 * @param drawRangeStart the start of the range | |
824 * @param drawRangeEnd the end of the range | |
825 * @param isWorldChange tells whether the range belongs to a annotation model event reporting a world change | |
826 * @since 3.3 | |
827 */ | |
828 private void updateDrawRanges(int drawRangeStart, int drawRangeEnd, bool isWorldChange) { | |
829 if (drawRangeStart !is Integer.MAX_VALUE) { | |
145 | 830 |
129 | 831 int maxRangeStart= drawRangeStart; |
832 int maxRangeEnd= drawRangeEnd; | |
145 | 833 |
129 | 834 if (fTotalDrawRange !is null) { |
835 maxRangeStart= Math.min(maxRangeStart, fTotalDrawRange.offset); | |
836 maxRangeEnd= Math.max(maxRangeEnd, fTotalDrawRange.offset + fTotalDrawRange.length); | |
837 } | |
145 | 838 |
129 | 839 if (fTotalDrawRange is null) |
840 fTotalDrawRange= new Position(0); | |
841 if (fCurrentDrawRange is null) | |
842 fCurrentDrawRange= new Position(0); | |
145 | 843 |
129 | 844 if (isWorldChange) { |
845 fTotalDrawRange.offset= drawRangeStart; | |
846 fTotalDrawRange.length= drawRangeEnd - drawRangeStart; | |
847 fCurrentDrawRange.offset= maxRangeStart; | |
848 fCurrentDrawRange.length= maxRangeEnd - maxRangeStart; | |
849 } else { | |
850 fTotalDrawRange.offset= maxRangeStart; | |
851 fTotalDrawRange.length= maxRangeEnd - maxRangeStart; | |
852 fCurrentDrawRange.offset=drawRangeStart; | |
853 fCurrentDrawRange.length= drawRangeEnd - drawRangeStart; | |
854 } | |
855 } else { | |
856 if (isWorldChange) { | |
857 fCurrentDrawRange= fTotalDrawRange; | |
858 fTotalDrawRange= null; | |
859 } else { | |
860 fCurrentDrawRange= null; | |
861 } | |
862 } | |
145 | 863 |
129 | 864 adaptToDocumentLength(fCurrentDrawRange); |
865 adaptToDocumentLength(fTotalDrawRange); | |
866 } | |
867 | |
868 /** | |
869 * Adapts the given position to the document length. | |
870 * | |
871 * @param position the position to adapt | |
872 * @since 3.0 | |
873 */ | |
874 private void adaptToDocumentLength(Position position) { | |
875 if (position is null) | |
876 return; | |
877 | |
878 int length= fSourceViewer.getDocument().getLength(); | |
879 position.offset= Math.min(position.offset, length); | |
880 position.length= Math.min(position.length, length - position.offset); | |
881 } | |
882 | |
883 /** | |
884 * Returns a decoration for the given annotation if this | |
885 * annotation is valid and shown by this painter. | |
886 * | |
887 * @param annotation the annotation | |
888 * @param decoration the decoration to be adapted and returned or <code>null</code> if a new one must be created | |
889 * @return the decoration or <code>null</code> if there's no valid one | |
890 * @since 3.0 | |
891 */ | |
892 private Decoration getDecoration(Annotation annotation, Decoration decoration) { | |
893 | |
894 if (annotation.isMarkedDeleted()) | |
895 return null; | |
896 | |
897 String type= annotation.getType(); | |
898 | |
899 Object paintingStrategy= getPaintingStrategy(type); | |
144 | 900 if (paintingStrategy is null || cast(NullStrategy)paintingStrategy ) |
129 | 901 return null; |
902 | |
903 Color color= getColor(type); | |
904 if (color is null) | |
905 return null; | |
906 | |
907 Position position= fModel.getPosition(annotation); | |
908 if (position is null || position.isDeleted()) | |
909 return null; | |
145 | 910 |
129 | 911 if (decoration is null) |
912 decoration= new Decoration(); | |
913 | |
914 decoration.fPosition= position; | |
915 decoration.fColor= color; | |
138 | 916 if ( cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
134 | 917 IAnnotationAccessExtension extension= cast(IAnnotationAccessExtension) fAnnotationAccess; |
129 | 918 decoration.fLayer= extension.getLayer(annotation); |
919 } else { | |
920 decoration.fLayer= IAnnotationAccessExtension.DEFAULT_LAYER; | |
921 } | |
145 | 922 |
129 | 923 decoration.fPaintingStrategy= paintingStrategy; |
145 | 924 |
129 | 925 return decoration; |
926 } | |
927 | |
928 /** | |
929 * Returns the painting strategy for the given annotation. | |
145 | 930 * |
129 | 931 * @param type the annotation type |
932 * @return the annotation painter | |
933 * @since 3.0 | |
934 */ | |
158 | 935 private Object getPaintingStrategy(String type) { |
129 | 936 Object strategy= fCachedAnnotationType2PaintingStrategy.get(type); |
937 if (strategy !is null) | |
938 return strategy; | |
939 | |
940 strategy= fPaintingStrategyId2PaintingStrategy.get(fAnnotationType2PaintingStrategyId.get(type)); | |
941 if (strategy !is null) { | |
942 fCachedAnnotationType2PaintingStrategy.put(type, strategy); | |
943 return strategy; | |
944 } | |
945 | |
138 | 946 if ( cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
134 | 947 IAnnotationAccessExtension ext = cast(IAnnotationAccessExtension) fAnnotationAccess; |
129 | 948 Object[] sts = ext.getSupertypes(type); |
949 for (int i= 0; i < sts.length; i++) { | |
950 strategy= fPaintingStrategyId2PaintingStrategy.get(fAnnotationType2PaintingStrategyId.get(sts[i])); | |
951 if (strategy !is null) { | |
952 fCachedAnnotationType2PaintingStrategy.put(type, strategy); | |
953 return strategy; | |
954 } | |
955 } | |
956 } | |
957 | |
958 fCachedAnnotationType2PaintingStrategy.put(type, NULL_STRATEGY); | |
959 return null; | |
960 | |
961 } | |
962 | |
963 /** | |
964 * Returns the color for the given annotation type | |
965 * | |
966 * @param annotationType the annotation type | |
967 * @return the color | |
968 * @since 3.0 | |
969 */ | |
158 | 970 private Color getColor(Object annotationType) { |
134 | 971 Color color= cast(Color)fCachedAnnotationType2Color.get(annotationType); |
129 | 972 if (color !is null) |
973 return color; | |
974 | |
134 | 975 color= cast(Color)fAnnotationType2Color.get(annotationType); |
129 | 976 if (color !is null) { |
977 fCachedAnnotationType2Color.put(annotationType, color); | |
978 return color; | |
979 } | |
980 | |
138 | 981 if ( cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
134 | 982 IAnnotationAccessExtension extension= cast(IAnnotationAccessExtension) fAnnotationAccess; |
129 | 983 Object[] superTypes= extension.getSupertypes(annotationType); |
984 if (superTypes !is null) { | |
985 for (int i= 0; i < superTypes.length; i++) { | |
134 | 986 color= cast(Color)fAnnotationType2Color.get(superTypes[i]); |
129 | 987 if (color !is null) { |
988 fCachedAnnotationType2Color.put(annotationType, color); | |
989 return color; | |
990 } | |
991 } | |
992 } | |
993 } | |
994 | |
995 return null; | |
996 } | |
997 | |
998 /** | |
999 * Recomputes the squiggles to be drawn and redraws them. | |
1000 * | |
1001 * @param event the annotation model event | |
1002 * @since 3.0 | |
1003 */ | |
1004 private void updatePainting(AnnotationModelEvent event) { | |
1005 disablePainting(event is null); | |
1006 | |
1007 catchupWithModel(event); | |
1008 | |
1009 if (!fInputDocumentAboutToBeChanged) | |
1010 invalidateTextPresentation(); | |
1011 | |
1012 enablePainting(); | |
1013 } | |
1014 | |
1015 private void invalidateTextPresentation() { | |
1016 IRegion r= null; | |
1017 synchronized (fHighlightedDecorationsMapLock) { | |
1018 if (fCurrentHighlightAnnotationRange !is null) | |
1019 r= new Region(fCurrentHighlightAnnotationRange.getOffset(), fCurrentHighlightAnnotationRange.getLength()); | |
1020 } | |
1021 if (r is null) | |
1022 return; | |
1023 | |
138 | 1024 if ( cast(ITextViewerExtension2)fSourceViewer ) { |
145 | 1025 if (DEBUG) |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1026 System.out_.println("AP: invalidating offset: " + r.getOffset() + ", length= " + r.getLength()); //$NON-NLS-1$ //$NON-NLS-2$ |
129 | 1027 |
134 | 1028 (cast(ITextViewerExtension2)fSourceViewer).invalidateTextPresentation(r.getOffset(), r.getLength()); |
129 | 1029 |
1030 } else { | |
1031 fSourceViewer.invalidateTextPresentation(); | |
1032 } | |
1033 } | |
1034 | |
1035 /* | |
1036 * @see dwtx.jface.text.ITextPresentationListener#applyTextPresentation(dwtx.jface.text.TextPresentation) | |
1037 * @since 3.0 | |
1038 */ | |
1039 public void applyTextPresentation(TextPresentation tp) { | |
1040 Set decorations; | |
1041 | |
1042 synchronized (fHighlightedDecorationsMapLock) { | |
1043 if (fHighlightedDecorationsMap is null || fHighlightedDecorationsMap.isEmpty()) | |
1044 return; | |
1045 | |
1046 decorations= new HashSet(fHighlightedDecorationsMap.entrySet()); | |
1047 } | |
1048 | |
1049 IRegion region= tp.getExtent(); | |
1050 | |
145 | 1051 if (DEBUG) |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1052 System.out_.println("AP: applying text presentation offset: " + region.getOffset() + ", length= " + region.getLength()); //$NON-NLS-1$ //$NON-NLS-2$ |
129 | 1053 |
1054 for (int layer= 0, maxLayer= 1; layer < maxLayer; layer++) { | |
1055 | |
1056 for (Iterator iter= decorations.iterator(); iter.hasNext();) { | |
157
7f75eaa8103a
volatile and cast Map.Entry
Frank Benoit <benoit@tionex.de>
parents:
153
diff
changeset
|
1057 Map.Entry entry= cast(Map.Entry)iter.next(); |
129 | 1058 |
134 | 1059 Annotation a= cast(Annotation)entry.getKey(); |
129 | 1060 if (a.isMarkedDeleted()) |
1061 continue; | |
1062 | |
134 | 1063 Decoration pp = cast(Decoration)entry.getValue(); |
129 | 1064 |
1065 maxLayer= Math.max(maxLayer, pp.fLayer + 1); // dynamically update layer maximum | |
1066 if (pp.fLayer !is layer) // wrong layer: skip annotation | |
1067 continue; | |
1068 | |
1069 Position p= pp.fPosition; | |
138 | 1070 if ( cast(ITextViewerExtension5)fSourceViewer ) { |
134 | 1071 ITextViewerExtension5 extension3= cast(ITextViewerExtension5) fSourceViewer; |
129 | 1072 if (null is extension3.modelRange2WidgetRange(new Region(p.getOffset(), p.getLength()))) |
1073 continue; | |
1074 } else if (!fSourceViewer.overlapsWithVisibleRegion(p.offset, p.length)) { | |
1075 continue; | |
1076 } | |
1077 | |
1078 int regionEnd= region.getOffset() + region.getLength(); | |
1079 int pEnd= p.getOffset() + p.getLength(); | |
1080 if (pEnd >= region.getOffset() && regionEnd > p.getOffset()) { | |
1081 int start= Math.max(p.getOffset(), region.getOffset()); | |
1082 int end= Math.min(regionEnd, pEnd); | |
1083 int length= Math.max(end - start, 0); | |
1084 StyleRange styleRange= new StyleRange(start, length, null, null); | |
134 | 1085 (cast(ITextStyleStrategy)pp.fPaintingStrategy).applyTextStyle(styleRange, pp.fColor); |
129 | 1086 tp.mergeStyleRange(styleRange); |
1087 } | |
1088 } | |
1089 } | |
1090 } | |
1091 | |
1092 /* | |
1093 * @see dwtx.jface.text.source.IAnnotationModelListener#modelChanged(dwtx.jface.text.source.IAnnotationModel) | |
1094 */ | |
158 | 1095 public synchronized void modelChanged(IAnnotationModel model) { |
145 | 1096 if (DEBUG) |
129 | 1097 System.err.println("AP: OLD API of AnnotationModelListener called"); //$NON-NLS-1$ |
1098 | |
1099 modelChanged(new AnnotationModelEvent(model)); | |
1100 } | |
1101 | |
1102 /* | |
1103 * @see dwtx.jface.text.source.IAnnotationModelListenerExtension#modelChanged(dwtx.jface.text.source.AnnotationModelEvent) | |
1104 */ | |
158 | 1105 public void modelChanged(AnnotationModelEvent event) { |
129 | 1106 Display textWidgetDisplay; |
1107 try { | |
1108 StyledText textWidget= fTextWidget; | |
1109 if (textWidget is null || textWidget.isDisposed()) | |
1110 return; | |
1111 textWidgetDisplay= textWidget.getDisplay(); | |
1112 } catch (DWTException ex) { | |
1113 if (ex.code is DWT.ERROR_WIDGET_DISPOSED) | |
1114 return; | |
1115 throw ex; | |
1116 } | |
1117 | |
1118 if (fIsSettingModel) { | |
1119 // inside the UI thread -> no need for posting | |
1120 if (textWidgetDisplay is Display.getCurrent()) | |
1121 updatePainting(event); | |
1122 else { | |
1123 /* | |
1124 * we can throw away the changes since | |
1125 * further update painting will happen | |
1126 */ | |
1127 return; | |
1128 } | |
1129 } else { | |
1130 if (DEBUG && event !is null && event.isWorldChange()) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1131 System.out_.println("AP: WORLD CHANGED, stack trace follows:"); //$NON-NLS-1$ |
145 | 1132 ExceptionPrintStackTrace( new Exception(""), Stdout ); |
129 | 1133 } |
1134 | |
1135 // XXX: posting here is a problem for annotations that are being | |
1136 // removed and the positions of which are not updated to document | |
1137 // changes any more. If the document gets modified between | |
1138 // now and running the posted runnable, the position information | |
1139 // is not accurate any longer. | |
158 | 1140 textWidgetDisplay.asyncExec( dgRunnable( (Event event_){ |
1141 if (fTextWidget !is null && !fTextWidget.isDisposed()) | |
1142 updatePainting(event_); | |
1143 }, event )); | |
129 | 1144 } |
1145 } | |
1146 | |
1147 /** | |
1148 * Sets the color in which the squiggly for the given annotation type should be drawn. | |
1149 * | |
1150 * @param annotationType the annotation type | |
1151 * @param color the color | |
1152 */ | |
1153 public void setAnnotationTypeColor(Object annotationType, Color color) { | |
1154 if (color !is null) | |
1155 fAnnotationType2Color.put(annotationType, color); | |
1156 else | |
1157 fAnnotationType2Color.remove(annotationType); | |
1158 fCachedAnnotationType2Color.clear(); | |
1159 } | |
1160 | |
1161 /** | |
1162 * Adds the given annotation type to the list of annotation types whose | |
1163 * annotations should be painted by this painter using squiggly drawing. If the annotation type | |
1164 * is already in this list, this method is without effect. | |
1165 * | |
1166 * @param annotationType the annotation type | |
1167 */ | |
1168 public void addAnnotationType(Object annotationType) { | |
1169 addAnnotationType(annotationType, SQUIGGLES); | |
1170 } | |
1171 | |
1172 /** | |
1173 * Adds the given annotation type to the list of annotation types whose | |
1174 * annotations should be painted by this painter using the given drawing strategy. | |
1175 * If the annotation type is already in this list, the old drawing strategy gets replaced. | |
1176 * | |
1177 * @param annotationType the annotation type | |
1178 * @param drawingStrategyID the id of the drawing strategy that should be used for this annotation type | |
1179 * @since 3.0 | |
1180 */ | |
1181 public void addAnnotationType(Object annotationType, Object drawingStrategyID) { | |
1182 fAnnotationType2PaintingStrategyId.put(annotationType, drawingStrategyID); | |
1183 fCachedAnnotationType2PaintingStrategy.clear(); | |
1184 | |
1185 if (fTextInputListener is null) { | |
135 | 1186 fTextInputListener= new class() ITextInputListener { |
129 | 1187 |
1188 /* | |
1189 * @see dwtx.jface.text.ITextInputListener#inputDocumentAboutToBeChanged(dwtx.jface.text.IDocument, dwtx.jface.text.IDocument) | |
1190 */ | |
1191 public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) { | |
1192 fInputDocumentAboutToBeChanged= true; | |
1193 } | |
1194 | |
1195 /* | |
1196 * @see dwtx.jface.text.ITextInputListener#inputDocumentChanged(dwtx.jface.text.IDocument, dwtx.jface.text.IDocument) | |
1197 */ | |
1198 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) { | |
1199 fInputDocumentAboutToBeChanged= false; | |
1200 } | |
1201 }; | |
1202 fSourceViewer.addTextInputListener(fTextInputListener); | |
1203 } | |
145 | 1204 |
129 | 1205 } |
1206 | |
1207 /** | |
1208 * Registers a new drawing strategy under the given ID. If there is already a | |
1209 * strategy registered under <code>id</code>, the old strategy gets replaced. | |
1210 * <p>The given id can be referenced when adding annotation types, see | |
1211 * {@link #addAnnotationType(Object, Object)}.</p> | |
1212 * | |
1213 * @param id the identifier under which the strategy can be referenced, not <code>null</code> | |
1214 * @param strategy the new strategy | |
1215 * @since 3.0 | |
1216 */ | |
1217 public void addDrawingStrategy(Object id, IDrawingStrategy strategy) { | |
1218 // don't permit null as null is used to signal that an annotation type is not | |
1219 // registered with a specific strategy, and that its annotation hierarchy should be searched | |
1220 if (id is null) | |
1221 throw new IllegalArgumentException(); | |
1222 fPaintingStrategyId2PaintingStrategy.put(id, strategy); | |
1223 fCachedAnnotationType2PaintingStrategy.clear(); | |
1224 } | |
1225 | |
1226 /** | |
1227 * Registers a new drawing strategy under the given ID. If there is already | |
1228 * a strategy registered under <code>id</code>, the old strategy gets | |
1229 * replaced. | |
1230 * <p> | |
1231 * The given id can be referenced when adding annotation types, see | |
1232 * {@link #addAnnotationType(Object, Object)}. | |
1233 * </p> | |
145 | 1234 * |
129 | 1235 * @param id the identifier under which the strategy can be referenced, not <code>null</code> |
1236 * @param strategy the new strategy | |
1237 * @since 3.4 | |
1238 */ | |
1239 public void addTextStyleStrategy(Object id, ITextStyleStrategy strategy) { | |
1240 // don't permit null as null is used to signal that an annotation type is not | |
1241 // registered with a specific strategy, and that its annotation hierarchy should be searched | |
1242 if (id is null) | |
1243 throw new IllegalArgumentException(); | |
1244 fPaintingStrategyId2PaintingStrategy.put(id, strategy); | |
1245 fCachedAnnotationType2PaintingStrategy.clear(); | |
1246 } | |
1247 | |
1248 /** | |
1249 * Adds the given annotation type to the list of annotation types whose | |
1250 * annotations should be highlighted this painter. If the annotation type | |
1251 * is already in this list, this method is without effect. | |
1252 * | |
1253 * @param annotationType the annotation type | |
1254 * @since 3.0 | |
1255 */ | |
1256 public void addHighlightAnnotationType(Object annotationType) { | |
1257 addAnnotationType(annotationType, HIGHLIGHTING); | |
1258 } | |
1259 | |
1260 /** | |
1261 * Removes the given annotation type from the list of annotation types whose | |
1262 * annotations are painted by this painter. If the annotation type is not | |
1263 * in this list, this method is without effect. | |
1264 * | |
1265 * @param annotationType the annotation type | |
1266 */ | |
1267 public void removeAnnotationType(Object annotationType) { | |
1268 fCachedAnnotationType2PaintingStrategy.clear(); | |
1269 fAnnotationType2PaintingStrategyId.remove(annotationType); | |
1270 if (fAnnotationType2PaintingStrategyId.isEmpty() && fTextInputListener !is null) { | |
1271 fSourceViewer.removeTextInputListener(fTextInputListener); | |
1272 fTextInputListener= null; | |
1273 fInputDocumentAboutToBeChanged= false; | |
1274 } | |
1275 } | |
1276 | |
1277 /** | |
1278 * Removes the given annotation type from the list of annotation types whose | |
1279 * annotations are highlighted by this painter. If the annotation type is not | |
1280 * in this list, this method is without effect. | |
1281 * | |
1282 * @param annotationType the annotation type | |
1283 * @since 3.0 | |
1284 */ | |
1285 public void removeHighlightAnnotationType(Object annotationType) { | |
1286 removeAnnotationType(annotationType); | |
1287 } | |
1288 | |
1289 /** | |
1290 * Clears the list of annotation types whose annotations are | |
1291 * painted by this painter. | |
1292 */ | |
1293 public void removeAllAnnotationTypes() { | |
1294 fCachedAnnotationType2PaintingStrategy.clear(); | |
1295 fAnnotationType2PaintingStrategyId.clear(); | |
1296 if (fTextInputListener !is null) { | |
1297 fSourceViewer.removeTextInputListener(fTextInputListener); | |
1298 fTextInputListener= null; | |
1299 } | |
1300 } | |
1301 | |
1302 /** | |
1303 * Returns whether the list of annotation types whose annotations are painted | |
1304 * by this painter contains at least on element. | |
1305 * | |
1306 * @return <code>true</code> if there is an annotation type whose annotations are painted | |
1307 */ | |
1308 public bool isPaintingAnnotations() { | |
1309 return !fAnnotationType2PaintingStrategyId.isEmpty(); | |
1310 } | |
1311 | |
1312 /* | |
1313 * @see dwtx.jface.text.IPainter#dispose() | |
1314 */ | |
1315 public void dispose() { | |
1316 | |
1317 if (fAnnotationType2Color !is null) { | |
1318 fAnnotationType2Color.clear(); | |
1319 fAnnotationType2Color= null; | |
1320 } | |
1321 | |
1322 if (fCachedAnnotationType2Color !is null) { | |
1323 fCachedAnnotationType2Color.clear(); | |
1324 fCachedAnnotationType2Color= null; | |
1325 } | |
1326 | |
1327 if (fCachedAnnotationType2PaintingStrategy !is null) { | |
1328 fCachedAnnotationType2PaintingStrategy.clear(); | |
1329 fCachedAnnotationType2PaintingStrategy= null; | |
1330 } | |
145 | 1331 |
129 | 1332 if (fAnnotationType2PaintingStrategyId !is null) { |
1333 fAnnotationType2PaintingStrategyId.clear(); | |
1334 fAnnotationType2PaintingStrategyId= null; | |
1335 } | |
145 | 1336 |
129 | 1337 fTextWidget= null; |
1338 fSourceViewer= null; | |
1339 fAnnotationAccess= null; | |
1340 fModel= null; | |
1341 synchronized (fDecorationMapLock) { | |
1342 fDecorationsMap= null; | |
1343 } | |
1344 synchronized (fHighlightedDecorationsMapLock) { | |
1345 fHighlightedDecorationsMap= null; | |
1346 } | |
1347 } | |
1348 | |
1349 /** | |
1350 * Returns the document offset of the upper left corner of the source viewer's view port, | |
1351 * possibly including partially visible lines. | |
1352 * | |
1353 * @return the document offset if the upper left corner of the view port | |
1354 */ | |
1355 private int getInclusiveTopIndexStartOffset() { | |
1356 | |
1357 if (fTextWidget !is null && !fTextWidget.isDisposed()) { | |
1358 int top= JFaceTextUtil.getPartialTopIndex(fSourceViewer); | |
1359 try { | |
1360 IDocument document= fSourceViewer.getDocument(); | |
1361 return document.getLineOffset(top); | |
1362 } catch (BadLocationException x) { | |
1363 } | |
1364 } | |
1365 | |
1366 return -1; | |
1367 } | |
1368 | |
1369 /** | |
1370 * Returns the first invisible document offset of the lower right corner of the source viewer's view port, | |
1371 * possibly including partially visible lines. | |
1372 * | |
1373 * @return the first invisible document offset of the lower right corner of the view port | |
1374 */ | |
1375 private int getExclusiveBottomIndexEndOffset() { | |
1376 | |
1377 if (fTextWidget !is null && !fTextWidget.isDisposed()) { | |
1378 int bottom= JFaceTextUtil.getPartialBottomIndex(fSourceViewer); | |
1379 try { | |
1380 IDocument document= fSourceViewer.getDocument(); | |
1381 | |
1382 if (bottom >= document.getNumberOfLines()) | |
1383 bottom= document.getNumberOfLines() - 1; | |
1384 | |
1385 return document.getLineOffset(bottom) + document.getLineLength(bottom); | |
1386 } catch (BadLocationException x) { | |
1387 } | |
1388 } | |
1389 | |
1390 return -1; | |
1391 } | |
1392 | |
1393 /* | |
1394 * @see dwt.events.PaintListener#paintControl(dwt.events.PaintEvent) | |
1395 */ | |
1396 public void paintControl(PaintEvent event) { | |
1397 if (fTextWidget !is null) | |
1398 handleDrawRequest(event); | |
1399 } | |
1400 | |
1401 /** | |
1402 * Handles the request to draw the annotations using the given graphical context. | |
1403 * | |
1404 * @param event the paint event or <code>null</code> | |
1405 */ | |
1406 private void handleDrawRequest(PaintEvent event) { | |
1407 | |
1408 if (fTextWidget is null) { | |
1409 // is already disposed | |
1410 return; | |
1411 } | |
1412 | |
1413 IRegion clippingRegion= computeClippingRegion(event, false); | |
1414 if (clippingRegion is null) | |
1415 return; | |
145 | 1416 |
129 | 1417 int vOffset= clippingRegion.getOffset(); |
1418 int vLength= clippingRegion.getLength(); | |
145 | 1419 |
129 | 1420 final GC gc= event !is null ? event.gc : null; |
1421 | |
1422 // Clone decorations | |
1423 Collection decorations; | |
1424 synchronized (fDecorationMapLock) { | |
1425 decorations= new ArrayList(fDecorationsMap.size()); | |
1426 decorations.addAll(fDecorationsMap.entrySet()); | |
1427 } | |
1428 | |
1429 /* | |
1430 * Create a new list of annotations to be drawn, since removing from decorations is more | |
1431 * expensive. One bucket per drawing layer. Use linked lists as addition is cheap here. | |
1432 */ | |
1433 ArrayList toBeDrawn= new ArrayList(10); | |
1434 for (Iterator e = decorations.iterator(); e.hasNext();) { | |
157
7f75eaa8103a
volatile and cast Map.Entry
Frank Benoit <benoit@tionex.de>
parents:
153
diff
changeset
|
1435 Map.Entry entry= cast(Map.Entry)e.next(); |
145 | 1436 |
134 | 1437 Annotation a= cast(Annotation)entry.getKey(); |
1438 Decoration pp = cast(Decoration)entry.getValue(); | |
129 | 1439 // prune any annotation that is not drawable or does not need drawing |
1440 if (!(a.isMarkedDeleted() || skip(a) || !pp.fPosition.overlapsWith(vOffset, vLength))) { | |
1441 // ensure sized appropriately | |
1442 for (int i= toBeDrawn.size(); i <= pp.fLayer; i++) | |
1443 toBeDrawn.add(new LinkedList()); | |
134 | 1444 (cast(List) toBeDrawn.get(pp.fLayer)).add(entry); |
129 | 1445 } |
1446 } | |
1447 IDocument document= fSourceViewer.getDocument(); | |
1448 for (Iterator it= toBeDrawn.iterator(); it.hasNext();) { | |
134 | 1449 List layer= cast(List) it.next(); |
129 | 1450 for (Iterator e = layer.iterator(); e.hasNext();) { |
157
7f75eaa8103a
volatile and cast Map.Entry
Frank Benoit <benoit@tionex.de>
parents:
153
diff
changeset
|
1451 Map.Entry entry= cast(Map.Entry)e.next(); |
134 | 1452 Annotation a= cast(Annotation)entry.getKey(); |
1453 Decoration pp = cast(Decoration)entry.getValue(); | |
129 | 1454 drawDecoration(pp, gc, a, clippingRegion, document); |
1455 } | |
1456 } | |
1457 } | |
145 | 1458 |
129 | 1459 private void drawDecoration(Decoration pp, GC gc, Annotation annotation, IRegion clippingRegion, IDocument document) { |
1460 if (clippingRegion is null) | |
1461 return; | |
1462 | |
145 | 1463 if (!(cast(IDrawingStrategy)pp.fPaintingStrategy )) |
129 | 1464 return; |
1465 | |
134 | 1466 IDrawingStrategy drawingStrategy= cast(IDrawingStrategy)pp.fPaintingStrategy; |
129 | 1467 |
1468 int clippingOffset= clippingRegion.getOffset(); | |
1469 int clippingLength= clippingRegion.getLength(); | |
1470 | |
1471 Position p= pp.fPosition; | |
1472 try { | |
1473 | |
1474 int startLine= document.getLineOfOffset(p.getOffset()); | |
1475 int lastInclusive= Math.max(p.getOffset(), p.getOffset() + p.getLength() - 1); | |
1476 int endLine= document.getLineOfOffset(lastInclusive); | |
1477 | |
1478 for (int i= startLine; i <= endLine; i++) { | |
1479 int lineOffset= document.getLineOffset(i); | |
1480 int paintStart= Math.max(lineOffset, p.getOffset()); | |
1481 String lineDelimiter= document.getLineDelimiter(i); | |
1482 int delimiterLength= lineDelimiter !is null ? lineDelimiter.length() : 0; | |
1483 int paintLength= Math.min(lineOffset + document.getLineLength(i) - delimiterLength, p.getOffset() + p.getLength()) - paintStart; | |
1484 if (paintLength >= 0 && overlapsWith(paintStart, paintLength, clippingOffset, clippingLength)) { | |
1485 // otherwise inside a line delimiter | |
1486 IRegion widgetRange= getWidgetRange(paintStart, paintLength); | |
1487 if (widgetRange !is null) { | |
1488 drawingStrategy.draw(annotation, gc, fTextWidget, widgetRange.getOffset(), widgetRange.getLength(), pp.fColor); | |
1489 } | |
1490 } | |
1491 } | |
1492 | |
1493 } catch (BadLocationException x) { | |
1494 } | |
1495 } | |
145 | 1496 |
129 | 1497 /** |
1498 * Computes the model (document) region that is covered by the paint event's clipping region. If | |
1499 * <code>event</code> is <code>null</code>, the model range covered by the visible editor | |
1500 * area (viewport) is returned. | |
145 | 1501 * |
129 | 1502 * @param event the paint event or <code>null</code> to use the entire viewport |
1503 * @param isClearing tells whether the clipping is need for clearing an annotation | |
1504 * @return the model region comprised by either the paint event's clipping region or the | |
1505 * viewport | |
1506 * @since 3.2 | |
1507 */ | |
1508 private IRegion computeClippingRegion(PaintEvent event, bool isClearing) { | |
1509 if (event is null) { | |
145 | 1510 |
129 | 1511 if (!isClearing && fCurrentDrawRange !is null) |
1512 return new Region(fCurrentDrawRange.offset, fCurrentDrawRange.length); | |
145 | 1513 |
129 | 1514 // trigger a repaint of the entire viewport |
1515 int vOffset= getInclusiveTopIndexStartOffset(); | |
1516 if (vOffset is -1) | |
1517 return null; | |
145 | 1518 |
129 | 1519 // http://bugs.eclipse.org/bugs/show_bug.cgi?id=17147 |
1520 int vLength= getExclusiveBottomIndexEndOffset() - vOffset; | |
145 | 1521 |
129 | 1522 return new Region(vOffset, vLength); |
1523 } | |
145 | 1524 |
129 | 1525 int widgetOffset; |
1526 try { | |
1527 int widgetClippingStartOffset= fTextWidget.getOffsetAtLocation(new Point(0, event.y)); | |
1528 int firstWidgetLine= fTextWidget.getLineAtOffset(widgetClippingStartOffset); | |
1529 widgetOffset= fTextWidget.getOffsetAtLine(firstWidgetLine); | |
1530 } catch (IllegalArgumentException ex1) { | |
1531 try { | |
1532 int firstVisibleLine= JFaceTextUtil.getPartialTopIndex(fTextWidget); | |
1533 widgetOffset= fTextWidget.getOffsetAtLine(firstVisibleLine); | |
1534 } catch (IllegalArgumentException ex2) { // above try code might fail too | |
1535 widgetOffset= 0; | |
1536 } | |
1537 } | |
145 | 1538 |
129 | 1539 int widgetEndOffset; |
1540 try { | |
1541 int widgetClippingEndOffset= fTextWidget.getOffsetAtLocation(new Point(0, event.y + event.height)); | |
1542 int lastWidgetLine= fTextWidget.getLineAtOffset(widgetClippingEndOffset); | |
1543 widgetEndOffset= fTextWidget.getOffsetAtLine(lastWidgetLine + 1); | |
1544 } catch (IllegalArgumentException ex1) { | |
1545 // happens if the editor is not "full", e.g. the last line of the document is visible in the editor | |
1546 try { | |
1547 int lastVisibleLine= JFaceTextUtil.getPartialBottomIndex(fTextWidget); | |
1548 if (lastVisibleLine is fTextWidget.getLineCount() - 1) | |
1549 // last line | |
1550 widgetEndOffset= fTextWidget.getCharCount(); | |
1551 else | |
1552 widgetEndOffset= fTextWidget.getOffsetAtLine(lastVisibleLine + 1) - 1; | |
1553 } catch (IllegalArgumentException ex2) { // above try code might fail too | |
1554 widgetEndOffset= fTextWidget.getCharCount(); | |
1555 } | |
1556 } | |
145 | 1557 |
129 | 1558 IRegion clippingRegion= getModelRange(widgetOffset, widgetEndOffset - widgetOffset); |
145 | 1559 |
129 | 1560 return clippingRegion; |
1561 } | |
1562 | |
1563 /** | |
1564 * Should the given annotation be skipped when handling draw requests? | |
1565 * | |
1566 * @param annotation the annotation | |
1567 * @return <code>true</code> iff the given annotation should be | |
1568 * skipped when handling draw requests | |
1569 * @since 3.0 | |
1570 */ | |
1571 protected bool skip(Annotation annotation) { | |
1572 return false; | |
1573 } | |
1574 | |
1575 /** | |
1576 * Returns the widget region that corresponds to the | |
1577 * given offset and length in the viewer's document. | |
145 | 1578 * |
129 | 1579 * @param modelOffset the model offset |
1580 * @param modelLength the model length | |
1581 * @return the corresponding widget region | |
1582 */ | |
1583 private IRegion getWidgetRange(int modelOffset, int modelLength) { | |
1584 fReusableRegion.setOffset(modelOffset); | |
1585 fReusableRegion.setLength(modelLength); | |
1586 | |
1587 if (fReusableRegion is null || fReusableRegion.getOffset() is Integer.MAX_VALUE) | |
1588 return null; | |
1589 | |
138 | 1590 if ( cast(ITextViewerExtension5)fSourceViewer ) { |
134 | 1591 ITextViewerExtension5 extension= cast(ITextViewerExtension5) fSourceViewer; |
129 | 1592 return extension.modelRange2WidgetRange(fReusableRegion); |
1593 } | |
1594 | |
1595 IRegion region= fSourceViewer.getVisibleRegion(); | |
1596 int offset= region.getOffset(); | |
1597 int length= region.getLength(); | |
1598 | |
1599 if (overlapsWith(fReusableRegion, region)) { | |
1600 int p1= Math.max(offset, fReusableRegion.getOffset()); | |
1601 int p2= Math.min(offset + length, fReusableRegion.getOffset() + fReusableRegion.getLength()); | |
1602 return new Region(p1 - offset, p2 - p1); | |
1603 } | |
1604 return null; | |
1605 } | |
1606 | |
1607 /** | |
1608 * Returns the model region that corresponds to the given region in the | |
1609 * viewer's text widget. | |
1610 * | |
1611 * @param offset the offset in the viewer's widget | |
1612 * @param length the length in the viewer's widget | |
1613 * @return the corresponding document region | |
1614 * @since 3.2 | |
1615 */ | |
1616 private IRegion getModelRange(int offset, int length) { | |
1617 if (offset is Integer.MAX_VALUE) | |
1618 return null; | |
1619 | |
138 | 1620 if ( cast(ITextViewerExtension5)fSourceViewer ) { |
134 | 1621 ITextViewerExtension5 extension= cast(ITextViewerExtension5) fSourceViewer; |
129 | 1622 return extension.widgetRange2ModelRange(new Region(offset, length)); |
1623 } | |
145 | 1624 |
129 | 1625 IRegion region= fSourceViewer.getVisibleRegion(); |
1626 return new Region(region.getOffset() + offset, length); | |
1627 } | |
145 | 1628 |
129 | 1629 /** |
1630 * Checks whether the intersection of the given text ranges | |
1631 * is empty or not. | |
1632 * | |
1633 * @param range1 the first range to check | |
1634 * @param range2 the second range to check | |
1635 * @return <code>true</code> if intersection is not empty | |
1636 */ | |
1637 private bool overlapsWith(IRegion range1, IRegion range2) { | |
1638 return overlapsWith(range1.getOffset(), range1.getLength(), range2.getOffset(), range2.getLength()); | |
1639 } | |
1640 | |
1641 /** | |
1642 * Checks whether the intersection of the given text ranges | |
1643 * is empty or not. | |
1644 * | |
1645 * @param offset1 offset of the first range | |
1646 * @param length1 length of the first range | |
1647 * @param offset2 offset of the second range | |
1648 * @param length2 length of the second range | |
1649 * @return <code>true</code> if intersection is not empty | |
1650 */ | |
1651 private bool overlapsWith(int offset1, int length1, int offset2, int length2) { | |
1652 int end= offset2 + length2; | |
1653 int thisEnd= offset1 + length1; | |
1654 | |
1655 if (length2 > 0) { | |
1656 if (length1 > 0) | |
1657 return offset1 < end && offset2 < thisEnd; | |
1658 return offset2 <= offset1 && offset1 < end; | |
1659 } | |
1660 | |
1661 if (length1 > 0) | |
1662 return offset1 <= offset2 && offset2 < thisEnd; | |
1663 return offset1 is offset2; | |
1664 } | |
1665 | |
1666 /* | |
1667 * @see dwtx.jface.text.IPainter#deactivate(bool) | |
1668 */ | |
1669 public void deactivate(bool redraw) { | |
1670 if (fIsActive) { | |
1671 fIsActive= false; | |
1672 disablePainting(redraw); | |
1673 setModel(null); | |
1674 catchupWithModel(null); | |
1675 } | |
1676 } | |
1677 | |
1678 /** | |
1679 * Returns whether the given reason causes a repaint. | |
1680 * | |
1681 * @param reason the reason | |
1682 * @return <code>true</code> if repaint reason, <code>false</code> otherwise | |
1683 * @since 3.0 | |
1684 */ | |
1685 protected bool isRepaintReason(int reason) { | |
1686 return CONFIGURATION is reason || INTERNAL is reason; | |
1687 } | |
1688 | |
1689 /** | |
1690 * Retrieves the annotation model from the given source viewer. | |
1691 * | |
1692 * @param sourceViewer the source viewer | |
1693 * @return the source viewer's annotation model or <code>null</code> if none can be found | |
1694 * @since 3.0 | |
1695 */ | |
1696 protected IAnnotationModel findAnnotationModel(ISourceViewer sourceViewer) { | |
1697 if(sourceViewer !is null) | |
1698 return sourceViewer.getAnnotationModel(); | |
1699 return null; | |
1700 } | |
1701 | |
1702 /* | |
1703 * @see dwtx.jface.text.IPainter#paint(int) | |
1704 */ | |
1705 public void paint(int reason) { | |
1706 if (fSourceViewer.getDocument() is null) { | |
1707 deactivate(false); | |
1708 return; | |
1709 } | |
1710 | |
1711 if (!fIsActive) { | |
1712 IAnnotationModel model= findAnnotationModel(fSourceViewer); | |
1713 if (model !is null) { | |
1714 fIsActive= true; | |
1715 setModel(model); | |
1716 } | |
1717 } else if (isRepaintReason(reason)) | |
1718 updatePainting(null); | |
1719 } | |
1720 | |
1721 /* | |
1722 * @see dwtx.jface.text.IPainter#setPositionManager(dwtx.jface.text.IPaintPositionManager) | |
1723 */ | |
1724 public void setPositionManager(IPaintPositionManager manager) { | |
1725 } | |
1726 } |