org.eclipse.swt.custom
Class WrappedContent

java.lang.Object
  |
  +--org.eclipse.swt.custom.WrappedContent
All Implemented Interfaces:
StyledTextContent

class WrappedContent
extends Object
implements StyledTextContent

An instance of class WrappedContent 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.

All public methods in this class implement the StyledTextContent interface. Package visible methods are internal API for use by StyledText.


Field Summary
(package private) static int LINE_LENGTH
           
(package private) static int LINE_OFFSET
           
(package private)  StyledTextContent logicalContent
           
(package private)  StyledTextRenderer renderer
           
(package private)  int visualLineCount
           
(package private)  int[][] visualLines
           
(package private) static int WRAP_LINE_LENGTH
           
(package private) static int WRAP_LINE_WIDTH
           
 
Constructor Summary
(package private) WrappedContent(StyledTextRenderer renderer, StyledTextContent logicalContent)
          Create a new instance.
 
Method Summary
 void addTextChangeListener(TextChangeListener listener)
          Enabled: Called by StyledText to add itself as an Observer to content changes.
private  void ensureSize(int numLines)
          Grow the lines array to at least the specified size.
 int getCharCount()
          Enabled: Return the number of characters in the content.
 String getLine(int lineIndex)
          Enabled: Return the line at the given line index without delimiters.
 int getLineAtOffset(int offset)
          Returns the visual (wrapped) line at given offset.
 int getLineCount()
          Enabled: Return the number of lines.
 String getLineDelimiter()
          Enabled: Return the line delimiter that should be used by the StyledText widget when inserting new lines.
 int getOffsetAtLine(int lineIndex)
          Enabled: Return the character offset of the first character of the given line.
 String getTextRange(int start, int length)
          Enabled: Returns a string representing the content at the given range.
private  int getTextWidth(String line, int logicalLineOffset, int visualLineOffset, int visualLineLength, StyleRange[] styles, int startX, GC gc)
          Measure the width of a segment in the specified logical line.
(package private)  int getVisualLineCount()
          Returns the number of visual (wrapped) lines.
private  int getWordEnd(String line, int startOffset, int offset)
          Returns the offset of the character after the word at the specified offset.
private  int getWordStart(String line, int startOffset, int offset)
          Returns the start offset of the word at the specified offset.
 void removeTextChangeListener(TextChangeListener listener)
          Enabled: Remove the specified text changed listener.
 void replaceTextRange(int start, int replaceLength, String text)
          Enabled: Replace the text with "newText" starting at position "start" for a length of "replaceLength".
(package private)  void reset(int startLine, int lineCount)
          Reset the visual (wrapped) lines in the specified range.
private  int reset(int startLine, int lineCount, boolean wrap)
          Reset the visual (wrapped) lines in the specified range.
private  void resetVisualLines(int startLine, int lineCount)
          Reset the visual (wrapped) lines in the specified range.
 void setText(String text)
          Enabled: Set text to "text".
private  void setVisualLine(int visualLineIndex, int visualLineOffset, int visualLineLength)
          Set the line wrap data for the specified visual (wrapped) line.
(package private)  void textChanged(int startOffset, int newLineCount, int replaceLineCount, int newCharCount, int replaceCharCount)
          Recalculates the line wrap for the lines affected by the text change.
private  int[] wrapLine(String line, int logicalLineOffset, int visualLineOffset, int startX, int width, int numChars, StyleRange[] styles, GC gc)
          Wrap the given logical line at the specified offset.
private  int wrapLineRange(int startLine, int endLine, int visualLineIndex)
          Wrap the logical lines in the given range at the current client area width of the StyledText widget
private  int wrapLineRange(int startLine, int endLine, int visualLineIndex, int width)
          Wrap the lines in the given range.
(package private)  void wrapLines()
          Wrap all logical lines at the current client area width of the StyledText widget
(package private)  void wrapLines(int width)
          Wrap all logical lines at the given width.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_OFFSET

static final int LINE_OFFSET

LINE_LENGTH

static final int LINE_LENGTH

WRAP_LINE_LENGTH

static final int WRAP_LINE_LENGTH

WRAP_LINE_WIDTH

static final int WRAP_LINE_WIDTH

renderer

StyledTextRenderer renderer

logicalContent

StyledTextContent logicalContent

visualLines

int[][] visualLines

visualLineCount

int visualLineCount
Constructor Detail

WrappedContent

WrappedContent(StyledTextRenderer renderer,
               StyledTextContent logicalContent)
Create a new instance.

Parameters:
renderer - StyledTextRenderer that renders the lines wrapped by the new instance.
logicalContent - StyledTextContent that provides the line data.
Method Detail

addTextChangeListener

public void addTextChangeListener(TextChangeListener listener)
Description copied from interface: StyledTextContent
Enabled: Called by StyledText to add itself as an Observer to content changes. See TextChangeListener for a description of the listener methods that are called when text changes occur.

Specified by:
addTextChangeListener in interface StyledTextContent
Parameters:
listener - the listener
See Also:
StyledTextContent.addTextChangeListener(TextChangeListener)

ensureSize

private void ensureSize(int numLines)
Grow the lines array to at least the specified size.

Parameters:
numLines - number of elements that the array should have at a minimum

getCharCount

public int getCharCount()
Description copied from interface: StyledTextContent
Enabled: Return the number of characters in the content.

Specified by:
getCharCount in interface StyledTextContent
Returns:
the number of characters in the content.
See Also:
StyledTextContent.getCharCount()

getLine

public String getLine(int lineIndex)
Description copied from interface: StyledTextContent
Enabled: Return the line at the given line index without delimiters.

Specified by:
getLine in interface StyledTextContent
Parameters:
lineIndex - index of the line to return. Does not include delimiters of preceeding lines. Index 0 is the first line of the content.
Returns:
the visual (wrapped) line at the specified index
See Also:
StyledTextContent.getLine(int)

getLineAtOffset

public int getLineAtOffset(int offset)
Returns the visual (wrapped) line at given offset.

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.

Specified by:
getLineAtOffset in interface StyledTextContent
Parameters:
offset - offset of the desired line.
Returns:
the index of the visual (wrapped) line at the specified offset
See Also:
StyledTextContent.getLineAtOffset(int)

getLineCount

public int getLineCount()
Description copied from interface: StyledTextContent
Enabled: Return the number of lines. Should answer 1 when no text is specified. The StyledText widget relies on this behavior for drawing the cursor.

Specified by:
getLineCount in interface StyledTextContent
Returns:
the number of visual (wrapped) lines
See Also:
StyledTextContent.getLineCount()

getLineDelimiter

public String getLineDelimiter()
Description copied from interface: StyledTextContent
Enabled: Return the line delimiter that should be used by the StyledText widget when inserting new lines. New lines entered using key strokes and paste operations use this line delimiter. Implementors may use System.getProperty("line.separator") to return the platform line delimiter.

Specified by:
getLineDelimiter in interface StyledTextContent
Returns:
the line delimiter that should be used by the StyledText widget when inserting new lines.
See Also:
StyledTextContent.getLineDelimiter()

getOffsetAtLine

public int getOffsetAtLine(int lineIndex)
Description copied from interface: StyledTextContent
Enabled: Return the character offset of the first character of the given line.

NOTE: When there is no text (i.e., no lines), getOffsetAtLine(0) is a valid call that should return 0.

Specified by:
getOffsetAtLine in interface StyledTextContent
Parameters:
lineIndex - index of the line. The first line is at index 0.
Returns:
the start offset of the visual (wrapped) line at the given index
See Also:
StyledTextContent.getOffsetAtLine(int)

getTextRange

public String getTextRange(int start,
                           int length)
Description copied from interface: StyledTextContent
Enabled: Returns a string representing the content at the given range.

Specified by:
getTextRange in interface StyledTextContent
Parameters:
start - the start offset of the text to return. Offset 0 is the first character of the document.
length - the length of the text to return
Returns:
the text at the given range
See Also:
StyledTextContent.getTextRange(int, int)

getVisualLineCount

int getVisualLineCount()
Returns the number of visual (wrapped) lines.

Returns:
the number of visual (wrapped) lines

getWordEnd

private int getWordEnd(String line,
                       int startOffset,
                       int offset)
Returns the offset of the character after the word at the specified offset.

Words are separated by spaces. Trailing spaces are considered part of the word.

Parameters:
line - logical line the word is in
startOffset - start offset of the line, relative to the start of the logical line.
offset - offset of the word to return the end of, relative to the start of the visual line.
Returns:
the offset of the character after the word at the specified offset.

getWordStart

private int getWordStart(String line,
                         int startOffset,
                         int offset)
Returns the start offset of the word at the specified offset. There are two classes of words formed by a sequence of characters:

Words are separated by spaces. Trailing spaces are considered part of the word.

Parameters:
line - logical line the word is in
startOffset - start offset of the line, relative to the start of the logical line.
offset - offset of the word to return the start of, relative to the start of the visual line.
Returns:
the start offset of the word at the specified offset.

removeTextChangeListener

public void removeTextChangeListener(TextChangeListener listener)
Description copied from interface: StyledTextContent
Enabled: Remove the specified text changed listener.

Specified by:
removeTextChangeListener in interface StyledTextContent
Parameters:
listener - the listener
See Also:
StyledTextContent.removeTextChangeListener(TextChangeListener)

reset

void reset(int startLine,
           int lineCount)
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.

Parameters:
startLine - index of the first visual line
lineCount - number of visual lines

reset

private int reset(int startLine,
                  int lineCount,
                  boolean wrap)
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.

Parameters:
startLine - index of the first visual line
lineCount - number of visual lines
wrap - true=rewrap the logical lines in the reset visual range false=don't rewrap lines. Visual lines will be left in an inconsistent state since there will be a range of unwrapped and unknown lines.
Returns:
the first visual line that was reset

resetVisualLines

private void resetVisualLines(int startLine,
                              int lineCount)
Reset the visual (wrapped) lines in the specified range.

Parameters:
startLine - index of the first visual line
lineCount - number of visual lines

replaceTextRange

public void replaceTextRange(int start,
                             int replaceLength,
                             String text)
Description copied from interface: StyledTextContent
Enabled: Replace the text with "newText" starting at position "start" for a length of "replaceLength".

Implementors have to notify the TextChangeListeners that were added using addTextChangeListener before and after the content is changed. A TextChangingEvent has to be sent to the textChanging method before the content is changed and a TextChangedEvent has to be sent to the textChanged method after the content has changed. The text change that occurs after the TextChangingEvent has been sent has to be consistent with the data provided in the TextChangingEvent. This data will be cached by the widget and will be used when the TextChangedEvent is received.

The TextChangingEvent should be set as follows:

NOTE: newLineCount is the number of inserted lines and replaceLineCount is the number of deleted lines based on the change that occurs visually. For example:

Specified by:
replaceTextRange in interface StyledTextContent
Parameters:
start - start offset of text to replace, none of the offsets include delimiters of preceeding lines, offset 0 is the first character of the document
replaceLength - start offset of text to replace
See Also:
StyledTextContent.replaceTextRange(int, int, String)

setText

public void setText(String text)
Description copied from interface: StyledTextContent
Enabled: Set text to "text". Implementors have to send a TextChangedEvent to the textSet method of the TextChangeListeners that were added using addTextChangeListener.

Specified by:
setText in interface StyledTextContent
Parameters:
text - the new text
See Also:
StyledTextContent.setText(String)

setVisualLine

private void setVisualLine(int visualLineIndex,
                           int visualLineOffset,
                           int visualLineLength)
Set the line wrap data for the specified visual (wrapped) line.

Parameters:
visualLineIndex - index of the visual line
visualLineOffset - start offset of the visual line, relative to the start of the document
visualLineLength - length of the visual line

textChanged

void textChanged(int startOffset,
                 int newLineCount,
                 int replaceLineCount,
                 int newCharCount,
                 int replaceCharCount)
Recalculates the line wrap for the lines affected by the text change.

Parameters:
startOffset - the start offset of the text change
newLineCount - the number of inserted lines
replaceLineCount - the number of deleted lines
newCharCount - the number of new characters
replaceCharCount - the number of deleted characters

getTextWidth

private int getTextWidth(String line,
                         int logicalLineOffset,
                         int visualLineOffset,
                         int visualLineLength,
                         StyleRange[] styles,
                         int startX,
                         GC gc)
Measure the width of a segment in the specified logical line.

Parameters:
line - the logical line
logicalLineOffset - start offset of the logical line, relative to the start of the document
visualLineOffset - offset to start measuring at/start offset of the visual line
visualLineLength - length of the segment to measure/the visual line
styles - StyleRanges to use during measuring
startX - x position of the visual line relative to the start of the logical line
gc - GC to use for measuring

wrapLine

private int[] wrapLine(String line,
                       int logicalLineOffset,
                       int visualLineOffset,
                       int startX,
                       int width,
                       int numChars,
                       StyleRange[] styles,
                       GC gc)
Wrap the given logical line at the specified offset. Called repeatedly until the entire logical lines has been split into visual (wrapped) lines.

Parameters:
line - the logical line
logicalLineOffset - offset of the logical line, relative to the beginning of the content
visualLineOffset - start offset of the new visual line, relative to the start of the logical line.
startX - x position of visualLineOffset, relative to the beginning of the logical line
width - width to wrap the line to
numChars - average number of characters that fit into width
styles - StyleRanges to use for measuring the wrapped line
gc - GC to use for measuring
Returns:
int[0]=length of the new visual line, int[1]=width in pixels of the new visual line

wrapLineRange

private int wrapLineRange(int startLine,
                          int endLine,
                          int visualLineIndex)
Wrap the logical lines in the given range at the current client area width of the StyledText widget

Parameters:
startLine - first logical line to wrap
endLine - line after last logical line
visualLineIndex - visual (wrapped) line index that startLine corresponds to.
Returns:
index of the line following the last wrapped line

wrapLineRange

private int wrapLineRange(int startLine,
                          int endLine,
                          int visualLineIndex,
                          int width)
Wrap the lines in the given range. Skip lines that have already been wrapped.

Parameters:
startLine - first logical line to wrap
endLine - line after last logical line
visualLineIndex - visual (wrapped) line index that startLine corresponds to.
width - line width to wrap at
Returns:
index of last wrapped line

wrapLines

void wrapLines()
Wrap all logical lines at the current client area width of the StyledText widget


wrapLines

void wrapLines(int width)
Wrap all logical lines at the given width.

Parameters:
width - width to wrap lines at


comments?