1 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 31 public final class JFaceTextUtil { 32 33 private JFaceTextUtil() { 34 } 36 37 38 47 public static int computeLineHeight(StyledText textWidget, int startLine, int endLine, int lineCount) { 48 return getLinePixel(textWidget, endLine) - getLinePixel(textWidget, startLine); 49 } 50 51 62 public static int getBottomIndex(StyledText widget) { 63 int lastPixel= computeLastVisiblePixel(widget); 64 65 int bottom= widget.getLineIndex(lastPixel); 67 68 if (bottom == 0) 70 return bottom; 71 72 int pixel= widget.getLinePixel(bottom); 73 if (pixel <= 0) 75 return bottom; 76 77 int offset= widget.getOffsetAtLine(bottom); 78 int height= widget.getLineHeight(offset); 79 80 if (pixel + height - 1 > lastPixel) 82 return bottom - 1; 83 84 return bottom; 86 } 87 88 94 public static int getPartialTopIndex(StyledText widget) { 95 int top= widget.getTopIndex(); 97 int pixels= widget.getLinePixel(top); 98 99 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 117 public static int getPartialBottomIndex(StyledText widget) { 118 int lastPixel= computeLastVisiblePixel(widget); 120 int bottom= widget.getLineIndex(lastPixel); 121 return bottom; 122 } 123 124 130 private static int computeLastVisiblePixel(StyledText widget) { 131 int caHeight= widget.getClientArea().height; 132 int lastPixel= caHeight - 1; 133 return lastPixel; 137 } 138 139 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 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 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 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 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 widgetLine= -1; 235 } 236 } 237 return widgetLine; 238 } 239 240 241 248 public static int getHiddenTopLinePixels(StyledText textWidget) { 249 int top= getPartialTopIndex(textWidget); 250 return -textWidget.getLinePixel(top); 251 } 252 253 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; int first= getLineIndex(textWidget, firstPixel); 267 int last= getLineIndex(textWidget, lastPixel); 268 return last - first; 269 } 270 } 271 return -1; 272 } 273 274 277 public static int getLinePixel(StyledText textWidget, int line) { 278 return textWidget.getLinePixel(line); 279 } 280 281 284 public static int getLineIndex(StyledText textWidget, int y) { 285 int lineIndex= textWidget.getLineIndex(y); 286 return lineIndex; 287 } 288 289 297 public static boolean isShowingEntireContents(StyledText widget) { 298 if (widget.getTopPixel() != 0) 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 |