Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/revisions/RevisionPainter.d @ 158:25f1f92fa3df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 26 Aug 2008 02:46:34 +0200 |
parents | f70d9508c95c |
children | 7926b636c282 |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2006, 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.internal.text.revisions.RevisionPainter; | |
14 | |
131 | 15 import dwtx.jface.internal.text.revisions.HunkComputer; // packageimport |
16 import dwtx.jface.internal.text.revisions.LineIndexOutOfBoundsException; // packageimport | |
17 import dwtx.jface.internal.text.revisions.Hunk; // packageimport | |
18 import dwtx.jface.internal.text.revisions.Colors; // packageimport | |
19 import dwtx.jface.internal.text.revisions.ChangeRegion; // packageimport | |
20 import dwtx.jface.internal.text.revisions.Range; // packageimport | |
21 import dwtx.jface.internal.text.revisions.RevisionSelectionProvider; // packageimport | |
22 | |
129 | 23 import dwt.dwthelper.utils; |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
24 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
25 |
129 | 26 import dwt.DWT; |
27 import dwt.custom.StyledText; | |
28 import dwt.events.DisposeEvent; | |
29 import dwt.events.DisposeListener; | |
30 import dwt.events.MouseEvent; | |
31 import dwt.events.MouseMoveListener; | |
32 import dwt.events.MouseTrackListener; | |
33 import dwt.graphics.Color; | |
34 import dwt.graphics.FontMetrics; | |
35 import dwt.graphics.GC; | |
36 import dwt.graphics.Point; | |
37 import dwt.graphics.RGB; | |
38 import dwt.graphics.Rectangle; | |
39 import dwt.widgets.Canvas; | |
40 import dwt.widgets.Control; | |
41 import dwt.widgets.Display; | |
42 import dwt.widgets.Event; | |
43 import dwt.widgets.Listener; | |
44 import dwt.widgets.Shell; | |
45 import dwtx.core.runtime.Assert; | |
46 import dwtx.core.runtime.ListenerList; | |
47 import dwtx.core.runtime.Platform; | |
48 import dwtx.jface.internal.text.html.BrowserInformationControl; | |
49 import dwtx.jface.internal.text.html.HTMLPrinter; | |
50 import dwtx.jface.resource.JFaceResources; | |
51 import dwtx.jface.text.AbstractReusableInformationControlCreator; | |
52 import dwtx.jface.text.BadLocationException; | |
53 import dwtx.jface.text.DefaultInformationControl; | |
54 import dwtx.jface.text.IDocument; | |
55 import dwtx.jface.text.IInformationControl; | |
56 import dwtx.jface.text.IInformationControlCreator; | |
57 import dwtx.jface.text.IRegion; | |
58 import dwtx.jface.text.ITextViewer; | |
59 import dwtx.jface.text.ITextViewerExtension5; | |
60 import dwtx.jface.text.JFaceTextUtil; | |
61 import dwtx.jface.text.Position; | |
62 import dwtx.jface.text.Region; | |
63 import dwtx.jface.text.information.IInformationProviderExtension2; | |
64 import dwtx.jface.text.revisions.IRevisionListener; | |
65 import dwtx.jface.text.revisions.IRevisionRulerColumnExtension; | |
66 import dwtx.jface.text.revisions.Revision; | |
67 import dwtx.jface.text.revisions.RevisionEvent; | |
68 import dwtx.jface.text.revisions.RevisionInformation; | |
69 import dwtx.jface.text.revisions.RevisionRange; | |
158 | 70 import dwtx.jface.text.revisions.IRevisionRulerColumnExtension; |
129 | 71 import dwtx.jface.text.source.Annotation; |
72 import dwtx.jface.text.source.CompositeRuler; | |
73 import dwtx.jface.text.source.IAnnotationHover; | |
74 import dwtx.jface.text.source.IAnnotationHoverExtension; | |
75 import dwtx.jface.text.source.IAnnotationHoverExtension2; | |
76 import dwtx.jface.text.source.IAnnotationModel; | |
77 import dwtx.jface.text.source.IAnnotationModelExtension; | |
78 import dwtx.jface.text.source.IAnnotationModelListener; | |
79 import dwtx.jface.text.source.IChangeRulerColumn; | |
80 import dwtx.jface.text.source.ILineDiffer; | |
81 import dwtx.jface.text.source.ILineRange; | |
82 import dwtx.jface.text.source.ISharedTextColors; | |
83 import dwtx.jface.text.source.ISourceViewer; | |
84 import dwtx.jface.text.source.IVerticalRulerColumn; | |
85 import dwtx.jface.text.source.LineRange; | |
86 | |
87 | |
88 /** | |
89 * A strategy for painting the live annotate colors onto the vertical ruler column. It also manages | |
90 * the revision hover. | |
150 | 91 * |
129 | 92 * @since 3.2 |
93 */ | |
94 public final class RevisionPainter { | |
95 /** Tells whether this class is in debug mode. */ | |
96 private static bool DEBUG= "true".equalsIgnoreCase(Platform.getDebugOption("dwtx.jface.text.source/debug/RevisionRulerColumn")); //$NON-NLS-1$//$NON-NLS-2$ | |
97 | |
98 // RGBs provided by UI Designer | |
147 | 99 private static const RGB BY_DATE_START_COLOR= new RGB(199, 134, 57); |
100 private static const RGB BY_DATE_END_COLOR= new RGB(241, 225, 206); | |
129 | 101 |
150 | 102 |
129 | 103 /** |
104 * The annotations created to show a revision in the overview ruler. | |
105 */ | |
106 private static final class RevisionAnnotation : Annotation { | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
107 public this(String text) { |
129 | 108 super("dwtx.ui.workbench.texteditor.revisionAnnotation", false, text); //$NON-NLS-1$ |
109 } | |
110 } | |
111 | |
112 /** | |
113 * The color tool manages revision colors and computes shaded colors based on the relative age | |
114 * and author of a revision. | |
115 */ | |
116 private final class ColorTool { | |
117 /** | |
118 * The average perceived intensity of a base color. 0 means black, 1 means white. A base | |
119 * revision color perceived as light such as yellow will be darkened, while colors perceived | |
120 * as dark such as blue will be lightened up. | |
121 */ | |
147 | 122 private static const float AVERAGE_INTENSITY= 0.5f; |
129 | 123 /** |
124 * The maximum shading in [0, 1] - this is the shade that the most recent revision will | |
125 * receive. | |
126 */ | |
147 | 127 private static const float MAX_SHADING= 0.7f; |
129 | 128 /** |
129 * The minimum shading in [0, 1] - this is the shade that the oldest revision will receive. | |
130 */ | |
147 | 131 private static const float MIN_SHADING= 0.2f; |
129 | 132 /** |
133 * The shade for the focus boxes. | |
134 */ | |
147 | 135 private static const float FOCUS_COLOR_SHADING= 1f; |
129 | 136 |
137 | |
138 /** | |
139 * A list of {@link Long}, storing the age of each revision in a sorted list. | |
140 */ | |
141 private List fRevisions; | |
142 /** | |
143 * The stored shaded colors. | |
144 */ | |
146 | 145 private const Map fColors= new HashMap(); |
129 | 146 /** |
147 * The stored focus colors. | |
148 */ | |
146 | 149 private const Map fFocusColors= new HashMap(); |
129 | 150 |
151 /** | |
152 * Sets the revision information, which is needed to compute the relative age of a revision. | |
150 | 153 * |
129 | 154 * @param info the new revision info, <code>null</code> for none. |
155 */ | |
156 public void setInfo(RevisionInformation info) { | |
157 fRevisions= null; | |
158 fColors.clear(); | |
159 fFocusColors.clear(); | |
150 | 160 |
129 | 161 if (info is null) |
162 return; | |
163 List revisions= new ArrayList(); | |
164 for (Iterator it= info.getRevisions().iterator(); it.hasNext();) { | |
134 | 165 Revision revision= cast(Revision) it.next(); |
129 | 166 revisions.add(new Long(computeAge(revision))); |
167 } | |
168 Collections.sort(revisions); | |
169 fRevisions= revisions; | |
170 } | |
171 | |
172 private RGB adaptColor(Revision revision, bool focus) { | |
173 RGB rgb; | |
174 float scale; | |
175 if (fRenderingMode is IRevisionRulerColumnExtension.AGE) { | |
176 int index= computeAgeIndex(revision); | |
177 if (index is -1 || fRevisions.size() is 0) { | |
178 rgb= getBackground().getRGB(); | |
179 } else { | |
180 // gradient from intense red for most recent to faint yellow for oldest | |
181 RGB[] gradient= Colors.palette(BY_DATE_START_COLOR, BY_DATE_END_COLOR, fRevisions.size()); | |
182 rgb= gradient[gradient.length - index - 1]; | |
183 } | |
184 scale= 0.99f; | |
185 } else if (fRenderingMode is IRevisionRulerColumnExtension.AUTHOR) { | |
186 rgb= revision.getColor(); | |
187 rgb= Colors.adjustBrightness(rgb, AVERAGE_INTENSITY); | |
188 scale= 0.6f; | |
189 } else if (fRenderingMode is IRevisionRulerColumnExtension.AUTHOR_SHADED_BY_AGE) { | |
190 rgb= revision.getColor(); | |
191 rgb= Colors.adjustBrightness(rgb, AVERAGE_INTENSITY); | |
192 int index= computeAgeIndex(revision); | |
193 int size= fRevisions.size(); | |
194 // relative age: newest is 0, oldest is 1 | |
195 // if there is only one revision, use an intermediate value to avoid extreme coloring | |
196 if (index is -1 || size < 2) | |
197 scale= 0.5f; | |
198 else | |
134 | 199 scale= cast(float) index / (size - 1); |
129 | 200 } else { |
201 Assert.isTrue(false); | |
202 return null; // dummy | |
203 } | |
204 rgb= getShadedColor(rgb, scale, focus); | |
205 return rgb; | |
206 } | |
207 | |
208 private int computeAgeIndex(Revision revision) { | |
209 long age= computeAge(revision); | |
210 int index= fRevisions.indexOf(new Long(age)); | |
211 return index; | |
212 } | |
150 | 213 |
129 | 214 private RGB getShadedColor(RGB color, float scale, bool focus) { |
215 Assert.isLegal(scale >= 0.0); | |
216 Assert.isLegal(scale <= 1.0); | |
217 RGB background= getBackground().getRGB(); | |
218 | |
219 // normalize to lie within [MIN_SHADING, MAX_SHADING] | |
220 // use more intense colors if the ruler is narrow (i.e. not showing line numbers) | |
221 bool makeIntense= getWidth() <= 15; | |
222 float intensityShift= makeIntense ? 0.3f : 0f; | |
223 float max= MAX_SHADING + intensityShift; | |
224 float min= MIN_SHADING + intensityShift; | |
225 scale= (max - min) * scale + min; | |
226 | |
227 // focus coloring | |
228 if (focus) { | |
229 scale += FOCUS_COLOR_SHADING; | |
230 if (scale > 1) { | |
231 background= new RGB(255 - background.red, 255 - background.green, 255 - background.blue); | |
232 scale= 2 - scale; | |
233 } | |
234 } | |
235 | |
236 return Colors.blend(background, color, scale); | |
237 } | |
238 | |
239 private long computeAge(Revision revision) { | |
240 return revision.getDate().getTime(); | |
241 } | |
242 | |
243 /** | |
244 * Returns the color for a revision based on relative age and author. | |
150 | 245 * |
129 | 246 * @param revision the revision |
247 * @param focus <code>true</code> to return the focus color | |
248 * @return the color for a revision | |
249 */ | |
250 public RGB getColor(Revision revision, bool focus) { | |
251 Map map= focus ? fFocusColors : fColors; | |
134 | 252 RGB color= cast(RGB) map.get(revision); |
129 | 253 if (color !is null) |
254 return color; | |
150 | 255 |
129 | 256 color= adaptColor(revision, focus); |
257 map.put(revision, color); | |
258 return color; | |
259 } | |
260 } | |
261 | |
262 /** | |
263 * Handles all the mouse interaction in this line number ruler column. | |
264 */ | |
265 private class MouseHandler : MouseMoveListener, MouseTrackListener, Listener { | |
266 | |
267 private RevisionRange fMouseDownRegion; | |
150 | 268 |
129 | 269 private void handleMouseUp(Event e) { |
270 if (e.button is 1) { | |
271 RevisionRange upRegion= fFocusRange; | |
272 RevisionRange downRegion= fMouseDownRegion; | |
273 fMouseDownRegion= null; | |
274 | |
275 if (upRegion is downRegion) { | |
276 Revision revision= upRegion is null ? null : upRegion.getRevision(); | |
277 if (revision is fSelectedRevision) | |
278 revision= null; // deselect already selected revision | |
279 handleRevisionSelected(revision); | |
280 } | |
281 } | |
282 } | |
283 | |
284 private void handleMouseDown(Event e) { | |
285 if (e.button is 3) | |
286 updateFocusRevision(null); // kill any focus as the ctx menu is going to show | |
287 if (e.button is 1) { | |
288 fMouseDownRegion= fFocusRange; | |
289 postRedraw(); | |
290 } | |
291 } | |
292 | |
293 /* | |
294 * @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event) | |
295 */ | |
296 public void handleEvent(Event event) { | |
297 switch (event.type) { | |
298 case DWT.MouseWheel: | |
299 handleMouseWheel(event); | |
300 break; | |
301 case DWT.MouseDown: | |
302 handleMouseDown(event); | |
303 break; | |
304 case DWT.MouseUp: | |
305 handleMouseUp(event); | |
306 break; | |
307 default: | |
308 Assert.isLegal(false); | |
309 } | |
310 } | |
311 | |
312 /* | |
313 * @see dwt.events.MouseTrackListener#mouseEnter(dwt.events.MouseEvent) | |
314 */ | |
315 public void mouseEnter(MouseEvent e) { | |
316 updateFocusLine(toDocumentLineNumber(e.y)); | |
317 } | |
318 | |
319 /* | |
320 * @see dwt.events.MouseTrackListener#mouseExit(dwt.events.MouseEvent) | |
321 */ | |
322 public void mouseExit(MouseEvent e) { | |
323 updateFocusLine(-1); | |
324 } | |
325 | |
326 /* | |
327 * @see dwt.events.MouseTrackListener#mouseHover(dwt.events.MouseEvent) | |
328 */ | |
329 public void mouseHover(MouseEvent e) { | |
330 } | |
331 | |
332 /* | |
333 * @see dwt.events.MouseMoveListener#mouseMove(dwt.events.MouseEvent) | |
334 */ | |
335 public void mouseMove(MouseEvent e) { | |
336 updateFocusLine(toDocumentLineNumber(e.y)); | |
337 } | |
338 } | |
339 | |
340 /** | |
341 * Internal listener class that will update the ruler when the underlying model changes. | |
342 */ | |
343 private class AnnotationListener : IAnnotationModelListener { | |
344 /* | |
345 * @see dwtx.jface.text.source.IAnnotationModelListener#modelChanged(dwtx.jface.text.source.IAnnotationModel) | |
346 */ | |
347 public void modelChanged(IAnnotationModel model) { | |
348 clearRangeCache(); | |
349 postRedraw(); | |
350 } | |
351 | |
352 } | |
353 | |
354 /** | |
355 * The information control creator. | |
356 */ | |
357 private static final class HoverInformationControlCreator : AbstractReusableInformationControlCreator { | |
358 private bool fIsFocusable; | |
150 | 359 |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
360 public this(bool isFocusable) { |
129 | 361 fIsFocusable= isFocusable; |
362 } | |
363 | |
364 /* | |
365 * @see dwtx.jface.internal.text.revisions.AbstractReusableInformationControlCreator#doCreateInformationControl(dwt.widgets.Shell) | |
366 */ | |
367 protected IInformationControl doCreateInformationControl(Shell parent) { | |
368 if (BrowserInformationControl.isAvailable(parent)) { | |
135 | 369 return new class(parent, JFaceResources.DIALOG_FONT, fIsFocusable) BrowserInformationControl { |
129 | 370 /* |
371 * @see dwtx.jface.internal.text.html.BrowserInformationControl#setInformation(java.lang.String) | |
372 * @since 3.3 | |
373 */ | |
374 public void setInformation(String content) { | |
375 content= addCSSToHTMLFragment(content); | |
376 super.setInformation(content); | |
377 } | |
150 | 378 |
129 | 379 /** |
380 * Adds a HTML header and CSS info if <code>html</code> is only an HTML fragment (has no | |
381 * <html> section). | |
150 | 382 * |
129 | 383 * @param html the html / text produced by a revision |
384 * @return modified html | |
385 */ | |
386 private String addCSSToHTMLFragment(String html) { | |
387 int max= Math.min(100, html.length()); | |
388 if (html.substring(0, max).indexOf("<html>") !is -1) //$NON-NLS-1$ | |
389 // there is already a header | |
390 return html; | |
150 | 391 |
129 | 392 StringBuffer info= new StringBuffer(512 + html.length()); |
393 HTMLPrinter.insertPageProlog(info, 0, fgStyleSheet); | |
394 info.append(html); | |
395 HTMLPrinter.addPageEpilog(info); | |
396 return info.toString(); | |
397 } | |
150 | 398 |
129 | 399 }; |
400 } | |
401 return new DefaultInformationControl(parent, fIsFocusable); | |
402 } | |
403 | |
404 /* | |
405 * @see dwtx.jface.text.AbstractReusableInformationControlCreator#canReplace(dwtx.jface.text.IInformationControlCreator) | |
406 */ | |
407 public bool canReplace(IInformationControlCreator creator) { | |
408 return creator.getClass() is getClass() | |
134 | 409 && (cast(HoverInformationControlCreator) creator).fIsFocusable is fIsFocusable; |
129 | 410 } |
411 } | |
412 | |
413 private static final String fgStyleSheet= "/* Font definitions */\n" + //$NON-NLS-1$ | |
414 "body, h1, h2, h3, h4, h5, h6, p, table, td, caption, th, ul, ol, dl, li, dd, dt {font-family: sans-serif; font-size: 9pt }\n" + //$NON-NLS-1$ | |
415 "pre { font-family: monospace; font-size: 9pt }\n" + //$NON-NLS-1$ | |
416 "\n" + //$NON-NLS-1$ | |
417 "/* Margins */\n" + //$NON-NLS-1$ | |
418 "body { overflow: auto; margin-top: 0; margin-bottom: 4; margin-left: 3; margin-right: 0 }\n" + //$NON-NLS-1$ | |
419 "h1 { margin-top: 5; margin-bottom: 1 } \n" + //$NON-NLS-1$ | |
420 "h2 { margin-top: 25; margin-bottom: 3 }\n" + //$NON-NLS-1$ | |
421 "h3 { margin-top: 20; margin-bottom: 3 }\n" + //$NON-NLS-1$ | |
422 "h4 { margin-top: 20; margin-bottom: 3 }\n" + //$NON-NLS-1$ | |
423 "h5 { margin-top: 0; margin-bottom: 0 }\n" + //$NON-NLS-1$ | |
424 "p { margin-top: 10px; margin-bottom: 10px }\n" + //$NON-NLS-1$ | |
425 "pre { margin-left: 6 }\n" + //$NON-NLS-1$ | |
426 "ul { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$ | |
427 "li { margin-top: 0; margin-bottom: 0 } \n" + //$NON-NLS-1$ | |
428 "li p { margin-top: 0; margin-bottom: 0 } \n" + //$NON-NLS-1$ | |
429 "ol { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$ | |
430 "dl { margin-top: 0; margin-bottom: 10 }\n" + //$NON-NLS-1$ | |
431 "dt { margin-top: 0; margin-bottom: 0; font-weight: bold }\n" + //$NON-NLS-1$ | |
432 "dd { margin-top: 0; margin-bottom: 0 }\n" + //$NON-NLS-1$ | |
433 "\n" + //$NON-NLS-1$ | |
434 "/* Styles and colors */\n" + //$NON-NLS-1$ | |
435 "a:link { color: #0000FF }\n" + //$NON-NLS-1$ | |
436 "a:hover { color: #000080 }\n" + //$NON-NLS-1$ | |
437 "a:visited { text-decoration: underline }\n" + //$NON-NLS-1$ | |
438 "h4 { font-style: italic }\n" + //$NON-NLS-1$ | |
439 "strong { font-weight: bold }\n" + //$NON-NLS-1$ | |
440 "em { font-style: italic }\n" + //$NON-NLS-1$ | |
441 "var { font-style: italic }\n" + //$NON-NLS-1$ | |
442 "th { font-weight: bold }\n" + //$NON-NLS-1$ | |
443 ""; //$NON-NLS-1$ | |
150 | 444 |
129 | 445 /** |
446 * The revision hover displays information about the currently selected revision. | |
447 */ | |
448 private final class RevisionHover : IAnnotationHover, IAnnotationHoverExtension, IAnnotationHoverExtension2, IInformationProviderExtension2 { | |
150 | 449 |
129 | 450 /* |
451 * @see dwtx.jface.text.source.IAnnotationHover#getHoverInfo(dwtx.jface.text.source.ISourceViewer, | |
452 * int) | |
453 */ | |
454 public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { | |
455 Object info= getHoverInfo(sourceViewer, getHoverLineRange(sourceViewer, lineNumber), 0); | |
456 return info is null ? null : info.toString(); | |
457 } | |
458 | |
459 /* | |
460 * @see dwtx.jface.text.source.IAnnotationHoverExtension#getHoverControlCreator() | |
461 */ | |
462 public IInformationControlCreator getHoverControlCreator() { | |
463 RevisionInformation revisionInfo= fRevisionInfo; | |
464 if (revisionInfo !is null) { | |
465 IInformationControlCreator creator= revisionInfo.getHoverControlCreator(); | |
466 if (creator !is null) | |
467 return creator; | |
468 } | |
469 return new HoverInformationControlCreator(false); | |
470 } | |
471 | |
472 /* | |
473 * @see dwtx.jface.text.source.IAnnotationHoverExtension#canHandleMouseCursor() | |
474 */ | |
475 public bool canHandleMouseCursor() { | |
476 return false; | |
477 } | |
478 | |
479 /* | |
480 * @see dwtx.jface.text.source.IAnnotationHoverExtension2#canHandleMouseWheel() | |
481 */ | |
482 public bool canHandleMouseWheel() { | |
483 return true; | |
484 } | |
485 | |
486 /* | |
487 * @see dwtx.jface.text.source.IAnnotationHoverExtension#getHoverInfo(dwtx.jface.text.source.ISourceViewer, | |
488 * dwtx.jface.text.source.ILineRange, int) | |
489 */ | |
490 public Object getHoverInfo(ISourceViewer sourceViewer, ILineRange lineRange, int visibleNumberOfLines) { | |
491 RevisionRange range= getRange(lineRange.getStartLine()); | |
492 Object info= range is null ? null : range.getRevision().getHoverInfo(); | |
493 return info; | |
494 } | |
495 | |
496 /* | |
497 * @see dwtx.jface.text.source.IAnnotationHoverExtension#getHoverLineRange(dwtx.jface.text.source.ISourceViewer, | |
498 * int) | |
499 */ | |
500 public ILineRange getHoverLineRange(ISourceViewer viewer, int lineNumber) { | |
501 RevisionRange range= getRange(lineNumber); | |
502 return range is null ? null : new LineRange(lineNumber, 1); | |
503 } | |
504 | |
505 /* | |
506 * @see dwtx.jface.text.information.IInformationProviderExtension2#getInformationPresenterControlCreator() | |
507 */ | |
508 public IInformationControlCreator getInformationPresenterControlCreator() { | |
509 RevisionInformation revisionInfo= fRevisionInfo; | |
510 if (revisionInfo !is null) { | |
511 IInformationControlCreator creator= revisionInfo.getInformationPresenterControlCreator(); | |
512 if (creator !is null) | |
513 return creator; | |
514 } | |
515 return new HoverInformationControlCreator(true); | |
516 } | |
517 } | |
518 | |
519 /* Listeners and helpers. */ | |
520 | |
521 /** The shared color provider. */ | |
146 | 522 private const ISharedTextColors fSharedColors; |
129 | 523 /** The color tool. */ |
146 | 524 private const ColorTool fColorTool= new ColorTool(); |
129 | 525 /** The mouse handler. */ |
146 | 526 private const MouseHandler fMouseHandler= new MouseHandler(); |
129 | 527 /** The hover. */ |
146 | 528 private const RevisionHover fHover= new RevisionHover(); |
129 | 529 /** The annotation listener. */ |
146 | 530 private const AnnotationListener fAnnotationListener= new AnnotationListener(); |
129 | 531 /** The selection provider. */ |
146 | 532 private const RevisionSelectionProvider fRevisionSelectionProvider= new RevisionSelectionProvider(this); |
129 | 533 /** |
534 * The list of revision listeners. | |
535 * @since 3.3. | |
536 */ | |
146 | 537 private const ListenerList fRevisionListeners= new ListenerList(ListenerList.IDENTITY); |
129 | 538 |
539 /* The context - column and viewer we are connected to. */ | |
540 | |
541 /** The vertical ruler column that delegates painting to this painter. */ | |
146 | 542 private const IVerticalRulerColumn fColumn; |
129 | 543 /** The parent ruler. */ |
544 private CompositeRuler fParentRuler; | |
545 /** The column's control, typically a {@link Canvas}, possibly <code>null</code>. */ | |
546 private Control fControl; | |
547 /** The text viewer that the column is attached to. */ | |
548 private ITextViewer fViewer; | |
549 /** The viewer's text widget. */ | |
550 private StyledText fWidget; | |
551 | |
552 /* The models we operate on. */ | |
553 | |
554 /** The revision model object. */ | |
555 private RevisionInformation fRevisionInfo; | |
556 /** The line differ. */ | |
557 private ILineDiffer fLineDiffer= null; | |
558 /** The annotation model. */ | |
559 private IAnnotationModel fAnnotationModel= null; | |
560 /** The background color, possibly <code>null</code>. */ | |
561 private Color fBackground; | |
562 | |
563 /* Cache. */ | |
564 | |
565 /** The cached list of ranges adapted to quick diff. */ | |
566 private List fRevisionRanges= null; | |
567 /** The annotations created for the overview ruler temporary display. */ | |
568 private List fAnnotations= new ArrayList(); | |
569 | |
570 /* State */ | |
571 | |
572 /** The current focus line, -1 for none. */ | |
573 private int fFocusLine= -1; | |
574 /** The current focus region, <code>null</code> if none. */ | |
575 private RevisionRange fFocusRange= null; | |
576 /** The current focus revision, <code>null</code> if none. */ | |
577 private Revision fFocusRevision= null; | |
578 /** | |
579 * The currently selected revision, <code>null</code> if none. The difference between | |
580 * {@link #fFocusRevision} and {@link #fSelectedRevision} may not be obvious: the focus revision | |
581 * is the one focused by the mouse (by hovering over a block of the revision), while the | |
582 * selected revision is sticky, i.e. is not removed when the mouse leaves the ruler. | |
583 * | |
584 * @since 3.3 | |
585 */ | |
586 private Revision fSelectedRevision= null; | |
587 /** <code>true</code> if the mouse wheel handler is installed, <code>false</code> otherwise. */ | |
588 private bool fWheelHandlerInstalled= false; | |
589 /** | |
590 * The revision rendering mode. | |
591 */ | |
158 | 592 private IRevisionRulerColumnExtension.RenderingMode fRenderingMode= IRevisionRulerColumnExtension.AUTHOR_SHADED_BY_AGE; |
129 | 593 /** |
594 * The required with in characters. | |
595 * @since 3.3 | |
596 */ | |
597 private int fRequiredWidth= -1; | |
598 /** | |
599 * The width of the revision field in chars to compute {@link #fAuthorInset} from. | |
600 * @since 3.3 | |
601 */ | |
602 private int fRevisionIdChars= 0; | |
603 /** | |
604 * <code>true</code> to show revision ids, <code>false</code> otherwise. | |
605 * @since 3.3 | |
606 */ | |
607 private bool fShowRevision= false; | |
608 /** | |
609 * <code>true</code> to show the author, <code>false</code> otherwise. | |
610 * @since 3.3 | |
611 */ | |
612 private bool fShowAuthor= false; | |
613 /** | |
614 * The author inset in pixels for when author *and* revision id are shown. | |
615 * @since 3.3 | |
616 */ | |
617 private int fAuthorInset; | |
618 /** | |
619 * The remembered ruler width (as changing the ruler width triggers recomputation of the colors. | |
620 * @since 3.3 | |
621 */ | |
622 private int fLastWidth= -1; | |
623 | |
624 /** | |
625 * Creates a new revision painter for a vertical ruler column. | |
150 | 626 * |
129 | 627 * @param column the column that will delegate{@link #paint(GC, ILineRange) painting} to the |
628 * newly created painter. | |
629 * @param sharedColors a shared colors object to store shaded colors in | |
630 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
631 public this(IVerticalRulerColumn column, ISharedTextColors sharedColors) { |
129 | 632 Assert.isLegal(column !is null); |
633 Assert.isLegal(sharedColors !is null); | |
634 fColumn= column; | |
635 fSharedColors= sharedColors; | |
636 } | |
637 | |
638 /** | |
639 * Sets the revision information to be drawn and triggers a redraw. | |
150 | 640 * |
129 | 641 * @param info the revision information to show, <code>null</code> to draw none |
642 */ | |
643 public void setRevisionInformation(RevisionInformation info) { | |
644 if (fRevisionInfo !is info) { | |
645 fRequiredWidth= -1; | |
646 fRevisionIdChars= 0; | |
647 fRevisionInfo= info; | |
648 clearRangeCache(); | |
649 updateFocusRange(null); | |
134 | 650 handleRevisionSelected(cast(Revision) null); |
129 | 651 fColorTool.setInfo(info); |
652 postRedraw(); | |
653 informListeners(); | |
654 } | |
655 } | |
656 | |
657 /** | |
658 * Changes the rendering mode and triggers redrawing if needed. | |
150 | 659 * |
129 | 660 * @param renderingMode the rendering mode |
661 * @since 3.3 | |
662 */ | |
158 | 663 public void setRenderingMode(IRevisionRulerColumnExtension.RenderingMode renderingMode) { |
129 | 664 Assert.isLegal(renderingMode !is null); |
665 if (fRenderingMode !is renderingMode) { | |
666 fRenderingMode= renderingMode; | |
667 fColorTool.setInfo(fRevisionInfo); | |
668 postRedraw(); | |
669 } | |
670 } | |
671 | |
672 /** | |
673 * Sets the background color. | |
150 | 674 * |
129 | 675 * @param background the background color, <code>null</code> for the platform's list |
676 * background | |
677 */ | |
678 public void setBackground(Color background) { | |
679 fBackground= background; | |
680 } | |
681 | |
682 /** | |
683 * Sets the parent ruler - the delegating column must call this method as soon as it creates its | |
684 * control. | |
150 | 685 * |
129 | 686 * @param parentRuler the parent ruler |
687 */ | |
688 public void setParentRuler(CompositeRuler parentRuler) { | |
689 fParentRuler= parentRuler; | |
690 } | |
691 | |
692 /** | |
693 * Delegates the painting of the quick diff colors to this painter. The painter will draw the | |
694 * color boxes onto the passed {@link GC} for all model (document) lines in | |
695 * <code>visibleModelLines</code>. | |
150 | 696 * |
129 | 697 * @param gc the {@link GC} to draw onto |
698 * @param visibleLines the lines (in document offsets) that are currently (perhaps only | |
699 * partially) visible | |
700 */ | |
701 public void paint(GC gc, ILineRange visibleLines) { | |
702 connectIfNeeded(); | |
703 if (!isConnected()) | |
704 return; | |
705 | |
706 // compute the horizontal indent of the author for the case that we show revision | |
707 // and author | |
708 if (fShowAuthor && fShowRevision) { | |
709 char[] string= new char[fRevisionIdChars + 1]; | |
710 Arrays.fill(string, '9'); | |
711 if (string.length > 1) { | |
712 string[0]= '.'; | |
713 string[1]= ' '; | |
714 } | |
715 fAuthorInset= gc.stringExtent(new String(string)).x; | |
716 } | |
150 | 717 |
129 | 718 // recompute colors (show intense colors if ruler is narrow) |
719 int width= getWidth(); | |
720 if (width !is fLastWidth) { | |
721 fColorTool.setInfo(fRevisionInfo); | |
722 fLastWidth= width; | |
723 } | |
150 | 724 |
129 | 725 // draw change regions |
726 List/* <RevisionRange> */ranges= getRanges(visibleLines); | |
727 for (Iterator it= ranges.iterator(); it.hasNext();) { | |
134 | 728 RevisionRange region= cast(RevisionRange) it.next(); |
129 | 729 paintRange(region, gc); |
730 } | |
731 } | |
732 | |
733 /** | |
734 * Ensures that the column is fully instantiated, i.e. has a control, and that the viewer is | |
735 * visible. | |
736 */ | |
737 private void connectIfNeeded() { | |
738 if (isConnected() || fParentRuler is null) | |
739 return; | |
740 | |
741 fViewer= fParentRuler.getTextViewer(); | |
742 if (fViewer is null) | |
743 return; | |
744 | |
745 fWidget= fViewer.getTextWidget(); | |
746 if (fWidget is null) | |
747 return; | |
748 | |
749 fControl= fColumn.getControl(); | |
750 if (fControl is null) | |
751 return; | |
752 | |
753 fControl.addMouseTrackListener(fMouseHandler); | |
754 fControl.addMouseMoveListener(fMouseHandler); | |
755 fControl.addListener(DWT.MouseUp, fMouseHandler); | |
756 fControl.addListener(DWT.MouseDown, fMouseHandler); | |
135 | 757 fControl.addDisposeListener(new class() DisposeListener { |
129 | 758 /* |
759 * @see dwt.events.DisposeListener#widgetDisposed(dwt.events.DisposeEvent) | |
760 */ | |
761 public void widgetDisposed(DisposeEvent e) { | |
762 handleDispose(); | |
763 } | |
764 }); | |
150 | 765 |
129 | 766 fRevisionSelectionProvider.install(fViewer); |
767 } | |
768 | |
769 /** | |
770 * Returns <code>true</code> if the column is fully connected. | |
150 | 771 * |
129 | 772 * @return <code>true</code> if the column is fully connected, false otherwise |
773 */ | |
774 private bool isConnected() { | |
775 return fControl !is null; | |
776 } | |
777 | |
778 /** | |
779 * Sets the annotation model. | |
150 | 780 * |
129 | 781 * @param model the annotation model, possibly <code>null</code> |
782 * @see IVerticalRulerColumn#setModel(IAnnotationModel) | |
783 */ | |
784 public void setModel(IAnnotationModel model) { | |
785 IAnnotationModel diffModel; | |
138 | 786 if ( cast(IAnnotationModelExtension)model ) |
134 | 787 diffModel= (cast(IAnnotationModelExtension) model).getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID); |
129 | 788 else |
789 diffModel= model; | |
790 | |
791 setDiffer(diffModel); | |
792 setAnnotationModel(model); | |
793 } | |
794 | |
795 /** | |
796 * Sets the annotation model. | |
150 | 797 * |
129 | 798 * @param model the annotation model. |
799 */ | |
800 private void setAnnotationModel(IAnnotationModel model) { | |
801 if (fAnnotationModel !is model) | |
802 fAnnotationModel= model; | |
803 } | |
804 | |
805 /** | |
806 * Sets the line differ. | |
150 | 807 * |
129 | 808 * @param differ the line differ or <code>null</code> if none |
809 */ | |
810 private void setDiffer(IAnnotationModel differ) { | |
142 | 811 if ( cast(ILineDiffer)differ || differ is null) { |
129 | 812 if (fLineDiffer !is differ) { |
813 if (fLineDiffer !is null) | |
134 | 814 (cast(IAnnotationModel) fLineDiffer).removeAnnotationModelListener(fAnnotationListener); |
815 fLineDiffer= cast(ILineDiffer) differ; | |
129 | 816 if (fLineDiffer !is null) |
134 | 817 (cast(IAnnotationModel) fLineDiffer).addAnnotationModelListener(fAnnotationListener); |
129 | 818 } |
819 } | |
820 } | |
821 | |
822 /** | |
823 * Disposes of the painter's resources. | |
824 */ | |
825 private void handleDispose() { | |
826 updateFocusLine(-1); | |
827 | |
828 if (fLineDiffer !is null) { | |
134 | 829 (cast(IAnnotationModel) fLineDiffer).removeAnnotationModelListener(fAnnotationListener); |
129 | 830 fLineDiffer= null; |
831 } | |
150 | 832 |
129 | 833 fRevisionSelectionProvider.uninstall(); |
834 } | |
835 | |
836 /** | |
837 * Paints a single change region onto <code>gc</code>. | |
150 | 838 * |
129 | 839 * @param range the range to paint |
840 * @param gc the {@link GC} to paint on | |
841 */ | |
842 private void paintRange(RevisionRange range, GC gc) { | |
843 ILineRange widgetRange= modelLinesToWidgetLines(range); | |
844 if (widgetRange is null) | |
845 return; | |
846 | |
847 Revision revision= range.getRevision(); | |
848 bool drawArmedFocus= range is fMouseHandler.fMouseDownRegion; | |
849 bool drawSelection= !drawArmedFocus && revision is fSelectedRevision; | |
850 bool drawFocus= !drawSelection && !drawArmedFocus && revision is fFocusRevision; | |
851 Rectangle box= computeBoxBounds(widgetRange); | |
852 | |
853 gc.setBackground(lookupColor(revision, false)); | |
854 if (drawArmedFocus) { | |
855 Color foreground= gc.getForeground(); | |
856 Color focusColor= lookupColor(revision, true); | |
857 gc.setForeground(focusColor); | |
858 gc.fillRectangle(box); | |
859 gc.drawRectangle(box.x, box.y, box.width - 1, box.height - 1); // highlight box | |
860 gc.drawRectangle(box.x + 1, box.y + 1, box.width - 3, box.height - 3); // inner highlight box | |
861 gc.setForeground(foreground); | |
862 } else if (drawFocus || drawSelection) { | |
863 Color foreground= gc.getForeground(); | |
864 Color focusColor= lookupColor(revision, true); | |
865 gc.setForeground(focusColor); | |
866 gc.fillRectangle(box); | |
867 gc.drawRectangle(box.x, box.y, box.width - 1, box.height - 1); // highlight box | |
868 gc.setForeground(foreground); | |
869 } else { | |
870 gc.fillRectangle(box); | |
871 } | |
872 | |
873 if ((fShowAuthor || fShowRevision)) { | |
874 int indentation= 1; | |
875 int baselineBias= getBaselineBias(gc, widgetRange.getStartLine()); | |
876 if (fShowAuthor && fShowRevision) { | |
877 gc.drawString(revision.getId(), indentation, box.y + baselineBias, true); | |
878 gc.drawString(revision.getAuthor(), fAuthorInset, box.y + baselineBias, true); | |
879 } else if (fShowAuthor) { | |
880 gc.drawString(revision.getAuthor(), indentation, box.y + baselineBias, true); | |
881 } else if (fShowRevision) { | |
882 gc.drawString(revision.getId(), indentation, box.y + baselineBias, true); | |
883 } | |
884 } | |
885 } | |
886 | |
887 /** | |
888 * Returns the difference between the baseline of the widget and the | |
889 * baseline as specified by the font for <code>gc</code>. When drawing | |
890 * line numbers, the returned bias should be added to obtain text lined up | |
891 * on the correct base line of the text widget. | |
892 * | |
893 * @param gc the <code>GC</code> to get the font metrics from | |
894 * @param widgetLine the widget line | |
895 * @return the baseline bias to use when drawing text that is lined up with | |
896 * <code>fCachedTextWidget</code> | |
897 * @since 3.3 | |
898 */ | |
899 private int getBaselineBias(GC gc, int widgetLine) { | |
900 /* | |
901 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=62951 | |
902 * widget line height may be more than the font height used for the | |
903 * line numbers, since font styles (bold, italics...) can have larger | |
904 * font metrics than the simple font used for the numbers. | |
905 */ | |
906 int offset= fWidget.getOffsetAtLine(widgetLine); | |
907 int widgetBaseline= fWidget.getBaseline(offset); | |
150 | 908 |
129 | 909 FontMetrics fm = gc.getFontMetrics(); |
910 int fontBaseline = fm.getAscent() + fm.getLeading(); | |
911 int baselineBias= widgetBaseline - fontBaseline; | |
912 return Math.max(0, baselineBias); | |
913 } | |
150 | 914 |
129 | 915 /** |
916 * Looks up the color for a certain revision. | |
150 | 917 * |
129 | 918 * @param revision the revision to get the color for |
919 * @param focus <code>true</code> if it is the focus revision | |
920 * @return the color for the revision | |
921 */ | |
922 private Color lookupColor(Revision revision, bool focus) { | |
923 return fSharedColors.getColor(fColorTool.getColor(revision, focus)); | |
924 } | |
925 | |
926 /** | |
927 * Returns the revision range that contains the given line, or | |
928 * <code>null</code> if there is none. | |
150 | 929 * |
129 | 930 * @param line the line of interest |
931 * @return the corresponding <code>RevisionRange</code> or <code>null</code> | |
932 */ | |
933 private RevisionRange getRange(int line) { | |
934 List ranges= getRangeCache(); | |
935 | |
936 if (ranges.isEmpty() || line is -1) | |
937 return null; | |
938 | |
939 for (Iterator it= ranges.iterator(); it.hasNext();) { | |
134 | 940 RevisionRange range= cast(RevisionRange) it.next(); |
129 | 941 if (contains(range, line)) |
942 return range; | |
943 } | |
944 | |
945 // line may be right after the last region | |
134 | 946 RevisionRange lastRegion= cast(RevisionRange) ranges.get(ranges.size() - 1); |
129 | 947 if (line is end(lastRegion)) |
948 return lastRegion; | |
949 return null; | |
950 } | |
951 | |
952 /** | |
953 * Returns the sublist of all <code>RevisionRange</code>s that intersect with the given lines. | |
150 | 954 * |
129 | 955 * @param lines the model based lines of interest |
956 * @return elementType: RevisionRange | |
957 */ | |
958 private List getRanges(ILineRange lines) { | |
959 List ranges= getRangeCache(); | |
960 | |
961 // return the interesting subset | |
962 int end= end(lines); | |
963 int first= -1, last= -1; | |
964 for (int i= 0; i < ranges.size(); i++) { | |
134 | 965 RevisionRange range= cast(RevisionRange) ranges.get(i); |
129 | 966 int rangeEnd= end(range); |
967 if (first is -1 && rangeEnd > lines.getStartLine()) | |
968 first= i; | |
969 if (first !is -1 && rangeEnd > end) { | |
970 last= i; | |
971 break; | |
972 } | |
973 } | |
974 if (first is -1) | |
975 return Collections.EMPTY_LIST; | |
976 if (last is -1) | |
977 last= ranges.size() - 1; // bottom index may be one too much | |
978 | |
979 return ranges.subList(first, last + 1); | |
980 } | |
981 | |
982 /** | |
983 * Gets all change ranges of the revisions in the revision model and adapts them to the current | |
984 * quick diff information. The list is cached. | |
150 | 985 * |
129 | 986 * @return the list of all change regions, with diff information applied |
987 */ | |
988 private List getRangeCache() { | |
989 if (fRevisionRanges is null) { | |
990 if (fRevisionInfo is null) { | |
991 fRevisionRanges= Collections.EMPTY_LIST; | |
992 } else { | |
993 Hunk[] hunks= HunkComputer.computeHunks(fLineDiffer, fViewer.getDocument().getNumberOfLines()); | |
994 fRevisionInfo.applyDiff(hunks); | |
995 fRevisionRanges= fRevisionInfo.getRanges(); | |
996 updateOverviewAnnotations(); | |
997 informListeners(); | |
998 } | |
999 } | |
1000 | |
1001 return fRevisionRanges; | |
1002 } | |
1003 | |
1004 /** | |
1005 * Clears the range cache. | |
1006 * | |
1007 * @since 3.3 | |
1008 */ | |
1009 private void clearRangeCache() { | |
1010 fRevisionRanges= null; | |
1011 } | |
150 | 1012 |
129 | 1013 /** |
1014 * Returns <code>true</code> if <code>range</code> contains <code>line</code>. A line is | |
1015 * not contained in a range if it is the range's exclusive end line. | |
150 | 1016 * |
129 | 1017 * @param range the range to check whether it contains <code>line</code> |
1018 * @param line the line the line | |
1019 * @return <code>true</code> if <code>range</code> contains <code>line</code>, | |
1020 * <code>false</code> if not | |
1021 */ | |
1022 private static bool contains(ILineRange range, int line) { | |
1023 return range.getStartLine() <= line && end(range) > line; | |
1024 } | |
1025 | |
1026 /** | |
1027 * Computes the end index of a line range. | |
150 | 1028 * |
129 | 1029 * @param range a line range |
1030 * @return the last line (exclusive) of <code>range</code> | |
1031 */ | |
1032 private static int end(ILineRange range) { | |
1033 return range.getStartLine() + range.getNumberOfLines(); | |
1034 } | |
1035 | |
1036 /** | |
1037 * Returns the visible extent of a document line range in widget lines. | |
150 | 1038 * |
129 | 1039 * @param range the document line range |
1040 * @return the visible extent of <code>range</code> in widget lines | |
1041 */ | |
1042 private ILineRange modelLinesToWidgetLines(ILineRange range) { | |
1043 int widgetStartLine= -1; | |
1044 int widgetEndLine= -1; | |
138 | 1045 if ( cast(ITextViewerExtension5)fViewer ) { |
134 | 1046 ITextViewerExtension5 extension= cast(ITextViewerExtension5) fViewer; |
129 | 1047 int modelEndLine= end(range); |
1048 for (int modelLine= range.getStartLine(); modelLine < modelEndLine; modelLine++) { | |
1049 int widgetLine= extension.modelLine2WidgetLine(modelLine); | |
1050 if (widgetLine !is -1) { | |
1051 if (widgetStartLine is -1) | |
1052 widgetStartLine= widgetLine; | |
1053 widgetEndLine= widgetLine; | |
1054 } | |
1055 } | |
1056 } else { | |
1057 IRegion region= fViewer.getVisibleRegion(); | |
1058 IDocument document= fViewer.getDocument(); | |
1059 try { | |
1060 int visibleStartLine= document.getLineOfOffset(region.getOffset()); | |
1061 int visibleEndLine= document.getLineOfOffset(region.getOffset() + region.getLength()); | |
1062 widgetStartLine= Math.max(0, range.getStartLine() - visibleStartLine); | |
1063 widgetEndLine= Math.min(visibleEndLine, end(range) - 1); | |
1064 } catch (BadLocationException x) { | |
1065 x.printStackTrace(); | |
1066 // ignore and return null | |
1067 } | |
1068 } | |
1069 if (widgetStartLine is -1 || widgetEndLine is -1) | |
1070 return null; | |
1071 return new LineRange(widgetStartLine, widgetEndLine - widgetStartLine + 1); | |
1072 } | |
1073 | |
1074 /** | |
1075 * Returns the revision hover. | |
150 | 1076 * |
129 | 1077 * @return the revision hover |
1078 */ | |
1079 public IAnnotationHover getHover() { | |
1080 return fHover; | |
1081 } | |
1082 | |
1083 /** | |
1084 * Computes and returns the bounds of the rectangle corresponding to a widget line range. The | |
1085 * rectangle is in pixel coordinates relative to the text widget's | |
1086 * {@link StyledText#getClientArea() client area} and has the width of the ruler. | |
150 | 1087 * |
129 | 1088 * @param range the widget line range |
1089 * @return the box bounds corresponding to <code>range</code> | |
1090 */ | |
1091 private Rectangle computeBoxBounds(ILineRange range) { | |
1092 int y1= fWidget.getLinePixel(range.getStartLine()); | |
1093 int y2= fWidget.getLinePixel(range.getStartLine() + range.getNumberOfLines()); | |
1094 | |
1095 return new Rectangle(0, y1, getWidth(), y2 - y1 - 1); | |
1096 } | |
1097 | |
1098 /** | |
1099 * Shows (or hides) the overview annotations. | |
1100 */ | |
1101 private void updateOverviewAnnotations() { | |
1102 if (fAnnotationModel is null) | |
1103 return; | |
150 | 1104 |
129 | 1105 Revision revision= fFocusRevision !is null ? fFocusRevision : fSelectedRevision; |
1106 | |
1107 Map added= null; | |
1108 if (revision !is null) { | |
1109 added= new HashMap(); | |
1110 for (Iterator it= revision.getRegions().iterator(); it.hasNext();) { | |
134 | 1111 RevisionRange range= cast(RevisionRange) it.next(); |
129 | 1112 try { |
1113 IRegion charRegion= toCharRegion(range); | |
1114 Position position= new Position(charRegion.getOffset(), charRegion.getLength()); | |
1115 Annotation annotation= new RevisionAnnotation(revision.getId()); | |
1116 added.put(annotation, position); | |
1117 } catch (BadLocationException x) { | |
1118 // ignore - document was changed, show no annotations | |
1119 } | |
1120 } | |
1121 } | |
1122 | |
138 | 1123 if ( cast(IAnnotationModelExtension)fAnnotationModel ) { |
134 | 1124 IAnnotationModelExtension ext= cast(IAnnotationModelExtension) fAnnotationModel; |
158 | 1125 ext.replaceAnnotations(arraycast!(Annotation)( fAnnotations.toArray()), added); |
129 | 1126 } else { |
1127 for (Iterator it= fAnnotations.iterator(); it.hasNext();) { | |
134 | 1128 Annotation annotation= cast(Annotation) it.next(); |
129 | 1129 fAnnotationModel.removeAnnotation(annotation); |
1130 } | |
1131 if (added !is null) { | |
1132 for (Iterator it= added.entrySet().iterator(); it.hasNext();) { | |
134 | 1133 Entry entry= cast(Entry) it.next(); |
1134 fAnnotationModel.addAnnotation(cast(Annotation) entry.getKey(), cast(Position) entry.getValue()); | |
129 | 1135 } |
1136 } | |
1137 } | |
1138 fAnnotations.clear(); | |
1139 if (added !is null) | |
1140 fAnnotations.addAll(added.keySet()); | |
1141 | |
1142 } | |
1143 | |
1144 /** | |
1145 * Returns the character offset based region of a line range. | |
150 | 1146 * |
129 | 1147 * @param lines the line range to convert |
1148 * @return the character offset range corresponding to <code>range</code> | |
1149 * @throws BadLocationException if the line range is not within the document bounds | |
1150 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
135
diff
changeset
|
1151 private IRegion toCharRegion(ILineRange lines) { |
129 | 1152 IDocument document= fViewer.getDocument(); |
1153 int offset= document.getLineOffset(lines.getStartLine()); | |
1154 int nextLine= end(lines); | |
1155 int endOffset; | |
1156 if (nextLine >= document.getNumberOfLines()) | |
1157 endOffset= document.getLength(); | |
1158 else | |
1159 endOffset= document.getLineOffset(nextLine); | |
1160 return new Region(offset, endOffset - offset); | |
1161 } | |
1162 | |
1163 /** | |
1164 * Handles the selection of a revision and informs listeners. | |
150 | 1165 * |
129 | 1166 * @param revision the selected revision, <code>null</code> for none |
1167 */ | |
1168 void handleRevisionSelected(Revision revision) { | |
1169 fSelectedRevision= revision; | |
1170 fRevisionSelectionProvider.revisionSelected(revision); | |
1171 updateOverviewAnnotations(); | |
1172 postRedraw(); | |
1173 } | |
1174 | |
1175 /** | |
1176 * Handles the selection of a revision id and informs listeners | |
150 | 1177 * |
129 | 1178 * @param id the selected revision id |
1179 */ | |
1180 void handleRevisionSelected(String id) { | |
1181 Assert.isLegal(id !is null); | |
1182 if (fRevisionInfo is null) | |
1183 return; | |
1184 | |
1185 for (Iterator it= fRevisionInfo.getRevisions().iterator(); it.hasNext();) { | |
134 | 1186 Revision revision= cast(Revision) it.next(); |
129 | 1187 if (id.equals(revision.getId())) { |
1188 handleRevisionSelected(revision); | |
1189 return; | |
1190 } | |
1191 } | |
1192 | |
1193 // clear selection if it does not exist | |
134 | 1194 handleRevisionSelected(cast(Revision) null); |
129 | 1195 } |
1196 | |
1197 /** | |
1198 * Returns the selection provider. | |
150 | 1199 * |
129 | 1200 * @return the selection provider |
1201 */ | |
1202 public RevisionSelectionProvider getRevisionSelectionProvider() { | |
1203 return fRevisionSelectionProvider; | |
1204 } | |
1205 | |
1206 /** | |
1207 * Updates the focus line with a new line. | |
150 | 1208 * |
129 | 1209 * @param line the new focus line, -1 for no focus |
1210 */ | |
1211 private void updateFocusLine(int line) { | |
1212 if (fFocusLine !is line) | |
1213 onFocusLineChanged(fFocusLine, line); | |
1214 } | |
1215 | |
1216 /** | |
1217 * Handles a changing focus line. | |
150 | 1218 * |
129 | 1219 * @param previousLine the old focus line (-1 for no focus) |
1220 * @param nextLine the new focus line (-1 for no focus) | |
1221 */ | |
1222 private void onFocusLineChanged(int previousLine, int nextLine) { | |
150 | 1223 if (DEBUG) |
1224 System.out_.println("line: " ~ previousLine ~ " > " ~ nextLine); //$NON-NLS-1$ //$NON-NLS-2$ | |
129 | 1225 fFocusLine= nextLine; |
1226 RevisionRange region= getRange(nextLine); | |
1227 updateFocusRange(region); | |
1228 } | |
1229 | |
1230 /** | |
1231 * Updates the focus range. | |
150 | 1232 * |
129 | 1233 * @param range the new focus range, <code>null</code> for no focus |
1234 */ | |
1235 private void updateFocusRange(RevisionRange range) { | |
1236 if (range !is fFocusRange) | |
1237 onFocusRangeChanged(fFocusRange, range); | |
1238 } | |
1239 | |
1240 /** | |
1241 * Handles a changing focus range. | |
150 | 1242 * |
129 | 1243 * @param previousRange the old focus range (<code>null</code> for no focus) |
1244 * @param nextRange the new focus range (<code>null</code> for no focus) | |
1245 */ | |
1246 private void onFocusRangeChanged(RevisionRange previousRange, RevisionRange nextRange) { | |
150 | 1247 if (DEBUG) |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
139
diff
changeset
|
1248 System.out_.println("range: " + previousRange + " > " + nextRange); //$NON-NLS-1$ //$NON-NLS-2$ |
129 | 1249 fFocusRange= nextRange; |
1250 Revision revision= nextRange is null ? null : nextRange.getRevision(); | |
1251 updateFocusRevision(revision); | |
1252 } | |
1253 | |
1254 private void updateFocusRevision(Revision revision) { | |
1255 if (fFocusRevision !is revision) | |
1256 onFocusRevisionChanged(fFocusRevision, revision); | |
1257 } | |
1258 | |
1259 /** | |
1260 * Handles a changing focus revision. | |
150 | 1261 * |
129 | 1262 * @param previousRevision the old focus revision (<code>null</code> for no focus) |
1263 * @param nextRevision the new focus revision (<code>null</code> for no focus) | |
1264 */ | |
1265 private void onFocusRevisionChanged(Revision previousRevision, Revision nextRevision) { | |
150 | 1266 if (DEBUG) |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
139
diff
changeset
|
1267 System.out_.println("revision: " + previousRevision + " > " + nextRevision); //$NON-NLS-1$ //$NON-NLS-2$ |
129 | 1268 fFocusRevision= nextRevision; |
1269 uninstallWheelHandler(); | |
1270 installWheelHandler(); | |
1271 updateOverviewAnnotations(); | |
1272 redraw(); // pick up new highlights | |
1273 } | |
1274 | |
1275 /** | |
1276 * Uninstalls the mouse wheel handler. | |
1277 */ | |
1278 private void uninstallWheelHandler() { | |
1279 fControl.removeListener(DWT.MouseWheel, fMouseHandler); | |
1280 fWheelHandlerInstalled= false; | |
1281 } | |
1282 | |
1283 /** | |
1284 * Installs the mouse wheel handler. | |
1285 */ | |
1286 private void installWheelHandler() { | |
1287 if (fFocusRevision !is null && !fWheelHandlerInstalled) { | |
1288 //FIXME: does not work on Windows, because Canvas cannot get focus and therefore does not send out mouse wheel events: | |
1289 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=81189 | |
1290 //see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=75766 | |
1291 fControl.addListener(DWT.MouseWheel, fMouseHandler); | |
1292 fWheelHandlerInstalled= true; | |
1293 } | |
1294 } | |
1295 | |
1296 /** | |
1297 * Handles a mouse wheel event. | |
150 | 1298 * |
129 | 1299 * @param event the mouse wheel event |
1300 */ | |
1301 private void handleMouseWheel(Event event) { | |
1302 bool up= event.count > 0; | |
1303 int documentHoverLine= fFocusLine; | |
1304 | |
1305 ILineRange nextWidgetRange= null; | |
1306 ILineRange last= null; | |
1307 List ranges= fFocusRevision.getRegions(); | |
1308 if (up) { | |
1309 for (Iterator it= ranges.iterator(); it.hasNext();) { | |
134 | 1310 RevisionRange range= cast(RevisionRange) it.next(); |
129 | 1311 ILineRange widgetRange= modelLinesToWidgetLines(range); |
1312 if (contains(range, documentHoverLine)) { | |
1313 nextWidgetRange= last; | |
1314 break; | |
1315 } | |
1316 if (widgetRange !is null) | |
1317 last= widgetRange; | |
1318 } | |
1319 } else { | |
1320 for (ListIterator it= ranges.listIterator(ranges.size()); it.hasPrevious();) { | |
134 | 1321 RevisionRange range= cast(RevisionRange) it.previous(); |
129 | 1322 ILineRange widgetRange= modelLinesToWidgetLines(range); |
1323 if (contains(range, documentHoverLine)) { | |
1324 nextWidgetRange= last; | |
1325 break; | |
1326 } | |
1327 if (widgetRange !is null) | |
1328 last= widgetRange; | |
1329 } | |
1330 } | |
1331 | |
1332 if (nextWidgetRange is null) | |
1333 return; | |
1334 | |
1335 int widgetCurrentFocusLine= modelLinesToWidgetLines(new LineRange(documentHoverLine, 1)).getStartLine(); | |
1336 int widgetNextFocusLine= nextWidgetRange.getStartLine(); | |
1337 int newTopPixel= fWidget.getTopPixel() + JFaceTextUtil.computeLineHeight(fWidget, widgetCurrentFocusLine, widgetNextFocusLine, widgetNextFocusLine - widgetCurrentFocusLine); | |
1338 fWidget.setTopPixel(newTopPixel); | |
1339 if (newTopPixel < 0) { | |
1340 Point cursorLocation= fWidget.getDisplay().getCursorLocation(); | |
1341 cursorLocation.y+= newTopPixel; | |
1342 fWidget.getDisplay().setCursorLocation(cursorLocation); | |
1343 } else { | |
1344 int topPixel= fWidget.getTopPixel(); | |
1345 if (topPixel < newTopPixel) { | |
1346 Point cursorLocation= fWidget.getDisplay().getCursorLocation(); | |
1347 cursorLocation.y+= newTopPixel - topPixel; | |
1348 fWidget.getDisplay().setCursorLocation(cursorLocation); | |
1349 } | |
1350 } | |
1351 updateFocusLine(toDocumentLineNumber(fWidget.toControl(fWidget.getDisplay().getCursorLocation()).y)); | |
1352 immediateUpdate(); | |
1353 } | |
1354 | |
1355 /** | |
1356 * Triggers a redraw in the display thread. | |
1357 */ | |
1358 private final void postRedraw() { | |
1359 if (isConnected() && !fControl.isDisposed()) { | |
1360 Display d= fControl.getDisplay(); | |
1361 if (d !is null) { | |
135 | 1362 d.asyncExec(new class() Runnable { |
129 | 1363 public void run() { |
1364 redraw(); | |
1365 } | |
1366 }); | |
1367 } | |
1368 } | |
1369 } | |
1370 | |
1371 /** | |
1372 * Translates a y coordinate in the pixel coordinates of the column's control to a document line | |
1373 * number. | |
150 | 1374 * |
129 | 1375 * @param y the y coordinate |
1376 * @return the corresponding document line, -1 for no line | |
1377 * @see CompositeRuler#toDocumentLineNumber(int) | |
1378 */ | |
1379 private int toDocumentLineNumber(int y) { | |
1380 return fParentRuler.toDocumentLineNumber(y); | |
1381 } | |
1382 | |
1383 /** | |
1384 * Triggers redrawing of the column. | |
1385 */ | |
1386 private void redraw() { | |
1387 fColumn.redraw(); | |
1388 } | |
1389 | |
1390 /** | |
1391 * Triggers immediate redrawing of the entire column - use with care. | |
1392 */ | |
1393 private void immediateUpdate() { | |
1394 fParentRuler.immediateUpdate(); | |
1395 } | |
1396 | |
1397 /** | |
1398 * Returns the width of the column. | |
150 | 1399 * |
129 | 1400 * @return the width of the column |
1401 */ | |
1402 private int getWidth() { | |
1403 return fColumn.getWidth(); | |
1404 } | |
1405 | |
1406 /** | |
1407 * Returns the System background color for list widgets. | |
150 | 1408 * |
129 | 1409 * @return the System background color for list widgets |
1410 */ | |
1411 private Color getBackground() { | |
1412 if (fBackground is null) | |
1413 return fWidget.getDisplay().getSystemColor(DWT.COLOR_LIST_BACKGROUND); | |
1414 return fBackground; | |
1415 } | |
1416 | |
1417 /** | |
1418 * Sets the hover later returned by {@link #getHover()}. | |
150 | 1419 * |
129 | 1420 * @param hover the hover |
1421 */ | |
1422 public void setHover(IAnnotationHover hover) { | |
1423 // TODO ignore for now - must make revision hover settable from outside | |
1424 } | |
1425 | |
1426 /** | |
1427 * Returns <code>true</code> if the receiver can provide a hover for a certain document line. | |
150 | 1428 * |
129 | 1429 * @param activeLine the document line of interest |
1430 * @return <code>true</code> if the receiver can provide a hover | |
1431 */ | |
1432 public bool hasHover(int activeLine) { | |
139
93a6ec48fd28
Regexp throws removal in interfaces
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
1433 return cast(ISourceViewer)fViewer && fHover.getHoverLineRange(cast(ISourceViewer) fViewer, activeLine) !is null; |
129 | 1434 } |
1435 | |
1436 /** | |
1437 * Returns the revision at a certain document offset, or <code>null</code> for none. | |
150 | 1438 * |
129 | 1439 * @param offset the document offset |
1440 * @return the revision at offset, or <code>null</code> for none | |
1441 */ | |
1442 Revision getRevision(int offset) { | |
1443 IDocument document= fViewer.getDocument(); | |
1444 int line; | |
1445 try { | |
1446 line= document.getLineOfOffset(offset); | |
1447 } catch (BadLocationException x) { | |
1448 return null; | |
1449 } | |
1450 if (line !is -1) { | |
1451 RevisionRange range= getRange(line); | |
1452 if (range !is null) | |
1453 return range.getRevision(); | |
1454 } | |
1455 return null; | |
1456 } | |
1457 | |
1458 /** | |
1459 * Returns <code>true</code> if a revision model has been set, <code>false</code> otherwise. | |
150 | 1460 * |
129 | 1461 * @return <code>true</code> if a revision model has been set, <code>false</code> otherwise |
1462 */ | |
1463 public bool hasInformation() { | |
1464 return fRevisionInfo !is null; | |
1465 } | |
1466 | |
1467 /** | |
1468 * Returns the width in chars required to display information. | |
150 | 1469 * |
129 | 1470 * @return the width in chars required to display information |
1471 * @since 3.3 | |
1472 */ | |
1473 public int getRequiredWidth() { | |
1474 if (fRequiredWidth is -1) { | |
1475 if (hasInformation() && (fShowRevision || fShowAuthor)) { | |
1476 int revisionWidth= 0; | |
1477 int authorWidth= 0; | |
1478 for (Iterator it= fRevisionInfo.getRevisions().iterator(); it.hasNext();) { | |
134 | 1479 Revision revision= cast(Revision) it.next(); |
129 | 1480 revisionWidth= Math.max(revisionWidth, revision.getId().length()); |
1481 authorWidth= Math.max(authorWidth, revision.getAuthor().length()); | |
1482 } | |
1483 fRevisionIdChars= revisionWidth + 1; | |
1484 if (fShowAuthor && fShowRevision) | |
1485 fRequiredWidth= revisionWidth + authorWidth + 2; | |
1486 else if (fShowAuthor) | |
1487 fRequiredWidth= authorWidth + 1; | |
1488 else | |
1489 fRequiredWidth= revisionWidth + 1; | |
1490 } else { | |
1491 fRequiredWidth= 0; | |
1492 } | |
1493 } | |
1494 return fRequiredWidth; | |
1495 } | |
1496 | |
1497 /** | |
1498 * Enables showing the revision id. | |
150 | 1499 * |
129 | 1500 * @param show <code>true</code> to show the revision, <code>false</code> to hide it |
1501 */ | |
1502 public void showRevisionId(bool show) { | |
1503 if (fShowRevision !is show) { | |
1504 fRequiredWidth= -1; | |
1505 fRevisionIdChars= 0; | |
1506 fShowRevision= show; | |
1507 postRedraw(); | |
1508 } | |
1509 } | |
150 | 1510 |
129 | 1511 /** |
1512 * Enables showing the revision author. | |
150 | 1513 * |
129 | 1514 * @param show <code>true</code> to show the author, <code>false</code> to hide it |
1515 */ | |
1516 public void showRevisionAuthor(bool show) { | |
1517 if (fShowAuthor !is show) { | |
1518 fRequiredWidth= -1; | |
1519 fRevisionIdChars= 0; | |
1520 fShowAuthor= show; | |
1521 postRedraw(); | |
1522 } | |
1523 } | |
1524 | |
1525 /** | |
1526 * Adds a revision listener. | |
150 | 1527 * |
129 | 1528 * @param listener the listener |
1529 * @since 3.3 | |
1530 */ | |
1531 public void addRevisionListener(IRevisionListener listener) { | |
1532 fRevisionListeners.add(listener); | |
1533 } | |
1534 | |
1535 /** | |
1536 * Removes a revision listener. | |
150 | 1537 * |
129 | 1538 * @param listener the listener |
1539 * @since 3.3 | |
1540 */ | |
1541 public void removeRevisionListener(IRevisionListener listener) { | |
1542 fRevisionListeners.remove(listener); | |
1543 } | |
1544 | |
1545 /** | |
1546 * Informs the revision listeners about a change. | |
150 | 1547 * |
129 | 1548 * @since 3.3 |
1549 */ | |
1550 private void informListeners() { | |
1551 if (fRevisionInfo is null || fRevisionListeners.isEmpty()) | |
1552 return; | |
1553 | |
1554 RevisionEvent event= new RevisionEvent(fRevisionInfo); | |
1555 Object[] listeners= fRevisionListeners.getListeners(); | |
1556 for (int i= 0; i < listeners.length; i++) { | |
134 | 1557 IRevisionListener listener= cast(IRevisionListener) listeners[i]; |
129 | 1558 listener.revisionInformationChanged(event); |
1559 } | |
1560 } | |
1561 } |