Mercurial > projects > dwt-addons
annotate dwtx/jface/text/source/AnnotationRulerColumn.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | 7926b636c282 |
children |
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 * Nikolay Botev <bono8106@hotmail.com> - [projection] Editor loses keyboard focus when expanding folded region - https://bugs.eclipse.org/bugs/show_bug.cgi?id=184255 | |
11 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.jface.text.source.AnnotationRulerColumn; | |
15 | |
131 | 16 import dwtx.jface.text.source.ISharedTextColors; // packageimport |
17 import dwtx.jface.text.source.ILineRange; // packageimport | |
18 import dwtx.jface.text.source.IAnnotationPresentation; // packageimport | |
19 import dwtx.jface.text.source.IVerticalRulerInfoExtension; // packageimport | |
20 import dwtx.jface.text.source.ICharacterPairMatcher; // packageimport | |
21 import dwtx.jface.text.source.TextInvocationContext; // packageimport | |
22 import dwtx.jface.text.source.LineChangeHover; // packageimport | |
23 import dwtx.jface.text.source.IChangeRulerColumn; // packageimport | |
24 import dwtx.jface.text.source.IAnnotationMap; // packageimport | |
25 import dwtx.jface.text.source.IAnnotationModelListenerExtension; // packageimport | |
26 import dwtx.jface.text.source.ISourceViewerExtension2; // packageimport | |
27 import dwtx.jface.text.source.IAnnotationHover; // packageimport | |
28 import dwtx.jface.text.source.ContentAssistantFacade; // packageimport | |
29 import dwtx.jface.text.source.IAnnotationAccess; // packageimport | |
30 import dwtx.jface.text.source.IVerticalRulerExtension; // packageimport | |
31 import dwtx.jface.text.source.IVerticalRulerColumn; // packageimport | |
32 import dwtx.jface.text.source.LineNumberRulerColumn; // packageimport | |
33 import dwtx.jface.text.source.MatchingCharacterPainter; // packageimport | |
34 import dwtx.jface.text.source.IAnnotationModelExtension; // packageimport | |
35 import dwtx.jface.text.source.ILineDifferExtension; // packageimport | |
36 import dwtx.jface.text.source.DefaultCharacterPairMatcher; // packageimport | |
37 import dwtx.jface.text.source.LineNumberChangeRulerColumn; // packageimport | |
38 import dwtx.jface.text.source.IAnnotationAccessExtension; // packageimport | |
39 import dwtx.jface.text.source.ISourceViewer; // packageimport | |
40 import dwtx.jface.text.source.AnnotationModel; // packageimport | |
41 import dwtx.jface.text.source.ILineDifferExtension2; // packageimport | |
42 import dwtx.jface.text.source.IAnnotationModelListener; // packageimport | |
43 import dwtx.jface.text.source.IVerticalRuler; // packageimport | |
44 import dwtx.jface.text.source.DefaultAnnotationHover; // packageimport | |
45 import dwtx.jface.text.source.SourceViewer; // packageimport | |
46 import dwtx.jface.text.source.SourceViewerConfiguration; // packageimport | |
47 import dwtx.jface.text.source.AnnotationBarHoverManager; // packageimport | |
48 import dwtx.jface.text.source.CompositeRuler; // packageimport | |
49 import dwtx.jface.text.source.ImageUtilities; // packageimport | |
50 import dwtx.jface.text.source.VisualAnnotationModel; // packageimport | |
51 import dwtx.jface.text.source.IAnnotationModel; // packageimport | |
52 import dwtx.jface.text.source.ISourceViewerExtension3; // packageimport | |
53 import dwtx.jface.text.source.ILineDiffInfo; // packageimport | |
54 import dwtx.jface.text.source.VerticalRulerEvent; // packageimport | |
55 import dwtx.jface.text.source.ChangeRulerColumn; // packageimport | |
56 import dwtx.jface.text.source.ILineDiffer; // packageimport | |
57 import dwtx.jface.text.source.AnnotationModelEvent; // packageimport | |
58 import dwtx.jface.text.source.AnnotationColumn; // 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.AnnotationPainter; // packageimport | |
74 import dwtx.jface.text.source.IAnnotationHoverExtension2; // packageimport | |
75 import dwtx.jface.text.source.OverviewRuler; // packageimport | |
76 import dwtx.jface.text.source.OverviewRulerHoverManager; // packageimport | |
77 | |
78 | |
129 | 79 import dwt.dwthelper.utils; |
80 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
81 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
82 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
83 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
84 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
85 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
86 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
87 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
88 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
89 |
129 | 90 |
91 import dwt.DWT; | |
92 import dwt.custom.StyledText; | |
93 import dwt.events.DisposeEvent; | |
94 import dwt.events.DisposeListener; | |
95 import dwt.events.MouseEvent; | |
96 import dwt.events.MouseListener; | |
97 import dwt.events.MouseMoveListener; | |
98 import dwt.events.PaintEvent; | |
99 import dwt.events.PaintListener; | |
100 import dwt.graphics.Cursor; | |
101 import dwt.graphics.Font; | |
102 import dwt.graphics.GC; | |
103 import dwt.graphics.Image; | |
104 import dwt.graphics.Point; | |
105 import dwt.graphics.Rectangle; | |
106 import dwt.widgets.Canvas; | |
107 import dwt.widgets.Composite; | |
108 import dwt.widgets.Control; | |
109 import dwt.widgets.Display; | |
110 import dwtx.jface.text.BadLocationException; | |
111 import dwtx.jface.text.IDocument; | |
112 import dwtx.jface.text.IRegion; | |
113 import dwtx.jface.text.ITextListener; | |
114 import dwtx.jface.text.ITextViewer; | |
115 import dwtx.jface.text.ITextViewerExtension5; | |
116 import dwtx.jface.text.IViewportListener; | |
117 import dwtx.jface.text.JFaceTextUtil; | |
118 import dwtx.jface.text.Position; | |
119 import dwtx.jface.text.TextEvent; | |
120 | |
121 | |
122 /** | |
123 * A vertical ruler column showing graphical representations of annotations. | |
124 * Will become final. | |
125 * <p> | |
126 * Do not subclass. | |
127 * </p> | |
128 * | |
129 * @since 2.0 | |
130 */ | |
131 public class AnnotationRulerColumn : IVerticalRulerColumn, IVerticalRulerInfo, IVerticalRulerInfoExtension { | |
132 | |
133 /** | |
134 * Internal listener class. | |
135 */ | |
136 class InternalListener : IViewportListener, IAnnotationModelListener, ITextListener { | |
137 | |
138 /* | |
139 * @see IViewportListener#viewportChanged(int) | |
140 */ | |
141 public void viewportChanged(int verticalPosition) { | |
142 if (verticalPosition !is fScrollPos) | |
143 redraw(); | |
144 } | |
145 | |
146 /* | |
147 * @see IAnnotationModelListener#modelChanged(IAnnotationModel) | |
148 */ | |
149 public void modelChanged(IAnnotationModel model) { | |
150 postRedraw(); | |
151 } | |
152 | |
153 /* | |
154 * @see ITextListener#textChanged(TextEvent) | |
155 */ | |
156 public void textChanged(TextEvent e) { | |
157 if (e.getViewerRedrawState()) | |
158 postRedraw(); | |
159 } | |
160 } | |
161 | |
162 /** | |
163 * Implementation of <code>IRegion</code> that can be reused | |
164 * by setting the offset and the length. | |
165 */ | |
162 | 166 private static class ReusableRegion : Position , IRegion { |
167 public override int getLength(){ | |
168 return super.getLength(); | |
169 } | |
170 public override int getOffset(){ | |
171 return super.getOffset(); | |
172 } | |
173 } | |
129 | 174 |
175 /** | |
176 * Pair of an annotation and their associated position. Used inside the paint method | |
177 * for sorting annotations based on the offset of their position. | |
178 * @since 3.0 | |
179 */ | |
180 private static class Tuple { | |
181 Annotation annotation; | |
182 Position position; | |
183 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
184 this(Annotation annotation, Position position) { |
129 | 185 this.annotation= annotation; |
186 this.position= position; | |
187 } | |
188 } | |
189 | |
190 /** | |
191 * Comparator for <code>Tuple</code>s. | |
192 * @since 3.0 | |
193 */ | |
194 private static class TupleComparator : Comparator { | |
195 /* | |
196 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) | |
197 */ | |
198 public int compare(Object o1, Object o2) { | |
134 | 199 Position p1= (cast(Tuple) o1).position; |
200 Position p2= (cast(Tuple) o2).position; | |
129 | 201 return p1.getOffset() - p2.getOffset(); |
202 } | |
203 } | |
204 | |
205 /** This column's parent ruler */ | |
206 private CompositeRuler fParentRuler; | |
207 /** The cached text viewer */ | |
208 private ITextViewer fCachedTextViewer; | |
209 /** The cached text widget */ | |
210 private StyledText fCachedTextWidget; | |
211 /** The ruler's canvas */ | |
212 private Canvas fCanvas; | |
213 /** The vertical ruler's model */ | |
214 private IAnnotationModel fModel; | |
215 /** Cache for the actual scroll position in pixels */ | |
216 private int fScrollPos; | |
217 /** The buffer for double buffering */ | |
218 private Image fBuffer; | |
219 /** The internal listener */ | |
159 | 220 private InternalListener fInternalListener; |
129 | 221 /** The width of this vertical ruler */ |
222 private int fWidth; | |
223 /** Switch for enabling/disabling the setModel method. */ | |
224 private bool fAllowSetModel= true; | |
225 /** | |
226 * The list of annotation types to be shown in this ruler. | |
227 * @since 3.0 | |
228 */ | |
159 | 229 private Set fConfiguredAnnotationTypes; |
129 | 230 /** |
231 * The list of allowed annotation types to be shown in this ruler. | |
232 * An allowed annotation type maps to <code>true</code>, a disallowed | |
233 * to <code>false</code>. | |
234 * @since 3.0 | |
235 */ | |
159 | 236 private Map fAllowedAnnotationTypes; |
129 | 237 /** |
238 * The annotation access extension. | |
239 * @since 3.0 | |
240 */ | |
241 private IAnnotationAccessExtension fAnnotationAccessExtension; | |
242 /** | |
243 * The hover for this column. | |
244 * @since 3.0 | |
245 */ | |
246 private IAnnotationHover fHover; | |
247 /** | |
248 * The cached annotations. | |
249 * @since 3.0 | |
250 */ | |
159 | 251 private List fCachedAnnotations; |
129 | 252 /** |
253 * The comparator for sorting annotations according to the offset of their position. | |
254 * @since 3.0 | |
255 */ | |
159 | 256 private Comparator fTupleComparator; |
129 | 257 /** |
258 * The hit detection cursor. | |
259 * @since 3.0 | |
260 */ | |
261 private Cursor fHitDetectionCursor; | |
262 /** | |
263 * The last cursor. | |
264 * @since 3.0 | |
265 */ | |
266 private Cursor fLastCursor; | |
267 /** | |
268 * This ruler's mouse listener. | |
269 * @since 3.0 | |
270 */ | |
271 private MouseListener fMouseListener; | |
272 | |
159 | 273 private void instanceInit(){ |
274 fInternalListener= new InternalListener(); | |
275 fConfiguredAnnotationTypes= new HashSet(); | |
276 fAllowedAnnotationTypes= new HashMap(); | |
277 fCachedAnnotations= new ArrayList(); | |
278 fTupleComparator= new TupleComparator(); | |
279 } | |
129 | 280 /** |
281 * Constructs this column with the given arguments. | |
282 * | |
283 * @param model the annotation model to get the annotations from | |
284 * @param width the width of the vertical ruler | |
285 * @param annotationAccess the annotation access | |
286 * @since 3.0 | |
287 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
288 public this(IAnnotationModel model, int width, IAnnotationAccess annotationAccess) { |
129 | 289 this(width, annotationAccess); |
290 fAllowSetModel= false; | |
291 fModel= model; | |
292 fModel.addAnnotationModelListener(fInternalListener); | |
293 } | |
294 | |
295 /** | |
296 * Constructs this column with the given arguments. | |
297 * | |
298 * @param width the width of the vertical ruler | |
299 * @param annotationAccess the annotation access | |
300 * @since 3.0 | |
301 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
302 public this(int width, IAnnotationAccess annotationAccess) { |
159 | 303 instanceInit(); |
129 | 304 fWidth= width; |
138 | 305 if ( cast(IAnnotationAccessExtension)annotationAccess ) |
134 | 306 fAnnotationAccessExtension= cast(IAnnotationAccessExtension) annotationAccess; |
129 | 307 } |
308 | |
309 /** | |
310 * Constructs this column with the given arguments. | |
311 * | |
312 * @param model the annotation model to get the annotations from | |
313 * @param width the width of the vertical ruler | |
314 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
315 public this(IAnnotationModel model, int width) { |
159 | 316 instanceInit(); |
129 | 317 fWidth= width; |
318 fAllowSetModel= false; | |
319 fModel= model; | |
320 fModel.addAnnotationModelListener(fInternalListener); | |
321 } | |
322 | |
323 /** | |
324 * Constructs this column with the given width. | |
325 * | |
326 * @param width the width of the vertical ruler | |
327 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
328 public this(int width) { |
159 | 329 instanceInit(); |
129 | 330 fWidth= width; |
331 } | |
332 | |
333 /* | |
334 * @see IVerticalRulerColumn#getControl() | |
335 */ | |
336 public Control getControl() { | |
337 return fCanvas; | |
338 } | |
339 | |
340 /* | |
341 * @see IVerticalRulerColumn#getWidth() | |
342 */ | |
343 public int getWidth() { | |
344 return fWidth; | |
345 } | |
346 | |
347 /* | |
348 * @see IVerticalRulerColumn#createControl(CompositeRuler, Composite) | |
349 */ | |
350 public Control createControl(CompositeRuler parentRuler, Composite parentControl) { | |
351 | |
352 fParentRuler= parentRuler; | |
353 fCachedTextViewer= parentRuler.getTextViewer(); | |
354 fCachedTextWidget= fCachedTextViewer.getTextWidget(); | |
355 | |
356 fHitDetectionCursor= new Cursor(parentControl.getDisplay(), DWT.CURSOR_HAND); | |
357 | |
358 fCanvas= createCanvas(parentControl); | |
359 | |
135 | 360 fCanvas.addPaintListener(new class() PaintListener { |
129 | 361 public void paintControl(PaintEvent event) { |
362 if (fCachedTextViewer !is null) | |
363 doubleBufferPaint(event.gc); | |
364 } | |
365 }); | |
366 | |
135 | 367 fCanvas.addDisposeListener(new class() DisposeListener { |
129 | 368 public void widgetDisposed(DisposeEvent e) { |
369 handleDispose(); | |
370 fCachedTextViewer= null; | |
371 fCachedTextWidget= null; | |
372 } | |
373 }); | |
374 | |
135 | 375 fMouseListener= new class() MouseListener { |
129 | 376 public void mouseUp(MouseEvent event) { |
377 int lineNumber; | |
378 if (isPropagatingMouseListener()) { | |
379 fParentRuler.setLocationOfLastMouseButtonActivity(event.x, event.y); | |
380 lineNumber= fParentRuler.getLineOfLastMouseButtonActivity(); | |
381 } else | |
382 lineNumber= fParentRuler.toDocumentLineNumber(event.y); | |
383 | |
384 if (1 is event.button) | |
385 mouseClicked(lineNumber); | |
386 } | |
387 | |
388 public void mouseDown(MouseEvent event) { | |
389 if (isPropagatingMouseListener()) | |
390 fParentRuler.setLocationOfLastMouseButtonActivity(event.x, event.y); | |
391 } | |
392 | |
393 public void mouseDoubleClick(MouseEvent event) { | |
394 int lineNumber; | |
395 if (isPropagatingMouseListener()) { | |
396 fParentRuler.setLocationOfLastMouseButtonActivity(event.x, event.y); | |
397 lineNumber= fParentRuler.getLineOfLastMouseButtonActivity(); | |
398 } else | |
399 lineNumber= fParentRuler.toDocumentLineNumber(event.y); | |
400 | |
401 if (1 is event.button) | |
402 mouseDoubleClicked(lineNumber); | |
403 } | |
404 }; | |
405 fCanvas.addMouseListener(fMouseListener); | |
406 | |
135 | 407 fCanvas.addMouseMoveListener(new class() MouseMoveListener { |
129 | 408 /* |
409 * @see dwt.events.MouseMoveListener#mouseMove(dwt.events.MouseEvent) | |
410 * @since 3.0 | |
411 */ | |
412 public void mouseMove(MouseEvent e) { | |
413 handleMouseMove(e); | |
414 } | |
415 }); | |
416 | |
417 if (fCachedTextViewer !is null) { | |
418 fCachedTextViewer.addViewportListener(fInternalListener); | |
419 fCachedTextViewer.addTextListener(fInternalListener); | |
420 } | |
421 | |
422 return fCanvas; | |
423 } | |
424 | |
425 /** | |
426 * Creates a canvas with the given parent. | |
427 * | |
428 * @param parent the parent | |
429 * @return the created canvas | |
430 */ | |
431 private Canvas createCanvas(Composite parent) { | |
135 | 432 return new class(parent, DWT.NO_BACKGROUND | DWT.NO_FOCUS) Canvas { |
162 | 433 this( Composite p, int s ){ |
434 super(p,s); | |
435 } | |
129 | 436 /* |
437 * @see dwt.widgets.Control#addMouseListener(dwt.events.MouseListener) | |
438 * @since 3.0 | |
439 */ | |
440 public void addMouseListener(MouseListener listener) { | |
441 if (isPropagatingMouseListener() || listener is fMouseListener) | |
442 super.addMouseListener(listener); | |
443 } | |
444 }; | |
445 } | |
446 | |
447 /** | |
448 * Tells whether this ruler column propagates mouse listener | |
449 * events to its parent. | |
450 * | |
451 * @return <code>true</code> if propagating to parent | |
452 * @since 3.0 | |
453 */ | |
454 protected bool isPropagatingMouseListener() { | |
455 return true; | |
456 } | |
457 | |
458 /** | |
459 * Hook method for a mouse double click event on the given ruler line. | |
460 * | |
461 * @param rulerLine the ruler line | |
462 */ | |
463 protected void mouseDoubleClicked(int rulerLine) { | |
464 } | |
465 | |
466 /** | |
467 * Hook method for a mouse click event on the given ruler line. | |
468 * | |
469 * @param rulerLine the ruler line | |
470 * @since 3.0 | |
471 */ | |
472 protected void mouseClicked(int rulerLine) { | |
473 } | |
474 | |
475 /** | |
476 * Handles mouse moves. | |
477 * | |
478 * @param event the mouse move event | |
479 */ | |
480 private void handleMouseMove(MouseEvent event) { | |
481 fParentRuler.setLocationOfLastMouseButtonActivity(event.x, event.y); | |
482 if (fCachedTextViewer !is null) { | |
483 int line= toDocumentLineNumber(event.y); | |
484 Cursor cursor= (hasAnnotation(line) ? fHitDetectionCursor : null); | |
485 if (cursor !is fLastCursor) { | |
486 fCanvas.setCursor(cursor); | |
487 fLastCursor= cursor; | |
488 } | |
489 } | |
490 } | |
491 | |
492 /** | |
493 * Tells whether the given line contains an annotation. | |
494 * | |
495 * @param lineNumber the line number | |
496 * @return <code>true</code> if the given line contains an annotation | |
497 */ | |
498 protected bool hasAnnotation(int lineNumber) { | |
499 | |
500 IAnnotationModel model= fModel; | |
138 | 501 if ( cast(IAnnotationModelExtension)fModel ) |
134 | 502 model= (cast(IAnnotationModelExtension)fModel).getAnnotationModel(SourceViewer.MODEL_ANNOTATION_MODEL); |
129 | 503 |
504 if (model is null) | |
505 return false; | |
506 | |
507 IRegion line; | |
508 try { | |
509 IDocument d= fCachedTextViewer.getDocument(); | |
510 if (d is null) | |
511 return false; | |
159 | 512 |
129 | 513 line= d.getLineInformation(lineNumber); |
514 } catch (BadLocationException ex) { | |
515 return false; | |
516 } | |
517 | |
518 int lineStart= line.getOffset(); | |
519 int lineLength= line.getLength(); | |
520 | |
521 Iterator e; | |
138 | 522 if ( cast(IAnnotationModelExtension2)fModel ) |
134 | 523 e= (cast(IAnnotationModelExtension2)fModel).getAnnotationIterator(lineStart, lineLength + 1, true, true); |
129 | 524 else |
525 e= model.getAnnotationIterator(); | |
526 | |
527 while (e.hasNext()) { | |
134 | 528 Annotation a= cast(Annotation) e.next(); |
129 | 529 |
530 if (a.isMarkedDeleted()) | |
531 continue; | |
532 | |
533 if (skip(a)) | |
534 continue; | |
535 | |
536 Position p= model.getPosition(a); | |
537 if (p is null || p.isDeleted()) | |
538 continue; | |
539 | |
540 if (p.overlapsWith(lineStart, lineLength) || p.length is 0 && p.offset is lineStart + lineLength) | |
541 return true; | |
542 } | |
543 | |
544 return false; | |
545 } | |
546 | |
547 /** | |
548 * Disposes the ruler's resources. | |
549 */ | |
550 private void handleDispose() { | |
551 | |
552 if (fCachedTextViewer !is null) { | |
553 fCachedTextViewer.removeViewportListener(fInternalListener); | |
554 fCachedTextViewer.removeTextListener(fInternalListener); | |
555 } | |
556 | |
557 if (fModel !is null) | |
558 fModel.removeAnnotationModelListener(fInternalListener); | |
559 | |
560 if (fBuffer !is null) { | |
561 fBuffer.dispose(); | |
562 fBuffer= null; | |
563 } | |
564 | |
565 if (fHitDetectionCursor !is null) { | |
566 fHitDetectionCursor.dispose(); | |
567 fHitDetectionCursor= null; | |
568 } | |
569 | |
570 fConfiguredAnnotationTypes.clear(); | |
571 fAllowedAnnotationTypes.clear(); | |
572 fAnnotationAccessExtension= null; | |
573 } | |
574 | |
575 /** | |
576 * Double buffer drawing. | |
577 * | |
578 * @param dest the GC to draw into | |
579 */ | |
580 private void doubleBufferPaint(GC dest) { | |
581 | |
582 Point size= fCanvas.getSize(); | |
583 | |
584 if (size.x <= 0 || size.y <= 0) | |
585 return; | |
586 | |
587 if (fBuffer !is null) { | |
588 Rectangle r= fBuffer.getBounds(); | |
589 if (r.width !is size.x || r.height !is size.y) { | |
590 fBuffer.dispose(); | |
591 fBuffer= null; | |
592 } | |
593 } | |
594 if (fBuffer is null) | |
595 fBuffer= new Image(fCanvas.getDisplay(), size.x, size.y); | |
596 | |
597 GC gc= new GC(fBuffer); | |
598 gc.setFont(fCachedTextWidget.getFont()); | |
599 try { | |
600 gc.setBackground(fCanvas.getBackground()); | |
601 gc.fillRectangle(0, 0, size.x, size.y); | |
602 | |
138 | 603 if ( cast(ITextViewerExtension5)fCachedTextViewer ) |
129 | 604 doPaint1(gc); |
605 else | |
606 doPaint(gc); | |
607 } finally { | |
608 gc.dispose(); | |
609 } | |
610 | |
611 dest.drawImage(fBuffer, 0, 0); | |
612 } | |
613 | |
614 /** | |
615 * Returns the document offset of the upper left corner of the source viewer's | |
616 * view port, possibly including partially visible lines. | |
617 * | |
618 * @return document offset of the upper left corner including partially visible lines | |
619 */ | |
620 protected int getInclusiveTopIndexStartOffset() { | |
621 if (fCachedTextWidget is null || fCachedTextWidget.isDisposed()) | |
622 return -1; | |
159 | 623 |
129 | 624 IDocument document= fCachedTextViewer.getDocument(); |
625 if (document is null) | |
626 return -1; | |
159 | 627 |
129 | 628 int top= JFaceTextUtil.getPartialTopIndex(fCachedTextViewer); |
629 try { | |
630 return document.getLineOffset(top); | |
631 } catch (BadLocationException x) { | |
632 return -1; | |
633 } | |
634 } | |
635 | |
636 /** | |
637 * Returns the first invisible document offset of the lower right corner of the source viewer's view port, | |
638 * possibly including partially visible lines. | |
639 * | |
640 * @return the first invisible document offset of the lower right corner of the view port | |
641 */ | |
642 private int getExclusiveBottomIndexEndOffset() { | |
643 if (fCachedTextWidget is null || fCachedTextWidget.isDisposed()) | |
644 return -1; | |
159 | 645 |
129 | 646 IDocument document= fCachedTextViewer.getDocument(); |
647 if (document is null) | |
648 return -1; | |
159 | 649 |
129 | 650 int bottom= JFaceTextUtil.getPartialBottomIndex(fCachedTextViewer); |
651 try { | |
652 if (bottom >= document.getNumberOfLines()) | |
653 bottom= document.getNumberOfLines() - 1; | |
654 return document.getLineOffset(bottom) + document.getLineLength(bottom); | |
655 } catch (BadLocationException x) { | |
656 return -1; | |
657 } | |
658 } | |
659 | |
660 /** | |
661 * Draws the vertical ruler w/o drawing the Canvas background. | |
662 * | |
663 * @param gc the GC to draw into | |
664 */ | |
665 protected void doPaint(GC gc) { | |
666 | |
667 if (fModel is null || fCachedTextViewer is null) | |
668 return; | |
669 | |
670 int topLeft= getInclusiveTopIndexStartOffset(); | |
671 // http://dev.eclipse.org/bugs/show_bug.cgi?id=14938 | |
672 // http://dev.eclipse.org/bugs/show_bug.cgi?id=22487 | |
673 // we want the exclusive offset (right after the last character) | |
674 int bottomRight= getExclusiveBottomIndexEndOffset(); | |
675 int viewPort= bottomRight - topLeft; | |
676 | |
677 fScrollPos= fCachedTextWidget.getTopPixel(); | |
678 Point dimension= fCanvas.getSize(); | |
679 | |
680 IDocument doc= fCachedTextViewer.getDocument(); | |
681 if (doc is null) | |
682 return; | |
683 | |
684 int topLine= -1, bottomLine= -1; | |
685 try { | |
686 IRegion region= fCachedTextViewer.getVisibleRegion(); | |
687 topLine= doc.getLineOfOffset(region.getOffset()); | |
688 bottomLine= doc.getLineOfOffset(region.getOffset() + region.getLength()); | |
689 } catch (BadLocationException x) { | |
690 return; | |
691 } | |
692 | |
693 // draw Annotations | |
694 Rectangle r= new Rectangle(0, 0, 0, 0); | |
695 int maxLayer= 1; // loop at least once through layers. | |
696 | |
697 for (int layer= 0; layer < maxLayer; layer++) { | |
698 Iterator iter; | |
138 | 699 if ( cast(IAnnotationModelExtension2)fModel ) |
134 | 700 iter= (cast(IAnnotationModelExtension2)fModel).getAnnotationIterator(topLeft, viewPort + 1, true, true); |
129 | 701 else |
702 iter= fModel.getAnnotationIterator(); | |
703 | |
704 while (iter.hasNext()) { | |
134 | 705 Annotation annotation= cast(Annotation) iter.next(); |
129 | 706 |
707 int lay= IAnnotationAccessExtension.DEFAULT_LAYER; | |
708 if (fAnnotationAccessExtension !is null) | |
709 lay= fAnnotationAccessExtension.getLayer(annotation); | |
710 maxLayer= Math.max(maxLayer, lay+1); // dynamically update layer maximum | |
711 if (lay !is layer) // wrong layer: skip annotation | |
712 continue; | |
713 | |
714 if (skip(annotation)) | |
715 continue; | |
716 | |
717 Position position= fModel.getPosition(annotation); | |
718 if (position is null) | |
719 continue; | |
720 | |
721 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=20284 | |
722 // Position.overlapsWith returns false if the position just starts at the end | |
723 // of the specified range. If the position has zero length, we want to include it anyhow | |
724 int viewPortSize= position.getLength() is 0 ? viewPort + 1 : viewPort; | |
725 if (!position.overlapsWith(topLeft, viewPortSize)) | |
726 continue; | |
727 | |
728 try { | |
729 | |
730 int offset= position.getOffset(); | |
731 int length= position.getLength(); | |
732 | |
733 int startLine= doc.getLineOfOffset(offset); | |
734 if (startLine < topLine) | |
735 startLine= topLine; | |
736 | |
737 int endLine= startLine; | |
738 if (length > 0) | |
739 endLine= doc.getLineOfOffset(offset + length - 1); | |
740 if (endLine > bottomLine) | |
741 endLine= bottomLine; | |
742 | |
743 startLine -= topLine; | |
744 endLine -= topLine; | |
745 | |
746 r.x= 0; | |
747 r.y= JFaceTextUtil.computeLineHeight(fCachedTextWidget, 0, startLine, startLine) - fScrollPos; | |
159 | 748 |
129 | 749 r.width= dimension.x; |
750 int lines= endLine - startLine; | |
159 | 751 |
129 | 752 r.height= JFaceTextUtil.computeLineHeight(fCachedTextWidget, startLine, endLine + 1, lines + 1); |
753 | |
754 if (r.y < dimension.y && fAnnotationAccessExtension !is null) // annotation within visible area | |
755 fAnnotationAccessExtension.paint(annotation, gc, fCanvas, r); | |
756 | |
757 } catch (BadLocationException x) { | |
758 } | |
759 } | |
760 } | |
761 } | |
762 | |
763 /** | |
764 * Draws the vertical ruler w/o drawing the Canvas background. Implementation based | |
765 * on <code>ITextViewerExtension5</code>. Will replace <code>doPaint(GC)</code>. | |
766 * | |
767 * @param gc the GC to draw into | |
768 */ | |
769 protected void doPaint1(GC gc) { | |
770 | |
771 if (fModel is null || fCachedTextViewer is null) | |
772 return; | |
773 | |
134 | 774 ITextViewerExtension5 extension= cast(ITextViewerExtension5) fCachedTextViewer; |
129 | 775 |
776 fScrollPos= fCachedTextWidget.getTopPixel(); | |
777 Point dimension= fCanvas.getSize(); | |
778 | |
779 int vOffset= getInclusiveTopIndexStartOffset(); | |
780 int vLength= getExclusiveBottomIndexEndOffset() - vOffset; | |
781 | |
782 // draw Annotations | |
783 Rectangle r= new Rectangle(0, 0, 0, 0); | |
784 ReusableRegion range= new ReusableRegion(); | |
785 | |
786 int minLayer= Integer.MAX_VALUE, maxLayer= Integer.MIN_VALUE; | |
787 fCachedAnnotations.clear(); | |
788 Iterator iter; | |
138 | 789 if ( cast(IAnnotationModelExtension2)fModel ) |
134 | 790 iter= (cast(IAnnotationModelExtension2)fModel).getAnnotationIterator(vOffset, vLength + 1, true, true); |
129 | 791 else |
792 iter= fModel.getAnnotationIterator(); | |
793 | |
794 while (iter.hasNext()) { | |
134 | 795 Annotation annotation= cast(Annotation) iter.next(); |
129 | 796 |
797 if (skip(annotation)) | |
798 continue; | |
799 | |
800 Position position= fModel.getPosition(annotation); | |
801 if (position is null) | |
802 continue; | |
803 | |
804 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=217710 | |
805 int extendedVLength= position.getLength() is 0 ? vLength + 1 : vLength; | |
806 if (!position.overlapsWith(vOffset, extendedVLength)) | |
807 continue; | |
808 | |
809 int lay= IAnnotationAccessExtension.DEFAULT_LAYER; | |
810 if (fAnnotationAccessExtension !is null) | |
811 lay= fAnnotationAccessExtension.getLayer(annotation); | |
812 | |
813 minLayer= Math.min(minLayer, lay); | |
814 maxLayer= Math.max(maxLayer, lay); | |
815 fCachedAnnotations.add(new Tuple(annotation, position)); | |
816 } | |
817 Collections.sort(fCachedAnnotations, fTupleComparator); | |
818 | |
819 for (int layer= minLayer; layer <= maxLayer; layer++) { | |
820 for (int i= 0, n= fCachedAnnotations.size(); i < n; i++) { | |
134 | 821 Tuple tuple= cast(Tuple) fCachedAnnotations.get(i); |
129 | 822 Annotation annotation= tuple.annotation; |
823 Position position= tuple.position; | |
824 | |
825 int lay= IAnnotationAccessExtension.DEFAULT_LAYER; | |
826 if (fAnnotationAccessExtension !is null) | |
827 lay= fAnnotationAccessExtension.getLayer(annotation); | |
828 if (lay !is layer) // wrong layer: skip annotation | |
829 continue; | |
830 | |
831 range.setOffset(position.getOffset()); | |
832 range.setLength(position.getLength()); | |
833 IRegion widgetRegion= extension.modelRange2WidgetRange(range); | |
834 if (widgetRegion is null) | |
835 continue; | |
836 | |
837 int startLine= extension.widgetLineOfWidgetOffset(widgetRegion.getOffset()); | |
838 if (startLine is -1) | |
839 continue; | |
840 | |
841 int endLine= extension.widgetLineOfWidgetOffset(widgetRegion.getOffset() + Math.max(widgetRegion.getLength() -1, 0)); | |
842 if (endLine is -1) | |
843 continue; | |
844 | |
845 r.x= 0; | |
846 r.y= JFaceTextUtil.computeLineHeight(fCachedTextWidget, 0, startLine, startLine) - fScrollPos; | |
159 | 847 |
129 | 848 r.width= dimension.x; |
849 int lines= endLine - startLine; | |
850 r.height= JFaceTextUtil.computeLineHeight(fCachedTextWidget, startLine, endLine + 1, lines + 1); | |
851 | |
852 if (r.y < dimension.y && fAnnotationAccessExtension !is null) // annotation within visible area | |
853 fAnnotationAccessExtension.paint(annotation, gc, fCanvas, r); | |
854 } | |
855 } | |
856 | |
857 fCachedAnnotations.clear(); | |
858 } | |
859 | |
860 | |
861 /** | |
862 * Post a redraw request for this column into the UI thread. | |
863 */ | |
864 private void postRedraw() { | |
865 if (fCanvas !is null && !fCanvas.isDisposed()) { | |
866 Display d= fCanvas.getDisplay(); | |
867 if (d !is null) { | |
135 | 868 d.asyncExec(new class() Runnable { |
129 | 869 public void run() { |
870 redraw(); | |
871 } | |
872 }); | |
873 } | |
874 } | |
875 } | |
876 | |
877 /* | |
878 * @see IVerticalRulerColumn#redraw() | |
879 */ | |
880 public void redraw() { | |
881 if (fCanvas !is null && !fCanvas.isDisposed()) { | |
882 GC gc= new GC(fCanvas); | |
883 doubleBufferPaint(gc); | |
884 gc.dispose(); | |
885 } | |
886 } | |
887 | |
888 /* | |
889 * @see IVerticalRulerColumn#setModel | |
890 */ | |
891 public void setModel(IAnnotationModel model) { | |
892 if (fAllowSetModel && model !is fModel) { | |
893 | |
894 if (fModel !is null) | |
895 fModel.removeAnnotationModelListener(fInternalListener); | |
896 | |
897 fModel= model; | |
898 | |
899 if (fModel !is null) | |
900 fModel.addAnnotationModelListener(fInternalListener); | |
901 | |
902 postRedraw(); | |
903 } | |
904 } | |
905 | |
906 /* | |
907 * @see IVerticalRulerColumn#setFont(Font) | |
908 */ | |
909 public void setFont(Font font) { | |
910 } | |
911 | |
912 /** | |
913 * Returns the cached text viewer. | |
914 * | |
915 * @return the cached text viewer | |
916 */ | |
917 protected ITextViewer getCachedTextViewer() { | |
918 return fCachedTextViewer; | |
919 } | |
920 | |
921 /* | |
922 * @see dwtx.jface.text.source.IVerticalRulerInfoExtension#getModel() | |
923 */ | |
924 public IAnnotationModel getModel() { | |
925 return fModel; | |
926 } | |
927 | |
928 /** | |
929 * Adds the given annotation type to this annotation ruler column. Starting | |
930 * with this call, annotations of the given type are shown in this annotation | |
931 * ruler column. | |
932 * | |
933 * @param annotationType the annotation type | |
934 * @since 3.0 | |
935 */ | |
936 public void addAnnotationType(Object annotationType) { | |
937 fConfiguredAnnotationTypes.add(annotationType); | |
938 fAllowedAnnotationTypes.clear(); | |
939 } | |
940 | |
941 /* | |
942 * @see dwtx.jface.text.source.IVerticalRulerInfo#getLineOfLastMouseButtonActivity() | |
943 * @since 3.0 | |
944 */ | |
945 public int getLineOfLastMouseButtonActivity() { | |
946 return fParentRuler.getLineOfLastMouseButtonActivity(); | |
947 } | |
948 | |
949 /* | |
950 * @see dwtx.jface.text.source.IVerticalRulerInfo#toDocumentLineNumber(int) | |
951 * @since 3.0 | |
952 */ | |
953 public int toDocumentLineNumber(int y_coordinate) { | |
954 return fParentRuler.toDocumentLineNumber(y_coordinate); | |
955 } | |
159 | 956 |
129 | 957 /** |
958 * Removes the given annotation type from this annotation ruler column. | |
959 * Annotations of the given type are no longer shown in this annotation | |
960 * ruler column. | |
961 * | |
962 * @param annotationType the annotation type | |
963 * @since 3.0 | |
964 */ | |
965 public void removeAnnotationType(Object annotationType) { | |
966 fConfiguredAnnotationTypes.remove(annotationType); | |
967 fAllowedAnnotationTypes.clear(); | |
968 } | |
969 | |
970 /** | |
971 * Returns whether the given annotation should be skipped by the drawing | |
972 * routine. | |
973 * | |
974 * @param annotation the annotation | |
975 * @return <code>true</code> if annotation of the given type should be | |
976 * skipped, <code>false</code> otherwise | |
977 * @since 3.0 | |
978 */ | |
979 private bool skip(Annotation annotation) { | |
162 | 980 Object annotationType= stringcast(annotation.getType()); |
134 | 981 Boolean allowed= cast(Boolean) fAllowedAnnotationTypes.get(annotationType); |
129 | 982 if (allowed !is null) |
983 return !allowed.booleanValue(); | |
984 | |
985 bool skip= skip(annotationType); | |
986 fAllowedAnnotationTypes.put(annotationType, !skip ? Boolean.TRUE : Boolean.FALSE); | |
987 return skip; | |
988 } | |
989 | |
990 /** | |
991 * Computes whether the annotation of the given type should be skipped or | |
992 * not. | |
993 * | |
994 * @param annotationType the annotation type | |
995 * @return <code>true</code> if annotation should be skipped, <code>false</code> | |
996 * otherwise | |
997 * @since 3.0 | |
998 */ | |
999 private bool skip(Object annotationType) { | |
1000 if (fAnnotationAccessExtension !is null) { | |
1001 Iterator e= fConfiguredAnnotationTypes.iterator(); | |
1002 while (e.hasNext()) { | |
1003 if (fAnnotationAccessExtension.isSubtype(annotationType, e.next())) | |
1004 return false; | |
1005 } | |
1006 return true; | |
1007 } | |
1008 return !fConfiguredAnnotationTypes.contains(annotationType); | |
1009 } | |
1010 | |
1011 /* | |
1012 * @see dwtx.jface.text.source.IVerticalRulerInfoExtension#getHover() | |
1013 * @since 3.0 | |
1014 */ | |
1015 public IAnnotationHover getHover() { | |
1016 return fHover; | |
1017 } | |
1018 | |
1019 /** | |
1020 * @param hover The hover to set. | |
1021 * @since 3.0 | |
1022 */ | |
1023 public void setHover(IAnnotationHover hover) { | |
1024 fHover= hover; | |
1025 } | |
1026 | |
1027 /* | |
1028 * @see dwtx.jface.text.source.IVerticalRulerInfoExtension#addVerticalRulerListener(dwtx.jface.text.source.IVerticalRulerListener) | |
1029 * @since 3.0 | |
1030 */ | |
1031 public void addVerticalRulerListener(IVerticalRulerListener listener) { | |
1032 throw new UnsupportedOperationException(); | |
1033 } | |
1034 | |
1035 /* | |
1036 * @see dwtx.jface.text.source.IVerticalRulerInfoExtension#removeVerticalRulerListener(dwtx.jface.text.source.IVerticalRulerListener) | |
1037 * @since 3.0 | |
1038 */ | |
1039 public void removeVerticalRulerListener(IVerticalRulerListener listener) { | |
1040 throw new UnsupportedOperationException(); | |
1041 } | |
1042 } |