?? wrappedcontent.java
字號:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.custom;import org.eclipse.swt.*;import org.eclipse.swt.graphics.*;/** * An instance of class <code>WrappedContent</code> is used by * StyledText to display wrapped lines. Lines are wrapped at word * breaks which are marked by a space character. Trailing space * behind words is kept on the current line. * If the last remaining word on a line can not be fully displayed * the line is wrapped character by character. * WrappedContent wraps a StyledTextContent which provides the line * data. The start offset and length of wrapped lines is calculated * and updated based on recalculation requests and text changes. * <p> * All public methods in this class implement the * <code>StyledTextContent</code> interface. Package visible * methods are internal API for use by <code>StyledText</code>. * </p> */class WrappedContent implements StyledTextContent { final static int LINE_OFFSET = 0; // index of line offset in visualLines array final static int LINE_LENGTH = 1; // index of line lenght in visualLines array StyledTextRenderer renderer; StyledTextContent logicalContent; int[][] visualLines; // start and length of each visual line int visualLineCount = 0;/** * Create a new instance. * * @param renderer <class>StyledTextRenderer</class> that renders * the lines wrapped by the new instance. * @param logicalContent StyledTextContent that provides the line * data. */WrappedContent(StyledTextRenderer renderer, StyledTextContent logicalContent) { this.renderer = renderer; this.logicalContent = logicalContent;}/** * @see StyledTextContent#addTextChangeListener(TextChangeListener) */public void addTextChangeListener(TextChangeListener listener) { logicalContent.addTextChangeListener(listener);}/** * Grow the lines array to at least the specified size. * <p> * * @param numLines number of elements that the array should have * at a minimum */private void ensureSize(int numLines) { int size = visualLines.length; if (size >= numLines) { return; } int[][] newLines = new int[Math.max(size * 2, numLines)][2]; System.arraycopy(visualLines, 0, newLines, 0, size); visualLines = newLines; resetVisualLines(size, visualLines.length - size); }/** * @see StyledTextContent#getCharCount() */public int getCharCount() { return logicalContent.getCharCount();}/** * @return the visual (wrapped) line at the specified index * @see StyledTextContent#getLine(int) */public String getLine(int lineIndex) { String line; // redirect call to logical content if there are no wrapped lines if (visualLineCount == 0) { line = logicalContent.getLine(lineIndex); } else { if (lineIndex >= visualLineCount || lineIndex < 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } line = logicalContent.getTextRange(visualLines[lineIndex][LINE_OFFSET], visualLines[lineIndex][LINE_LENGTH]); } return line;}/** * Returns the visual (wrapped) line at given offset. * <p> * The offset is ambiguous if it identifies the end of a visual line and * there is another visual line below. In this case the end of the visual * line has the same offset as the beginning of the next visual line * since the visual line break is not represented by any character in the * logical line. * In this ambiguous case the offset is assumed to represent the end of a * visual line and the index of the first visual line is returned. * </p> * * @param offset offset of the desired line. * @return the index of the visual (wrapped) line at the specified offset * @see StyledTextContent#getLineAtOffset(int) */public int getLineAtOffset(int offset) { int lastLine = visualLineCount - 1; int lastChar; // redirect call to logical content if there are no wrapped lines if (visualLineCount == 0) { return logicalContent.getLineAtOffset(offset); } // can't use getCharCount to get the number of characters since this // method is called in textChanged, when the logicalContent used by // getCharCount has already changed. at that point the visual lines // have not been updated yet and we thus need to use the old character // count which is only available in the visual content. lastChar = visualLines[lastLine][LINE_OFFSET] + visualLines[lastLine][LINE_LENGTH]; if (offset < 0 || (offset > 0 && offset > lastChar)) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } // if last line and the line is not empty you can ask for // a position that doesn't exist (the one to the right of the // last character) - for inserting if (offset == lastChar) { return lastLine; } int high = visualLineCount; int low = -1; int index = visualLineCount; while (high - low > 1) { index = (high + low) / 2; int lineStart = visualLines[index][LINE_OFFSET]; if (offset >= lineStart) { int lineEnd = lineStart + visualLines[index][LINE_LENGTH]; low = index; if (offset <= lineEnd) { break; } } else { high = index; } } if (low > 0 && offset == visualLines[low - 1][LINE_OFFSET] + visualLines[low - 1][LINE_LENGTH]) { // end of a visual line/beginning of next visual line is ambiguous // (they have the same offset). always return the first visual line low--; } return low;}/** * @return the number of visual (wrapped) lines * @see StyledTextContent#getLineCount() */public int getLineCount() { int lineCount = visualLineCount; // redirect call to logical content if there are no wrapped lines if (visualLineCount == 0) { lineCount = logicalContent.getLineCount(); } return lineCount;}/** * @see StyledTextContent#getLineDelimiter() */public String getLineDelimiter() { return logicalContent.getLineDelimiter();}/** * @return the start offset of the visual (wrapped) line at the given * index * @see StyledTextContent#getOffsetAtLine(int) */public int getOffsetAtLine(int lineIndex) { int offset; // redirect call to logical content if there are no wrapped lines if (visualLineCount == 0) { offset = logicalContent.getOffsetAtLine(lineIndex); } else { if (lineIndex >= visualLineCount || lineIndex < 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } offset = visualLines[lineIndex][LINE_OFFSET]; } return offset;}/** * @see StyledTextContent#getTextRange(int, int) */public String getTextRange(int start, int length) { return logicalContent.getTextRange(start, length);}/** * Returns the number of visual (wrapped) lines. * * @return the number of visual (wrapped) lines */int getVisualLineCount() { return visualLineCount; }/** * @see StyledTextContent#removeTextChangeListener(TextChangeListener) */public void removeTextChangeListener(TextChangeListener listener) { logicalContent.removeTextChangeListener(listener);}/** * Reset the visual (wrapped) lines in the specified range. * If the range specifies partial logical lines (e.g., startLine is * the second of two visual lines) it is extended to reset all visual * lines of a logical line. * Following the reset the logical lines in the reset visual range are * rewrapped. * <p> * * @param startLine index of the first visual line * @param lineCount number of visual lines */void reset(int startLine, int lineCount) { if (lineCount <= 0 || visualLineCount == 0) { return; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -