129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.source.MatchingCharacterPainter;
|
|
14
|
131
|
15 import dwtx.jface.text.source.ISharedTextColors; // packageimport
|
|
16 import dwtx.jface.text.source.ILineRange; // packageimport
|
|
17 import dwtx.jface.text.source.IAnnotationPresentation; // packageimport
|
|
18 import dwtx.jface.text.source.IVerticalRulerInfoExtension; // packageimport
|
|
19 import dwtx.jface.text.source.ICharacterPairMatcher; // packageimport
|
|
20 import dwtx.jface.text.source.TextInvocationContext; // packageimport
|
|
21 import dwtx.jface.text.source.LineChangeHover; // packageimport
|
|
22 import dwtx.jface.text.source.IChangeRulerColumn; // packageimport
|
|
23 import dwtx.jface.text.source.IAnnotationMap; // packageimport
|
|
24 import dwtx.jface.text.source.IAnnotationModelListenerExtension; // packageimport
|
|
25 import dwtx.jface.text.source.ISourceViewerExtension2; // packageimport
|
|
26 import dwtx.jface.text.source.IAnnotationHover; // packageimport
|
|
27 import dwtx.jface.text.source.ContentAssistantFacade; // packageimport
|
|
28 import dwtx.jface.text.source.IAnnotationAccess; // packageimport
|
|
29 import dwtx.jface.text.source.IVerticalRulerExtension; // packageimport
|
|
30 import dwtx.jface.text.source.IVerticalRulerColumn; // packageimport
|
|
31 import dwtx.jface.text.source.LineNumberRulerColumn; // packageimport
|
|
32 import dwtx.jface.text.source.IAnnotationModelExtension; // packageimport
|
|
33 import dwtx.jface.text.source.ILineDifferExtension; // packageimport
|
|
34 import dwtx.jface.text.source.DefaultCharacterPairMatcher; // packageimport
|
|
35 import dwtx.jface.text.source.LineNumberChangeRulerColumn; // packageimport
|
|
36 import dwtx.jface.text.source.IAnnotationAccessExtension; // packageimport
|
|
37 import dwtx.jface.text.source.ISourceViewer; // packageimport
|
|
38 import dwtx.jface.text.source.AnnotationModel; // packageimport
|
|
39 import dwtx.jface.text.source.ILineDifferExtension2; // packageimport
|
|
40 import dwtx.jface.text.source.IAnnotationModelListener; // packageimport
|
|
41 import dwtx.jface.text.source.IVerticalRuler; // packageimport
|
|
42 import dwtx.jface.text.source.DefaultAnnotationHover; // packageimport
|
|
43 import dwtx.jface.text.source.SourceViewer; // packageimport
|
|
44 import dwtx.jface.text.source.SourceViewerConfiguration; // packageimport
|
|
45 import dwtx.jface.text.source.AnnotationBarHoverManager; // packageimport
|
|
46 import dwtx.jface.text.source.CompositeRuler; // packageimport
|
|
47 import dwtx.jface.text.source.ImageUtilities; // packageimport
|
|
48 import dwtx.jface.text.source.VisualAnnotationModel; // packageimport
|
|
49 import dwtx.jface.text.source.IAnnotationModel; // packageimport
|
|
50 import dwtx.jface.text.source.ISourceViewerExtension3; // packageimport
|
|
51 import dwtx.jface.text.source.ILineDiffInfo; // packageimport
|
|
52 import dwtx.jface.text.source.VerticalRulerEvent; // packageimport
|
|
53 import dwtx.jface.text.source.ChangeRulerColumn; // packageimport
|
|
54 import dwtx.jface.text.source.ILineDiffer; // packageimport
|
|
55 import dwtx.jface.text.source.AnnotationModelEvent; // packageimport
|
|
56 import dwtx.jface.text.source.AnnotationColumn; // packageimport
|
|
57 import dwtx.jface.text.source.AnnotationRulerColumn; // packageimport
|
|
58 import dwtx.jface.text.source.IAnnotationHoverExtension; // packageimport
|
|
59 import dwtx.jface.text.source.AbstractRulerColumn; // packageimport
|
|
60 import dwtx.jface.text.source.ISourceViewerExtension; // packageimport
|
|
61 import dwtx.jface.text.source.AnnotationMap; // packageimport
|
|
62 import dwtx.jface.text.source.IVerticalRulerInfo; // packageimport
|
|
63 import dwtx.jface.text.source.IAnnotationModelExtension2; // packageimport
|
|
64 import dwtx.jface.text.source.LineRange; // packageimport
|
|
65 import dwtx.jface.text.source.IAnnotationAccessExtension2; // packageimport
|
|
66 import dwtx.jface.text.source.VerticalRuler; // packageimport
|
|
67 import dwtx.jface.text.source.JFaceTextMessages; // packageimport
|
|
68 import dwtx.jface.text.source.IOverviewRuler; // packageimport
|
|
69 import dwtx.jface.text.source.Annotation; // packageimport
|
|
70 import dwtx.jface.text.source.IVerticalRulerListener; // packageimport
|
|
71 import dwtx.jface.text.source.ISourceViewerExtension4; // packageimport
|
|
72 import dwtx.jface.text.source.AnnotationPainter; // packageimport
|
|
73 import dwtx.jface.text.source.IAnnotationHoverExtension2; // packageimport
|
|
74 import dwtx.jface.text.source.OverviewRuler; // packageimport
|
|
75 import dwtx.jface.text.source.OverviewRulerHoverManager; // packageimport
|
|
76
|
|
77
|
129
|
78 import dwt.dwthelper.utils;
|
|
79
|
|
80
|
|
81
|
|
82
|
|
83 import dwt.custom.StyledText;
|
|
84 import dwt.events.PaintEvent;
|
|
85 import dwt.events.PaintListener;
|
|
86 import dwt.graphics.Color;
|
|
87 import dwt.graphics.GC;
|
|
88 import dwt.graphics.Point;
|
|
89 import dwt.graphics.Rectangle;
|
|
90 import dwtx.jface.text.BadLocationException;
|
|
91 import dwtx.jface.text.IDocument;
|
|
92 import dwtx.jface.text.IPaintPositionManager;
|
|
93 import dwtx.jface.text.IPainter;
|
|
94 import dwtx.jface.text.IRegion;
|
|
95 import dwtx.jface.text.ITextViewerExtension5;
|
|
96 import dwtx.jface.text.Position;
|
|
97 import dwtx.jface.text.Region;
|
|
98
|
|
99 /**
|
|
100 * Highlights the peer character matching the character near the caret position.
|
|
101 * This painter can be configured with an
|
|
102 * {@link dwtx.jface.text.source.ICharacterPairMatcher}.
|
|
103 * <p>
|
|
104 * Clients instantiate and configure object of this class.</p>
|
|
105 *
|
|
106 * @since 2.1
|
|
107 */
|
|
108 public final class MatchingCharacterPainter : IPainter, PaintListener {
|
|
109
|
|
110 /** Indicates whether this painter is active */
|
|
111 private bool fIsActive= false;
|
|
112 /** The source viewer this painter is associated with */
|
|
113 private ISourceViewer fSourceViewer;
|
|
114 /** The viewer's widget */
|
|
115 private StyledText fTextWidget;
|
|
116 /** The color in which to highlight the peer character */
|
|
117 private Color fColor;
|
|
118 /** The paint position manager */
|
|
119 private IPaintPositionManager fPaintPositionManager;
|
|
120 /** The strategy for finding matching characters */
|
|
121 private ICharacterPairMatcher fMatcher;
|
|
122 /** The position tracking the matching characters */
|
|
123 private Position fPairPosition= new Position(0, 0);
|
|
124 /** The anchor indicating whether the character is left or right of the caret */
|
|
125 private int fAnchor;
|
|
126
|
|
127
|
|
128 /**
|
|
129 * Creates a new MatchingCharacterPainter for the given source viewer using
|
|
130 * the given character pair matcher. The character matcher is not adopted by
|
|
131 * this painter. Thus, it is not disposed. However, this painter requires
|
|
132 * exclusive access to the given pair matcher.
|
|
133 *
|
|
134 * @param sourceViewer
|
|
135 * @param matcher
|
|
136 */
|
|
137 public MatchingCharacterPainter(ISourceViewer sourceViewer, ICharacterPairMatcher matcher) {
|
|
138 fSourceViewer= sourceViewer;
|
|
139 fMatcher= matcher;
|
|
140 fTextWidget= sourceViewer.getTextWidget();
|
|
141 }
|
|
142
|
|
143 /**
|
|
144 * Sets the color in which to highlight the match character.
|
|
145 *
|
|
146 * @param color the color
|
|
147 */
|
|
148 public void setColor(Color color) {
|
|
149 fColor= color;
|
|
150 }
|
|
151
|
|
152 /*
|
|
153 * @see dwtx.jface.text.IPainter#dispose()
|
|
154 */
|
|
155 public void dispose() {
|
|
156 if (fMatcher !is null) {
|
|
157 fMatcher.clear();
|
|
158 fMatcher= null;
|
|
159 }
|
|
160
|
|
161 fColor= null;
|
|
162 fTextWidget= null;
|
|
163 }
|
|
164
|
|
165 /*
|
|
166 * @see dwtx.jface.text.IPainter#deactivate(bool)
|
|
167 */
|
|
168 public void deactivate(bool redraw) {
|
|
169 if (fIsActive) {
|
|
170 fIsActive= false;
|
|
171 fTextWidget.removePaintListener(this);
|
|
172 if (fPaintPositionManager !is null)
|
|
173 fPaintPositionManager.unmanagePosition(fPairPosition);
|
|
174 if (redraw)
|
|
175 handleDrawRequest(null);
|
|
176 }
|
|
177 }
|
|
178
|
|
179 /*
|
|
180 * @see dwt.events.PaintListener#paintControl(dwt.events.PaintEvent)
|
|
181 */
|
|
182 public void paintControl(PaintEvent event) {
|
|
183 if (fTextWidget !is null)
|
|
184 handleDrawRequest(event.gc);
|
|
185 }
|
|
186
|
|
187 /**
|
|
188 * Handles a redraw request.
|
|
189 *
|
|
190 * @param gc the GC to draw into.
|
|
191 */
|
|
192 private void handleDrawRequest(GC gc) {
|
|
193
|
|
194 if (fPairPosition.isDeleted)
|
|
195 return;
|
|
196
|
|
197 int offset= fPairPosition.getOffset();
|
|
198 int length= fPairPosition.getLength();
|
|
199 if (length < 1)
|
|
200 return;
|
|
201
|
|
202 if (fSourceViewer instanceof ITextViewerExtension5) {
|
|
203 ITextViewerExtension5 extension= (ITextViewerExtension5) fSourceViewer;
|
|
204 IRegion widgetRange= extension.modelRange2WidgetRange(new Region(offset, length));
|
|
205 if (widgetRange is null)
|
|
206 return;
|
|
207
|
|
208 try {
|
|
209 // don't draw if the pair position is really hidden and widgetRange just
|
|
210 // marks the coverage around it.
|
|
211 IDocument doc= fSourceViewer.getDocument();
|
|
212 int startLine= doc.getLineOfOffset(offset);
|
|
213 int endLine= doc.getLineOfOffset(offset + length);
|
|
214 if (extension.modelLine2WidgetLine(startLine) is -1 || extension.modelLine2WidgetLine(endLine) is -1)
|
|
215 return;
|
|
216 } catch (BadLocationException e) {
|
|
217 return;
|
|
218 }
|
|
219
|
|
220 offset= widgetRange.getOffset();
|
|
221 length= widgetRange.getLength();
|
|
222
|
|
223 } else {
|
|
224 IRegion region= fSourceViewer.getVisibleRegion();
|
|
225 if (region.getOffset() > offset || region.getOffset() + region.getLength() < offset + length)
|
|
226 return;
|
|
227 offset -= region.getOffset();
|
|
228 }
|
|
229
|
|
230 if (ICharacterPairMatcher.RIGHT is fAnchor)
|
|
231 draw(gc, offset, 1);
|
|
232 else
|
|
233 draw(gc, offset + length -1, 1);
|
|
234 }
|
|
235
|
|
236 /**
|
|
237 * Highlights the given widget region.
|
|
238 *
|
|
239 * @param gc the GC to draw into
|
|
240 * @param offset the offset of the widget region
|
|
241 * @param length the length of the widget region
|
|
242 */
|
|
243 private void draw(GC gc, int offset, int length) {
|
|
244 if (gc !is null) {
|
|
245
|
|
246 gc.setForeground(fColor);
|
|
247
|
|
248 Rectangle bounds;
|
|
249 if (length > 0)
|
|
250 bounds= fTextWidget.getTextBounds(offset, offset + length - 1);
|
|
251 else {
|
|
252 Point loc= fTextWidget.getLocationAtOffset(offset);
|
|
253 bounds= new Rectangle(loc.x, loc.y, 1, fTextWidget.getLineHeight(offset));
|
|
254 }
|
|
255
|
|
256 // draw box around line segment
|
|
257 gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1);
|
|
258
|
|
259 // draw box around character area
|
|
260 // int widgetBaseline= fTextWidget.getBaseline();
|
|
261 // FontMetrics fm= gc.getFontMetrics();
|
|
262 // int fontBaseline= fm.getAscent() + fm.getLeading();
|
|
263 // int fontBias= widgetBaseline - fontBaseline;
|
|
264
|
|
265 // gc.drawRectangle(left.x, left.y + fontBias, right.x - left.x - 1, fm.getHeight() - 1);
|
|
266
|
|
267 } else {
|
|
268 fTextWidget.redrawRange(offset, length, true);
|
|
269 }
|
|
270 }
|
|
271
|
|
272 /*
|
|
273 * @see dwtx.jface.text.IPainter#paint(int)
|
|
274 */
|
|
275 public void paint(int reason) {
|
|
276
|
|
277 IDocument document= fSourceViewer.getDocument();
|
|
278 if (document is null) {
|
|
279 deactivate(false);
|
|
280 return;
|
|
281 }
|
|
282
|
|
283 Point selection= fSourceViewer.getSelectedRange();
|
|
284 if (selection.y > 0) {
|
|
285 deactivate(true);
|
|
286 return;
|
|
287 }
|
|
288
|
|
289 IRegion pair= fMatcher.match(document, selection.x);
|
|
290 if (pair is null) {
|
|
291 deactivate(true);
|
|
292 return;
|
|
293 }
|
|
294
|
|
295 if (fIsActive) {
|
|
296
|
|
297 if (IPainter.CONFIGURATION is reason) {
|
|
298
|
|
299 // redraw current highlighting
|
|
300 handleDrawRequest(null);
|
|
301
|
|
302 } else if (pair.getOffset() !is fPairPosition.getOffset() ||
|
|
303 pair.getLength() !is fPairPosition.getLength() ||
|
|
304 fMatcher.getAnchor() !is fAnchor) {
|
|
305
|
|
306 // otherwise only do something if position is different
|
|
307
|
|
308 // remove old highlighting
|
|
309 handleDrawRequest(null);
|
|
310 // update position
|
|
311 fPairPosition.isDeleted= false;
|
|
312 fPairPosition.offset= pair.getOffset();
|
|
313 fPairPosition.length= pair.getLength();
|
|
314 fAnchor= fMatcher.getAnchor();
|
|
315 // apply new highlighting
|
|
316 handleDrawRequest(null);
|
|
317
|
|
318 }
|
|
319 } else {
|
|
320
|
|
321 fIsActive= true;
|
|
322
|
|
323 fPairPosition.isDeleted= false;
|
|
324 fPairPosition.offset= pair.getOffset();
|
|
325 fPairPosition.length= pair.getLength();
|
|
326 fAnchor= fMatcher.getAnchor();
|
|
327
|
|
328 fTextWidget.addPaintListener(this);
|
|
329 fPaintPositionManager.managePosition(fPairPosition);
|
|
330 handleDrawRequest(null);
|
|
331 }
|
|
332 }
|
|
333
|
|
334 /*
|
|
335 * @see dwtx.jface.text.IPainter#setPositionManager(dwtx.jface.text.IPaintPositionManager)
|
|
336 */
|
|
337 public void setPositionManager(IPaintPositionManager manager) {
|
|
338 fPaintPositionManager= manager;
|
|
339 }
|
|
340 }
|