KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > JFaceTextUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  *******************************************************************************/

11 package org.eclipse.jface.internal.text;
12
13 import org.eclipse.swt.custom.StyledText;
14 import org.eclipse.swt.graphics.Rectangle;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.ITextViewerExtension5;
21 import org.eclipse.jface.text.source.ILineRange;
22 import org.eclipse.jface.text.source.LineRange;
23
24
25 /**
26  * This class is here to help transitioning to the
27  * new StyledText APIs.
28  *
29  * @since 3.2
30  */

31 public final class JFaceTextUtil {
32     
33     private JFaceTextUtil() {
34         // Do not instantiate
35
}
36     
37
38     /**
39      * Computes the line height for the given line range.
40      *
41      * @param textWidget the <code>StyledText</code> widget
42      * @param startLine the start line
43      * @param endLine the end line (exclusive)
44      * @param lineCount the line count used by the old API
45      * @return the height of all lines starting with <code>startLine</code> and ending above <code>endLime</code>
46      */

47     public static int computeLineHeight(StyledText textWidget, int startLine, int endLine, int lineCount) {
48         return getLinePixel(textWidget, endLine) - getLinePixel(textWidget, startLine);
49     }
50     
51     /**
52      * Returns the last fully visible line of the widget. The exact semantics of "last fully visible
53      * line" are:
54      * <ul>
55      * <li>the last line of which the last pixel is visible, if any
56      * <li>otherwise, the only line that is partially visible
57      * </ul>
58      *
59      * @param widget the widget
60      * @return the last fully visible line
61      */

62     public static int getBottomIndex(StyledText widget) {
63         int lastPixel= computeLastVisiblePixel(widget);
64         
65         // bottom is in [0 .. lineCount - 1]
66
int bottom= widget.getLineIndex(lastPixel);
67
68         // bottom is the first line - no more checking
69
if (bottom == 0)
70             return bottom;
71         
72         int pixel= widget.getLinePixel(bottom);
73         // bottom starts on or before the client area start - bottom is the only visible line
74
if (pixel <= 0)
75             return bottom;
76         
77         int offset= widget.getOffsetAtLine(bottom);
78         int height= widget.getLineHeight(offset);
79         
80         // bottom is not showing entirely - use the previous line
81
if (pixel + height - 1 > lastPixel)
82             return bottom - 1;
83         
84         // bottom is fully visible and its last line is exactly the last pixel
85
return bottom;
86     }
87
88     /**
89      * Returns the index of the first (possibly only partially) visible line of the widget
90      *
91      * @param widget the widget
92      * @return the index of the first line of which a pixel is visible
93      */

94     public static int getPartialTopIndex(StyledText widget) {
95         // see StyledText#getPartialTopIndex()
96
int top= widget.getTopIndex();
97         int pixels= widget.getLinePixel(top);
98         
99         // FIXME remove when https://bugs.eclipse.org/bugs/show_bug.cgi?id=123770 is fixed
100
if (pixels == -widget.getLineHeight(widget.getOffsetAtLine(top))) {
101             top++;
102             pixels= 0;
103         }
104
105         if (pixels > 0)
106             top--;
107         
108         return top;
109     }
110
111     /**
112      * Returns the index of the last (possibly only partially) visible line of the widget
113      *
114      * @param widget the text widget
115      * @return the index of the last line of which a pixel is visible
116      */

117     public static int getPartialBottomIndex(StyledText widget) {
118         // @see StyledText#getPartialBottomIndex()
119
int lastPixel= computeLastVisiblePixel(widget);
120         int bottom= widget.getLineIndex(lastPixel);
121         return bottom;
122     }
123
124     /**
125      * Returns the last visible pixel in the widget's client area.
126      *
127      * @param widget the widget
128      * @return the last visible pixel in the widget's client area
129      */

130     private static int computeLastVisiblePixel(StyledText widget) {
131         int caHeight= widget.getClientArea().height;
132         int lastPixel= caHeight - 1;
133         // XXX what if there is a margin? can't take trim as this includes the scrollbars which are not part of the client area
134
// if ((textWidget.getStyle() & SWT.BORDER) != 0)
135
// lastPixel -= 4;
136
return lastPixel;
137     }
138     
139     /**
140      * Returns the line index of the first visible model line in the viewer. The line may be only
141      * partially visible.
142      *
143      * @param viewer the text viewer
144      * @return the first line of which a pixel is visible, or -1 for no line
145      */

146     public static int getPartialTopIndex(ITextViewer viewer) {
147         StyledText widget= viewer.getTextWidget();
148         int widgetTop= getPartialTopIndex(widget);
149         return widgetLine2ModelLine(viewer, widgetTop);
150     }
151     
152     /**
153      * Returns the last, possibly partially, visible line in the view port.
154      *
155      * @param viewer the text viewer
156      * @return the last, possibly partially, visible line in the view port
157      */

158     public static int getPartialBottomIndex(ITextViewer viewer) {
159         StyledText textWidget= viewer.getTextWidget();
160         int widgetBottom= getPartialBottomIndex(textWidget);
161         return widgetLine2ModelLine(viewer, widgetBottom);
162     }
163
164     /**
165      * Returns the range of lines that is visible in the viewer, including any partially visible
166      * lines.
167      *
168      * @param viewer the viewer
169      * @return the range of lines that is visible in the viewer, <code>null</code> if no lines are
170      * visible
171      */

172     public static ILineRange getVisibleModelLines(ITextViewer viewer) {
173         int top= getPartialTopIndex(viewer);
174         int bottom= getPartialBottomIndex(viewer);
175         if (top == -1 || bottom == -1)
176             return null;
177         return new LineRange(top, bottom - top + 1);
178     }
179
180     /**
181      * Converts a widget line into a model (i.e. {@link IDocument}) line using the
182      * {@link ITextViewerExtension5} if available, otherwise by adapting the widget line to the
183      * viewer's {@link ITextViewer#getVisibleRegion() visible region}.
184      *
185      * @param viewer the viewer
186      * @param widgetLine the widget line to convert.
187      * @return the model line corresponding to <code>widgetLine</code> or -1 to signal that there
188      * is no corresponding model line
189      */

190     public static int widgetLine2ModelLine(ITextViewer viewer, int widgetLine) {
191         int modelLine;
192         if (viewer instanceof ITextViewerExtension5) {
193             ITextViewerExtension5 extension= (ITextViewerExtension5) viewer;
194             modelLine= extension.widgetLine2ModelLine(widgetLine);
195         } else {
196             try {
197                 IRegion r= viewer.getVisibleRegion();
198                 IDocument d= viewer.getDocument();
199                 modelLine= widgetLine + d.getLineOfOffset(r.getOffset());
200             } catch (BadLocationException x) {
201                 modelLine= widgetLine;
202             }
203         }
204         return modelLine;
205     }
206     
207     /**
208      * Converts a model (i.e. {@link IDocument}) line into a widget line using the
209      * {@link ITextViewerExtension5} if available, otherwise by adapting the model line to the
210      * viewer's {@link ITextViewer#getVisibleRegion() visible region}.
211      *
212      * @param viewer the viewer
213      * @param modelLine the model line to convert.
214      * @return the widget line corresponding to <code>modelLine</code> or -1 to signal that there
215      * is no corresponding widget line
216      */

217     public static int modelLineToWidgetLine(ITextViewer viewer, final int modelLine) {
218         int widgetLine;
219         if (viewer instanceof ITextViewerExtension5) {
220             ITextViewerExtension5 extension= (ITextViewerExtension5) viewer;
221             widgetLine= extension.modelLine2WidgetLine(modelLine);
222         } else {
223             IRegion region= viewer.getVisibleRegion();
224             IDocument document= viewer.getDocument();
225             try {
226                 int visibleStartLine= document.getLineOfOffset(region.getOffset());
227                 int visibleEndLine= document.getLineOfOffset(region.getOffset() + region.getLength());
228                 if (modelLine < visibleStartLine || modelLine > visibleEndLine)
229                     widgetLine= -1;
230                 else
231                 widgetLine= modelLine - visibleStartLine;
232             } catch (BadLocationException x) {
233                 // ignore and return -1
234
widgetLine= -1;
235             }
236         }
237         return widgetLine;
238     }
239
240
241     /**
242      * Returns the number of hidden pixels of the first partially visible line. If there is no
243      * partially visible line, zero is returned.
244      *
245      * @param textWidget the widget
246      * @return the number of hidden pixels of the first partial line, always &gt;= 0
247      */

248     public static int getHiddenTopLinePixels(StyledText textWidget) {
249         int top= getPartialTopIndex(textWidget);
250         return -textWidget.getLinePixel(top);
251     }
252     
253     /**
254      * Returns the number of lines in the view port.
255      *
256      * @param textWidget
257      * @return the number of lines visible in the view port <code>-1</code> if there's no client area
258      * @deprecated this method should not be used - it relies on the widget using a uniform line height
259      */

260     public static int getVisibleLinesInViewport(StyledText textWidget) {
261         if (textWidget != null) {
262             Rectangle clArea= textWidget.getClientArea();
263             if (!clArea.isEmpty()) {
264                 int firstPixel= 0;
265                 int lastPixel= clArea.height - 1; // XXX what about margins? don't take trims as they include scrollbars
266
int first= getLineIndex(textWidget, firstPixel);
267                 int last= getLineIndex(textWidget, lastPixel);
268                 return last - first;
269             }
270         }
271         return -1;
272     }
273
274     /*
275      * @see StyledText#getLinePixel(int)
276      */

277     public static int getLinePixel(StyledText textWidget, int line) {
278         return textWidget.getLinePixel(line);
279     }
280     
281     /*
282      * @see StyledText#getLineIndex(int)
283      */

284     public static int getLineIndex(StyledText textWidget, int y) {
285         int lineIndex= textWidget.getLineIndex(y);
286         return lineIndex;
287     }
288
289     /**
290      * Returns <code>true</code> if the widget displays the entire contents, i.e. it cannot
291      * be vertically scrolled.
292      *
293      * @param widget the widget
294      * @return <code>true</code> if the widget displays the entire contents, i.e. it cannot
295      * be vertically scrolled, <code>false</code> otherwise
296      */

297     public static boolean isShowingEntireContents(StyledText widget) {
298         if (widget.getTopPixel() != 0) // more efficient shortcut
299
return false;
300         
301         int lastVisiblePixel= computeLastVisiblePixel(widget);
302         int lastPossiblePixel= widget.getLinePixel(widget.getLineCount());
303         return lastPossiblePixel <= lastVisiblePixel;
304     }
305
306 }
307
Popular Tags