KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.text;
12
13 import org.eclipse.swt.custom.StyledText;
14
15 import org.eclipse.jface.text.source.ILineRange;
16 import org.eclipse.jface.text.source.LineRange;
17
18 /**
19  * A collection of JFace Text functions.
20  * <p>
21  * This class is neither intended to be instantiated nor instantiated.
22  * </p>
23  *
24  * @since 3.3
25  */

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

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

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

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

111     public static int getPartialBottomIndex(StyledText widget) {
112         // @see StyledText#getPartialBottomIndex()
113
int lastPixel= computeLastVisiblePixel(widget);
114         int bottom= widget.getLineIndex(lastPixel);
115         return bottom;
116     }
117
118     /**
119      * Returns the last visible pixel in the widget's client area.
120      *
121      * @param widget the widget
122      * @return the last visible pixel in the widget's client area
123      */

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

140     public static int getPartialTopIndex(ITextViewer viewer) {
141         StyledText widget= viewer.getTextWidget();
142         int widgetTop= getPartialTopIndex(widget);
143         return widgetLine2ModelLine(viewer, widgetTop);
144     }
145     
146     /**
147      * Returns the last, possibly partially, visible line in the view port.
148      *
149      * @param viewer the text viewer
150      * @return the last, possibly partially, visible line in the view port
151      */

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

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

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

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

242     public static int getHiddenTopLinePixels(StyledText textWidget) {
243         int top= getPartialTopIndex(textWidget);
244         return -textWidget.getLinePixel(top);
245     }
246     
247     /*
248      * @see StyledText#getLinePixel(int)
249      */

250     public static int getLinePixel(StyledText textWidget, int line) {
251         return textWidget.getLinePixel(line);
252     }
253     
254     /*
255      * @see StyledText#getLineIndex(int)
256      */

257     public static int getLineIndex(StyledText textWidget, int y) {
258         int lineIndex= textWidget.getLineIndex(y);
259         return lineIndex;
260     }
261
262     /**
263      * Returns <code>true</code> if the widget displays the entire contents, i.e. it cannot
264      * be vertically scrolled.
265      *
266      * @param widget the widget
267      * @return <code>true</code> if the widget displays the entire contents, i.e. it cannot
268      * be vertically scrolled, <code>false</code> otherwise
269      */

270     public static boolean isShowingEntireContents(StyledText widget) {
271         if (widget.getTopPixel() != 0) // more efficient shortcut
272
return false;
273         
274         int lastVisiblePixel= computeLastVisiblePixel(widget);
275         int lastPossiblePixel= widget.getLinePixel(widget.getLineCount());
276         return lastPossiblePixel <= lastVisiblePixel;
277     }
278
279 }
280
Popular Tags