Mercurial > projects > dwt-addons
annotate dwtx/jface/text/source/AnnotationPainter.d @ 159:7926b636c282
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 27 Aug 2008 01:57:58 +0200 |
parents | 25f1f92fa3df |
children | 1a5b8f8129df |
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 */ | |
159 | 351 private static bool DEBUG_; |
352 private static bool DEBUG_init = false; | |
353 private static bool DEBUG(){ | |
354 if( !DEBUG_init ){ | |
355 DEBUG_init = true; | |
356 DEBUG_ = "true".equalsIgnoreCase(Platform.getDebugOption("dwtx.jface.text/debug/AnnotationPainter")); //$NON-NLS-1$//$NON-NLS-2$ | |
357 } | |
358 return DEBUG_; | |
359 } | |
360 | |
129 | 361 /** |
362 * The squiggly painter strategy. | |
363 * @since 3.0 | |
364 */ | |
147 | 365 private static const IDrawingStrategy SQUIGGLES_STRATEGY= new SquigglesStrategy(); |
129 | 366 |
367 /** | |
368 * This strategy is used to mark the <code>null</code> value in the chache | |
369 * maps. | |
145 | 370 * |
129 | 371 * @since 3.4 |
372 */ | |
147 | 373 private static const IDrawingStrategy NULL_STRATEGY= new NullStrategy(); |
129 | 374 /** |
375 * The squiggles painter id. | |
376 * @since 3.0 | |
377 */ | |
147 | 378 private static const Object SQUIGGLES= new Object(); |
129 | 379 /** |
380 * The squiggly painter strategy. | |
145 | 381 * |
129 | 382 * @since 3.4 |
383 */ | |
147 | 384 private static const ITextStyleStrategy HIGHLIGHTING_STRATEGY= new HighlightingStrategy(); |
129 | 385 |
386 /** | |
387 * The highlighting text style strategy id. | |
145 | 388 * |
129 | 389 * @since 3.4 |
390 */ | |
147 | 391 private static const Object HIGHLIGHTING= new Object(); |
129 | 392 |
393 /** | |
394 * The presentation information (decoration) for an annotation. Each such | |
395 * object represents one decoration drawn on the text area, such as squiggly lines | |
396 * and underlines. | |
397 */ | |
398 private static class Decoration { | |
399 /** The position of this decoration */ | |
400 private Position fPosition; | |
401 /** The color of this decoration */ | |
402 private Color fColor; | |
403 /** | |
404 * The annotation's layer | |
405 * @since 3.0 | |
406 */ | |
407 private int fLayer; | |
408 /** | |
409 * The painting strategy for this decoration. | |
410 * @since 3.0 | |
411 */ | |
412 private Object fPaintingStrategy; | |
413 } | |
414 | |
415 | |
416 /** Indicates whether this painter is active */ | |
417 private bool fIsActive= false; | |
418 /** Indicates whether this painter is managing decorations */ | |
419 private bool fIsPainting= false; | |
420 /** 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
|
421 private /+volatile+/ bool fIsSettingModel= false; |
129 | 422 /** The associated source viewer */ |
423 private ISourceViewer fSourceViewer; | |
424 /** The cached widget of the source viewer */ | |
425 private StyledText fTextWidget; | |
426 /** The annotation model providing the annotations to be drawn */ | |
427 private IAnnotationModel fModel; | |
428 /** The annotation access */ | |
429 private IAnnotationAccess fAnnotationAccess; | |
430 /** | |
431 * The map with decorations | |
432 * @since 3.0 | |
433 */ | |
434 private Map fDecorationsMap= new HashMap(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=50767 | |
435 /** | |
436 * The map with of highlighted decorations. | |
437 * @since 3.0 | |
438 */ | |
439 private Map fHighlightedDecorationsMap= new HashMap(); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=50767 | |
440 /** | |
441 * Mutex for highlighted decorations map. | |
442 * @since 3.0 | |
443 */ | |
444 private Object fDecorationMapLock= new Object(); | |
445 /** | |
446 * Mutex for for decorations map. | |
447 * @since 3.0 | |
448 */ | |
449 private Object fHighlightedDecorationsMapLock= new Object(); | |
450 /** | |
451 * Maps an annotation type to its registered color. | |
145 | 452 * |
129 | 453 * @see #setAnnotationTypeColor(Object, Color) |
454 */ | |
455 private Map fAnnotationType2Color= new HashMap(); | |
456 | |
457 /** | |
458 * Cache that maps the annotation type to its color. | |
459 * @since 3.4 | |
460 */ | |
461 private Map fCachedAnnotationType2Color= new HashMap(); | |
462 /** | |
463 * The range in which the current highlight annotations can be found. | |
464 * @since 3.0 | |
465 */ | |
466 private Position fCurrentHighlightAnnotationRange= null; | |
467 /** | |
468 * The range in which all added, removed and changed highlight | |
469 * annotations can be found since the last world change. | |
470 * @since 3.0 | |
471 */ | |
472 private Position fTotalHighlightAnnotationRange= null; | |
473 /** | |
474 * The range in which the currently drawn annotations can be found. | |
475 * @since 3.3 | |
476 */ | |
477 private Position fCurrentDrawRange= null; | |
478 /** | |
479 * The range in which all added, removed and changed drawn | |
480 * annotations can be found since the last world change. | |
481 * @since 3.3 | |
482 */ | |
483 private Position fTotalDrawRange= null; | |
484 /** | |
485 * The text input listener. | |
486 * @since 3.0 | |
487 */ | |
488 private ITextInputListener fTextInputListener; | |
489 /** | |
490 * Flag which tells that a new document input is currently being set. | |
491 * @since 3.0 | |
492 */ | |
493 private bool fInputDocumentAboutToBeChanged; | |
494 /** | |
495 * Maps annotation types to painting strategy identifiers. | |
145 | 496 * |
129 | 497 * @see #addAnnotationType(Object, Object) |
498 * @since 3.0 | |
499 */ | |
500 private Map fAnnotationType2PaintingStrategyId= new HashMap(); | |
501 /** | |
502 * Maps annotation types to painting strategy identifiers. | |
503 * @since 3.4 | |
504 */ | |
505 private Map fCachedAnnotationType2PaintingStrategy= new HashMap(); | |
506 | |
507 /** | |
508 * Maps painting strategy identifiers to painting strategies. | |
145 | 509 * |
129 | 510 * @since 3.0 |
511 */ | |
512 private Map fPaintingStrategyId2PaintingStrategy= new HashMap(); | |
513 | |
514 /** | |
515 * Reuse this region for performance reasons. | |
516 * @since 3.3 | |
517 */ | |
518 private ReusableRegion fReusableRegion= new ReusableRegion(); | |
145 | 519 |
129 | 520 /** |
521 * Creates a new annotation painter for the given source viewer and with the | |
522 * given annotation access. The painter is not initialized, i.e. no | |
523 * annotation types are configured to be painted. | |
524 * | |
525 * @param sourceViewer the source viewer for this painter | |
526 * @param access the annotation access for this painter | |
527 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
528 public this(ISourceViewer sourceViewer, IAnnotationAccess access) { |
129 | 529 fSourceViewer= sourceViewer; |
530 fAnnotationAccess= access; | |
531 fTextWidget= sourceViewer.getTextWidget(); | |
532 | |
533 // default drawing strategies: squiggles were the only decoration style before version 3.0 | |
534 fPaintingStrategyId2PaintingStrategy.put(SQUIGGLES, SQUIGGLES_STRATEGY); | |
535 fPaintingStrategyId2PaintingStrategy.put(HIGHLIGHTING, HIGHLIGHTING_STRATEGY); | |
536 } | |
537 | |
538 /** | |
539 * Returns whether this painter has to draw any squiggles. | |
540 * | |
541 * @return <code>true</code> if there are squiggles to be drawn, <code>false</code> otherwise | |
542 */ | |
543 private bool hasDecorations() { | |
544 synchronized (fDecorationMapLock) { | |
545 return !fDecorationsMap.isEmpty(); | |
546 } | |
547 } | |
548 | |
549 /** | |
550 * Enables painting. This painter registers a paint listener with the | |
551 * source viewer's widget. | |
552 */ | |
553 private void enablePainting() { | |
554 if (!fIsPainting && hasDecorations()) { | |
555 fIsPainting= true; | |
556 fTextWidget.addPaintListener(this); | |
557 handleDrawRequest(null); | |
558 } | |
559 } | |
560 | |
561 /** | |
562 * Disables painting, if is has previously been enabled. Removes | |
563 * any paint listeners registered with the source viewer's widget. | |
564 * | |
565 * @param redraw <code>true</code> if the widget should be redrawn after disabling | |
566 */ | |
567 private void disablePainting(bool redraw) { | |
568 if (fIsPainting) { | |
569 fIsPainting= false; | |
570 fTextWidget.removePaintListener(this); | |
571 if (redraw && hasDecorations()) | |
572 handleDrawRequest(null); | |
573 } | |
574 } | |
575 | |
576 /** | |
577 * Sets the annotation model for this painter. Registers this painter | |
578 * as listener of the give model, if the model is not <code>null</code>. | |
579 * | |
580 * @param model the annotation model | |
581 */ | |
582 private void setModel(IAnnotationModel model) { | |
583 if (fModel !is model) { | |
584 if (fModel !is null) | |
585 fModel.removeAnnotationModelListener(this); | |
586 fModel= model; | |
587 if (fModel !is null) { | |
588 try { | |
589 fIsSettingModel= true; | |
590 fModel.addAnnotationModelListener(this); | |
591 } finally { | |
592 fIsSettingModel= false; | |
593 } | |
594 } | |
595 } | |
596 } | |
597 | |
598 /** | |
599 * Updates the set of decorations based on the current state of | |
600 * the painter's annotation model. | |
601 * | |
602 * @param event the annotation model event | |
603 */ | |
604 private void catchupWithModel(AnnotationModelEvent event) { | |
605 | |
606 synchronized (fDecorationMapLock) { | |
607 if (fDecorationsMap is null) | |
608 return; | |
609 } | |
610 | |
611 IRegion clippingRegion= computeClippingRegion(null, true); | |
612 IDocument document= fSourceViewer.getDocument(); | |
613 | |
614 int highlightAnnotationRangeStart= Integer.MAX_VALUE; | |
615 int highlightAnnotationRangeEnd= -1; | |
145 | 616 |
129 | 617 int drawRangeStart= Integer.MAX_VALUE; |
618 int drawRangeEnd= -1; | |
619 | |
620 if (fModel !is null) { | |
621 | |
622 Map decorationsMap; | |
623 Map highlightedDecorationsMap; | |
624 | |
625 // Clone decoration maps | |
626 synchronized (fDecorationMapLock) { | |
627 decorationsMap= new HashMap(fDecorationsMap); | |
628 } | |
629 synchronized (fHighlightedDecorationsMapLock) { | |
630 highlightedDecorationsMap= new HashMap(fHighlightedDecorationsMap); | |
631 } | |
632 | |
633 bool isWorldChange= false; | |
634 | |
635 Iterator e; | |
636 if (event is null || event.isWorldChange()) { | |
637 isWorldChange= true; | |
638 | |
639 if (DEBUG && event is null) | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
640 System.out_.println("AP: INTERNAL CHANGE"); //$NON-NLS-1$ |
129 | 641 |
642 Iterator iter= decorationsMap.entrySet().iterator(); | |
643 while (iter.hasNext()) { | |
157
7f75eaa8103a
volatile and cast Map.Entry
Frank Benoit <benoit@tionex.de>
parents:
153
diff
changeset
|
644 Map.Entry entry= cast(Map.Entry)iter.next(); |
134 | 645 Annotation annotation= cast(Annotation)entry.getKey(); |
646 Decoration decoration= cast(Decoration)entry.getValue(); | |
129 | 647 drawDecoration(decoration, null, annotation, clippingRegion, document); |
648 } | |
145 | 649 |
129 | 650 decorationsMap.clear(); |
145 | 651 |
129 | 652 highlightedDecorationsMap.clear(); |
653 | |
654 e= fModel.getAnnotationIterator(); | |
655 | |
656 | |
657 } else { | |
658 | |
659 // Remove annotations | |
660 Annotation[] removedAnnotations= event.getRemovedAnnotations(); | |
661 for (int i=0, length= removedAnnotations.length; i < length; i++) { | |
662 Annotation annotation= removedAnnotations[i]; | |
134 | 663 Decoration decoration= cast(Decoration)highlightedDecorationsMap.remove(annotation); |
129 | 664 if (decoration !is null) { |
665 Position position= decoration.fPosition; | |
666 if (position !is null) { | |
667 highlightAnnotationRangeStart= Math.min(highlightAnnotationRangeStart, position.offset); | |
668 highlightAnnotationRangeEnd= Math.max(highlightAnnotationRangeEnd, position.offset + position.length); | |
669 } | |
670 } | |
134 | 671 decoration= cast(Decoration)decorationsMap.remove(annotation); |
129 | 672 if (decoration !is null) { |
673 drawDecoration(decoration, null, annotation, clippingRegion, document); | |
674 Position position= decoration.fPosition; | |
675 if (position !is null) { | |
676 drawRangeStart= Math.min(drawRangeStart, position.offset); | |
677 drawRangeEnd= Math.max(drawRangeEnd, position.offset + position.length); | |
678 } | |
679 } | |
145 | 680 |
129 | 681 } |
682 | |
683 // Update existing annotations | |
684 Annotation[] changedAnnotations= event.getChangedAnnotations(); | |
685 for (int i=0, length= changedAnnotations.length; i < length; i++) { | |
686 Annotation annotation= changedAnnotations[i]; | |
687 | |
688 bool isHighlighting= false; | |
145 | 689 |
134 | 690 Decoration decoration= cast(Decoration)highlightedDecorationsMap.get(annotation); |
129 | 691 |
692 if (decoration !is null) { | |
693 isHighlighting= true; | |
694 // The call below updates the decoration - no need to create new decoration | |
695 decoration= getDecoration(annotation, decoration); | |
696 if (decoration is null) | |
697 highlightedDecorationsMap.remove(annotation); | |
698 } else { | |
699 decoration= getDecoration(annotation, decoration); | |
145 | 700 if (decoration !is null && cast(ITextStyleStrategy)decoration.fPaintingStrategy ) { |
129 | 701 highlightedDecorationsMap.put(annotation, decoration); |
702 isHighlighting= true; | |
703 } | |
704 } | |
705 | |
706 bool usesDrawingStrategy= !isHighlighting && decoration !is null; | |
707 | |
708 Position position= null; | |
709 if (decoration is null) | |
710 position= fModel.getPosition(annotation); | |
711 else | |
712 position= decoration.fPosition; | |
713 | |
714 if (position !is null && !position.isDeleted()) { | |
715 if (isHighlighting) { | |
716 highlightAnnotationRangeStart= Math.min(highlightAnnotationRangeStart, position.offset); | |
717 highlightAnnotationRangeEnd= Math.max(highlightAnnotationRangeEnd, position.offset + position.length); | |
718 } | |
719 if (usesDrawingStrategy) { | |
720 drawRangeStart= Math.min(drawRangeStart, position.offset); | |
721 drawRangeEnd= Math.max(drawRangeEnd, position.offset + position.length); | |
722 } | |
723 } else { | |
724 highlightedDecorationsMap.remove(annotation); | |
725 } | |
726 | |
727 if (usesDrawingStrategy) { | |
134 | 728 Decoration oldDecoration= cast(Decoration)decorationsMap.get(annotation); |
129 | 729 if (oldDecoration !is null) { |
730 drawDecoration(oldDecoration, null, annotation, clippingRegion, document); | |
145 | 731 |
129 | 732 if (decoration !is null) |
733 decorationsMap.put(annotation, decoration); | |
734 else if (oldDecoration !is null) | |
735 decorationsMap.remove(annotation); | |
736 } | |
737 } | |
738 } | |
739 | |
740 e= Arrays.asList(event.getAddedAnnotations()).iterator(); | |
741 } | |
742 | |
743 // Add new annotations | |
744 while (e.hasNext()) { | |
134 | 745 Annotation annotation= cast(Annotation) e.next(); |
129 | 746 Decoration pp= getDecoration(annotation, null); |
747 if (pp !is null) { | |
145 | 748 if (cast(IDrawingStrategy)pp.fPaintingStrategy ) { |
129 | 749 decorationsMap.put(annotation, pp); |
750 drawRangeStart= Math.min(drawRangeStart, pp.fPosition.offset); | |
751 drawRangeEnd= Math.max(drawRangeEnd, pp.fPosition.offset + pp.fPosition.length); | |
145 | 752 } else if (cast(ITextStyleStrategy)pp.fPaintingStrategy ) { |
129 | 753 highlightedDecorationsMap.put(annotation, pp); |
754 highlightAnnotationRangeStart= Math.min(highlightAnnotationRangeStart, pp.fPosition.offset); | |
755 highlightAnnotationRangeEnd= Math.max(highlightAnnotationRangeEnd, pp.fPosition.offset + pp.fPosition.length); | |
756 } | |
145 | 757 |
129 | 758 } |
759 } | |
760 | |
761 synchronized (fDecorationMapLock) { | |
762 fDecorationsMap= decorationsMap; | |
763 updateDrawRanges(drawRangeStart, drawRangeEnd, isWorldChange); | |
764 } | |
765 | |
766 synchronized (fHighlightedDecorationsMapLock) { | |
767 fHighlightedDecorationsMap= highlightedDecorationsMap; | |
768 updateHighlightRanges(highlightAnnotationRangeStart, highlightAnnotationRangeEnd, isWorldChange); | |
769 } | |
770 } else { | |
771 // annotation model is null -> clear all | |
772 synchronized (fDecorationMapLock) { | |
773 fDecorationsMap.clear(); | |
774 } | |
775 synchronized (fHighlightedDecorationsMapLock) { | |
776 fHighlightedDecorationsMap.clear(); | |
777 } | |
778 } | |
779 } | |
780 | |
781 /** | |
782 * Updates the remembered highlight ranges. | |
783 * | |
784 * @param highlightAnnotationRangeStart the start of the range | |
785 * @param highlightAnnotationRangeEnd the end of the range | |
786 * @param isWorldChange tells whether the range belongs to a annotation model event reporting a world change | |
787 * @since 3.0 | |
788 */ | |
789 private void updateHighlightRanges(int highlightAnnotationRangeStart, int highlightAnnotationRangeEnd, bool isWorldChange) { | |
790 if (highlightAnnotationRangeStart !is Integer.MAX_VALUE) { | |
791 | |
792 int maxRangeStart= highlightAnnotationRangeStart; | |
793 int maxRangeEnd= highlightAnnotationRangeEnd; | |
794 | |
795 if (fTotalHighlightAnnotationRange !is null) { | |
796 maxRangeStart= Math.min(maxRangeStart, fTotalHighlightAnnotationRange.offset); | |
797 maxRangeEnd= Math.max(maxRangeEnd, fTotalHighlightAnnotationRange.offset + fTotalHighlightAnnotationRange.length); | |
798 } | |
799 | |
800 if (fTotalHighlightAnnotationRange is null) | |
801 fTotalHighlightAnnotationRange= new Position(0); | |
802 if (fCurrentHighlightAnnotationRange is null) | |
803 fCurrentHighlightAnnotationRange= new Position(0); | |
804 | |
805 if (isWorldChange) { | |
806 fTotalHighlightAnnotationRange.offset= highlightAnnotationRangeStart; | |
807 fTotalHighlightAnnotationRange.length= highlightAnnotationRangeEnd - highlightAnnotationRangeStart; | |
808 fCurrentHighlightAnnotationRange.offset= maxRangeStart; | |
809 fCurrentHighlightAnnotationRange.length= maxRangeEnd - maxRangeStart; | |
810 } else { | |
811 fTotalHighlightAnnotationRange.offset= maxRangeStart; | |
812 fTotalHighlightAnnotationRange.length= maxRangeEnd - maxRangeStart; | |
813 fCurrentHighlightAnnotationRange.offset=highlightAnnotationRangeStart; | |
814 fCurrentHighlightAnnotationRange.length= highlightAnnotationRangeEnd - highlightAnnotationRangeStart; | |
815 } | |
816 } else { | |
817 if (isWorldChange) { | |
818 fCurrentHighlightAnnotationRange= fTotalHighlightAnnotationRange; | |
819 fTotalHighlightAnnotationRange= null; | |
820 } else { | |
821 fCurrentHighlightAnnotationRange= null; | |
822 } | |
823 } | |
824 | |
825 adaptToDocumentLength(fCurrentHighlightAnnotationRange); | |
826 adaptToDocumentLength(fTotalHighlightAnnotationRange); | |
827 } | |
145 | 828 |
129 | 829 /** |
830 * Updates the remembered decoration ranges. | |
831 * | |
832 * @param drawRangeStart the start of the range | |
833 * @param drawRangeEnd the end of the range | |
834 * @param isWorldChange tells whether the range belongs to a annotation model event reporting a world change | |
835 * @since 3.3 | |
836 */ | |
837 private void updateDrawRanges(int drawRangeStart, int drawRangeEnd, bool isWorldChange) { | |
838 if (drawRangeStart !is Integer.MAX_VALUE) { | |
145 | 839 |
129 | 840 int maxRangeStart= drawRangeStart; |
841 int maxRangeEnd= drawRangeEnd; | |
145 | 842 |
129 | 843 if (fTotalDrawRange !is null) { |
844 maxRangeStart= Math.min(maxRangeStart, fTotalDrawRange.offset); | |
845 maxRangeEnd= Math.max(maxRangeEnd, fTotalDrawRange.offset + fTotalDrawRange.length); | |
846 } | |
145 | 847 |
129 | 848 if (fTotalDrawRange is null) |
849 fTotalDrawRange= new Position(0); | |
850 if (fCurrentDrawRange is null) | |
851 fCurrentDrawRange= new Position(0); | |
145 | 852 |
129 | 853 if (isWorldChange) { |
854 fTotalDrawRange.offset= drawRangeStart; | |
855 fTotalDrawRange.length= drawRangeEnd - drawRangeStart; | |
856 fCurrentDrawRange.offset= maxRangeStart; | |
857 fCurrentDrawRange.length= maxRangeEnd - maxRangeStart; | |
858 } else { | |
859 fTotalDrawRange.offset= maxRangeStart; | |
860 fTotalDrawRange.length= maxRangeEnd - maxRangeStart; | |
861 fCurrentDrawRange.offset=drawRangeStart; | |
862 fCurrentDrawRange.length= drawRangeEnd - drawRangeStart; | |
863 } | |
864 } else { | |
865 if (isWorldChange) { | |
866 fCurrentDrawRange= fTotalDrawRange; | |
867 fTotalDrawRange= null; | |
868 } else { | |
869 fCurrentDrawRange= null; | |
870 } | |
871 } | |
145 | 872 |
129 | 873 adaptToDocumentLength(fCurrentDrawRange); |
874 adaptToDocumentLength(fTotalDrawRange); | |
875 } | |
876 | |
877 /** | |
878 * Adapts the given position to the document length. | |
879 * | |
880 * @param position the position to adapt | |
881 * @since 3.0 | |
882 */ | |
883 private void adaptToDocumentLength(Position position) { | |
884 if (position is null) | |
885 return; | |
886 | |
887 int length= fSourceViewer.getDocument().getLength(); | |
888 position.offset= Math.min(position.offset, length); | |
889 position.length= Math.min(position.length, length - position.offset); | |
890 } | |
891 | |
892 /** | |
893 * Returns a decoration for the given annotation if this | |
894 * annotation is valid and shown by this painter. | |
895 * | |
896 * @param annotation the annotation | |
897 * @param decoration the decoration to be adapted and returned or <code>null</code> if a new one must be created | |
898 * @return the decoration or <code>null</code> if there's no valid one | |
899 * @since 3.0 | |
900 */ | |
901 private Decoration getDecoration(Annotation annotation, Decoration decoration) { | |
902 | |
903 if (annotation.isMarkedDeleted()) | |
904 return null; | |
905 | |
906 String type= annotation.getType(); | |
907 | |
908 Object paintingStrategy= getPaintingStrategy(type); | |
144 | 909 if (paintingStrategy is null || cast(NullStrategy)paintingStrategy ) |
129 | 910 return null; |
911 | |
912 Color color= getColor(type); | |
913 if (color is null) | |
914 return null; | |
915 | |
916 Position position= fModel.getPosition(annotation); | |
917 if (position is null || position.isDeleted()) | |
918 return null; | |
145 | 919 |
129 | 920 if (decoration is null) |
921 decoration= new Decoration(); | |
922 | |
923 decoration.fPosition= position; | |
924 decoration.fColor= color; | |
138 | 925 if ( cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
134 | 926 IAnnotationAccessExtension extension= cast(IAnnotationAccessExtension) fAnnotationAccess; |
129 | 927 decoration.fLayer= extension.getLayer(annotation); |
928 } else { | |
929 decoration.fLayer= IAnnotationAccessExtension.DEFAULT_LAYER; | |
930 } | |
145 | 931 |
129 | 932 decoration.fPaintingStrategy= paintingStrategy; |
145 | 933 |
129 | 934 return decoration; |
935 } | |
936 | |
937 /** | |
938 * Returns the painting strategy for the given annotation. | |
145 | 939 * |
129 | 940 * @param type the annotation type |
941 * @return the annotation painter | |
942 * @since 3.0 | |
943 */ | |
158 | 944 private Object getPaintingStrategy(String type) { |
129 | 945 Object strategy= fCachedAnnotationType2PaintingStrategy.get(type); |
946 if (strategy !is null) | |
947 return strategy; | |
948 | |
949 strategy= fPaintingStrategyId2PaintingStrategy.get(fAnnotationType2PaintingStrategyId.get(type)); | |
950 if (strategy !is null) { | |
951 fCachedAnnotationType2PaintingStrategy.put(type, strategy); | |
952 return strategy; | |
953 } | |
954 | |
138 | 955 if ( cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
134 | 956 IAnnotationAccessExtension ext = cast(IAnnotationAccessExtension) fAnnotationAccess; |
129 | 957 Object[] sts = ext.getSupertypes(type); |
958 for (int i= 0; i < sts.length; i++) { | |
959 strategy= fPaintingStrategyId2PaintingStrategy.get(fAnnotationType2PaintingStrategyId.get(sts[i])); | |
960 if (strategy !is null) { | |
961 fCachedAnnotationType2PaintingStrategy.put(type, strategy); | |
962 return strategy; | |
963 } | |
964 } | |
965 } | |
966 | |
967 fCachedAnnotationType2PaintingStrategy.put(type, NULL_STRATEGY); | |
968 return null; | |
969 | |
970 } | |
971 | |
972 /** | |
973 * Returns the color for the given annotation type | |
974 * | |
975 * @param annotationType the annotation type | |
976 * @return the color | |
977 * @since 3.0 | |
978 */ | |
158 | 979 private Color getColor(Object annotationType) { |
134 | 980 Color color= cast(Color)fCachedAnnotationType2Color.get(annotationType); |
129 | 981 if (color !is null) |
982 return color; | |
983 | |
134 | 984 color= cast(Color)fAnnotationType2Color.get(annotationType); |
129 | 985 if (color !is null) { |
986 fCachedAnnotationType2Color.put(annotationType, color); | |
987 return color; | |
988 } | |
989 | |
138 | 990 if ( cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
134 | 991 IAnnotationAccessExtension extension= cast(IAnnotationAccessExtension) fAnnotationAccess; |
129 | 992 Object[] superTypes= extension.getSupertypes(annotationType); |
993 if (superTypes !is null) { | |
994 for (int i= 0; i < superTypes.length; i++) { | |
134 | 995 color= cast(Color)fAnnotationType2Color.get(superTypes[i]); |
129 | 996 if (color !is null) { |
997 fCachedAnnotationType2Color.put(annotationType, color); | |
998 return color; | |
999 } | |
1000 } | |
1001 } | |
1002 } | |
1003 | |
1004 return null; | |
1005 } | |
1006 | |
1007 /** | |
1008 * Recomputes the squiggles to be drawn and redraws them. | |
1009 * | |
1010 * @param event the annotation model event | |
1011 * @since 3.0 | |
1012 */ | |
1013 private void updatePainting(AnnotationModelEvent event) { | |
1014 disablePainting(event is null); | |
1015 | |
1016 catchupWithModel(event); | |
1017 | |
1018 if (!fInputDocumentAboutToBeChanged) | |
1019 invalidateTextPresentation(); | |
1020 | |
1021 enablePainting(); | |
1022 } | |
1023 | |
1024 private void invalidateTextPresentation() { | |
1025 IRegion r= null; | |
1026 synchronized (fHighlightedDecorationsMapLock) { | |
1027 if (fCurrentHighlightAnnotationRange !is null) | |
1028 r= new Region(fCurrentHighlightAnnotationRange.getOffset(), fCurrentHighlightAnnotationRange.getLength()); | |
1029 } | |
1030 if (r is null) | |
1031 return; | |
1032 | |
138 | 1033 if ( cast(ITextViewerExtension2)fSourceViewer ) { |
145 | 1034 if (DEBUG) |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1035 System.out_.println("AP: invalidating offset: " + r.getOffset() + ", length= " + r.getLength()); //$NON-NLS-1$ //$NON-NLS-2$ |
129 | 1036 |
134 | 1037 (cast(ITextViewerExtension2)fSourceViewer).invalidateTextPresentation(r.getOffset(), r.getLength()); |
129 | 1038 |
1039 } else { | |
1040 fSourceViewer.invalidateTextPresentation(); | |
1041 } | |
1042 } | |
1043 | |
1044 /* | |
1045 * @see dwtx.jface.text.ITextPresentationListener#applyTextPresentation(dwtx.jface.text.TextPresentation) | |
1046 * @since 3.0 | |
1047 */ | |
1048 public void applyTextPresentation(TextPresentation tp) { | |
1049 Set decorations; | |
1050 | |
1051 synchronized (fHighlightedDecorationsMapLock) { | |
1052 if (fHighlightedDecorationsMap is null || fHighlightedDecorationsMap.isEmpty()) | |
1053 return; | |
1054 | |
1055 decorations= new HashSet(fHighlightedDecorationsMap.entrySet()); | |
1056 } | |
1057 | |
1058 IRegion region= tp.getExtent(); | |
1059 | |
145 | 1060 if (DEBUG) |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1061 System.out_.println("AP: applying text presentation offset: " + region.getOffset() + ", length= " + region.getLength()); //$NON-NLS-1$ //$NON-NLS-2$ |
129 | 1062 |
1063 for (int layer= 0, maxLayer= 1; layer < maxLayer; layer++) { | |
1064 | |
1065 for (Iterator iter= decorations.iterator(); iter.hasNext();) { | |
157
7f75eaa8103a
volatile and cast Map.Entry
Frank Benoit <benoit@tionex.de>
parents:
153
diff
changeset
|
1066 Map.Entry entry= cast(Map.Entry)iter.next(); |
129 | 1067 |
134 | 1068 Annotation a= cast(Annotation)entry.getKey(); |
129 | 1069 if (a.isMarkedDeleted()) |
1070 continue; | |
1071 | |
134 | 1072 Decoration pp = cast(Decoration)entry.getValue(); |
129 | 1073 |
1074 maxLayer= Math.max(maxLayer, pp.fLayer + 1); // dynamically update layer maximum | |
1075 if (pp.fLayer !is layer) // wrong layer: skip annotation | |
1076 continue; | |
1077 | |
1078 Position p= pp.fPosition; | |
138 | 1079 if ( cast(ITextViewerExtension5)fSourceViewer ) { |
134 | 1080 ITextViewerExtension5 extension3= cast(ITextViewerExtension5) fSourceViewer; |
129 | 1081 if (null is extension3.modelRange2WidgetRange(new Region(p.getOffset(), p.getLength()))) |
1082 continue; | |
1083 } else if (!fSourceViewer.overlapsWithVisibleRegion(p.offset, p.length)) { | |
1084 continue; | |
1085 } | |
1086 | |
1087 int regionEnd= region.getOffset() + region.getLength(); | |
1088 int pEnd= p.getOffset() + p.getLength(); | |
1089 if (pEnd >= region.getOffset() && regionEnd > p.getOffset()) { | |
1090 int start= Math.max(p.getOffset(), region.getOffset()); | |
1091 int end= Math.min(regionEnd, pEnd); | |
1092 int length= Math.max(end - start, 0); | |
1093 StyleRange styleRange= new StyleRange(start, length, null, null); | |
134 | 1094 (cast(ITextStyleStrategy)pp.fPaintingStrategy).applyTextStyle(styleRange, pp.fColor); |
129 | 1095 tp.mergeStyleRange(styleRange); |
1096 } | |
1097 } | |
1098 } | |
1099 } | |
1100 | |
1101 /* | |
1102 * @see dwtx.jface.text.source.IAnnotationModelListener#modelChanged(dwtx.jface.text.source.IAnnotationModel) | |
1103 */ | |
158 | 1104 public synchronized void modelChanged(IAnnotationModel model) { |
145 | 1105 if (DEBUG) |
129 | 1106 System.err.println("AP: OLD API of AnnotationModelListener called"); //$NON-NLS-1$ |
1107 | |
1108 modelChanged(new AnnotationModelEvent(model)); | |
1109 } | |
1110 | |
1111 /* | |
1112 * @see dwtx.jface.text.source.IAnnotationModelListenerExtension#modelChanged(dwtx.jface.text.source.AnnotationModelEvent) | |
1113 */ | |
158 | 1114 public void modelChanged(AnnotationModelEvent event) { |
129 | 1115 Display textWidgetDisplay; |
1116 try { | |
1117 StyledText textWidget= fTextWidget; | |
1118 if (textWidget is null || textWidget.isDisposed()) | |
1119 return; | |
1120 textWidgetDisplay= textWidget.getDisplay(); | |
1121 } catch (DWTException ex) { | |
1122 if (ex.code is DWT.ERROR_WIDGET_DISPOSED) | |
1123 return; | |
1124 throw ex; | |
1125 } | |
1126 | |
1127 if (fIsSettingModel) { | |
1128 // inside the UI thread -> no need for posting | |
1129 if (textWidgetDisplay is Display.getCurrent()) | |
1130 updatePainting(event); | |
1131 else { | |
1132 /* | |
1133 * we can throw away the changes since | |
1134 * further update painting will happen | |
1135 */ | |
1136 return; | |
1137 } | |
1138 } else { | |
1139 if (DEBUG && event !is null && event.isWorldChange()) { | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1140 System.out_.println("AP: WORLD CHANGED, stack trace follows:"); //$NON-NLS-1$ |
145 | 1141 ExceptionPrintStackTrace( new Exception(""), Stdout ); |
129 | 1142 } |
1143 | |
1144 // XXX: posting here is a problem for annotations that are being | |
1145 // removed and the positions of which are not updated to document | |
1146 // changes any more. If the document gets modified between | |
1147 // now and running the posted runnable, the position information | |
1148 // is not accurate any longer. | |
158 | 1149 textWidgetDisplay.asyncExec( dgRunnable( (Event event_){ |
1150 if (fTextWidget !is null && !fTextWidget.isDisposed()) | |
1151 updatePainting(event_); | |
1152 }, event )); | |
129 | 1153 } |
1154 } | |
1155 | |
1156 /** | |
1157 * Sets the color in which the squiggly for the given annotation type should be drawn. | |
1158 * | |
1159 * @param annotationType the annotation type | |
1160 * @param color the color | |
1161 */ | |
1162 public void setAnnotationTypeColor(Object annotationType, Color color) { | |
1163 if (color !is null) | |
1164 fAnnotationType2Color.put(annotationType, color); | |
1165 else | |
1166 fAnnotationType2Color.remove(annotationType); | |
1167 fCachedAnnotationType2Color.clear(); | |
1168 } | |
1169 | |
1170 /** | |
1171 * Adds the given annotation type to the list of annotation types whose | |
1172 * annotations should be painted by this painter using squiggly drawing. If the annotation type | |
1173 * is already in this list, this method is without effect. | |
1174 * | |
1175 * @param annotationType the annotation type | |
1176 */ | |
1177 public void addAnnotationType(Object annotationType) { | |
1178 addAnnotationType(annotationType, SQUIGGLES); | |
1179 } | |
1180 | |
1181 /** | |
1182 * Adds the given annotation type to the list of annotation types whose | |
1183 * annotations should be painted by this painter using the given drawing strategy. | |
1184 * If the annotation type is already in this list, the old drawing strategy gets replaced. | |
1185 * | |
1186 * @param annotationType the annotation type | |
1187 * @param drawingStrategyID the id of the drawing strategy that should be used for this annotation type | |
1188 * @since 3.0 | |
1189 */ | |
1190 public void addAnnotationType(Object annotationType, Object drawingStrategyID) { | |
1191 fAnnotationType2PaintingStrategyId.put(annotationType, drawingStrategyID); | |
1192 fCachedAnnotationType2PaintingStrategy.clear(); | |
1193 | |
1194 if (fTextInputListener is null) { | |
135 | 1195 fTextInputListener= new class() ITextInputListener { |
129 | 1196 |
1197 /* | |
1198 * @see dwtx.jface.text.ITextInputListener#inputDocumentAboutToBeChanged(dwtx.jface.text.IDocument, dwtx.jface.text.IDocument) | |
1199 */ | |
1200 public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) { | |
1201 fInputDocumentAboutToBeChanged= true; | |
1202 } | |
1203 | |
1204 /* | |
1205 * @see dwtx.jface.text.ITextInputListener#inputDocumentChanged(dwtx.jface.text.IDocument, dwtx.jface.text.IDocument) | |
1206 */ | |
1207 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) { | |
1208 fInputDocumentAboutToBeChanged= false; | |
1209 } | |
1210 }; | |
1211 fSourceViewer.addTextInputListener(fTextInputListener); | |
1212 } | |
145 | 1213 |
129 | 1214 } |
1215 | |
1216 /** | |
1217 * Registers a new drawing strategy under the given ID. If there is already a | |
1218 * strategy registered under <code>id</code>, the old strategy gets replaced. | |
1219 * <p>The given id can be referenced when adding annotation types, see | |
1220 * {@link #addAnnotationType(Object, Object)}.</p> | |
1221 * | |
1222 * @param id the identifier under which the strategy can be referenced, not <code>null</code> | |
1223 * @param strategy the new strategy | |
1224 * @since 3.0 | |
1225 */ | |
1226 public void addDrawingStrategy(Object id, IDrawingStrategy strategy) { | |
1227 // don't permit null as null is used to signal that an annotation type is not | |
1228 // registered with a specific strategy, and that its annotation hierarchy should be searched | |
1229 if (id is null) | |
1230 throw new IllegalArgumentException(); | |
1231 fPaintingStrategyId2PaintingStrategy.put(id, strategy); | |
1232 fCachedAnnotationType2PaintingStrategy.clear(); | |
1233 } | |
1234 | |
1235 /** | |
1236 * Registers a new drawing strategy under the given ID. If there is already | |
1237 * a strategy registered under <code>id</code>, the old strategy gets | |
1238 * replaced. | |
1239 * <p> | |
1240 * The given id can be referenced when adding annotation types, see | |
1241 * {@link #addAnnotationType(Object, Object)}. | |
1242 * </p> | |
145 | 1243 * |
129 | 1244 * @param id the identifier under which the strategy can be referenced, not <code>null</code> |
1245 * @param strategy the new strategy | |
1246 * @since 3.4 | |
1247 */ | |
1248 public void addTextStyleStrategy(Object id, ITextStyleStrategy strategy) { | |
1249 // don't permit null as null is used to signal that an annotation type is not | |
1250 // registered with a specific strategy, and that its annotation hierarchy should be searched | |
1251 if (id is null) | |
1252 throw new IllegalArgumentException(); | |
1253 fPaintingStrategyId2PaintingStrategy.put(id, strategy); | |
1254 fCachedAnnotationType2PaintingStrategy.clear(); | |
1255 } | |
1256 | |
1257 /** | |
1258 * Adds the given annotation type to the list of annotation types whose | |
1259 * annotations should be highlighted this painter. If the annotation type | |
1260 * is already in this list, this method is without effect. | |
1261 * | |
1262 * @param annotationType the annotation type | |
1263 * @since 3.0 | |
1264 */ | |
1265 public void addHighlightAnnotationType(Object annotationType) { | |
1266 addAnnotationType(annotationType, HIGHLIGHTING); | |
1267 } | |
1268 | |
1269 /** | |
1270 * Removes the given annotation type from the list of annotation types whose | |
1271 * annotations are painted by this painter. If the annotation type is not | |
1272 * in this list, this method is without effect. | |
1273 * | |
1274 * @param annotationType the annotation type | |
1275 */ | |
1276 public void removeAnnotationType(Object annotationType) { | |
1277 fCachedAnnotationType2PaintingStrategy.clear(); | |
1278 fAnnotationType2PaintingStrategyId.remove(annotationType); | |
1279 if (fAnnotationType2PaintingStrategyId.isEmpty() && fTextInputListener !is null) { | |
1280 fSourceViewer.removeTextInputListener(fTextInputListener); | |
1281 fTextInputListener= null; | |
1282 fInputDocumentAboutToBeChanged= false; | |
1283 } | |
1284 } | |
1285 | |
1286 /** | |
1287 * Removes the given annotation type from the list of annotation types whose | |
1288 * annotations are highlighted by this painter. If the annotation type is not | |
1289 * in this list, this method is without effect. | |
1290 * | |
1291 * @param annotationType the annotation type | |
1292 * @since 3.0 | |
1293 */ | |
1294 public void removeHighlightAnnotationType(Object annotationType) { | |
1295 removeAnnotationType(annotationType); | |
1296 } | |
1297 | |
1298 /** | |
1299 * Clears the list of annotation types whose annotations are | |
1300 * painted by this painter. | |
1301 */ | |
1302 public void removeAllAnnotationTypes() { | |
1303 fCachedAnnotationType2PaintingStrategy.clear(); | |
1304 fAnnotationType2PaintingStrategyId.clear(); | |
1305 if (fTextInputListener !is null) { | |
1306 fSourceViewer.removeTextInputListener(fTextInputListener); | |
1307 fTextInputListener= null; | |
1308 } | |
1309 } | |
1310 | |
1311 /** | |
1312 * Returns whether the list of annotation types whose annotations are painted | |
1313 * by this painter contains at least on element. | |
1314 * | |
1315 * @return <code>true</code> if there is an annotation type whose annotations are painted | |
1316 */ | |
1317 public bool isPaintingAnnotations() { | |
1318 return !fAnnotationType2PaintingStrategyId.isEmpty(); | |
1319 } | |
1320 | |
1321 /* | |
1322 * @see dwtx.jface.text.IPainter#dispose() | |
1323 */ | |
1324 public void dispose() { | |
1325 | |
1326 if (fAnnotationType2Color !is null) { | |
1327 fAnnotationType2Color.clear(); | |
1328 fAnnotationType2Color= null; | |
1329 } | |
1330 | |
1331 if (fCachedAnnotationType2Color !is null) { | |
1332 fCachedAnnotationType2Color.clear(); | |
1333 fCachedAnnotationType2Color= null; | |
1334 } | |
1335 | |
1336 if (fCachedAnnotationType2PaintingStrategy !is null) { | |
1337 fCachedAnnotationType2PaintingStrategy.clear(); | |
1338 fCachedAnnotationType2PaintingStrategy= null; | |
1339 } | |
145 | 1340 |
129 | 1341 if (fAnnotationType2PaintingStrategyId !is null) { |
1342 fAnnotationType2PaintingStrategyId.clear(); | |
1343 fAnnotationType2PaintingStrategyId= null; | |
1344 } | |
145 | 1345 |
129 | 1346 fTextWidget= null; |
1347 fSourceViewer= null; | |
1348 fAnnotationAccess= null; | |
1349 fModel= null; | |
1350 synchronized (fDecorationMapLock) { | |
1351 fDecorationsMap= null; | |
1352 } | |
1353 synchronized (fHighlightedDecorationsMapLock) { | |
1354 fHighlightedDecorationsMap= null; | |
1355 } | |
1356 } | |
1357 | |
1358 /** | |
1359 * Returns the document offset of the upper left corner of the source viewer's view port, | |
1360 * possibly including partially visible lines. | |
1361 * | |
1362 * @return the document offset if the upper left corner of the view port | |
1363 */ | |
1364 private int getInclusiveTopIndexStartOffset() { | |
1365 | |
1366 if (fTextWidget !is null && !fTextWidget.isDisposed()) { | |
1367 int top= JFaceTextUtil.getPartialTopIndex(fSourceViewer); | |
1368 try { | |
1369 IDocument document= fSourceViewer.getDocument(); | |
1370 return document.getLineOffset(top); | |
1371 } catch (BadLocationException x) { | |
1372 } | |
1373 } | |
1374 | |
1375 return -1; | |
1376 } | |
1377 | |
1378 /** | |
1379 * Returns the first invisible document offset of the lower right corner of the source viewer's view port, | |
1380 * possibly including partially visible lines. | |
1381 * | |
1382 * @return the first invisible document offset of the lower right corner of the view port | |
1383 */ | |
1384 private int getExclusiveBottomIndexEndOffset() { | |
1385 | |
1386 if (fTextWidget !is null && !fTextWidget.isDisposed()) { | |
1387 int bottom= JFaceTextUtil.getPartialBottomIndex(fSourceViewer); | |
1388 try { | |
1389 IDocument document= fSourceViewer.getDocument(); | |
1390 | |
1391 if (bottom >= document.getNumberOfLines()) | |
1392 bottom= document.getNumberOfLines() - 1; | |
1393 | |
1394 return document.getLineOffset(bottom) + document.getLineLength(bottom); | |
1395 } catch (BadLocationException x) { | |
1396 } | |
1397 } | |
1398 | |
1399 return -1; | |
1400 } | |
1401 | |
1402 /* | |
1403 * @see dwt.events.PaintListener#paintControl(dwt.events.PaintEvent) | |
1404 */ | |
1405 public void paintControl(PaintEvent event) { | |
1406 if (fTextWidget !is null) | |
1407 handleDrawRequest(event); | |
1408 } | |
1409 | |
1410 /** | |
1411 * Handles the request to draw the annotations using the given graphical context. | |
1412 * | |
1413 * @param event the paint event or <code>null</code> | |
1414 */ | |
1415 private void handleDrawRequest(PaintEvent event) { | |
1416 | |
1417 if (fTextWidget is null) { | |
1418 // is already disposed | |
1419 return; | |
1420 } | |
1421 | |
1422 IRegion clippingRegion= computeClippingRegion(event, false); | |
1423 if (clippingRegion is null) | |
1424 return; | |
145 | 1425 |
129 | 1426 int vOffset= clippingRegion.getOffset(); |
1427 int vLength= clippingRegion.getLength(); | |
145 | 1428 |
129 | 1429 final GC gc= event !is null ? event.gc : null; |
1430 | |
1431 // Clone decorations | |
1432 Collection decorations; | |
1433 synchronized (fDecorationMapLock) { | |
1434 decorations= new ArrayList(fDecorationsMap.size()); | |
1435 decorations.addAll(fDecorationsMap.entrySet()); | |
1436 } | |
1437 | |
1438 /* | |
1439 * Create a new list of annotations to be drawn, since removing from decorations is more | |
1440 * expensive. One bucket per drawing layer. Use linked lists as addition is cheap here. | |
1441 */ | |
1442 ArrayList toBeDrawn= new ArrayList(10); | |
1443 for (Iterator e = decorations.iterator(); e.hasNext();) { | |
157
7f75eaa8103a
volatile and cast Map.Entry
Frank Benoit <benoit@tionex.de>
parents:
153
diff
changeset
|
1444 Map.Entry entry= cast(Map.Entry)e.next(); |
145 | 1445 |
134 | 1446 Annotation a= cast(Annotation)entry.getKey(); |
1447 Decoration pp = cast(Decoration)entry.getValue(); | |
129 | 1448 // prune any annotation that is not drawable or does not need drawing |
1449 if (!(a.isMarkedDeleted() || skip(a) || !pp.fPosition.overlapsWith(vOffset, vLength))) { | |
1450 // ensure sized appropriately | |
1451 for (int i= toBeDrawn.size(); i <= pp.fLayer; i++) | |
1452 toBeDrawn.add(new LinkedList()); | |
134 | 1453 (cast(List) toBeDrawn.get(pp.fLayer)).add(entry); |
129 | 1454 } |
1455 } | |
1456 IDocument document= fSourceViewer.getDocument(); | |
1457 for (Iterator it= toBeDrawn.iterator(); it.hasNext();) { | |
134 | 1458 List layer= cast(List) it.next(); |
129 | 1459 for (Iterator e = layer.iterator(); e.hasNext();) { |
157
7f75eaa8103a
volatile and cast Map.Entry
Frank Benoit <benoit@tionex.de>
parents:
153
diff
changeset
|
1460 Map.Entry entry= cast(Map.Entry)e.next(); |
134 | 1461 Annotation a= cast(Annotation)entry.getKey(); |
1462 Decoration pp = cast(Decoration)entry.getValue(); | |
129 | 1463 drawDecoration(pp, gc, a, clippingRegion, document); |
1464 } | |
1465 } | |
1466 } | |
145 | 1467 |
129 | 1468 private void drawDecoration(Decoration pp, GC gc, Annotation annotation, IRegion clippingRegion, IDocument document) { |
1469 if (clippingRegion is null) | |
1470 return; | |
1471 | |
145 | 1472 if (!(cast(IDrawingStrategy)pp.fPaintingStrategy )) |
129 | 1473 return; |
1474 | |
134 | 1475 IDrawingStrategy drawingStrategy= cast(IDrawingStrategy)pp.fPaintingStrategy; |
129 | 1476 |
1477 int clippingOffset= clippingRegion.getOffset(); | |
1478 int clippingLength= clippingRegion.getLength(); | |
1479 | |
1480 Position p= pp.fPosition; | |
1481 try { | |
1482 | |
1483 int startLine= document.getLineOfOffset(p.getOffset()); | |
1484 int lastInclusive= Math.max(p.getOffset(), p.getOffset() + p.getLength() - 1); | |
1485 int endLine= document.getLineOfOffset(lastInclusive); | |
1486 | |
1487 for (int i= startLine; i <= endLine; i++) { | |
1488 int lineOffset= document.getLineOffset(i); | |
1489 int paintStart= Math.max(lineOffset, p.getOffset()); | |
1490 String lineDelimiter= document.getLineDelimiter(i); | |
1491 int delimiterLength= lineDelimiter !is null ? lineDelimiter.length() : 0; | |
1492 int paintLength= Math.min(lineOffset + document.getLineLength(i) - delimiterLength, p.getOffset() + p.getLength()) - paintStart; | |
1493 if (paintLength >= 0 && overlapsWith(paintStart, paintLength, clippingOffset, clippingLength)) { | |
1494 // otherwise inside a line delimiter | |
1495 IRegion widgetRange= getWidgetRange(paintStart, paintLength); | |
1496 if (widgetRange !is null) { | |
1497 drawingStrategy.draw(annotation, gc, fTextWidget, widgetRange.getOffset(), widgetRange.getLength(), pp.fColor); | |
1498 } | |
1499 } | |
1500 } | |
1501 | |
1502 } catch (BadLocationException x) { | |
1503 } | |
1504 } | |
145 | 1505 |
129 | 1506 /** |
1507 * Computes the model (document) region that is covered by the paint event's clipping region. If | |
1508 * <code>event</code> is <code>null</code>, the model range covered by the visible editor | |
1509 * area (viewport) is returned. | |
145 | 1510 * |
129 | 1511 * @param event the paint event or <code>null</code> to use the entire viewport |
1512 * @param isClearing tells whether the clipping is need for clearing an annotation | |
1513 * @return the model region comprised by either the paint event's clipping region or the | |
1514 * viewport | |
1515 * @since 3.2 | |
1516 */ | |
1517 private IRegion computeClippingRegion(PaintEvent event, bool isClearing) { | |
1518 if (event is null) { | |
145 | 1519 |
129 | 1520 if (!isClearing && fCurrentDrawRange !is null) |
1521 return new Region(fCurrentDrawRange.offset, fCurrentDrawRange.length); | |
145 | 1522 |
129 | 1523 // trigger a repaint of the entire viewport |
1524 int vOffset= getInclusiveTopIndexStartOffset(); | |
1525 if (vOffset is -1) | |
1526 return null; | |
145 | 1527 |
129 | 1528 // http://bugs.eclipse.org/bugs/show_bug.cgi?id=17147 |
1529 int vLength= getExclusiveBottomIndexEndOffset() - vOffset; | |
145 | 1530 |
129 | 1531 return new Region(vOffset, vLength); |
1532 } | |
145 | 1533 |
129 | 1534 int widgetOffset; |
1535 try { | |
1536 int widgetClippingStartOffset= fTextWidget.getOffsetAtLocation(new Point(0, event.y)); | |
1537 int firstWidgetLine= fTextWidget.getLineAtOffset(widgetClippingStartOffset); | |
1538 widgetOffset= fTextWidget.getOffsetAtLine(firstWidgetLine); | |
1539 } catch (IllegalArgumentException ex1) { | |
1540 try { | |
1541 int firstVisibleLine= JFaceTextUtil.getPartialTopIndex(fTextWidget); | |
1542 widgetOffset= fTextWidget.getOffsetAtLine(firstVisibleLine); | |
1543 } catch (IllegalArgumentException ex2) { // above try code might fail too | |
1544 widgetOffset= 0; | |
1545 } | |
1546 } | |
145 | 1547 |
129 | 1548 int widgetEndOffset; |
1549 try { | |
1550 int widgetClippingEndOffset= fTextWidget.getOffsetAtLocation(new Point(0, event.y + event.height)); | |
1551 int lastWidgetLine= fTextWidget.getLineAtOffset(widgetClippingEndOffset); | |
1552 widgetEndOffset= fTextWidget.getOffsetAtLine(lastWidgetLine + 1); | |
1553 } catch (IllegalArgumentException ex1) { | |
1554 // happens if the editor is not "full", e.g. the last line of the document is visible in the editor | |
1555 try { | |
1556 int lastVisibleLine= JFaceTextUtil.getPartialBottomIndex(fTextWidget); | |
1557 if (lastVisibleLine is fTextWidget.getLineCount() - 1) | |
1558 // last line | |
1559 widgetEndOffset= fTextWidget.getCharCount(); | |
1560 else | |
1561 widgetEndOffset= fTextWidget.getOffsetAtLine(lastVisibleLine + 1) - 1; | |
1562 } catch (IllegalArgumentException ex2) { // above try code might fail too | |
1563 widgetEndOffset= fTextWidget.getCharCount(); | |
1564 } | |
1565 } | |
145 | 1566 |
129 | 1567 IRegion clippingRegion= getModelRange(widgetOffset, widgetEndOffset - widgetOffset); |
145 | 1568 |
129 | 1569 return clippingRegion; |
1570 } | |
1571 | |
1572 /** | |
1573 * Should the given annotation be skipped when handling draw requests? | |
1574 * | |
1575 * @param annotation the annotation | |
1576 * @return <code>true</code> iff the given annotation should be | |
1577 * skipped when handling draw requests | |
1578 * @since 3.0 | |
1579 */ | |
1580 protected bool skip(Annotation annotation) { | |
1581 return false; | |
1582 } | |
1583 | |
1584 /** | |
1585 * Returns the widget region that corresponds to the | |
1586 * given offset and length in the viewer's document. | |
145 | 1587 * |
129 | 1588 * @param modelOffset the model offset |
1589 * @param modelLength the model length | |
1590 * @return the corresponding widget region | |
1591 */ | |
1592 private IRegion getWidgetRange(int modelOffset, int modelLength) { | |
1593 fReusableRegion.setOffset(modelOffset); | |
1594 fReusableRegion.setLength(modelLength); | |
1595 | |
1596 if (fReusableRegion is null || fReusableRegion.getOffset() is Integer.MAX_VALUE) | |
1597 return null; | |
1598 | |
138 | 1599 if ( cast(ITextViewerExtension5)fSourceViewer ) { |
134 | 1600 ITextViewerExtension5 extension= cast(ITextViewerExtension5) fSourceViewer; |
129 | 1601 return extension.modelRange2WidgetRange(fReusableRegion); |
1602 } | |
1603 | |
1604 IRegion region= fSourceViewer.getVisibleRegion(); | |
1605 int offset= region.getOffset(); | |
1606 int length= region.getLength(); | |
1607 | |
1608 if (overlapsWith(fReusableRegion, region)) { | |
1609 int p1= Math.max(offset, fReusableRegion.getOffset()); | |
1610 int p2= Math.min(offset + length, fReusableRegion.getOffset() + fReusableRegion.getLength()); | |
1611 return new Region(p1 - offset, p2 - p1); | |
1612 } | |
1613 return null; | |
1614 } | |
1615 | |
1616 /** | |
1617 * Returns the model region that corresponds to the given region in the | |
1618 * viewer's text widget. | |
1619 * | |
1620 * @param offset the offset in the viewer's widget | |
1621 * @param length the length in the viewer's widget | |
1622 * @return the corresponding document region | |
1623 * @since 3.2 | |
1624 */ | |
1625 private IRegion getModelRange(int offset, int length) { | |
1626 if (offset is Integer.MAX_VALUE) | |
1627 return null; | |
1628 | |
138 | 1629 if ( cast(ITextViewerExtension5)fSourceViewer ) { |
134 | 1630 ITextViewerExtension5 extension= cast(ITextViewerExtension5) fSourceViewer; |
129 | 1631 return extension.widgetRange2ModelRange(new Region(offset, length)); |
1632 } | |
145 | 1633 |
129 | 1634 IRegion region= fSourceViewer.getVisibleRegion(); |
1635 return new Region(region.getOffset() + offset, length); | |
1636 } | |
145 | 1637 |
129 | 1638 /** |
1639 * Checks whether the intersection of the given text ranges | |
1640 * is empty or not. | |
1641 * | |
1642 * @param range1 the first range to check | |
1643 * @param range2 the second range to check | |
1644 * @return <code>true</code> if intersection is not empty | |
1645 */ | |
1646 private bool overlapsWith(IRegion range1, IRegion range2) { | |
1647 return overlapsWith(range1.getOffset(), range1.getLength(), range2.getOffset(), range2.getLength()); | |
1648 } | |
1649 | |
1650 /** | |
1651 * Checks whether the intersection of the given text ranges | |
1652 * is empty or not. | |
1653 * | |
1654 * @param offset1 offset of the first range | |
1655 * @param length1 length of the first range | |
1656 * @param offset2 offset of the second range | |
1657 * @param length2 length of the second range | |
1658 * @return <code>true</code> if intersection is not empty | |
1659 */ | |
1660 private bool overlapsWith(int offset1, int length1, int offset2, int length2) { | |
1661 int end= offset2 + length2; | |
1662 int thisEnd= offset1 + length1; | |
1663 | |
1664 if (length2 > 0) { | |
1665 if (length1 > 0) | |
1666 return offset1 < end && offset2 < thisEnd; | |
1667 return offset2 <= offset1 && offset1 < end; | |
1668 } | |
1669 | |
1670 if (length1 > 0) | |
1671 return offset1 <= offset2 && offset2 < thisEnd; | |
1672 return offset1 is offset2; | |
1673 } | |
1674 | |
1675 /* | |
1676 * @see dwtx.jface.text.IPainter#deactivate(bool) | |
1677 */ | |
1678 public void deactivate(bool redraw) { | |
1679 if (fIsActive) { | |
1680 fIsActive= false; | |
1681 disablePainting(redraw); | |
1682 setModel(null); | |
1683 catchupWithModel(null); | |
1684 } | |
1685 } | |
1686 | |
1687 /** | |
1688 * Returns whether the given reason causes a repaint. | |
1689 * | |
1690 * @param reason the reason | |
1691 * @return <code>true</code> if repaint reason, <code>false</code> otherwise | |
1692 * @since 3.0 | |
1693 */ | |
1694 protected bool isRepaintReason(int reason) { | |
1695 return CONFIGURATION is reason || INTERNAL is reason; | |
1696 } | |
1697 | |
1698 /** | |
1699 * Retrieves the annotation model from the given source viewer. | |
1700 * | |
1701 * @param sourceViewer the source viewer | |
1702 * @return the source viewer's annotation model or <code>null</code> if none can be found | |
1703 * @since 3.0 | |
1704 */ | |
1705 protected IAnnotationModel findAnnotationModel(ISourceViewer sourceViewer) { | |
1706 if(sourceViewer !is null) | |
1707 return sourceViewer.getAnnotationModel(); | |
1708 return null; | |
1709 } | |
1710 | |
1711 /* | |
1712 * @see dwtx.jface.text.IPainter#paint(int) | |
1713 */ | |
1714 public void paint(int reason) { | |
1715 if (fSourceViewer.getDocument() is null) { | |
1716 deactivate(false); | |
1717 return; | |
1718 } | |
1719 | |
1720 if (!fIsActive) { | |
1721 IAnnotationModel model= findAnnotationModel(fSourceViewer); | |
1722 if (model !is null) { | |
1723 fIsActive= true; | |
1724 setModel(model); | |
1725 } | |
1726 } else if (isRepaintReason(reason)) | |
1727 updatePainting(null); | |
1728 } | |
1729 | |
1730 /* | |
1731 * @see dwtx.jface.text.IPainter#setPositionManager(dwtx.jface.text.IPaintPositionManager) | |
1732 */ | |
1733 public void setPositionManager(IPaintPositionManager manager) { | |
1734 } | |
1735 } |