Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2006 Wind River Systems, Inc. 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 |
* Anton Leherbauer (Wind River Systems) - initial API and implementation - https://bugs.eclipse.org/bugs/show_bug.cgi?id=22712 |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jface.text; |
12 |
|
13 |
import org.eclipse.swt.custom.StyleRange; |
14 |
import org.eclipse.swt.custom.StyledText; |
15 |
import org.eclipse.swt.custom.StyledTextContent; |
16 |
import org.eclipse.swt.events.PaintEvent; |
17 |
import org.eclipse.swt.events.PaintListener; |
18 |
import org.eclipse.swt.graphics.Color; |
19 |
import org.eclipse.swt.graphics.GC; |
20 |
import org.eclipse.swt.graphics.Point; |
21 |
|
22 |
|
23 |
/** |
24 |
* A painter for drawing visible characters for (invisible) whitespace |
25 |
* characters. |
26 |
* |
27 |
* @since 3.3 |
28 |
*/ |
29 |
public class InvisibleCharacterPainter implements IPainter, PaintListener { |
30 |
|
31 |
private static final char SPACE_SIGN= '\u00b7'; |
32 |
private static final char TAB_SIGN= '\u00bb'; |
33 |
private static final char CARRIAGE_RETURN_SIGN= '\u00a4'; |
34 |
private static final char LINE_FEED_SIGN= '\u00b6'; |
35 |
|
36 |
/** Indicates whether this painter is active */ |
37 |
private boolean fIsActive= false; |
38 |
/** The source viewer this painter is attached to */ |
39 |
private ITextViewer fTextViewer; |
40 |
/** The viewer's widget */ |
41 |
private StyledText fTextWidget; |
42 |
|
43 |
/** |
44 |
* Creates a new painter for the given text viewer. |
45 |
* @param textViewer the text viewer the painter should be attached to |
46 |
*/ |
47 |
public InvisibleCharacterPainter(ITextViewer textViewer) { |
48 |
super(); |
49 |
fTextViewer= textViewer; |
50 |
fTextWidget= textViewer.getTextWidget(); |
51 |
} |
52 |
|
53 |
/* |
54 |
* @see org.eclipse.jface.text.IPainter#dispose() |
55 |
*/ |
56 |
public void dispose() { |
57 |
fTextViewer= null; |
58 |
fTextWidget= null; |
59 |
} |
60 |
|
61 |
/* |
62 |
* @see org.eclipse.jface.text.IPainter#paint(int) |
63 |
*/ |
64 |
public void paint(int reason) { |
65 |
IDocument document= fTextViewer.getDocument(); |
66 |
if (document == null) { |
67 |
deactivate(false); |
68 |
return; |
69 |
} |
70 |
if (!fIsActive) { |
71 |
fIsActive= true; |
72 |
fTextWidget.addPaintListener(this); |
73 |
redrawAll(true); |
74 |
} else if (reason == CONFIGURATION || reason == INTERNAL) { |
75 |
redrawAll(false); |
76 |
} else if (reason == TEXT_CHANGE) { |
77 |
// redraw current line only |
78 |
try { |
79 |
IRegion lineRegion = |
80 |
document.getLineInformationOfOffset(getDocumentOffset(fTextWidget.getCaretOffset())); |
81 |
int widgetOffset= getWidgetOffset(lineRegion.getOffset()); |
82 |
int charCount= fTextWidget.getCharCount(); |
83 |
int redrawLength= Math.min(lineRegion.getLength(), charCount - widgetOffset); |
84 |
if (widgetOffset >= 0 && redrawLength > 0) { |
85 |
fTextWidget.redrawRange(widgetOffset, redrawLength, true); |
86 |
} |
87 |
} catch (BadLocationException e) { |
88 |
// ignore |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
/* |
94 |
* @see org.eclipse.jface.text.IPainter#deactivate(boolean) |
95 |
*/ |
96 |
public void deactivate(boolean redraw) { |
97 |
if (fIsActive) { |
98 |
fIsActive= false; |
99 |
fTextWidget.removePaintListener(this); |
100 |
if (redraw) { |
101 |
redrawAll(true); |
102 |
} |
103 |
} |
104 |
} |
105 |
|
106 |
/* |
107 |
* @see org.eclipse.jface.text.IPainter#setPositionManager(org.eclipse.jface.text.IPaintPositionManager) |
108 |
*/ |
109 |
public void setPositionManager(IPaintPositionManager manager) { |
110 |
// no need for a position manager |
111 |
} |
112 |
|
113 |
/* |
114 |
* @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent) |
115 |
*/ |
116 |
public void paintControl(PaintEvent event) { |
117 |
if (fTextWidget != null) { |
118 |
handleDrawRequest(event.gc, event.x, event.y, event.width, event.height); |
119 |
} |
120 |
} |
121 |
|
122 |
/** |
123 |
* Draw characters in view range. |
124 |
* @param gc |
125 |
* @param x |
126 |
* @param y |
127 |
* @param w |
128 |
* @param h |
129 |
*/ |
130 |
private void handleDrawRequest(GC gc, int x, int y, int w, int h) { |
131 |
int lineCount= fTextWidget.getLineCount(); |
132 |
int startLine= (y + fTextWidget.getTopPixel()) / fTextWidget.getLineHeight(); |
133 |
int endLine= (y + h - 1 + fTextWidget.getTopPixel()) / fTextWidget.getLineHeight(); |
134 |
if (startLine <= endLine && startLine < lineCount) { |
135 |
int startOffset= fTextWidget.getOffsetAtLine(startLine); |
136 |
int endOffset = |
137 |
endLine < lineCount - 1 ? fTextWidget.getOffsetAtLine(endLine + 1) : fTextWidget.getCharCount(); |
138 |
int alpha= gc.getAlpha(); |
139 |
gc.setAlpha(100); |
140 |
handleDrawRequest(gc, startOffset, endOffset); |
141 |
gc.setAlpha(alpha); |
142 |
} |
143 |
} |
144 |
|
145 |
/** |
146 |
* Draw characters of content range. |
147 |
* @param gc |
148 |
* @param startOffset inclusive start index |
149 |
* @param endOffset exclusive end index |
150 |
*/ |
151 |
private void handleDrawRequest(GC gc, int startOffset, int endOffset) { |
152 |
StyledTextContent content= fTextWidget.getContent(); |
153 |
int length= endOffset - startOffset; |
154 |
String text= content.getTextRange(startOffset, length); |
155 |
StyleRange styleRange= null; |
156 |
Color fg= null; |
157 |
Point selection= fTextWidget.getSelection(); |
158 |
StringBuffer visibleChar= new StringBuffer(10); |
159 |
for (int textOffset= 0; textOffset <= length; ++textOffset) { |
160 |
int delta= 0; |
161 |
boolean eol= false; |
162 |
if (textOffset < length) { |
163 |
delta= 1; |
164 |
char c= text.charAt(textOffset); |
165 |
switch (c) { |
166 |
case ' ' : |
167 |
visibleChar.append(SPACE_SIGN); |
168 |
// 'continue' would improve performance but may produce drawing errors |
169 |
// for long runs of space if width of space and dot differ |
170 |
break; |
171 |
case '\t' : |
172 |
visibleChar.append(TAB_SIGN); |
173 |
break; |
174 |
case '\r' : |
175 |
visibleChar.append(CARRIAGE_RETURN_SIGN); |
176 |
if (textOffset >= length - 1 || text.charAt(textOffset + 1) != '\n') { |
177 |
eol= true; |
178 |
break; |
179 |
} |
180 |
continue; |
181 |
case '\n' : |
182 |
visibleChar.append(LINE_FEED_SIGN); |
183 |
eol= true; |
184 |
break; |
185 |
default : |
186 |
delta= 0; |
187 |
break; |
188 |
} |
189 |
} |
190 |
if (visibleChar.length() > 0) { |
191 |
int widgetOffset= startOffset + textOffset - visibleChar.length() + delta; |
192 |
if (!eol || !isFoldedLine(content.getLineAtOffset(widgetOffset))) { |
193 |
if (widgetOffset >= selection.x && widgetOffset < selection.y) { |
194 |
fg= fTextWidget.getSelectionForeground(); |
195 |
} else if (styleRange == null || styleRange.start + styleRange.length <= widgetOffset) { |
196 |
styleRange= fTextWidget.getStyleRangeAtOffset(widgetOffset); |
197 |
if (styleRange == null || styleRange.foreground == null) { |
198 |
fg= fTextWidget.getForeground(); |
199 |
} else { |
200 |
fg= styleRange.foreground; |
201 |
} |
202 |
} |
203 |
draw(gc, widgetOffset, visibleChar.toString(), fg); |
204 |
} |
205 |
visibleChar.delete(0, visibleChar.length()); |
206 |
} |
207 |
} |
208 |
} |
209 |
|
210 |
/** |
211 |
* Check if the given widget line is a folded line. |
212 |
* @param widgetLine the widget line number |
213 |
* @return <code>true</code> if the line is folded |
214 |
*/ |
215 |
private boolean isFoldedLine(int widgetLine) { |
216 |
if (fTextViewer instanceof ITextViewerExtension5) { |
217 |
ITextViewerExtension5 extension= (ITextViewerExtension5)fTextViewer; |
218 |
int modelLine= extension.widgetLine2ModelLine(widgetLine); |
219 |
int widgetLine2= extension.modelLine2WidgetLine(modelLine + 1); |
220 |
return widgetLine2 == -1; |
221 |
} |
222 |
return false; |
223 |
} |
224 |
|
225 |
/** |
226 |
* Redraw all of the text widgets visible content. |
227 |
* @param redrawBackground If true, clean background before painting text. |
228 |
*/ |
229 |
private void redrawAll(boolean redrawBackground) { |
230 |
int startLine= fTextWidget.getTopPixel() / fTextWidget.getLineHeight(); |
231 |
int startOffset= fTextWidget.getOffsetAtLine(startLine); |
232 |
int endLine= 1 + (fTextWidget.getTopPixel() + fTextWidget.getClientArea().height) / fTextWidget.getLineHeight(); |
233 |
int endOffset; |
234 |
if (endLine >= fTextWidget.getLineCount()) { |
235 |
endOffset= fTextWidget.getCharCount(); |
236 |
} else { |
237 |
endOffset= fTextWidget.getOffsetAtLine(endLine); |
238 |
} |
239 |
if (startOffset < endOffset) { |
240 |
// add 2 for line separator characters |
241 |
endOffset= Math.min(endOffset + 2, fTextWidget.getCharCount()); |
242 |
int redrawOffset= startOffset; |
243 |
int redrawLength= endOffset - redrawOffset; |
244 |
fTextWidget.redrawRange(startOffset, redrawLength, redrawBackground); |
245 |
} |
246 |
} |
247 |
|
248 |
/** |
249 |
* Draw string at widget offset. |
250 |
* @param gc |
251 |
* @param offset the widget offset |
252 |
* @param s the string to be drawn |
253 |
* @param fg the foreground color |
254 |
*/ |
255 |
private void draw(GC gc, int offset, String s, Color fg) { |
256 |
Point pos= fTextWidget.getLocationAtOffset(offset); |
257 |
gc.setForeground(fg); |
258 |
gc.drawString(s, pos.x, pos.y, true); |
259 |
} |
260 |
|
261 |
/** |
262 |
* Convert a document offset to the corresponding widget offset. |
263 |
* @param documentOffset |
264 |
* @return widget offset |
265 |
*/ |
266 |
private int getWidgetOffset(int documentOffset) { |
267 |
if (fTextViewer instanceof ITextViewerExtension5) { |
268 |
ITextViewerExtension5 extension= (ITextViewerExtension5)fTextViewer; |
269 |
return extension.modelOffset2WidgetOffset(documentOffset); |
270 |
} |
271 |
IRegion visible= fTextViewer.getVisibleRegion(); |
272 |
int widgetOffset= documentOffset - visible.getOffset(); |
273 |
if (widgetOffset > visible.getLength()) { |
274 |
return -1; |
275 |
} |
276 |
return widgetOffset; |
277 |
} |
278 |
|
279 |
/** |
280 |
* Convert a widget offset to the corresponding document offset. |
281 |
* @param widgetOffset |
282 |
* @return document offset |
283 |
*/ |
284 |
private int getDocumentOffset(int widgetOffset) { |
285 |
if (fTextViewer instanceof ITextViewerExtension5) { |
286 |
ITextViewerExtension5 extension= (ITextViewerExtension5)fTextViewer; |
287 |
return extension.widgetOffset2ModelOffset(widgetOffset); |
288 |
} |
289 |
IRegion visible= fTextViewer.getVisibleRegion(); |
290 |
if (widgetOffset > visible.getLength()) { |
291 |
return -1; |
292 |
} |
293 |
return widgetOffset + visible.getOffset(); |
294 |
} |
295 |
|
296 |
} |