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
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18
|
|
19
|
|
20 import dwt.custom.StyledText;
|
|
21 import dwt.events.PaintEvent;
|
|
22 import dwt.events.PaintListener;
|
|
23 import dwt.graphics.Color;
|
|
24 import dwt.graphics.GC;
|
|
25 import dwt.graphics.Point;
|
|
26 import dwt.graphics.Rectangle;
|
|
27 import dwtx.jface.text.BadLocationException;
|
|
28 import dwtx.jface.text.IDocument;
|
|
29 import dwtx.jface.text.IPaintPositionManager;
|
|
30 import dwtx.jface.text.IPainter;
|
|
31 import dwtx.jface.text.IRegion;
|
|
32 import dwtx.jface.text.ITextViewerExtension5;
|
|
33 import dwtx.jface.text.Position;
|
|
34 import dwtx.jface.text.Region;
|
|
35
|
|
36 /**
|
|
37 * Highlights the peer character matching the character near the caret position.
|
|
38 * This painter can be configured with an
|
|
39 * {@link dwtx.jface.text.source.ICharacterPairMatcher}.
|
|
40 * <p>
|
|
41 * Clients instantiate and configure object of this class.</p>
|
|
42 *
|
|
43 * @since 2.1
|
|
44 */
|
|
45 public final class MatchingCharacterPainter : IPainter, PaintListener {
|
|
46
|
|
47 /** Indicates whether this painter is active */
|
|
48 private bool fIsActive= false;
|
|
49 /** The source viewer this painter is associated with */
|
|
50 private ISourceViewer fSourceViewer;
|
|
51 /** The viewer's widget */
|
|
52 private StyledText fTextWidget;
|
|
53 /** The color in which to highlight the peer character */
|
|
54 private Color fColor;
|
|
55 /** The paint position manager */
|
|
56 private IPaintPositionManager fPaintPositionManager;
|
|
57 /** The strategy for finding matching characters */
|
|
58 private ICharacterPairMatcher fMatcher;
|
|
59 /** The position tracking the matching characters */
|
|
60 private Position fPairPosition= new Position(0, 0);
|
|
61 /** The anchor indicating whether the character is left or right of the caret */
|
|
62 private int fAnchor;
|
|
63
|
|
64
|
|
65 /**
|
|
66 * Creates a new MatchingCharacterPainter for the given source viewer using
|
|
67 * the given character pair matcher. The character matcher is not adopted by
|
|
68 * this painter. Thus, it is not disposed. However, this painter requires
|
|
69 * exclusive access to the given pair matcher.
|
|
70 *
|
|
71 * @param sourceViewer
|
|
72 * @param matcher
|
|
73 */
|
|
74 public MatchingCharacterPainter(ISourceViewer sourceViewer, ICharacterPairMatcher matcher) {
|
|
75 fSourceViewer= sourceViewer;
|
|
76 fMatcher= matcher;
|
|
77 fTextWidget= sourceViewer.getTextWidget();
|
|
78 }
|
|
79
|
|
80 /**
|
|
81 * Sets the color in which to highlight the match character.
|
|
82 *
|
|
83 * @param color the color
|
|
84 */
|
|
85 public void setColor(Color color) {
|
|
86 fColor= color;
|
|
87 }
|
|
88
|
|
89 /*
|
|
90 * @see dwtx.jface.text.IPainter#dispose()
|
|
91 */
|
|
92 public void dispose() {
|
|
93 if (fMatcher !is null) {
|
|
94 fMatcher.clear();
|
|
95 fMatcher= null;
|
|
96 }
|
|
97
|
|
98 fColor= null;
|
|
99 fTextWidget= null;
|
|
100 }
|
|
101
|
|
102 /*
|
|
103 * @see dwtx.jface.text.IPainter#deactivate(bool)
|
|
104 */
|
|
105 public void deactivate(bool redraw) {
|
|
106 if (fIsActive) {
|
|
107 fIsActive= false;
|
|
108 fTextWidget.removePaintListener(this);
|
|
109 if (fPaintPositionManager !is null)
|
|
110 fPaintPositionManager.unmanagePosition(fPairPosition);
|
|
111 if (redraw)
|
|
112 handleDrawRequest(null);
|
|
113 }
|
|
114 }
|
|
115
|
|
116 /*
|
|
117 * @see dwt.events.PaintListener#paintControl(dwt.events.PaintEvent)
|
|
118 */
|
|
119 public void paintControl(PaintEvent event) {
|
|
120 if (fTextWidget !is null)
|
|
121 handleDrawRequest(event.gc);
|
|
122 }
|
|
123
|
|
124 /**
|
|
125 * Handles a redraw request.
|
|
126 *
|
|
127 * @param gc the GC to draw into.
|
|
128 */
|
|
129 private void handleDrawRequest(GC gc) {
|
|
130
|
|
131 if (fPairPosition.isDeleted)
|
|
132 return;
|
|
133
|
|
134 int offset= fPairPosition.getOffset();
|
|
135 int length= fPairPosition.getLength();
|
|
136 if (length < 1)
|
|
137 return;
|
|
138
|
|
139 if (fSourceViewer instanceof ITextViewerExtension5) {
|
|
140 ITextViewerExtension5 extension= (ITextViewerExtension5) fSourceViewer;
|
|
141 IRegion widgetRange= extension.modelRange2WidgetRange(new Region(offset, length));
|
|
142 if (widgetRange is null)
|
|
143 return;
|
|
144
|
|
145 try {
|
|
146 // don't draw if the pair position is really hidden and widgetRange just
|
|
147 // marks the coverage around it.
|
|
148 IDocument doc= fSourceViewer.getDocument();
|
|
149 int startLine= doc.getLineOfOffset(offset);
|
|
150 int endLine= doc.getLineOfOffset(offset + length);
|
|
151 if (extension.modelLine2WidgetLine(startLine) is -1 || extension.modelLine2WidgetLine(endLine) is -1)
|
|
152 return;
|
|
153 } catch (BadLocationException e) {
|
|
154 return;
|
|
155 }
|
|
156
|
|
157 offset= widgetRange.getOffset();
|
|
158 length= widgetRange.getLength();
|
|
159
|
|
160 } else {
|
|
161 IRegion region= fSourceViewer.getVisibleRegion();
|
|
162 if (region.getOffset() > offset || region.getOffset() + region.getLength() < offset + length)
|
|
163 return;
|
|
164 offset -= region.getOffset();
|
|
165 }
|
|
166
|
|
167 if (ICharacterPairMatcher.RIGHT is fAnchor)
|
|
168 draw(gc, offset, 1);
|
|
169 else
|
|
170 draw(gc, offset + length -1, 1);
|
|
171 }
|
|
172
|
|
173 /**
|
|
174 * Highlights the given widget region.
|
|
175 *
|
|
176 * @param gc the GC to draw into
|
|
177 * @param offset the offset of the widget region
|
|
178 * @param length the length of the widget region
|
|
179 */
|
|
180 private void draw(GC gc, int offset, int length) {
|
|
181 if (gc !is null) {
|
|
182
|
|
183 gc.setForeground(fColor);
|
|
184
|
|
185 Rectangle bounds;
|
|
186 if (length > 0)
|
|
187 bounds= fTextWidget.getTextBounds(offset, offset + length - 1);
|
|
188 else {
|
|
189 Point loc= fTextWidget.getLocationAtOffset(offset);
|
|
190 bounds= new Rectangle(loc.x, loc.y, 1, fTextWidget.getLineHeight(offset));
|
|
191 }
|
|
192
|
|
193 // draw box around line segment
|
|
194 gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1);
|
|
195
|
|
196 // draw box around character area
|
|
197 // int widgetBaseline= fTextWidget.getBaseline();
|
|
198 // FontMetrics fm= gc.getFontMetrics();
|
|
199 // int fontBaseline= fm.getAscent() + fm.getLeading();
|
|
200 // int fontBias= widgetBaseline - fontBaseline;
|
|
201
|
|
202 // gc.drawRectangle(left.x, left.y + fontBias, right.x - left.x - 1, fm.getHeight() - 1);
|
|
203
|
|
204 } else {
|
|
205 fTextWidget.redrawRange(offset, length, true);
|
|
206 }
|
|
207 }
|
|
208
|
|
209 /*
|
|
210 * @see dwtx.jface.text.IPainter#paint(int)
|
|
211 */
|
|
212 public void paint(int reason) {
|
|
213
|
|
214 IDocument document= fSourceViewer.getDocument();
|
|
215 if (document is null) {
|
|
216 deactivate(false);
|
|
217 return;
|
|
218 }
|
|
219
|
|
220 Point selection= fSourceViewer.getSelectedRange();
|
|
221 if (selection.y > 0) {
|
|
222 deactivate(true);
|
|
223 return;
|
|
224 }
|
|
225
|
|
226 IRegion pair= fMatcher.match(document, selection.x);
|
|
227 if (pair is null) {
|
|
228 deactivate(true);
|
|
229 return;
|
|
230 }
|
|
231
|
|
232 if (fIsActive) {
|
|
233
|
|
234 if (IPainter.CONFIGURATION is reason) {
|
|
235
|
|
236 // redraw current highlighting
|
|
237 handleDrawRequest(null);
|
|
238
|
|
239 } else if (pair.getOffset() !is fPairPosition.getOffset() ||
|
|
240 pair.getLength() !is fPairPosition.getLength() ||
|
|
241 fMatcher.getAnchor() !is fAnchor) {
|
|
242
|
|
243 // otherwise only do something if position is different
|
|
244
|
|
245 // remove old highlighting
|
|
246 handleDrawRequest(null);
|
|
247 // update position
|
|
248 fPairPosition.isDeleted= false;
|
|
249 fPairPosition.offset= pair.getOffset();
|
|
250 fPairPosition.length= pair.getLength();
|
|
251 fAnchor= fMatcher.getAnchor();
|
|
252 // apply new highlighting
|
|
253 handleDrawRequest(null);
|
|
254
|
|
255 }
|
|
256 } else {
|
|
257
|
|
258 fIsActive= true;
|
|
259
|
|
260 fPairPosition.isDeleted= false;
|
|
261 fPairPosition.offset= pair.getOffset();
|
|
262 fPairPosition.length= pair.getLength();
|
|
263 fAnchor= fMatcher.getAnchor();
|
|
264
|
|
265 fTextWidget.addPaintListener(this);
|
|
266 fPaintPositionManager.managePosition(fPairPosition);
|
|
267 handleDrawRequest(null);
|
|
268 }
|
|
269 }
|
|
270
|
|
271 /*
|
|
272 * @see dwtx.jface.text.IPainter#setPositionManager(dwtx.jface.text.IPaintPositionManager)
|
|
273 */
|
|
274 public void setPositionManager(IPaintPositionManager manager) {
|
|
275 fPaintPositionManager= manager;
|
|
276 }
|
|
277 }
|