Top Description Methods
org.eclipse.swt.custom

public Interface StyledTextContent

Known Direct Implementers
org.eclipse.swt.custom.DefaultContent

Clients may implement the StyledTextContent interface to provide a custom store for the StyledText widget content. The StyledText widget interacts with its StyledTextContent in order to access and update the text that is being displayed and edited in the widget. A custom content implementation can be set in the widget using the StyledText.setContent API.

Method Summary

Modifier and TypeMethod and Description
public void
addTextChangeListener(TextChangeListener
the listener
listener
)

Called by StyledText to add itself as an Observer to content changes.

public int

Returns:

the number of characters in the content.
getCharCount
()

Return the number of characters in the content.

public String

Returns:

the line text without delimiters For example, if text = "\r\n\r\n", and delimiter = "\r\n", then:
  • getLine(0) == ""
  • getLine(1) == ""
  • getLine(2) == ""
or if text = "A\nBC\nD", and delimiter = "\n", then:
  • getLine(0) == "A"
  • getLine(1) == "BC"
  • getLine(2) == "D"
getLine
(int
index of the line to return. Does not include delimiters of preceding lines. Index 0 is the first line of the content.
lineIndex
)

Return the line at the given line index without delimiters.

public int

Returns:

the line index. The first line is at index 0. If the character at offset is a delimiter character, answer the line index of the line that is delimited. For example, if text = "\r\n\r\n", and delimiter = "\r\n", then:
  • getLineAtOffset(0) == 0
  • getLineAtOffset(1) == 0
  • getLineAtOffset(2) == 1
  • getLineAtOffset(3) == 1
  • getLineAtOffset(4) == 2
or if text = "A\nBC\nD", and delimiter = "\n", then:
  • getLineAtOffset(0) == 0
  • getLineAtOffset(1) == 0
  • getLineAtOffset(2) == 1
  • getLineAtOffset(3) == 1
  • getLineAtOffset(4) == 1
  • getLineAtOffset(5) == 2
  • getLineAtOffset(6) == 2
getLineAtOffset
(int
offset of the line to return. The first character of the document is at offset 0. An offset of getCharCount() is valid and should answer the line index of the last line.
offset
)

Return the line index at the given character offset.

public int

Returns:

the number of lines. For example:
  • text value ==> getLineCount
  • null ==> 1
  • "" ==> 1
  • "a\n" ==> 2
  • "\n\n" ==> 3
getLineCount
()

Return the number of lines.

public String

Returns:

the line delimiter that should be used by the StyledText widget when inserting new lines.
getLineDelimiter
()

Return the line delimiter that should be used by the StyledText widget when inserting new lines.

public int

Returns:

offset offset of the first character of the line. The first character of the document is at offset 0. The return value should include line delimiters. For example, if text = "\r\ntest\r\n" and delimiter = "\r\n", then:
  • getOffsetAtLine(0) == 0
  • getOffsetAtLine(1) == 2
  • getOffsetAtLine(2) == 8
getOffsetAtLine
(int
index of the line. The first line is at index 0.
lineIndex
)

Return the character offset of the first character of the given line.

public String

Returns:

the text at the given range
getTextRange
(int
the start offset of the text to return. Offset 0 is the first character of the document.
start
,
int
the length of the text to return
length
)

Returns a string representing the content at the given range.

public void
removeTextChangeListener(TextChangeListener
the listener which should no longer be notified
listener
)

Remove the specified text changed listener.

public void
replaceTextRange(int
start offset of text to replace, none of the offsets include delimiters of preceding lines, offset 0 is the first character of the document
start
,
int
length of text to replace
replaceLength
,
String
text to replace
text
)

Replace the text with "newText" starting at position "start" for a length of "replaceLength".

public void
setText(String
the new text
text
)

Set text to "text".

Method Detail

addTextChangeListenerback to summary
public void addTextChangeListener(TextChangeListener listener)

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.

Parameters
listener:TextChangeListener

the listener

Exceptions
IllegalArgumentException:
  • ERROR_NULL_ARGUMENT when listener is null
getCharCountback to summary
public int getCharCount()

Return the number of characters in the content.

Returns:int

the number of characters in the content.

getLineback to summary
public String getLine(int lineIndex)

Return the line at the given line index without delimiters.

Parameters
lineIndex:int

index of the line to return. Does not include delimiters of preceding lines. Index 0 is the first line of the content.

Returns:String

the line text without delimiters For example, if text = "\r\n\r\n", and delimiter = "\r\n", then:

  • getLine(0) == ""
  • getLine(1) == ""
  • getLine(2) == ""
or if text = "A\nBC\nD", and delimiter = "\n", then:
  • getLine(0) == "A"
  • getLine(1) == "BC"
  • getLine(2) == "D"
getLineAtOffsetback to summary
public int getLineAtOffset(int offset)

Return the line index at the given character offset.

Parameters
offset:int

offset of the line to return. The first character of the document is at offset 0. An offset of getCharCount() is valid and should answer the line index of the last line.

Returns:int

the line index. The first line is at index 0. If the character at offset is a delimiter character, answer the line index of the line that is delimited. For example, if text = "\r\n\r\n", and delimiter = "\r\n", then:

  • getLineAtOffset(0) == 0
  • getLineAtOffset(1) == 0
  • getLineAtOffset(2) == 1
  • getLineAtOffset(3) == 1
  • getLineAtOffset(4) == 2
or if text = "A\nBC\nD", and delimiter = "\n", then:
  • getLineAtOffset(0) == 0
  • getLineAtOffset(1) == 0
  • getLineAtOffset(2) == 1
  • getLineAtOffset(3) == 1
  • getLineAtOffset(4) == 1
  • getLineAtOffset(5) == 2
  • getLineAtOffset(6) == 2
getLineCountback to summary
public int getLineCount()

Return the number of lines. Should answer 1 when no text is specified. The StyledText widget relies on this behavior for drawing the cursor.

Returns:int

the number of lines. For example:

  • text value ==> getLineCount
  • null ==> 1
  • "" ==> 1
  • "a\n" ==> 2
  • "\n\n" ==> 3
getLineDelimiterback to summary
public String getLineDelimiter()

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.lineSeparator() to return the platform line delimiter.

Returns:String

the line delimiter that should be used by the StyledText widget when inserting new lines.

getOffsetAtLineback to summary
public int getOffsetAtLine(int lineIndex)

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.

Parameters
lineIndex:int

index of the line. The first line is at index 0.

Returns:int

offset offset of the first character of the line. The first character of the document is at offset 0. The return value should include line delimiters. For example, if text = "\r\ntest\r\n" and delimiter = "\r\n", then:

  • getOffsetAtLine(0) == 0
  • getOffsetAtLine(1) == 2
  • getOffsetAtLine(2) == 8
getTextRangeback to summary
public String getTextRange(int start, int length)

Returns a string representing the content at the given range.

Parameters
start:int

the start offset of the text to return. Offset 0 is the first character of the document.

length:int

the length of the text to return

Returns:String

the text at the given range

removeTextChangeListenerback to summary
public void removeTextChangeListener(TextChangeListener listener)

Remove the specified text changed listener.

Parameters
listener:TextChangeListener

the listener which should no longer be notified

Exceptions
IllegalArgumentException:
  • ERROR_NULL_ARGUMENT when listener is null
replaceTextRangeback to summary
public void replaceTextRange(int start, int replaceLength, String text)

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:

  • event.start = start of the replaced text
  • event.newText = text that is going to be inserted or empty String if no text will be inserted
  • event.replaceCharCount = length of text that is going to be replaced
  • event.newCharCount = length of text that is going to be inserted
  • event.replaceLineCount = number of lines that are going to be replaced
  • event.newLineCount = number of new lines that are going to be inserted
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:
  • (replaceText, newText) ==> (replaceLineCount, newLineCount)
  • ("", "\n") ==> (0, 1)
  • ("\n\n", "a") ==> (2, 0)
  • ("a", "\n\n") ==> (0, 2)
  • ("\n", "") ==> (1, 0)
Parameters
start:int

start offset of text to replace, none of the offsets include delimiters of preceding lines, offset 0 is the first character of the document

replaceLength:int

length of text to replace

text:String

text to replace

See Also
TextChangeListener
setTextback to summary
public void setText(String text)

Set text to "text". Implementors have to send a TextChangedEvent to the textSet method of the TextChangeListeners that were added using addTextChangeListener.

Parameters
text:String

the new text

See Also
TextChangeListener