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