Mercurial > projects > dwt-addons
annotate dwtx/jface/text/source/OverviewRuler.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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.source.OverviewRuler; | |
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.AnnotationPainter; // packageimport | |
74 import dwtx.jface.text.source.IAnnotationHoverExtension2; // packageimport | |
75 import dwtx.jface.text.source.OverviewRulerHoverManager; // packageimport | |
76 | |
77 | |
129 | 78 import dwt.dwthelper.utils; |
79 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
80 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
81 |
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 |
129 | 87 |
88 import dwt.DWT; | |
89 import dwt.custom.StyledText; | |
90 import dwt.events.DisposeEvent; | |
91 import dwt.events.DisposeListener; | |
92 import dwt.events.MouseAdapter; | |
93 import dwt.events.MouseEvent; | |
94 import dwt.events.MouseMoveListener; | |
95 import dwt.events.MouseTrackAdapter; | |
96 import dwt.events.PaintEvent; | |
97 import dwt.events.PaintListener; | |
98 import dwt.graphics.Color; | |
99 import dwt.graphics.Cursor; | |
100 import dwt.graphics.GC; | |
101 import dwt.graphics.Image; | |
102 import dwt.graphics.Point; | |
103 import dwt.graphics.RGB; | |
104 import dwt.graphics.Rectangle; | |
105 import dwt.widgets.Canvas; | |
106 import dwt.widgets.Composite; | |
107 import dwt.widgets.Control; | |
108 import dwt.widgets.Display; | |
109 import dwtx.jface.text.BadLocationException; | |
110 import dwtx.jface.text.IDocument; | |
111 import dwtx.jface.text.IRegion; | |
112 import dwtx.jface.text.ITextListener; | |
113 import dwtx.jface.text.ITextViewer; | |
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.TextEvent; | |
119 import dwtx.jface.text.source.projection.AnnotationBag; | |
120 | |
121 | |
122 /** | |
123 * Ruler presented next to a source viewer showing all annotations of the | |
124 * viewer's annotation model in a compact format. The ruler has the same height | |
125 * as the source viewer. | |
126 * <p> | |
127 * Clients usually instantiate and configure objects of this class.</p> | |
128 * | |
129 * @since 2.1 | |
130 */ | |
131 public class OverviewRuler : IOverviewRuler { | |
132 | |
133 /** | |
134 * Internal listener class. | |
135 */ | |
136 class InternalListener : ITextListener, IAnnotationModelListener, IAnnotationModelListenerExtension { | |
137 | |
138 /* | |
139 * @see ITextListener#textChanged | |
140 */ | |
141 public void textChanged(TextEvent e) { | |
142 if (fTextViewer !is null && e.getDocumentEvent() is null && e.getViewerRedrawState()) { | |
143 // handle only changes of visible document | |
144 redraw(); | |
145 } | |
146 } | |
147 | |
148 /* | |
149 * @see IAnnotationModelListener#modelChanged(IAnnotationModel) | |
150 */ | |
151 public void modelChanged(IAnnotationModel model) { | |
152 update(); | |
153 } | |
154 | |
155 /* | |
156 * @see dwtx.jface.text.source.IAnnotationModelListenerExtension#modelChanged(dwtx.jface.text.source.AnnotationModelEvent) | |
157 * @since 3.3 | |
158 */ | |
159 public void modelChanged(AnnotationModelEvent event) { | |
160 if (!event.isValid()) | |
161 return; | |
145 | 162 |
129 | 163 if (event.isWorldChange()) { |
164 update(); | |
165 return; | |
166 } | |
145 | 167 |
129 | 168 Annotation[] annotations= event.getAddedAnnotations(); |
169 int length= annotations.length; | |
170 for (int i= 0; i < length; i++) { | |
171 if (!skip(annotations[i].getType())) { | |
172 update(); | |
173 return; | |
174 } | |
175 } | |
145 | 176 |
129 | 177 annotations= event.getRemovedAnnotations(); |
178 length= annotations.length; | |
179 for (int i= 0; i < length; i++) { | |
180 if (!skip(annotations[i].getType())) { | |
181 update(); | |
182 return; | |
183 } | |
184 } | |
145 | 185 |
129 | 186 annotations= event.getChangedAnnotations(); |
187 length= annotations.length; | |
188 for (int i= 0; i < length; i++) { | |
189 if (!skip(annotations[i].getType())) { | |
190 update(); | |
191 return; | |
192 } | |
193 } | |
145 | 194 |
129 | 195 } |
196 } | |
197 | |
198 /** | |
199 * Enumerates the annotations of a specified type and characteristics | |
200 * of the associated annotation model. | |
201 */ | |
202 class FilterIterator : Iterator { | |
203 | |
204 final static int TEMPORARY= 1 << 1; | |
205 final static int PERSISTENT= 1 << 2; | |
206 final static int IGNORE_BAGS= 1 << 3; | |
207 | |
208 private Iterator fIterator; | |
209 private Object fType; | |
210 private Annotation fNext; | |
211 private int fStyle; | |
212 | |
213 /** | |
214 * Creates a new filter iterator with the given specification. | |
215 * | |
216 * @param annotationType the annotation type | |
217 * @param style the style | |
218 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
219 public this(Object annotationType, int style) { |
129 | 220 fType= annotationType; |
221 fStyle= style; | |
222 if (fModel !is null) { | |
223 fIterator= fModel.getAnnotationIterator(); | |
224 skip(); | |
225 } | |
226 } | |
227 | |
228 /** | |
229 * Creates a new filter iterator with the given specification. | |
230 * | |
231 * @param annotationType the annotation type | |
232 * @param style the style | |
233 * @param iterator the iterator | |
234 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
235 public this(Object annotationType, int style, Iterator iterator) { |
129 | 236 fType= annotationType; |
237 fStyle= style; | |
238 fIterator= iterator; | |
239 skip(); | |
240 } | |
241 | |
242 private void skip() { | |
243 | |
244 bool temp= (fStyle & TEMPORARY) !is 0; | |
245 bool pers= (fStyle & PERSISTENT) !is 0; | |
246 bool ignr= (fStyle & IGNORE_BAGS) !is 0; | |
247 | |
248 while (fIterator.hasNext()) { | |
134 | 249 Annotation next= cast(Annotation) fIterator.next(); |
129 | 250 |
251 if (next.isMarkedDeleted()) | |
252 continue; | |
253 | |
138 | 254 if (ignr && ( cast(AnnotationBag)next )) |
129 | 255 continue; |
256 | |
257 fNext= next; | |
162 | 258 Object annotationType= stringcast(next.getType()); |
259 if (fType is null || fType.opEquals(annotationType) || !fConfiguredAnnotationTypes.contains(annotationType) && isSubtype(annotationType)) { | |
129 | 260 if (temp && pers) return; |
261 if (pers && next.isPersistent()) return; | |
262 if (temp && !next.isPersistent()) return; | |
263 } | |
264 } | |
265 fNext= null; | |
266 } | |
267 | |
268 private bool isSubtype(Object annotationType) { | |
138 | 269 if ( cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
134 | 270 IAnnotationAccessExtension extension= cast(IAnnotationAccessExtension) fAnnotationAccess; |
129 | 271 return extension.isSubtype(annotationType, fType); |
272 } | |
162 | 273 return cast(bool) fType.opEquals(annotationType); |
129 | 274 } |
275 | |
276 /* | |
277 * @see Iterator#hasNext() | |
278 */ | |
279 public bool hasNext() { | |
280 return fNext !is null; | |
281 } | |
282 /* | |
283 * @see Iterator#next() | |
284 */ | |
285 public Object next() { | |
286 try { | |
287 return fNext; | |
288 } finally { | |
289 if (fIterator !is null) | |
290 skip(); | |
291 } | |
292 } | |
293 /* | |
294 * @see Iterator#remove() | |
295 */ | |
296 public void remove() { | |
297 throw new UnsupportedOperationException(); | |
298 } | |
299 } | |
300 | |
301 /** | |
302 * The painter of the overview ruler's header. | |
303 */ | |
304 class HeaderPainter : PaintListener { | |
305 | |
306 private Color fIndicatorColor; | |
307 private Color fSeparatorColor; | |
308 | |
309 /** | |
310 * Creates a new header painter. | |
311 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
312 public this() { |
129 | 313 fSeparatorColor= fHeader.getDisplay().getSystemColor(DWT.COLOR_WIDGET_NORMAL_SHADOW); |
314 } | |
315 | |
316 /** | |
317 * Sets the header color. | |
318 * | |
319 * @param color the header color | |
320 */ | |
321 public void setColor(Color color) { | |
322 fIndicatorColor= color; | |
323 } | |
324 | |
325 private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topLeft, Color bottomRight) { | |
326 gc.setForeground(topLeft is null ? fSeparatorColor : topLeft); | |
327 gc.drawLine(x, y, x + w -1, y); | |
328 gc.drawLine(x, y, x, y + h -1); | |
329 | |
330 gc.setForeground(bottomRight is null ? fSeparatorColor : bottomRight); | |
331 gc.drawLine(x + w, y, x + w, y + h); | |
332 gc.drawLine(x, y + h, x + w, y + h); | |
333 } | |
334 | |
335 public void paintControl(PaintEvent e) { | |
336 if (fIndicatorColor is null) | |
337 return; | |
338 | |
339 Point s= fHeader.getSize(); | |
340 | |
341 e.gc.setBackground(fIndicatorColor); | |
342 Rectangle r= new Rectangle(INSET, (s.y - (2*ANNOTATION_HEIGHT)) / 2, s.x - (2*INSET), 2*ANNOTATION_HEIGHT); | |
343 e.gc.fillRectangle(r); | |
344 Display d= fHeader.getDisplay(); | |
345 if (d !is null) | |
346 // drawBevelRect(e.gc, r.x, r.y, r.width -1, r.height -1, d.getSystemColor(DWT.COLOR_WIDGET_NORMAL_SHADOW), d.getSystemColor(DWT.COLOR_WIDGET_HIGHLIGHT_SHADOW)); | |
347 drawBevelRect(e.gc, r.x, r.y, r.width -1, r.height -1, null, null); | |
348 | |
349 e.gc.setForeground(fSeparatorColor); | |
350 e.gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance | |
351 e.gc.drawLine(0, s.y -1, s.x -1, s.y -1); | |
352 } | |
353 } | |
354 | |
147 | 355 private static const int INSET= 2; |
356 private static const int ANNOTATION_HEIGHT= 4; | |
129 | 357 private static bool ANNOTATION_HEIGHT_SCALABLE= true; |
358 | |
359 | |
360 /** The model of the overview ruler */ | |
361 private IAnnotationModel fModel; | |
362 /** The view to which this ruler is connected */ | |
363 private ITextViewer fTextViewer; | |
364 /** The ruler's canvas */ | |
365 private Canvas fCanvas; | |
366 /** The ruler's header */ | |
367 private Canvas fHeader; | |
368 /** The buffer for double buffering */ | |
369 private Image fBuffer; | |
370 /** The internal listener */ | |
159 | 371 private InternalListener fInternalListener; |
129 | 372 /** The width of this vertical ruler */ |
373 private int fWidth; | |
374 /** The hit detection cursor */ | |
375 private Cursor fHitDetectionCursor; | |
376 /** The last cursor */ | |
377 private Cursor fLastCursor; | |
378 /** The line of the last mouse button activity */ | |
379 private int fLastMouseButtonActivityLine= -1; | |
380 /** The actual annotation height */ | |
381 private int fAnnotationHeight= -1; | |
382 /** The annotation access */ | |
383 private IAnnotationAccess fAnnotationAccess; | |
384 /** The header painter */ | |
385 private HeaderPainter fHeaderPainter; | |
386 /** | |
387 * The list of annotation types to be shown in this ruler. | |
388 * @since 3.0 | |
389 */ | |
159 | 390 private Set fConfiguredAnnotationTypes; |
129 | 391 /** |
392 * The list of annotation types to be shown in the header of this ruler. | |
393 * @since 3.0 | |
394 */ | |
159 | 395 private Set fConfiguredHeaderAnnotationTypes; |
129 | 396 /** The mapping between annotation types and colors */ |
159 | 397 private Map fAnnotationTypes2Colors; |
129 | 398 /** The color manager */ |
399 private ISharedTextColors fSharedTextColors; | |
400 /** | |
401 * All available annotation types sorted by layer. | |
402 * | |
403 * @since 3.0 | |
404 */ | |
159 | 405 private List fAnnotationsSortedByLayer; |
129 | 406 /** |
407 * All available layers sorted by layer. | |
408 * This list may contain duplicates. | |
409 * @since 3.0 | |
410 */ | |
159 | 411 private List fLayersSortedByLayer; |
129 | 412 /** |
413 * Map of allowed annotation types. | |
414 * An allowed annotation type maps to <code>true</code>, a disallowed | |
415 * to <code>false</code>. | |
416 * @since 3.0 | |
417 */ | |
159 | 418 private Map fAllowedAnnotationTypes; |
129 | 419 /** |
420 * Map of allowed header annotation types. | |
421 * An allowed annotation type maps to <code>true</code>, a disallowed | |
422 * to <code>false</code>. | |
423 * @since 3.0 | |
424 */ | |
159 | 425 private Map fAllowedHeaderAnnotationTypes; |
129 | 426 /** |
427 * The cached annotations. | |
428 * @since 3.0 | |
429 */ | |
159 | 430 private List fCachedAnnotations; |
145 | 431 |
129 | 432 /** |
433 * Redraw runnable lock | |
434 * @since 3.3 | |
435 */ | |
159 | 436 private Object fRunnableLock; |
129 | 437 /** |
438 * Redraw runnable state | |
439 * @since 3.3 | |
440 */ | |
441 private bool fIsRunnablePosted= false; | |
442 /** | |
443 * Redraw runnable | |
444 * @since 3.3 | |
445 */ | |
159 | 446 private Runnable fRunnable; |
129 | 447 /** |
448 * Tells whether temporary annotations are drawn with | |
449 * a separate color. This color will be computed by | |
450 * discoloring the original annotation color. | |
145 | 451 * |
129 | 452 * @since 3.4 |
453 */ | |
454 private bool fIsTemporaryAnnotationDiscolored; | |
455 | |
456 | |
457 /** | |
458 * Constructs a overview ruler of the given width using the given annotation access and the given | |
459 * color manager. | |
460 * <p><strong>Note:</strong> As of 3.4, temporary annotations are no longer discolored. | |
461 * Use {@link #OverviewRuler(IAnnotationAccess, int, ISharedTextColors, bool)} if you | |
462 * want to keep the old behavior.</p> | |
463 * | |
464 * @param annotationAccess the annotation access | |
465 * @param width the width of the vertical ruler | |
466 * @param sharedColors the color manager | |
467 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
468 public this(IAnnotationAccess annotationAccess, int width, ISharedTextColors sharedColors) { |
129 | 469 this(annotationAccess, width, sharedColors, false); |
470 } | |
471 | |
472 /** | |
473 * Constructs a overview ruler of the given width using the given annotation | |
474 * access and the given color manager. | |
145 | 475 * |
129 | 476 * @param annotationAccess the annotation access |
477 * @param width the width of the vertical ruler | |
478 * @param sharedColors the color manager | |
479 * @param discolorTemporaryAnnotation <code>true</code> if temporary annotations should be discolored | |
480 * @since 3.4 | |
481 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
482 public this(IAnnotationAccess annotationAccess, int width, ISharedTextColors sharedColors, bool discolorTemporaryAnnotation) { |
159 | 483 // DWT instance init |
484 fInternalListener= new InternalListener(); | |
485 fConfiguredAnnotationTypes= new HashSet(); | |
486 fConfiguredHeaderAnnotationTypes= new HashSet(); | |
487 fAnnotationTypes2Colors= new HashMap(); | |
488 fAnnotationsSortedByLayer= new ArrayList(); | |
489 fLayersSortedByLayer= new ArrayList(); | |
490 fAllowedAnnotationTypes= new HashMap(); | |
491 fAllowedHeaderAnnotationTypes= new HashMap(); | |
492 fCachedAnnotations= new ArrayList(); | |
493 fRunnableLock= new Object(); | |
494 fRunnable= dgRunnable( { | |
495 synchronized (fRunnableLock) { | |
496 fIsRunnablePosted= false; | |
497 } | |
498 redraw(); | |
499 updateHeader(); | |
500 }); | |
501 | |
129 | 502 fAnnotationAccess= annotationAccess; |
503 fWidth= width; | |
504 fSharedTextColors= sharedColors; | |
505 fIsTemporaryAnnotationDiscolored= discolorTemporaryAnnotation; | |
506 } | |
507 | |
508 /* | |
509 * @see dwtx.jface.text.source.IVerticalRulerInfo#getControl() | |
510 */ | |
511 public Control getControl() { | |
512 return fCanvas; | |
513 } | |
514 | |
515 /* | |
516 * @see dwtx.jface.text.source.IVerticalRulerInfo#getWidth() | |
517 */ | |
518 public int getWidth() { | |
519 return fWidth; | |
520 } | |
521 | |
522 /* | |
523 * @see dwtx.jface.text.source.IVerticalRuler#setModel(dwtx.jface.text.source.IAnnotationModel) | |
524 */ | |
525 public void setModel(IAnnotationModel model) { | |
526 if (model !is fModel || model !is null) { | |
527 | |
528 if (fModel !is null) | |
529 fModel.removeAnnotationModelListener(fInternalListener); | |
530 | |
531 fModel= model; | |
532 | |
533 if (fModel !is null) | |
534 fModel.addAnnotationModelListener(fInternalListener); | |
535 | |
536 update(); | |
537 } | |
538 } | |
539 | |
540 /* | |
541 * @see dwtx.jface.text.source.IVerticalRuler#createControl(dwt.widgets.Composite, dwtx.jface.text.ITextViewer) | |
542 */ | |
543 public Control createControl(Composite parent, ITextViewer textViewer) { | |
544 | |
545 fTextViewer= textViewer; | |
546 | |
547 fHitDetectionCursor= new Cursor(parent.getDisplay(), DWT.CURSOR_HAND); | |
548 | |
549 fHeader= new Canvas(parent, DWT.NONE); | |
550 | |
138 | 551 if ( cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
135 | 552 fHeader.addMouseTrackListener(new class() MouseTrackAdapter { |
129 | 553 /* |
554 * @see dwt.events.MouseTrackAdapter#mouseHover(dwt.events.MouseEvent) | |
555 * @since 3.3 | |
556 */ | |
557 public void mouseEnter(MouseEvent e) { | |
558 updateHeaderToolTipText(); | |
559 } | |
560 }); | |
561 } | |
562 | |
563 fCanvas= new Canvas(parent, DWT.NO_BACKGROUND); | |
564 | |
135 | 565 fCanvas.addPaintListener(new class() PaintListener { |
129 | 566 public void paintControl(PaintEvent event) { |
567 if (fTextViewer !is null) | |
568 doubleBufferPaint(event.gc); | |
569 } | |
570 }); | |
571 | |
135 | 572 fCanvas.addDisposeListener(new class() DisposeListener { |
129 | 573 public void widgetDisposed(DisposeEvent event) { |
574 handleDispose(); | |
575 fTextViewer= null; | |
576 } | |
577 }); | |
578 | |
135 | 579 fCanvas.addMouseListener(new class() MouseAdapter { |
129 | 580 public void mouseDown(MouseEvent event) { |
581 handleMouseDown(event); | |
582 } | |
583 }); | |
584 | |
135 | 585 fCanvas.addMouseMoveListener(new class() MouseMoveListener { |
129 | 586 public void mouseMove(MouseEvent event) { |
587 handleMouseMove(event); | |
588 } | |
589 }); | |
590 | |
591 if (fTextViewer !is null) | |
592 fTextViewer.addTextListener(fInternalListener); | |
593 | |
594 return fCanvas; | |
595 } | |
596 | |
597 /** | |
598 * Disposes the ruler's resources. | |
599 */ | |
600 private void handleDispose() { | |
601 | |
602 if (fTextViewer !is null) { | |
603 fTextViewer.removeTextListener(fInternalListener); | |
604 fTextViewer= null; | |
605 } | |
606 | |
607 if (fModel !is null) | |
608 fModel.removeAnnotationModelListener(fInternalListener); | |
609 | |
610 if (fBuffer !is null) { | |
611 fBuffer.dispose(); | |
612 fBuffer= null; | |
613 } | |
614 | |
615 if (fHitDetectionCursor !is null) { | |
616 fHitDetectionCursor.dispose(); | |
617 fHitDetectionCursor= null; | |
618 } | |
619 | |
620 fConfiguredAnnotationTypes.clear(); | |
621 fAllowedAnnotationTypes.clear(); | |
622 fConfiguredHeaderAnnotationTypes.clear(); | |
623 fAllowedHeaderAnnotationTypes.clear(); | |
624 fAnnotationTypes2Colors.clear(); | |
625 fAnnotationsSortedByLayer.clear(); | |
626 fLayersSortedByLayer.clear(); | |
627 } | |
628 | |
629 /** | |
630 * Double buffer drawing. | |
631 * | |
632 * @param dest the GC to draw into | |
633 */ | |
634 private void doubleBufferPaint(GC dest) { | |
635 | |
636 Point size= fCanvas.getSize(); | |
637 | |
638 if (size.x <= 0 || size.y <= 0) | |
639 return; | |
640 | |
641 if (fBuffer !is null) { | |
642 Rectangle r= fBuffer.getBounds(); | |
643 if (r.width !is size.x || r.height !is size.y) { | |
644 fBuffer.dispose(); | |
645 fBuffer= null; | |
646 } | |
647 } | |
648 if (fBuffer is null) | |
649 fBuffer= new Image(fCanvas.getDisplay(), size.x, size.y); | |
650 | |
651 GC gc= new GC(fBuffer); | |
652 try { | |
653 gc.setBackground(fCanvas.getBackground()); | |
654 gc.fillRectangle(0, 0, size.x, size.y); | |
655 | |
656 cacheAnnotations(); | |
145 | 657 |
138 | 658 if ( cast(ITextViewerExtension5)fTextViewer ) |
129 | 659 doPaint1(gc); |
660 else | |
661 doPaint(gc); | |
662 | |
663 } finally { | |
664 gc.dispose(); | |
665 } | |
666 | |
667 dest.drawImage(fBuffer, 0, 0); | |
668 } | |
669 | |
670 /** | |
671 * Draws this overview ruler. | |
672 * | |
673 * @param gc the GC to draw into | |
674 */ | |
675 private void doPaint(GC gc) { | |
676 | |
677 Rectangle r= new Rectangle(0, 0, 0, 0); | |
678 int yy, hh= ANNOTATION_HEIGHT; | |
679 | |
680 IDocument document= fTextViewer.getDocument(); | |
681 IRegion visible= fTextViewer.getVisibleRegion(); | |
682 | |
683 StyledText textWidget= fTextViewer.getTextWidget(); | |
684 int maxLines= textWidget.getLineCount(); | |
685 | |
686 Point size= fCanvas.getSize(); | |
687 int writable= JFaceTextUtil.computeLineHeight(textWidget, 0, maxLines, maxLines); | |
145 | 688 |
129 | 689 if (size.y > writable) |
690 size.y= Math.max(writable - fHeader.getSize().y, 0); | |
691 | |
692 for (Iterator iterator= fAnnotationsSortedByLayer.iterator(); iterator.hasNext();) { | |
693 Object annotationType= iterator.next(); | |
694 | |
695 if (skip(annotationType)) | |
696 continue; | |
697 | |
145 | 698 int[] style= [ FilterIterator.PERSISTENT, FilterIterator.TEMPORARY ]; |
129 | 699 for (int t=0; t < style.length; t++) { |
700 | |
701 Iterator e= new FilterIterator(annotationType, style[t], fCachedAnnotations.iterator()); | |
702 Color fill= getFillColor(annotationType, style[t] is FilterIterator.TEMPORARY); | |
703 Color stroke= getStrokeColor(annotationType, style[t] is FilterIterator.TEMPORARY); | |
704 | |
705 for (int i= 0; e.hasNext(); i++) { | |
706 | |
134 | 707 Annotation a= cast(Annotation) e.next(); |
129 | 708 Position p= fModel.getPosition(a); |
709 | |
710 if (p is null || !p.overlapsWith(visible.getOffset(), visible.getLength())) | |
711 continue; | |
712 | |
713 int annotationOffset= Math.max(p.getOffset(), visible.getOffset()); | |
714 int annotationEnd= Math.min(p.getOffset() + p.getLength(), visible.getOffset() + visible.getLength()); | |
715 int annotationLength= annotationEnd - annotationOffset; | |
716 | |
717 try { | |
145 | 718 if (ANNOTATION_HEIGHT_SCALABLE) { |
129 | 719 int numbersOfLines= document.getNumberOfLines(annotationOffset, annotationLength); |
720 // don't count empty trailing lines | |
721 IRegion lastLine= document.getLineInformationOfOffset(annotationOffset + annotationLength); | |
722 if (lastLine.getOffset() is annotationOffset + annotationLength) { | |
723 numbersOfLines -= 2; | |
724 hh= (numbersOfLines * size.y) / maxLines + ANNOTATION_HEIGHT; | |
725 if (hh < ANNOTATION_HEIGHT) | |
726 hh= ANNOTATION_HEIGHT; | |
727 } else | |
728 hh= ANNOTATION_HEIGHT; | |
729 } | |
730 fAnnotationHeight= hh; | |
731 | |
732 int startLine= textWidget.getLineAtOffset(annotationOffset - visible.getOffset()); | |
733 yy= Math.min((startLine * size.y) / maxLines, size.y - hh); | |
734 | |
735 if (fill !is null) { | |
736 gc.setBackground(fill); | |
737 gc.fillRectangle(INSET, yy, size.x-(2*INSET), hh); | |
738 } | |
739 | |
740 if (stroke !is null) { | |
741 gc.setForeground(stroke); | |
742 r.x= INSET; | |
743 r.y= yy; | |
744 r.width= size.x - (2 * INSET); | |
745 r.height= hh; | |
746 gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance | |
747 gc.drawRectangle(r); | |
748 } | |
749 } catch (BadLocationException x) { | |
750 } | |
751 } | |
752 } | |
753 } | |
754 } | |
755 | |
756 private void cacheAnnotations() { | |
757 fCachedAnnotations.clear(); | |
758 if (fModel !is null) { | |
759 Iterator iter= fModel.getAnnotationIterator(); | |
760 while (iter.hasNext()) { | |
134 | 761 Annotation annotation= cast(Annotation) iter.next(); |
129 | 762 |
763 if (annotation.isMarkedDeleted()) | |
764 continue; | |
765 | |
766 if (skip(annotation.getType())) | |
767 continue; | |
768 | |
769 fCachedAnnotations.add(annotation); | |
770 } | |
771 } | |
772 } | |
773 | |
774 /** | |
775 * Draws this overview ruler. Uses <code>ITextViewerExtension5</code> for | |
776 * its implementation. Will replace <code>doPaint(GC)</code>. | |
777 * | |
778 * @param gc the GC to draw into | |
779 */ | |
780 private void doPaint1(GC gc) { | |
781 | |
782 Rectangle r= new Rectangle(0, 0, 0, 0); | |
783 int yy, hh= ANNOTATION_HEIGHT; | |
784 | |
134 | 785 ITextViewerExtension5 extension= cast(ITextViewerExtension5) fTextViewer; |
129 | 786 IDocument document= fTextViewer.getDocument(); |
787 StyledText textWidget= fTextViewer.getTextWidget(); | |
788 | |
789 int maxLines= textWidget.getLineCount(); | |
790 Point size= fCanvas.getSize(); | |
791 int writable= JFaceTextUtil.computeLineHeight(textWidget, 0, maxLines, maxLines); | |
792 if (size.y > writable) | |
793 size.y= Math.max(writable - fHeader.getSize().y, 0); | |
794 | |
795 for (Iterator iterator= fAnnotationsSortedByLayer.iterator(); iterator.hasNext();) { | |
796 Object annotationType= iterator.next(); | |
797 | |
798 if (skip(annotationType)) | |
799 continue; | |
800 | |
145 | 801 int[] style= [ FilterIterator.PERSISTENT, FilterIterator.TEMPORARY ]; |
129 | 802 for (int t=0; t < style.length; t++) { |
803 | |
804 Iterator e= new FilterIterator(annotationType, style[t], fCachedAnnotations.iterator()); | |
805 Color fill= getFillColor(annotationType, style[t] is FilterIterator.TEMPORARY); | |
806 Color stroke= getStrokeColor(annotationType, style[t] is FilterIterator.TEMPORARY); | |
807 | |
808 for (int i= 0; e.hasNext(); i++) { | |
809 | |
134 | 810 Annotation a= cast(Annotation) e.next(); |
129 | 811 Position p= fModel.getPosition(a); |
812 | |
813 if (p is null) | |
814 continue; | |
815 | |
816 IRegion widgetRegion= extension.modelRange2WidgetRange(new Region(p.getOffset(), p.getLength())); | |
817 if (widgetRegion is null) | |
818 continue; | |
819 | |
820 try { | |
145 | 821 if (ANNOTATION_HEIGHT_SCALABLE) { |
129 | 822 int numbersOfLines= document.getNumberOfLines(p.getOffset(), p.getLength()); |
823 // don't count empty trailing lines | |
824 IRegion lastLine= document.getLineInformationOfOffset(p.getOffset() + p.getLength()); | |
825 if (lastLine.getOffset() is p.getOffset() + p.getLength()) { | |
826 numbersOfLines -= 2; | |
827 hh= (numbersOfLines * size.y) / maxLines + ANNOTATION_HEIGHT; | |
828 if (hh < ANNOTATION_HEIGHT) | |
829 hh= ANNOTATION_HEIGHT; | |
830 } else | |
831 hh= ANNOTATION_HEIGHT; | |
832 } | |
833 fAnnotationHeight= hh; | |
834 | |
835 int startLine= textWidget.getLineAtOffset(widgetRegion.getOffset()); | |
836 yy= Math.min((startLine * size.y) / maxLines, size.y - hh); | |
837 | |
838 if (fill !is null) { | |
839 gc.setBackground(fill); | |
840 gc.fillRectangle(INSET, yy, size.x-(2*INSET), hh); | |
841 } | |
842 | |
843 if (stroke !is null) { | |
844 gc.setForeground(stroke); | |
845 r.x= INSET; | |
846 r.y= yy; | |
847 r.width= size.x - (2 * INSET); | |
848 r.height= hh; | |
849 gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance | |
850 gc.drawRectangle(r); | |
851 } | |
852 } catch (BadLocationException x) { | |
853 } | |
854 } | |
855 } | |
856 } | |
857 } | |
858 | |
859 /* | |
860 * @see dwtx.jface.text.source.IVerticalRuler#update() | |
861 */ | |
862 public void update() { | |
863 if (fCanvas !is null && !fCanvas.isDisposed()) { | |
864 Display d= fCanvas.getDisplay(); | |
865 if (d !is null) { | |
866 synchronized (fRunnableLock) { | |
867 if (fIsRunnablePosted) | |
868 return; | |
869 fIsRunnablePosted= true; | |
870 } | |
871 d.asyncExec(fRunnable); | |
872 } | |
873 } | |
874 } | |
875 | |
876 /** | |
877 * Redraws the overview ruler. | |
878 */ | |
879 private void redraw() { | |
880 if (fTextViewer is null || fModel is null) | |
881 return; | |
882 | |
883 if (fCanvas !is null && !fCanvas.isDisposed()) { | |
884 GC gc= new GC(fCanvas); | |
885 doubleBufferPaint(gc); | |
886 gc.dispose(); | |
887 } | |
888 } | |
889 | |
890 /** | |
891 * Translates a given y-coordinate of this ruler into the corresponding | |
892 * document lines. The number of lines depends on the concrete scaling | |
893 * given as the ration between the height of this ruler and the length | |
894 * of the document. | |
895 * | |
896 * @param y_coordinate the y-coordinate | |
897 * @return the corresponding document lines | |
898 */ | |
899 private int[] toLineNumbers(int y_coordinate) { | |
900 | |
901 StyledText textWidget= fTextViewer.getTextWidget(); | |
902 int maxLines= textWidget.getContent().getLineCount(); | |
903 | |
904 int rulerLength= fCanvas.getSize().y; | |
905 int writable= JFaceTextUtil.computeLineHeight(textWidget, 0, maxLines, maxLines); | |
906 | |
907 if (rulerLength > writable) | |
908 rulerLength= Math.max(writable - fHeader.getSize().y, 0); | |
909 | |
910 if (y_coordinate >= writable || y_coordinate >= rulerLength) | |
145 | 911 return [-1, -1]; |
129 | 912 |
913 int[] lines= new int[2]; | |
914 | |
915 int pixel0= Math.max(y_coordinate - 1, 0); | |
916 int pixel1= Math.min(rulerLength, y_coordinate + 1); | |
917 rulerLength= Math.max(rulerLength, 1); | |
918 | |
919 lines[0]= (pixel0 * maxLines) / rulerLength; | |
920 lines[1]= (pixel1 * maxLines) / rulerLength; | |
921 | |
138 | 922 if ( cast(ITextViewerExtension5)fTextViewer ) { |
134 | 923 ITextViewerExtension5 extension= cast(ITextViewerExtension5) fTextViewer; |
129 | 924 lines[0]= extension.widgetLine2ModelLine(lines[0]); |
925 lines[1]= extension.widgetLine2ModelLine(lines[1]); | |
926 } else { | |
927 try { | |
928 IRegion visible= fTextViewer.getVisibleRegion(); | |
929 int lineNumber= fTextViewer.getDocument().getLineOfOffset(visible.getOffset()); | |
930 lines[0] += lineNumber; | |
931 lines[1] += lineNumber; | |
932 } catch (BadLocationException x) { | |
933 } | |
934 } | |
935 | |
936 return lines; | |
937 } | |
938 | |
939 /** | |
940 * Returns the position of the first annotation found in the given line range. | |
941 * | |
942 * @param lineNumbers the line range | |
943 * @return the position of the first found annotation | |
944 */ | |
945 private Position getAnnotationPosition(int[] lineNumbers) { | |
946 if (lineNumbers[0] is -1) | |
947 return null; | |
948 | |
949 Position found= null; | |
950 | |
951 try { | |
952 IDocument d= fTextViewer.getDocument(); | |
953 IRegion line= d.getLineInformation(lineNumbers[0]); | |
954 | |
955 int start= line.getOffset(); | |
956 | |
957 line= d.getLineInformation(lineNumbers[lineNumbers.length - 1]); | |
958 int end= line.getOffset() + line.getLength(); | |
959 | |
960 for (int i= fAnnotationsSortedByLayer.size() -1; i >= 0; i--) { | |
961 | |
962 Object annotationType= fAnnotationsSortedByLayer.get(i); | |
963 | |
964 Iterator e= new FilterIterator(annotationType, FilterIterator.PERSISTENT | FilterIterator.TEMPORARY); | |
965 while (e.hasNext() && found is null) { | |
134 | 966 Annotation a= cast(Annotation) e.next(); |
129 | 967 if (a.isMarkedDeleted()) |
968 continue; | |
969 | |
970 if (skip(a.getType())) | |
971 continue; | |
972 | |
973 Position p= fModel.getPosition(a); | |
974 if (p is null) | |
975 continue; | |
976 | |
977 int posOffset= p.getOffset(); | |
978 int posEnd= posOffset + p.getLength(); | |
979 IRegion region= d.getLineInformationOfOffset(posEnd); | |
980 // trailing empty lines don't count | |
981 if (posEnd > posOffset && region.getOffset() is posEnd) { | |
982 posEnd--; | |
983 region= d.getLineInformationOfOffset(posEnd); | |
984 } | |
985 | |
986 if (posOffset <= end && posEnd >= start) | |
987 found= p; | |
988 } | |
989 } | |
990 } catch (BadLocationException x) { | |
991 } | |
992 | |
993 return found; | |
994 } | |
995 | |
996 /** | |
997 * Returns the line which corresponds best to one of | |
998 * the underlying annotations at the given y-coordinate. | |
999 * | |
1000 * @param lineNumbers the line numbers | |
1001 * @return the best matching line or <code>-1</code> if no such line can be found | |
1002 */ | |
1003 private int findBestMatchingLineNumber(int[] lineNumbers) { | |
1004 if (lineNumbers is null || lineNumbers.length < 1) | |
1005 return -1; | |
1006 | |
1007 try { | |
1008 Position pos= getAnnotationPosition(lineNumbers); | |
1009 if (pos is null) | |
1010 return -1; | |
1011 return fTextViewer.getDocument().getLineOfOffset(pos.getOffset()); | |
1012 } catch (BadLocationException ex) { | |
1013 return -1; | |
1014 } | |
1015 } | |
1016 | |
1017 /** | |
1018 * Handles mouse clicks. | |
1019 * | |
1020 * @param event the mouse button down event | |
1021 */ | |
1022 private void handleMouseDown(MouseEvent event) { | |
1023 if (fTextViewer !is null) { | |
1024 int[] lines= toLineNumbers(event.y); | |
1025 Position p= getAnnotationPosition(lines); | |
1026 if (p is null && event.button is 1) { | |
1027 try { | |
1028 p= new Position(fTextViewer.getDocument().getLineInformation(lines[0]).getOffset(), 0); | |
1029 } catch (BadLocationException e) { | |
1030 // do nothing | |
1031 } | |
1032 } | |
1033 if (p !is null) { | |
1034 fTextViewer.revealRange(p.getOffset(), p.getLength()); | |
1035 fTextViewer.setSelectedRange(p.getOffset(), p.getLength()); | |
1036 } | |
1037 fTextViewer.getTextWidget().setFocus(); | |
1038 } | |
1039 fLastMouseButtonActivityLine= toDocumentLineNumber(event.y); | |
1040 } | |
1041 | |
1042 /** | |
1043 * Handles mouse moves. | |
1044 * | |
1045 * @param event the mouse move event | |
1046 */ | |
1047 private void handleMouseMove(MouseEvent event) { | |
1048 if (fTextViewer !is null) { | |
1049 int[] lines= toLineNumbers(event.y); | |
1050 Position p= getAnnotationPosition(lines); | |
1051 Cursor cursor= (p !is null ? fHitDetectionCursor : null); | |
1052 if (cursor !is fLastCursor) { | |
1053 fCanvas.setCursor(cursor); | |
1054 fLastCursor= cursor; | |
1055 } | |
1056 } | |
1057 } | |
1058 | |
1059 /* | |
1060 * @see dwtx.jface.text.source.IOverviewRuler#addAnnotationType(java.lang.Object) | |
1061 */ | |
1062 public void addAnnotationType(Object annotationType) { | |
1063 fConfiguredAnnotationTypes.add(annotationType); | |
1064 fAllowedAnnotationTypes.clear(); | |
1065 } | |
1066 | |
1067 /* | |
1068 * @see dwtx.jface.text.source.IOverviewRuler#removeAnnotationType(java.lang.Object) | |
1069 */ | |
1070 public void removeAnnotationType(Object annotationType) { | |
1071 fConfiguredAnnotationTypes.remove(annotationType); | |
1072 fAllowedAnnotationTypes.clear(); | |
1073 } | |
1074 | |
1075 /* | |
1076 * @see dwtx.jface.text.source.IOverviewRuler#setAnnotationTypeLayer(java.lang.Object, int) | |
1077 */ | |
1078 public void setAnnotationTypeLayer(Object annotationType, int layer) { | |
1079 int j= fAnnotationsSortedByLayer.indexOf(annotationType); | |
1080 if (j !is -1) { | |
1081 fAnnotationsSortedByLayer.remove(j); | |
1082 fLayersSortedByLayer.remove(j); | |
1083 } | |
1084 | |
1085 if (layer >= 0) { | |
1086 int i= 0; | |
1087 int size= fLayersSortedByLayer.size(); | |
134 | 1088 while (i < size && layer >= (cast(Integer)fLayersSortedByLayer.get(i)).intValue()) |
129 | 1089 i++; |
1090 Integer layerObj= new Integer(layer); | |
1091 fLayersSortedByLayer.add(i, layerObj); | |
1092 fAnnotationsSortedByLayer.add(i, annotationType); | |
1093 } | |
1094 } | |
1095 | |
1096 /* | |
1097 * @see dwtx.jface.text.source.IOverviewRuler#setAnnotationTypeColor(java.lang.Object, dwt.graphics.Color) | |
1098 */ | |
1099 public void setAnnotationTypeColor(Object annotationType, Color color) { | |
1100 if (color !is null) | |
1101 fAnnotationTypes2Colors.put(annotationType, color); | |
1102 else | |
1103 fAnnotationTypes2Colors.remove(annotationType); | |
1104 } | |
1105 | |
1106 /** | |
1107 * Returns whether the given annotation type should be skipped by the drawing routine. | |
1108 * | |
1109 * @param annotationType the annotation type | |
1110 * @return <code>true</code> if annotation of the given type should be skipped | |
1111 */ | |
1112 private bool skip(Object annotationType) { | |
1113 return !contains(annotationType, fAllowedAnnotationTypes, fConfiguredAnnotationTypes); | |
1114 } | |
162 | 1115 private bool skip(String annotationType) { |
1116 return !contains(stringcast(annotationType), fAllowedAnnotationTypes, fConfiguredAnnotationTypes); | |
1117 } | |
129 | 1118 |
1119 /** | |
1120 * Returns whether the given annotation type should be skipped by the drawing routine of the header. | |
1121 * | |
1122 * @param annotationType the annotation type | |
1123 * @return <code>true</code> if annotation of the given type should be skipped | |
1124 * @since 3.0 | |
1125 */ | |
1126 private bool skipInHeader(Object annotationType) { | |
1127 return !contains(annotationType, fAllowedHeaderAnnotationTypes, fConfiguredHeaderAnnotationTypes); | |
1128 } | |
1129 | |
1130 /** | |
1131 * Returns whether the given annotation type is mapped to <code>true</code> | |
1132 * in the given <code>allowed</code> map or covered by the <code>configured</code> | |
1133 * set. | |
1134 * | |
1135 * @param annotationType the annotation type | |
1136 * @param allowed the map with allowed annotation types mapped to booleans | |
1137 * @param configured the set with configured annotation types | |
1138 * @return <code>true</code> if annotation is contained, <code>false</code> | |
1139 * otherwise | |
1140 * @since 3.0 | |
1141 */ | |
1142 private bool contains(Object annotationType, Map allowed, Set configured) { | |
134 | 1143 Boolean cached= cast(Boolean) allowed.get(annotationType); |
129 | 1144 if (cached !is null) |
1145 return cached.booleanValue(); | |
1146 | |
1147 bool covered= isCovered(annotationType, configured); | |
1148 allowed.put(annotationType, covered ? Boolean.TRUE : Boolean.FALSE); | |
1149 return covered; | |
1150 } | |
1151 | |
1152 /** | |
1153 * Computes whether the annotations of the given type are covered by the given <code>configured</code> | |
1154 * set. This is the case if either the type of the annotation or any of its | |
1155 * super types is contained in the <code>configured</code> set. | |
1156 * | |
1157 * @param annotationType the annotation type | |
1158 * @param configured the set with configured annotation types | |
1159 * @return <code>true</code> if annotation is covered, <code>false</code> | |
1160 * otherwise | |
1161 * @since 3.0 | |
1162 */ | |
1163 private bool isCovered(Object annotationType, Set configured) { | |
138 | 1164 if ( cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
134 | 1165 IAnnotationAccessExtension extension= cast(IAnnotationAccessExtension) fAnnotationAccess; |
129 | 1166 Iterator e= configured.iterator(); |
1167 while (e.hasNext()) { | |
1168 if (extension.isSubtype(annotationType,e.next())) | |
1169 return true; | |
1170 } | |
1171 return false; | |
1172 } | |
1173 return configured.contains(annotationType); | |
1174 } | |
1175 | |
1176 /** | |
1177 * Returns a specification of a color that lies between the given | |
1178 * foreground and background color using the given scale factor. | |
1179 * | |
1180 * @param fg the foreground color | |
1181 * @param bg the background color | |
1182 * @param scale the scale factor | |
1183 * @return the interpolated color | |
1184 */ | |
1185 private static RGB interpolate(RGB fg, RGB bg, double scale) { | |
1186 return new RGB( | |
134 | 1187 cast(int) ((1.0-scale) * fg.red + scale * bg.red), |
1188 cast(int) ((1.0-scale) * fg.green + scale * bg.green), | |
1189 cast(int) ((1.0-scale) * fg.blue + scale * bg.blue) | |
129 | 1190 ); |
1191 } | |
1192 | |
1193 /** | |
1194 * Returns the grey value in which the given color would be drawn in grey-scale. | |
1195 * | |
1196 * @param rgb the color | |
1197 * @return the grey-scale value | |
1198 */ | |
1199 private static double greyLevel(RGB rgb) { | |
1200 if (rgb.red is rgb.green && rgb.green is rgb.blue) | |
1201 return rgb.red; | |
1202 return (0.299 * rgb.red + 0.587 * rgb.green + 0.114 * rgb.blue + 0.5); | |
1203 } | |
1204 | |
1205 /** | |
1206 * Returns whether the given color is dark or light depending on the colors grey-scale level. | |
1207 * | |
1208 * @param rgb the color | |
1209 * @return <code>true</code> if the color is dark, <code>false</code> if it is light | |
1210 */ | |
1211 private static bool isDark(RGB rgb) { | |
1212 return greyLevel(rgb) > 128; | |
1213 } | |
1214 | |
1215 /** | |
1216 * Returns a color based on the color configured for the given annotation type and the given scale factor. | |
1217 * | |
1218 * @param annotationType the annotation type | |
1219 * @param scale the scale factor | |
1220 * @return the computed color | |
1221 */ | |
1222 private Color getColor(Object annotationType, double scale) { | |
1223 Color base= findColor(annotationType); | |
1224 if (base is null) | |
1225 return null; | |
1226 | |
1227 RGB baseRGB= base.getRGB(); | |
1228 RGB background= fCanvas.getBackground().getRGB(); | |
1229 | |
1230 bool darkBase= isDark(baseRGB); | |
1231 bool darkBackground= isDark(background); | |
1232 if (darkBase && darkBackground) | |
1233 background= new RGB(255, 255, 255); | |
1234 else if (!darkBase && !darkBackground) | |
1235 background= new RGB(0, 0, 0); | |
1236 | |
1237 return fSharedTextColors.getColor(interpolate(baseRGB, background, scale)); | |
1238 } | |
1239 | |
1240 /** | |
1241 * Returns the color for the given annotation type | |
1242 * | |
1243 * @param annotationType the annotation type | |
1244 * @return the color | |
1245 * @since 3.0 | |
1246 */ | |
1247 private Color findColor(Object annotationType) { | |
134 | 1248 Color color= cast(Color) fAnnotationTypes2Colors.get(annotationType); |
129 | 1249 if (color !is null) |
1250 return color; | |
1251 | |
138 | 1252 if ( cast(IAnnotationAccessExtension)fAnnotationAccess ) { |
134 | 1253 IAnnotationAccessExtension extension= cast(IAnnotationAccessExtension) fAnnotationAccess; |
129 | 1254 Object[] superTypes= extension.getSupertypes(annotationType); |
1255 if (superTypes !is null) { | |
1256 for (int i= 0; i < superTypes.length; i++) { | |
134 | 1257 color= cast(Color) fAnnotationTypes2Colors.get(superTypes[i]); |
129 | 1258 if (color !is null) |
1259 return color; | |
1260 } | |
1261 } | |
1262 } | |
1263 | |
1264 return null; | |
1265 } | |
1266 | |
1267 /** | |
1268 * Returns the stroke color for the given annotation type and characteristics. | |
1269 * | |
1270 * @param annotationType the annotation type | |
1271 * @param temporary <code>true</code> if for temporary annotations | |
1272 * @return the stroke color | |
1273 */ | |
1274 private Color getStrokeColor(Object annotationType, bool temporary) { | |
1275 return getColor(annotationType, temporary && fIsTemporaryAnnotationDiscolored ? 0.5 : 0.2); | |
1276 } | |
1277 | |
1278 /** | |
1279 * Returns the fill color for the given annotation type and characteristics. | |
1280 * | |
1281 * @param annotationType the annotation type | |
1282 * @param temporary <code>true</code> if for temporary annotations | |
1283 * @return the fill color | |
1284 */ | |
1285 private Color getFillColor(Object annotationType, bool temporary) { | |
1286 return getColor(annotationType, temporary && fIsTemporaryAnnotationDiscolored ? 0.9 : 0.75); | |
1287 } | |
1288 | |
1289 /* | |
1290 * @see IVerticalRulerInfo#getLineOfLastMouseButtonActivity() | |
1291 */ | |
1292 public int getLineOfLastMouseButtonActivity() { | |
1293 if (fLastMouseButtonActivityLine >= fTextViewer.getDocument().getNumberOfLines()) | |
1294 fLastMouseButtonActivityLine= -1; | |
1295 return fLastMouseButtonActivityLine; | |
1296 } | |
1297 | |
1298 /* | |
1299 * @see IVerticalRulerInfo#toDocumentLineNumber(int) | |
1300 */ | |
1301 public int toDocumentLineNumber(int y_coordinate) { | |
1302 | |
1303 if (fTextViewer is null || y_coordinate is -1) | |
1304 return -1; | |
1305 | |
1306 int[] lineNumbers= toLineNumbers(y_coordinate); | |
1307 int bestLine= findBestMatchingLineNumber(lineNumbers); | |
1308 if (bestLine is -1 && lineNumbers.length > 0) | |
1309 return lineNumbers[0]; | |
1310 return bestLine; | |
1311 } | |
1312 | |
1313 /* | |
1314 * @see dwtx.jface.text.source.IVerticalRuler#getModel() | |
1315 */ | |
1316 public IAnnotationModel getModel() { | |
1317 return fModel; | |
1318 } | |
1319 | |
1320 /* | |
1321 * @see dwtx.jface.text.source.IOverviewRuler#getAnnotationHeight() | |
1322 */ | |
1323 public int getAnnotationHeight() { | |
1324 return fAnnotationHeight; | |
1325 } | |
1326 | |
1327 /* | |
1328 * @see dwtx.jface.text.source.IOverviewRuler#hasAnnotation(int) | |
1329 */ | |
1330 public bool hasAnnotation(int y) { | |
1331 return findBestMatchingLineNumber(toLineNumbers(y)) !is -1; | |
1332 } | |
1333 | |
1334 /* | |
1335 * @see dwtx.jface.text.source.IOverviewRuler#getHeaderControl() | |
1336 */ | |
1337 public Control getHeaderControl() { | |
1338 return fHeader; | |
1339 } | |
1340 | |
1341 /* | |
1342 * @see dwtx.jface.text.source.IOverviewRuler#addHeaderAnnotationType(java.lang.Object) | |
1343 */ | |
1344 public void addHeaderAnnotationType(Object annotationType) { | |
1345 fConfiguredHeaderAnnotationTypes.add(annotationType); | |
1346 fAllowedHeaderAnnotationTypes.clear(); | |
1347 } | |
1348 | |
1349 /* | |
1350 * @see dwtx.jface.text.source.IOverviewRuler#removeHeaderAnnotationType(java.lang.Object) | |
1351 */ | |
1352 public void removeHeaderAnnotationType(Object annotationType) { | |
1353 fConfiguredHeaderAnnotationTypes.remove(annotationType); | |
1354 fAllowedHeaderAnnotationTypes.clear(); | |
1355 } | |
1356 | |
1357 /** | |
1358 * Updates the header of this ruler. | |
1359 */ | |
1360 private void updateHeader() { | |
1361 if (fHeader is null || fHeader.isDisposed()) | |
1362 return; | |
145 | 1363 |
129 | 1364 fHeader.setToolTipText(null); |
1365 | |
1366 Object colorType= null; | |
1367 outer: for (int i= fAnnotationsSortedByLayer.size() -1; i >= 0; i--) { | |
1368 Object annotationType= fAnnotationsSortedByLayer.get(i); | |
1369 if (skipInHeader(annotationType) || skip(annotationType)) | |
1370 continue; | |
145 | 1371 |
129 | 1372 Iterator e= new FilterIterator(annotationType, FilterIterator.PERSISTENT | FilterIterator.TEMPORARY | FilterIterator.IGNORE_BAGS, fCachedAnnotations.iterator()); |
1373 while (e.hasNext()) { | |
1374 if (e.next() !is null) { | |
1375 colorType= annotationType; | |
1376 break outer; | |
1377 } | |
1378 } | |
1379 } | |
1380 | |
1381 Color color= null; | |
1382 if (colorType !is null) | |
1383 color= findColor(colorType); | |
1384 | |
1385 if (color is null) { | |
1386 if (fHeaderPainter !is null) | |
1387 fHeaderPainter.setColor(null); | |
1388 } else { | |
1389 if (fHeaderPainter is null) { | |
1390 fHeaderPainter= new HeaderPainter(); | |
1391 fHeader.addPaintListener(fHeaderPainter); | |
1392 } | |
1393 fHeaderPainter.setColor(color); | |
1394 } | |
145 | 1395 |
129 | 1396 fHeader.redraw(); |
1397 | |
1398 } | |
145 | 1399 |
129 | 1400 /** |
1401 * Updates the header tool tip text of this ruler. | |
1402 */ | |
1403 private void updateHeaderToolTipText() { | |
1404 if (fHeader is null || fHeader.isDisposed()) | |
1405 return; | |
145 | 1406 |
129 | 1407 if (fHeader.getToolTipText() !is null) |
1408 return; | |
1409 | |
1410 String overview= ""; //$NON-NLS-1$ | |
1411 | |
1412 for (int i= fAnnotationsSortedByLayer.size() -1; i >= 0; i--) { | |
1413 | |
1414 Object annotationType= fAnnotationsSortedByLayer.get(i); | |
1415 | |
1416 if (skipInHeader(annotationType) || skip(annotationType)) | |
1417 continue; | |
1418 | |
1419 int count= 0; | |
1420 String annotationTypeLabel= null; | |
1421 | |
1422 Iterator e= new FilterIterator(annotationType, FilterIterator.PERSISTENT | FilterIterator.TEMPORARY | FilterIterator.IGNORE_BAGS, fCachedAnnotations.iterator()); | |
1423 while (e.hasNext()) { | |
134 | 1424 Annotation annotation= cast(Annotation)e.next(); |
129 | 1425 if (annotation !is null) { |
1426 if (annotationTypeLabel is null) | |
134 | 1427 annotationTypeLabel= (cast(IAnnotationAccessExtension)fAnnotationAccess).getTypeLabel(annotation); |
129 | 1428 count++; |
1429 } | |
1430 } | |
1431 | |
1432 if (annotationTypeLabel !is null) { | |
1433 if (overview.length() > 0) | |
162 | 1434 overview ~= "\n"; //$NON-NLS-1$ |
1435 overview ~= JFaceTextMessages.getFormattedString("OverviewRulerHeader.toolTipTextEntry", stringcast(annotationTypeLabel), new Integer(count) ); //$NON-NLS-1$ | |
129 | 1436 } |
1437 } | |
145 | 1438 |
129 | 1439 if (overview.length() > 0) |
1440 fHeader.setToolTipText(overview); | |
1441 } | |
1442 } |