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
|
|
80 import java.util.ArrayList;
|
|
81 import java.util.HashMap;
|
|
82 import java.util.HashSet;
|
|
83 import java.util.Iterator;
|
|
84 import java.util.List;
|
|
85 import java.util.Map;
|
|
86 import java.util.Set;
|
|
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;
|
|
162
|
|
163 if (event.isWorldChange()) {
|
|
164 update();
|
|
165 return;
|
|
166 }
|
|
167
|
|
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 }
|
|
176
|
|
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 }
|
|
185
|
|
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 }
|
|
194
|
|
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 */
|
|
219 public FilterIterator(Object annotationType, int style) {
|
|
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 */
|
|
235 public FilterIterator(Object annotationType, int style, Iterator iterator) {
|
|
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()) {
|
|
249 Annotation next= (Annotation) fIterator.next();
|
|
250
|
|
251 if (next.isMarkedDeleted())
|
|
252 continue;
|
|
253
|
|
254 if (ignr && (next instanceof AnnotationBag))
|
|
255 continue;
|
|
256
|
|
257 fNext= next;
|
|
258 Object annotationType= next.getType();
|
|
259 if (fType is null || fType.equals(annotationType) || !fConfiguredAnnotationTypes.contains(annotationType) && isSubtype(annotationType)) {
|
|
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) {
|
|
269 if (fAnnotationAccess instanceof IAnnotationAccessExtension) {
|
|
270 IAnnotationAccessExtension extension= (IAnnotationAccessExtension) fAnnotationAccess;
|
|
271 return extension.isSubtype(annotationType, fType);
|
|
272 }
|
|
273 return fType.equals(annotationType);
|
|
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 */
|
|
312 public HeaderPainter() {
|
|
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
|
|
355 private static final int INSET= 2;
|
|
356 private static final int ANNOTATION_HEIGHT= 4;
|
|
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 */
|
|
371 private InternalListener fInternalListener= new InternalListener();
|
|
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 */
|
|
390 private Set fConfiguredAnnotationTypes= new HashSet();
|
|
391 /**
|
|
392 * The list of annotation types to be shown in the header of this ruler.
|
|
393 * @since 3.0
|
|
394 */
|
|
395 private Set fConfiguredHeaderAnnotationTypes= new HashSet();
|
|
396 /** The mapping between annotation types and colors */
|
|
397 private Map fAnnotationTypes2Colors= new HashMap();
|
|
398 /** The color manager */
|
|
399 private ISharedTextColors fSharedTextColors;
|
|
400 /**
|
|
401 * All available annotation types sorted by layer.
|
|
402 *
|
|
403 * @since 3.0
|
|
404 */
|
|
405 private List fAnnotationsSortedByLayer= new ArrayList();
|
|
406 /**
|
|
407 * All available layers sorted by layer.
|
|
408 * This list may contain duplicates.
|
|
409 * @since 3.0
|
|
410 */
|
|
411 private List fLayersSortedByLayer= new ArrayList();
|
|
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 */
|
|
418 private Map fAllowedAnnotationTypes= new HashMap();
|
|
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 */
|
|
425 private Map fAllowedHeaderAnnotationTypes= new HashMap();
|
|
426 /**
|
|
427 * The cached annotations.
|
|
428 * @since 3.0
|
|
429 */
|
|
430 private List fCachedAnnotations= new ArrayList();
|
|
431
|
|
432 /**
|
|
433 * Redraw runnable lock
|
|
434 * @since 3.3
|
|
435 */
|
|
436 private Object fRunnableLock= new Object();
|
|
437 /**
|
|
438 * Redraw runnable state
|
|
439 * @since 3.3
|
|
440 */
|
|
441 private bool fIsRunnablePosted= false;
|
|
442 /**
|
|
443 * Redraw runnable
|
|
444 * @since 3.3
|
|
445 */
|
|
446 private Runnable fRunnable= new Runnable() {
|
|
447 public void run() {
|
|
448 synchronized (fRunnableLock) {
|
|
449 fIsRunnablePosted= false;
|
|
450 }
|
|
451 redraw();
|
|
452 updateHeader();
|
|
453 }
|
|
454 };
|
|
455 /**
|
|
456 * Tells whether temporary annotations are drawn with
|
|
457 * a separate color. This color will be computed by
|
|
458 * discoloring the original annotation color.
|
|
459 *
|
|
460 * @since 3.4
|
|
461 */
|
|
462 private bool fIsTemporaryAnnotationDiscolored;
|
|
463
|
|
464
|
|
465 /**
|
|
466 * Constructs a overview ruler of the given width using the given annotation access and the given
|
|
467 * color manager.
|
|
468 * <p><strong>Note:</strong> As of 3.4, temporary annotations are no longer discolored.
|
|
469 * Use {@link #OverviewRuler(IAnnotationAccess, int, ISharedTextColors, bool)} if you
|
|
470 * want to keep the old behavior.</p>
|
|
471 *
|
|
472 * @param annotationAccess the annotation access
|
|
473 * @param width the width of the vertical ruler
|
|
474 * @param sharedColors the color manager
|
|
475 */
|
|
476 public OverviewRuler(IAnnotationAccess annotationAccess, int width, ISharedTextColors sharedColors) {
|
|
477 this(annotationAccess, width, sharedColors, false);
|
|
478 }
|
|
479
|
|
480 /**
|
|
481 * Constructs a overview ruler of the given width using the given annotation
|
|
482 * access and the given color manager.
|
|
483 *
|
|
484 * @param annotationAccess the annotation access
|
|
485 * @param width the width of the vertical ruler
|
|
486 * @param sharedColors the color manager
|
|
487 * @param discolorTemporaryAnnotation <code>true</code> if temporary annotations should be discolored
|
|
488 * @since 3.4
|
|
489 */
|
|
490 public OverviewRuler(IAnnotationAccess annotationAccess, int width, ISharedTextColors sharedColors, bool discolorTemporaryAnnotation) {
|
|
491 fAnnotationAccess= annotationAccess;
|
|
492 fWidth= width;
|
|
493 fSharedTextColors= sharedColors;
|
|
494 fIsTemporaryAnnotationDiscolored= discolorTemporaryAnnotation;
|
|
495 }
|
|
496
|
|
497 /*
|
|
498 * @see dwtx.jface.text.source.IVerticalRulerInfo#getControl()
|
|
499 */
|
|
500 public Control getControl() {
|
|
501 return fCanvas;
|
|
502 }
|
|
503
|
|
504 /*
|
|
505 * @see dwtx.jface.text.source.IVerticalRulerInfo#getWidth()
|
|
506 */
|
|
507 public int getWidth() {
|
|
508 return fWidth;
|
|
509 }
|
|
510
|
|
511 /*
|
|
512 * @see dwtx.jface.text.source.IVerticalRuler#setModel(dwtx.jface.text.source.IAnnotationModel)
|
|
513 */
|
|
514 public void setModel(IAnnotationModel model) {
|
|
515 if (model !is fModel || model !is null) {
|
|
516
|
|
517 if (fModel !is null)
|
|
518 fModel.removeAnnotationModelListener(fInternalListener);
|
|
519
|
|
520 fModel= model;
|
|
521
|
|
522 if (fModel !is null)
|
|
523 fModel.addAnnotationModelListener(fInternalListener);
|
|
524
|
|
525 update();
|
|
526 }
|
|
527 }
|
|
528
|
|
529 /*
|
|
530 * @see dwtx.jface.text.source.IVerticalRuler#createControl(dwt.widgets.Composite, dwtx.jface.text.ITextViewer)
|
|
531 */
|
|
532 public Control createControl(Composite parent, ITextViewer textViewer) {
|
|
533
|
|
534 fTextViewer= textViewer;
|
|
535
|
|
536 fHitDetectionCursor= new Cursor(parent.getDisplay(), DWT.CURSOR_HAND);
|
|
537
|
|
538 fHeader= new Canvas(parent, DWT.NONE);
|
|
539
|
|
540 if (fAnnotationAccess instanceof IAnnotationAccessExtension) {
|
|
541 fHeader.addMouseTrackListener(new MouseTrackAdapter() {
|
|
542 /*
|
|
543 * @see dwt.events.MouseTrackAdapter#mouseHover(dwt.events.MouseEvent)
|
|
544 * @since 3.3
|
|
545 */
|
|
546 public void mouseEnter(MouseEvent e) {
|
|
547 updateHeaderToolTipText();
|
|
548 }
|
|
549 });
|
|
550 }
|
|
551
|
|
552 fCanvas= new Canvas(parent, DWT.NO_BACKGROUND);
|
|
553
|
|
554 fCanvas.addPaintListener(new PaintListener() {
|
|
555 public void paintControl(PaintEvent event) {
|
|
556 if (fTextViewer !is null)
|
|
557 doubleBufferPaint(event.gc);
|
|
558 }
|
|
559 });
|
|
560
|
|
561 fCanvas.addDisposeListener(new DisposeListener() {
|
|
562 public void widgetDisposed(DisposeEvent event) {
|
|
563 handleDispose();
|
|
564 fTextViewer= null;
|
|
565 }
|
|
566 });
|
|
567
|
|
568 fCanvas.addMouseListener(new MouseAdapter() {
|
|
569 public void mouseDown(MouseEvent event) {
|
|
570 handleMouseDown(event);
|
|
571 }
|
|
572 });
|
|
573
|
|
574 fCanvas.addMouseMoveListener(new MouseMoveListener() {
|
|
575 public void mouseMove(MouseEvent event) {
|
|
576 handleMouseMove(event);
|
|
577 }
|
|
578 });
|
|
579
|
|
580 if (fTextViewer !is null)
|
|
581 fTextViewer.addTextListener(fInternalListener);
|
|
582
|
|
583 return fCanvas;
|
|
584 }
|
|
585
|
|
586 /**
|
|
587 * Disposes the ruler's resources.
|
|
588 */
|
|
589 private void handleDispose() {
|
|
590
|
|
591 if (fTextViewer !is null) {
|
|
592 fTextViewer.removeTextListener(fInternalListener);
|
|
593 fTextViewer= null;
|
|
594 }
|
|
595
|
|
596 if (fModel !is null)
|
|
597 fModel.removeAnnotationModelListener(fInternalListener);
|
|
598
|
|
599 if (fBuffer !is null) {
|
|
600 fBuffer.dispose();
|
|
601 fBuffer= null;
|
|
602 }
|
|
603
|
|
604 if (fHitDetectionCursor !is null) {
|
|
605 fHitDetectionCursor.dispose();
|
|
606 fHitDetectionCursor= null;
|
|
607 }
|
|
608
|
|
609 fConfiguredAnnotationTypes.clear();
|
|
610 fAllowedAnnotationTypes.clear();
|
|
611 fConfiguredHeaderAnnotationTypes.clear();
|
|
612 fAllowedHeaderAnnotationTypes.clear();
|
|
613 fAnnotationTypes2Colors.clear();
|
|
614 fAnnotationsSortedByLayer.clear();
|
|
615 fLayersSortedByLayer.clear();
|
|
616 }
|
|
617
|
|
618 /**
|
|
619 * Double buffer drawing.
|
|
620 *
|
|
621 * @param dest the GC to draw into
|
|
622 */
|
|
623 private void doubleBufferPaint(GC dest) {
|
|
624
|
|
625 Point size= fCanvas.getSize();
|
|
626
|
|
627 if (size.x <= 0 || size.y <= 0)
|
|
628 return;
|
|
629
|
|
630 if (fBuffer !is null) {
|
|
631 Rectangle r= fBuffer.getBounds();
|
|
632 if (r.width !is size.x || r.height !is size.y) {
|
|
633 fBuffer.dispose();
|
|
634 fBuffer= null;
|
|
635 }
|
|
636 }
|
|
637 if (fBuffer is null)
|
|
638 fBuffer= new Image(fCanvas.getDisplay(), size.x, size.y);
|
|
639
|
|
640 GC gc= new GC(fBuffer);
|
|
641 try {
|
|
642 gc.setBackground(fCanvas.getBackground());
|
|
643 gc.fillRectangle(0, 0, size.x, size.y);
|
|
644
|
|
645 cacheAnnotations();
|
|
646
|
|
647 if (fTextViewer instanceof ITextViewerExtension5)
|
|
648 doPaint1(gc);
|
|
649 else
|
|
650 doPaint(gc);
|
|
651
|
|
652 } finally {
|
|
653 gc.dispose();
|
|
654 }
|
|
655
|
|
656 dest.drawImage(fBuffer, 0, 0);
|
|
657 }
|
|
658
|
|
659 /**
|
|
660 * Draws this overview ruler.
|
|
661 *
|
|
662 * @param gc the GC to draw into
|
|
663 */
|
|
664 private void doPaint(GC gc) {
|
|
665
|
|
666 Rectangle r= new Rectangle(0, 0, 0, 0);
|
|
667 int yy, hh= ANNOTATION_HEIGHT;
|
|
668
|
|
669 IDocument document= fTextViewer.getDocument();
|
|
670 IRegion visible= fTextViewer.getVisibleRegion();
|
|
671
|
|
672 StyledText textWidget= fTextViewer.getTextWidget();
|
|
673 int maxLines= textWidget.getLineCount();
|
|
674
|
|
675 Point size= fCanvas.getSize();
|
|
676 int writable= JFaceTextUtil.computeLineHeight(textWidget, 0, maxLines, maxLines);
|
|
677
|
|
678 if (size.y > writable)
|
|
679 size.y= Math.max(writable - fHeader.getSize().y, 0);
|
|
680
|
|
681 for (Iterator iterator= fAnnotationsSortedByLayer.iterator(); iterator.hasNext();) {
|
|
682 Object annotationType= iterator.next();
|
|
683
|
|
684 if (skip(annotationType))
|
|
685 continue;
|
|
686
|
|
687 int[] style= new int[] { FilterIterator.PERSISTENT, FilterIterator.TEMPORARY };
|
|
688 for (int t=0; t < style.length; t++) {
|
|
689
|
|
690 Iterator e= new FilterIterator(annotationType, style[t], fCachedAnnotations.iterator());
|
|
691 Color fill= getFillColor(annotationType, style[t] is FilterIterator.TEMPORARY);
|
|
692 Color stroke= getStrokeColor(annotationType, style[t] is FilterIterator.TEMPORARY);
|
|
693
|
|
694 for (int i= 0; e.hasNext(); i++) {
|
|
695
|
|
696 Annotation a= (Annotation) e.next();
|
|
697 Position p= fModel.getPosition(a);
|
|
698
|
|
699 if (p is null || !p.overlapsWith(visible.getOffset(), visible.getLength()))
|
|
700 continue;
|
|
701
|
|
702 int annotationOffset= Math.max(p.getOffset(), visible.getOffset());
|
|
703 int annotationEnd= Math.min(p.getOffset() + p.getLength(), visible.getOffset() + visible.getLength());
|
|
704 int annotationLength= annotationEnd - annotationOffset;
|
|
705
|
|
706 try {
|
|
707 if (ANNOTATION_HEIGHT_SCALABLE) {
|
|
708 int numbersOfLines= document.getNumberOfLines(annotationOffset, annotationLength);
|
|
709 // don't count empty trailing lines
|
|
710 IRegion lastLine= document.getLineInformationOfOffset(annotationOffset + annotationLength);
|
|
711 if (lastLine.getOffset() is annotationOffset + annotationLength) {
|
|
712 numbersOfLines -= 2;
|
|
713 hh= (numbersOfLines * size.y) / maxLines + ANNOTATION_HEIGHT;
|
|
714 if (hh < ANNOTATION_HEIGHT)
|
|
715 hh= ANNOTATION_HEIGHT;
|
|
716 } else
|
|
717 hh= ANNOTATION_HEIGHT;
|
|
718 }
|
|
719 fAnnotationHeight= hh;
|
|
720
|
|
721 int startLine= textWidget.getLineAtOffset(annotationOffset - visible.getOffset());
|
|
722 yy= Math.min((startLine * size.y) / maxLines, size.y - hh);
|
|
723
|
|
724 if (fill !is null) {
|
|
725 gc.setBackground(fill);
|
|
726 gc.fillRectangle(INSET, yy, size.x-(2*INSET), hh);
|
|
727 }
|
|
728
|
|
729 if (stroke !is null) {
|
|
730 gc.setForeground(stroke);
|
|
731 r.x= INSET;
|
|
732 r.y= yy;
|
|
733 r.width= size.x - (2 * INSET);
|
|
734 r.height= hh;
|
|
735 gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance
|
|
736 gc.drawRectangle(r);
|
|
737 }
|
|
738 } catch (BadLocationException x) {
|
|
739 }
|
|
740 }
|
|
741 }
|
|
742 }
|
|
743 }
|
|
744
|
|
745 private void cacheAnnotations() {
|
|
746 fCachedAnnotations.clear();
|
|
747 if (fModel !is null) {
|
|
748 Iterator iter= fModel.getAnnotationIterator();
|
|
749 while (iter.hasNext()) {
|
|
750 Annotation annotation= (Annotation) iter.next();
|
|
751
|
|
752 if (annotation.isMarkedDeleted())
|
|
753 continue;
|
|
754
|
|
755 if (skip(annotation.getType()))
|
|
756 continue;
|
|
757
|
|
758 fCachedAnnotations.add(annotation);
|
|
759 }
|
|
760 }
|
|
761 }
|
|
762
|
|
763 /**
|
|
764 * Draws this overview ruler. Uses <code>ITextViewerExtension5</code> for
|
|
765 * its implementation. Will replace <code>doPaint(GC)</code>.
|
|
766 *
|
|
767 * @param gc the GC to draw into
|
|
768 */
|
|
769 private void doPaint1(GC gc) {
|
|
770
|
|
771 Rectangle r= new Rectangle(0, 0, 0, 0);
|
|
772 int yy, hh= ANNOTATION_HEIGHT;
|
|
773
|
|
774 ITextViewerExtension5 extension= (ITextViewerExtension5) fTextViewer;
|
|
775 IDocument document= fTextViewer.getDocument();
|
|
776 StyledText textWidget= fTextViewer.getTextWidget();
|
|
777
|
|
778 int maxLines= textWidget.getLineCount();
|
|
779 Point size= fCanvas.getSize();
|
|
780 int writable= JFaceTextUtil.computeLineHeight(textWidget, 0, maxLines, maxLines);
|
|
781 if (size.y > writable)
|
|
782 size.y= Math.max(writable - fHeader.getSize().y, 0);
|
|
783
|
|
784 for (Iterator iterator= fAnnotationsSortedByLayer.iterator(); iterator.hasNext();) {
|
|
785 Object annotationType= iterator.next();
|
|
786
|
|
787 if (skip(annotationType))
|
|
788 continue;
|
|
789
|
|
790 int[] style= new int[] { FilterIterator.PERSISTENT, FilterIterator.TEMPORARY };
|
|
791 for (int t=0; t < style.length; t++) {
|
|
792
|
|
793 Iterator e= new FilterIterator(annotationType, style[t], fCachedAnnotations.iterator());
|
|
794 Color fill= getFillColor(annotationType, style[t] is FilterIterator.TEMPORARY);
|
|
795 Color stroke= getStrokeColor(annotationType, style[t] is FilterIterator.TEMPORARY);
|
|
796
|
|
797 for (int i= 0; e.hasNext(); i++) {
|
|
798
|
|
799 Annotation a= (Annotation) e.next();
|
|
800 Position p= fModel.getPosition(a);
|
|
801
|
|
802 if (p is null)
|
|
803 continue;
|
|
804
|
|
805 IRegion widgetRegion= extension.modelRange2WidgetRange(new Region(p.getOffset(), p.getLength()));
|
|
806 if (widgetRegion is null)
|
|
807 continue;
|
|
808
|
|
809 try {
|
|
810 if (ANNOTATION_HEIGHT_SCALABLE) {
|
|
811 int numbersOfLines= document.getNumberOfLines(p.getOffset(), p.getLength());
|
|
812 // don't count empty trailing lines
|
|
813 IRegion lastLine= document.getLineInformationOfOffset(p.getOffset() + p.getLength());
|
|
814 if (lastLine.getOffset() is p.getOffset() + p.getLength()) {
|
|
815 numbersOfLines -= 2;
|
|
816 hh= (numbersOfLines * size.y) / maxLines + ANNOTATION_HEIGHT;
|
|
817 if (hh < ANNOTATION_HEIGHT)
|
|
818 hh= ANNOTATION_HEIGHT;
|
|
819 } else
|
|
820 hh= ANNOTATION_HEIGHT;
|
|
821 }
|
|
822 fAnnotationHeight= hh;
|
|
823
|
|
824 int startLine= textWidget.getLineAtOffset(widgetRegion.getOffset());
|
|
825 yy= Math.min((startLine * size.y) / maxLines, size.y - hh);
|
|
826
|
|
827 if (fill !is null) {
|
|
828 gc.setBackground(fill);
|
|
829 gc.fillRectangle(INSET, yy, size.x-(2*INSET), hh);
|
|
830 }
|
|
831
|
|
832 if (stroke !is null) {
|
|
833 gc.setForeground(stroke);
|
|
834 r.x= INSET;
|
|
835 r.y= yy;
|
|
836 r.width= size.x - (2 * INSET);
|
|
837 r.height= hh;
|
|
838 gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance
|
|
839 gc.drawRectangle(r);
|
|
840 }
|
|
841 } catch (BadLocationException x) {
|
|
842 }
|
|
843 }
|
|
844 }
|
|
845 }
|
|
846 }
|
|
847
|
|
848 /*
|
|
849 * @see dwtx.jface.text.source.IVerticalRuler#update()
|
|
850 */
|
|
851 public void update() {
|
|
852 if (fCanvas !is null && !fCanvas.isDisposed()) {
|
|
853 Display d= fCanvas.getDisplay();
|
|
854 if (d !is null) {
|
|
855 synchronized (fRunnableLock) {
|
|
856 if (fIsRunnablePosted)
|
|
857 return;
|
|
858 fIsRunnablePosted= true;
|
|
859 }
|
|
860 d.asyncExec(fRunnable);
|
|
861 }
|
|
862 }
|
|
863 }
|
|
864
|
|
865 /**
|
|
866 * Redraws the overview ruler.
|
|
867 */
|
|
868 private void redraw() {
|
|
869 if (fTextViewer is null || fModel is null)
|
|
870 return;
|
|
871
|
|
872 if (fCanvas !is null && !fCanvas.isDisposed()) {
|
|
873 GC gc= new GC(fCanvas);
|
|
874 doubleBufferPaint(gc);
|
|
875 gc.dispose();
|
|
876 }
|
|
877 }
|
|
878
|
|
879 /**
|
|
880 * Translates a given y-coordinate of this ruler into the corresponding
|
|
881 * document lines. The number of lines depends on the concrete scaling
|
|
882 * given as the ration between the height of this ruler and the length
|
|
883 * of the document.
|
|
884 *
|
|
885 * @param y_coordinate the y-coordinate
|
|
886 * @return the corresponding document lines
|
|
887 */
|
|
888 private int[] toLineNumbers(int y_coordinate) {
|
|
889
|
|
890 StyledText textWidget= fTextViewer.getTextWidget();
|
|
891 int maxLines= textWidget.getContent().getLineCount();
|
|
892
|
|
893 int rulerLength= fCanvas.getSize().y;
|
|
894 int writable= JFaceTextUtil.computeLineHeight(textWidget, 0, maxLines, maxLines);
|
|
895
|
|
896 if (rulerLength > writable)
|
|
897 rulerLength= Math.max(writable - fHeader.getSize().y, 0);
|
|
898
|
|
899 if (y_coordinate >= writable || y_coordinate >= rulerLength)
|
|
900 return new int[] {-1, -1};
|
|
901
|
|
902 int[] lines= new int[2];
|
|
903
|
|
904 int pixel0= Math.max(y_coordinate - 1, 0);
|
|
905 int pixel1= Math.min(rulerLength, y_coordinate + 1);
|
|
906 rulerLength= Math.max(rulerLength, 1);
|
|
907
|
|
908 lines[0]= (pixel0 * maxLines) / rulerLength;
|
|
909 lines[1]= (pixel1 * maxLines) / rulerLength;
|
|
910
|
|
911 if (fTextViewer instanceof ITextViewerExtension5) {
|
|
912 ITextViewerExtension5 extension= (ITextViewerExtension5) fTextViewer;
|
|
913 lines[0]= extension.widgetLine2ModelLine(lines[0]);
|
|
914 lines[1]= extension.widgetLine2ModelLine(lines[1]);
|
|
915 } else {
|
|
916 try {
|
|
917 IRegion visible= fTextViewer.getVisibleRegion();
|
|
918 int lineNumber= fTextViewer.getDocument().getLineOfOffset(visible.getOffset());
|
|
919 lines[0] += lineNumber;
|
|
920 lines[1] += lineNumber;
|
|
921 } catch (BadLocationException x) {
|
|
922 }
|
|
923 }
|
|
924
|
|
925 return lines;
|
|
926 }
|
|
927
|
|
928 /**
|
|
929 * Returns the position of the first annotation found in the given line range.
|
|
930 *
|
|
931 * @param lineNumbers the line range
|
|
932 * @return the position of the first found annotation
|
|
933 */
|
|
934 private Position getAnnotationPosition(int[] lineNumbers) {
|
|
935 if (lineNumbers[0] is -1)
|
|
936 return null;
|
|
937
|
|
938 Position found= null;
|
|
939
|
|
940 try {
|
|
941 IDocument d= fTextViewer.getDocument();
|
|
942 IRegion line= d.getLineInformation(lineNumbers[0]);
|
|
943
|
|
944 int start= line.getOffset();
|
|
945
|
|
946 line= d.getLineInformation(lineNumbers[lineNumbers.length - 1]);
|
|
947 int end= line.getOffset() + line.getLength();
|
|
948
|
|
949 for (int i= fAnnotationsSortedByLayer.size() -1; i >= 0; i--) {
|
|
950
|
|
951 Object annotationType= fAnnotationsSortedByLayer.get(i);
|
|
952
|
|
953 Iterator e= new FilterIterator(annotationType, FilterIterator.PERSISTENT | FilterIterator.TEMPORARY);
|
|
954 while (e.hasNext() && found is null) {
|
|
955 Annotation a= (Annotation) e.next();
|
|
956 if (a.isMarkedDeleted())
|
|
957 continue;
|
|
958
|
|
959 if (skip(a.getType()))
|
|
960 continue;
|
|
961
|
|
962 Position p= fModel.getPosition(a);
|
|
963 if (p is null)
|
|
964 continue;
|
|
965
|
|
966 int posOffset= p.getOffset();
|
|
967 int posEnd= posOffset + p.getLength();
|
|
968 IRegion region= d.getLineInformationOfOffset(posEnd);
|
|
969 // trailing empty lines don't count
|
|
970 if (posEnd > posOffset && region.getOffset() is posEnd) {
|
|
971 posEnd--;
|
|
972 region= d.getLineInformationOfOffset(posEnd);
|
|
973 }
|
|
974
|
|
975 if (posOffset <= end && posEnd >= start)
|
|
976 found= p;
|
|
977 }
|
|
978 }
|
|
979 } catch (BadLocationException x) {
|
|
980 }
|
|
981
|
|
982 return found;
|
|
983 }
|
|
984
|
|
985 /**
|
|
986 * Returns the line which corresponds best to one of
|
|
987 * the underlying annotations at the given y-coordinate.
|
|
988 *
|
|
989 * @param lineNumbers the line numbers
|
|
990 * @return the best matching line or <code>-1</code> if no such line can be found
|
|
991 */
|
|
992 private int findBestMatchingLineNumber(int[] lineNumbers) {
|
|
993 if (lineNumbers is null || lineNumbers.length < 1)
|
|
994 return -1;
|
|
995
|
|
996 try {
|
|
997 Position pos= getAnnotationPosition(lineNumbers);
|
|
998 if (pos is null)
|
|
999 return -1;
|
|
1000 return fTextViewer.getDocument().getLineOfOffset(pos.getOffset());
|
|
1001 } catch (BadLocationException ex) {
|
|
1002 return -1;
|
|
1003 }
|
|
1004 }
|
|
1005
|
|
1006 /**
|
|
1007 * Handles mouse clicks.
|
|
1008 *
|
|
1009 * @param event the mouse button down event
|
|
1010 */
|
|
1011 private void handleMouseDown(MouseEvent event) {
|
|
1012 if (fTextViewer !is null) {
|
|
1013 int[] lines= toLineNumbers(event.y);
|
|
1014 Position p= getAnnotationPosition(lines);
|
|
1015 if (p is null && event.button is 1) {
|
|
1016 try {
|
|
1017 p= new Position(fTextViewer.getDocument().getLineInformation(lines[0]).getOffset(), 0);
|
|
1018 } catch (BadLocationException e) {
|
|
1019 // do nothing
|
|
1020 }
|
|
1021 }
|
|
1022 if (p !is null) {
|
|
1023 fTextViewer.revealRange(p.getOffset(), p.getLength());
|
|
1024 fTextViewer.setSelectedRange(p.getOffset(), p.getLength());
|
|
1025 }
|
|
1026 fTextViewer.getTextWidget().setFocus();
|
|
1027 }
|
|
1028 fLastMouseButtonActivityLine= toDocumentLineNumber(event.y);
|
|
1029 }
|
|
1030
|
|
1031 /**
|
|
1032 * Handles mouse moves.
|
|
1033 *
|
|
1034 * @param event the mouse move event
|
|
1035 */
|
|
1036 private void handleMouseMove(MouseEvent event) {
|
|
1037 if (fTextViewer !is null) {
|
|
1038 int[] lines= toLineNumbers(event.y);
|
|
1039 Position p= getAnnotationPosition(lines);
|
|
1040 Cursor cursor= (p !is null ? fHitDetectionCursor : null);
|
|
1041 if (cursor !is fLastCursor) {
|
|
1042 fCanvas.setCursor(cursor);
|
|
1043 fLastCursor= cursor;
|
|
1044 }
|
|
1045 }
|
|
1046 }
|
|
1047
|
|
1048 /*
|
|
1049 * @see dwtx.jface.text.source.IOverviewRuler#addAnnotationType(java.lang.Object)
|
|
1050 */
|
|
1051 public void addAnnotationType(Object annotationType) {
|
|
1052 fConfiguredAnnotationTypes.add(annotationType);
|
|
1053 fAllowedAnnotationTypes.clear();
|
|
1054 }
|
|
1055
|
|
1056 /*
|
|
1057 * @see dwtx.jface.text.source.IOverviewRuler#removeAnnotationType(java.lang.Object)
|
|
1058 */
|
|
1059 public void removeAnnotationType(Object annotationType) {
|
|
1060 fConfiguredAnnotationTypes.remove(annotationType);
|
|
1061 fAllowedAnnotationTypes.clear();
|
|
1062 }
|
|
1063
|
|
1064 /*
|
|
1065 * @see dwtx.jface.text.source.IOverviewRuler#setAnnotationTypeLayer(java.lang.Object, int)
|
|
1066 */
|
|
1067 public void setAnnotationTypeLayer(Object annotationType, int layer) {
|
|
1068 int j= fAnnotationsSortedByLayer.indexOf(annotationType);
|
|
1069 if (j !is -1) {
|
|
1070 fAnnotationsSortedByLayer.remove(j);
|
|
1071 fLayersSortedByLayer.remove(j);
|
|
1072 }
|
|
1073
|
|
1074 if (layer >= 0) {
|
|
1075 int i= 0;
|
|
1076 int size= fLayersSortedByLayer.size();
|
|
1077 while (i < size && layer >= ((Integer)fLayersSortedByLayer.get(i)).intValue())
|
|
1078 i++;
|
|
1079 Integer layerObj= new Integer(layer);
|
|
1080 fLayersSortedByLayer.add(i, layerObj);
|
|
1081 fAnnotationsSortedByLayer.add(i, annotationType);
|
|
1082 }
|
|
1083 }
|
|
1084
|
|
1085 /*
|
|
1086 * @see dwtx.jface.text.source.IOverviewRuler#setAnnotationTypeColor(java.lang.Object, dwt.graphics.Color)
|
|
1087 */
|
|
1088 public void setAnnotationTypeColor(Object annotationType, Color color) {
|
|
1089 if (color !is null)
|
|
1090 fAnnotationTypes2Colors.put(annotationType, color);
|
|
1091 else
|
|
1092 fAnnotationTypes2Colors.remove(annotationType);
|
|
1093 }
|
|
1094
|
|
1095 /**
|
|
1096 * Returns whether the given annotation type should be skipped by the drawing routine.
|
|
1097 *
|
|
1098 * @param annotationType the annotation type
|
|
1099 * @return <code>true</code> if annotation of the given type should be skipped
|
|
1100 */
|
|
1101 private bool skip(Object annotationType) {
|
|
1102 return !contains(annotationType, fAllowedAnnotationTypes, fConfiguredAnnotationTypes);
|
|
1103 }
|
|
1104
|
|
1105 /**
|
|
1106 * Returns whether the given annotation type should be skipped by the drawing routine of the header.
|
|
1107 *
|
|
1108 * @param annotationType the annotation type
|
|
1109 * @return <code>true</code> if annotation of the given type should be skipped
|
|
1110 * @since 3.0
|
|
1111 */
|
|
1112 private bool skipInHeader(Object annotationType) {
|
|
1113 return !contains(annotationType, fAllowedHeaderAnnotationTypes, fConfiguredHeaderAnnotationTypes);
|
|
1114 }
|
|
1115
|
|
1116 /**
|
|
1117 * Returns whether the given annotation type is mapped to <code>true</code>
|
|
1118 * in the given <code>allowed</code> map or covered by the <code>configured</code>
|
|
1119 * set.
|
|
1120 *
|
|
1121 * @param annotationType the annotation type
|
|
1122 * @param allowed the map with allowed annotation types mapped to booleans
|
|
1123 * @param configured the set with configured annotation types
|
|
1124 * @return <code>true</code> if annotation is contained, <code>false</code>
|
|
1125 * otherwise
|
|
1126 * @since 3.0
|
|
1127 */
|
|
1128 private bool contains(Object annotationType, Map allowed, Set configured) {
|
|
1129 Boolean cached= (Boolean) allowed.get(annotationType);
|
|
1130 if (cached !is null)
|
|
1131 return cached.booleanValue();
|
|
1132
|
|
1133 bool covered= isCovered(annotationType, configured);
|
|
1134 allowed.put(annotationType, covered ? Boolean.TRUE : Boolean.FALSE);
|
|
1135 return covered;
|
|
1136 }
|
|
1137
|
|
1138 /**
|
|
1139 * Computes whether the annotations of the given type are covered by the given <code>configured</code>
|
|
1140 * set. This is the case if either the type of the annotation or any of its
|
|
1141 * super types is contained in the <code>configured</code> set.
|
|
1142 *
|
|
1143 * @param annotationType the annotation type
|
|
1144 * @param configured the set with configured annotation types
|
|
1145 * @return <code>true</code> if annotation is covered, <code>false</code>
|
|
1146 * otherwise
|
|
1147 * @since 3.0
|
|
1148 */
|
|
1149 private bool isCovered(Object annotationType, Set configured) {
|
|
1150 if (fAnnotationAccess instanceof IAnnotationAccessExtension) {
|
|
1151 IAnnotationAccessExtension extension= (IAnnotationAccessExtension) fAnnotationAccess;
|
|
1152 Iterator e= configured.iterator();
|
|
1153 while (e.hasNext()) {
|
|
1154 if (extension.isSubtype(annotationType,e.next()))
|
|
1155 return true;
|
|
1156 }
|
|
1157 return false;
|
|
1158 }
|
|
1159 return configured.contains(annotationType);
|
|
1160 }
|
|
1161
|
|
1162 /**
|
|
1163 * Returns a specification of a color that lies between the given
|
|
1164 * foreground and background color using the given scale factor.
|
|
1165 *
|
|
1166 * @param fg the foreground color
|
|
1167 * @param bg the background color
|
|
1168 * @param scale the scale factor
|
|
1169 * @return the interpolated color
|
|
1170 */
|
|
1171 private static RGB interpolate(RGB fg, RGB bg, double scale) {
|
|
1172 return new RGB(
|
|
1173 (int) ((1.0-scale) * fg.red + scale * bg.red),
|
|
1174 (int) ((1.0-scale) * fg.green + scale * bg.green),
|
|
1175 (int) ((1.0-scale) * fg.blue + scale * bg.blue)
|
|
1176 );
|
|
1177 }
|
|
1178
|
|
1179 /**
|
|
1180 * Returns the grey value in which the given color would be drawn in grey-scale.
|
|
1181 *
|
|
1182 * @param rgb the color
|
|
1183 * @return the grey-scale value
|
|
1184 */
|
|
1185 private static double greyLevel(RGB rgb) {
|
|
1186 if (rgb.red is rgb.green && rgb.green is rgb.blue)
|
|
1187 return rgb.red;
|
|
1188 return (0.299 * rgb.red + 0.587 * rgb.green + 0.114 * rgb.blue + 0.5);
|
|
1189 }
|
|
1190
|
|
1191 /**
|
|
1192 * Returns whether the given color is dark or light depending on the colors grey-scale level.
|
|
1193 *
|
|
1194 * @param rgb the color
|
|
1195 * @return <code>true</code> if the color is dark, <code>false</code> if it is light
|
|
1196 */
|
|
1197 private static bool isDark(RGB rgb) {
|
|
1198 return greyLevel(rgb) > 128;
|
|
1199 }
|
|
1200
|
|
1201 /**
|
|
1202 * Returns a color based on the color configured for the given annotation type and the given scale factor.
|
|
1203 *
|
|
1204 * @param annotationType the annotation type
|
|
1205 * @param scale the scale factor
|
|
1206 * @return the computed color
|
|
1207 */
|
|
1208 private Color getColor(Object annotationType, double scale) {
|
|
1209 Color base= findColor(annotationType);
|
|
1210 if (base is null)
|
|
1211 return null;
|
|
1212
|
|
1213 RGB baseRGB= base.getRGB();
|
|
1214 RGB background= fCanvas.getBackground().getRGB();
|
|
1215
|
|
1216 bool darkBase= isDark(baseRGB);
|
|
1217 bool darkBackground= isDark(background);
|
|
1218 if (darkBase && darkBackground)
|
|
1219 background= new RGB(255, 255, 255);
|
|
1220 else if (!darkBase && !darkBackground)
|
|
1221 background= new RGB(0, 0, 0);
|
|
1222
|
|
1223 return fSharedTextColors.getColor(interpolate(baseRGB, background, scale));
|
|
1224 }
|
|
1225
|
|
1226 /**
|
|
1227 * Returns the color for the given annotation type
|
|
1228 *
|
|
1229 * @param annotationType the annotation type
|
|
1230 * @return the color
|
|
1231 * @since 3.0
|
|
1232 */
|
|
1233 private Color findColor(Object annotationType) {
|
|
1234 Color color= (Color) fAnnotationTypes2Colors.get(annotationType);
|
|
1235 if (color !is null)
|
|
1236 return color;
|
|
1237
|
|
1238 if (fAnnotationAccess instanceof IAnnotationAccessExtension) {
|
|
1239 IAnnotationAccessExtension extension= (IAnnotationAccessExtension) fAnnotationAccess;
|
|
1240 Object[] superTypes= extension.getSupertypes(annotationType);
|
|
1241 if (superTypes !is null) {
|
|
1242 for (int i= 0; i < superTypes.length; i++) {
|
|
1243 color= (Color) fAnnotationTypes2Colors.get(superTypes[i]);
|
|
1244 if (color !is null)
|
|
1245 return color;
|
|
1246 }
|
|
1247 }
|
|
1248 }
|
|
1249
|
|
1250 return null;
|
|
1251 }
|
|
1252
|
|
1253 /**
|
|
1254 * Returns the stroke color for the given annotation type and characteristics.
|
|
1255 *
|
|
1256 * @param annotationType the annotation type
|
|
1257 * @param temporary <code>true</code> if for temporary annotations
|
|
1258 * @return the stroke color
|
|
1259 */
|
|
1260 private Color getStrokeColor(Object annotationType, bool temporary) {
|
|
1261 return getColor(annotationType, temporary && fIsTemporaryAnnotationDiscolored ? 0.5 : 0.2);
|
|
1262 }
|
|
1263
|
|
1264 /**
|
|
1265 * Returns the fill color for the given annotation type and characteristics.
|
|
1266 *
|
|
1267 * @param annotationType the annotation type
|
|
1268 * @param temporary <code>true</code> if for temporary annotations
|
|
1269 * @return the fill color
|
|
1270 */
|
|
1271 private Color getFillColor(Object annotationType, bool temporary) {
|
|
1272 return getColor(annotationType, temporary && fIsTemporaryAnnotationDiscolored ? 0.9 : 0.75);
|
|
1273 }
|
|
1274
|
|
1275 /*
|
|
1276 * @see IVerticalRulerInfo#getLineOfLastMouseButtonActivity()
|
|
1277 */
|
|
1278 public int getLineOfLastMouseButtonActivity() {
|
|
1279 if (fLastMouseButtonActivityLine >= fTextViewer.getDocument().getNumberOfLines())
|
|
1280 fLastMouseButtonActivityLine= -1;
|
|
1281 return fLastMouseButtonActivityLine;
|
|
1282 }
|
|
1283
|
|
1284 /*
|
|
1285 * @see IVerticalRulerInfo#toDocumentLineNumber(int)
|
|
1286 */
|
|
1287 public int toDocumentLineNumber(int y_coordinate) {
|
|
1288
|
|
1289 if (fTextViewer is null || y_coordinate is -1)
|
|
1290 return -1;
|
|
1291
|
|
1292 int[] lineNumbers= toLineNumbers(y_coordinate);
|
|
1293 int bestLine= findBestMatchingLineNumber(lineNumbers);
|
|
1294 if (bestLine is -1 && lineNumbers.length > 0)
|
|
1295 return lineNumbers[0];
|
|
1296 return bestLine;
|
|
1297 }
|
|
1298
|
|
1299 /*
|
|
1300 * @see dwtx.jface.text.source.IVerticalRuler#getModel()
|
|
1301 */
|
|
1302 public IAnnotationModel getModel() {
|
|
1303 return fModel;
|
|
1304 }
|
|
1305
|
|
1306 /*
|
|
1307 * @see dwtx.jface.text.source.IOverviewRuler#getAnnotationHeight()
|
|
1308 */
|
|
1309 public int getAnnotationHeight() {
|
|
1310 return fAnnotationHeight;
|
|
1311 }
|
|
1312
|
|
1313 /*
|
|
1314 * @see dwtx.jface.text.source.IOverviewRuler#hasAnnotation(int)
|
|
1315 */
|
|
1316 public bool hasAnnotation(int y) {
|
|
1317 return findBestMatchingLineNumber(toLineNumbers(y)) !is -1;
|
|
1318 }
|
|
1319
|
|
1320 /*
|
|
1321 * @see dwtx.jface.text.source.IOverviewRuler#getHeaderControl()
|
|
1322 */
|
|
1323 public Control getHeaderControl() {
|
|
1324 return fHeader;
|
|
1325 }
|
|
1326
|
|
1327 /*
|
|
1328 * @see dwtx.jface.text.source.IOverviewRuler#addHeaderAnnotationType(java.lang.Object)
|
|
1329 */
|
|
1330 public void addHeaderAnnotationType(Object annotationType) {
|
|
1331 fConfiguredHeaderAnnotationTypes.add(annotationType);
|
|
1332 fAllowedHeaderAnnotationTypes.clear();
|
|
1333 }
|
|
1334
|
|
1335 /*
|
|
1336 * @see dwtx.jface.text.source.IOverviewRuler#removeHeaderAnnotationType(java.lang.Object)
|
|
1337 */
|
|
1338 public void removeHeaderAnnotationType(Object annotationType) {
|
|
1339 fConfiguredHeaderAnnotationTypes.remove(annotationType);
|
|
1340 fAllowedHeaderAnnotationTypes.clear();
|
|
1341 }
|
|
1342
|
|
1343 /**
|
|
1344 * Updates the header of this ruler.
|
|
1345 */
|
|
1346 private void updateHeader() {
|
|
1347 if (fHeader is null || fHeader.isDisposed())
|
|
1348 return;
|
|
1349
|
|
1350 fHeader.setToolTipText(null);
|
|
1351
|
|
1352 Object colorType= null;
|
|
1353 outer: for (int i= fAnnotationsSortedByLayer.size() -1; i >= 0; i--) {
|
|
1354 Object annotationType= fAnnotationsSortedByLayer.get(i);
|
|
1355 if (skipInHeader(annotationType) || skip(annotationType))
|
|
1356 continue;
|
|
1357
|
|
1358 Iterator e= new FilterIterator(annotationType, FilterIterator.PERSISTENT | FilterIterator.TEMPORARY | FilterIterator.IGNORE_BAGS, fCachedAnnotations.iterator());
|
|
1359 while (e.hasNext()) {
|
|
1360 if (e.next() !is null) {
|
|
1361 colorType= annotationType;
|
|
1362 break outer;
|
|
1363 }
|
|
1364 }
|
|
1365 }
|
|
1366
|
|
1367 Color color= null;
|
|
1368 if (colorType !is null)
|
|
1369 color= findColor(colorType);
|
|
1370
|
|
1371 if (color is null) {
|
|
1372 if (fHeaderPainter !is null)
|
|
1373 fHeaderPainter.setColor(null);
|
|
1374 } else {
|
|
1375 if (fHeaderPainter is null) {
|
|
1376 fHeaderPainter= new HeaderPainter();
|
|
1377 fHeader.addPaintListener(fHeaderPainter);
|
|
1378 }
|
|
1379 fHeaderPainter.setColor(color);
|
|
1380 }
|
|
1381
|
|
1382 fHeader.redraw();
|
|
1383
|
|
1384 }
|
|
1385
|
|
1386 /**
|
|
1387 * Updates the header tool tip text of this ruler.
|
|
1388 */
|
|
1389 private void updateHeaderToolTipText() {
|
|
1390 if (fHeader is null || fHeader.isDisposed())
|
|
1391 return;
|
|
1392
|
|
1393 if (fHeader.getToolTipText() !is null)
|
|
1394 return;
|
|
1395
|
|
1396 String overview= ""; //$NON-NLS-1$
|
|
1397
|
|
1398 for (int i= fAnnotationsSortedByLayer.size() -1; i >= 0; i--) {
|
|
1399
|
|
1400 Object annotationType= fAnnotationsSortedByLayer.get(i);
|
|
1401
|
|
1402 if (skipInHeader(annotationType) || skip(annotationType))
|
|
1403 continue;
|
|
1404
|
|
1405 int count= 0;
|
|
1406 String annotationTypeLabel= null;
|
|
1407
|
|
1408 Iterator e= new FilterIterator(annotationType, FilterIterator.PERSISTENT | FilterIterator.TEMPORARY | FilterIterator.IGNORE_BAGS, fCachedAnnotations.iterator());
|
|
1409 while (e.hasNext()) {
|
|
1410 Annotation annotation= (Annotation)e.next();
|
|
1411 if (annotation !is null) {
|
|
1412 if (annotationTypeLabel is null)
|
|
1413 annotationTypeLabel= ((IAnnotationAccessExtension)fAnnotationAccess).getTypeLabel(annotation);
|
|
1414 count++;
|
|
1415 }
|
|
1416 }
|
|
1417
|
|
1418 if (annotationTypeLabel !is null) {
|
|
1419 if (overview.length() > 0)
|
|
1420 overview += "\n"; //$NON-NLS-1$
|
|
1421 overview += JFaceTextMessages.getFormattedString("OverviewRulerHeader.toolTipTextEntry", new Object[] {annotationTypeLabel, new Integer(count)}); //$NON-NLS-1$
|
|
1422 }
|
|
1423 }
|
|
1424
|
|
1425 if (overview.length() > 0)
|
|
1426 fHeader.setToolTipText(overview);
|
|
1427 }
|
|
1428 }
|