Class DefaultContent

java.lang.Object
org.eclipse.swt.custom.DefaultContent
All Implemented Interfaces:
StyledTextContent

class DefaultContent extends Object implements StyledTextContent
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) int
     
    (package private) int
     
    (package private) int
     
    (package private) int
     
    (package private) int
     
    (package private) int
     
    private static final String
     
    (package private) int[][]
     
    (package private) int
     
    (package private) int
     
    (package private) List<StyledTextListener>
     
    (package private) char[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new DefaultContent and initializes it.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    addLineIndex(int start, int length)
    Adds a line to the end of the line indexes array.
    (package private) int[][]
    addLineIndex(int start, int length, int[][] linesArray, int count)
    Adds a line index to the end of linesArray.
    void
    Adds a TextChangeListener listening for TextChangingEvent and TextChangedEvent.
    (package private) void
    adjustGap(int position, int sizeHint, int line)
    Adjusts the gap to accommodate a text change that is occurring.
    (package private) void
    delete(int position, int length, int numLines)
    Deletes text.
    (package private) void
    error(int code)
    Reports an SWT error.
    (package private) void
    expandLinesBy(int numLines)
    Increases the line indexes array to accommodate more lines.
    (package private) boolean
    Returns whether or not a gap exists in the text store.
    int
    Return the number of characters in the content.
    (package private) String
    getFullLine(int index)
    Returns the line at the given index with delimiters.
    getLine(int index)
    Returns the line at index without delimiters.
    int
    getLineAtOffset(int charPosition)
    Returns the line at the given offset.
    (package private) int
    Returns the line index at the given physical offset.
    int
    Return the number of lines.
    Returns the line delimiter that should be used by the StyledText widget when inserting new lines.
    int
    getOffsetAtLine(int lineIndex)
    Returns the logical offset of the given line.
    (package private) String
    getPhysicalLine(int index)
    Returns the physical line at the given index (i.e., with delimiters and the gap).
    (package private) String
    getPhysicalText(int start, int length)
    Returns a string representing the continuous content of the text store.
    getTextRange(int start, int length)
    Returns a string representing the logical content of the text store (i.e., gap stripped out).
    (package private) void
    Calculates the indexes of each line in the text store.
    (package private) int[][]
    indexLines(int offset, int length, int numLines)
    Calculates the indexes of each line of text in the given range.
    (package private) void
    insert(int position, String text)
    Inserts text.
    (package private) boolean
    isDelimiter(char ch)
    Returns whether or not the given character is a line delimiter.
    private boolean
    isInsideCRLF(int pos)
     
    (package private) int
    lineCount(int startOffset, int length)
    Returns the number of lines that are in the specified text.
    (package private) int
    Returns the number of lines that are in the specified text.
    (package private) void
    moveAndResizeGap(int position, int size, int newGapLine)
    Moves the gap and adjusts its size in anticipation of a text change.
    void
    Removes the specified TextChangeListener.
    void
    replaceTextRange(int start, int replaceLength, String newText)
    Replaces the text with newText starting at position start for a length of replaceLength.
    (package private) void
    Sends the text listeners the TextChanged event.
    void
    Sets the content to text and removes the gap since there are no sensible predictions about where the next change will occur.
    private void
    validateReplace(int start, int replaceLength)
    Validates the replace operation.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LineDelimiter

      private static final String LineDelimiter
    • textListeners

      List<StyledTextListener> textListeners
    • textStore

      char[] textStore
    • gapStart

      int gapStart
    • gapEnd

      int gapEnd
    • gapLine

      int gapLine
    • highWatermark

      int highWatermark
    • lowWatermark

      int lowWatermark
    • lines

      int[][] lines
    • lineCount

      int lineCount
    • expandExp

      int expandExp
    • replaceExpandExp

      int replaceExpandExp
  • Constructor Details

    • DefaultContent

      DefaultContent()
      Creates a new DefaultContent and initializes it. A StyledTextContent will always have at least one empty line.
  • Method Details

    • addLineIndex

      void addLineIndex(int start, int length)
      Adds a line to the end of the line indexes array. Increases the size of the array if necessary. lineCount is updated to reflect the new entry.

      Parameters:
      start - the start of the line
      length - the length of the line
    • addLineIndex

      int[][] addLineIndex(int start, int length, int[][] linesArray, int count)
      Adds a line index to the end of linesArray. Increases the size of the array if necessary and returns a new array.

      Parameters:
      start - the start of the line
      length - the length of the line
      linesArray - the array to which to add the line index
      count - the position at which to add the line
      Returns:
      a new array of line indexes
    • addTextChangeListener

      public void addTextChangeListener(TextChangeListener listener)
      Adds a TextChangeListener listening for TextChangingEvent and TextChangedEvent. A TextChangingEvent is sent before changes to the text occur. A TextChangedEvent is sent after changes to the text occurred.

      Specified by:
      addTextChangeListener in interface StyledTextContent
      Parameters:
      listener - the listener
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT when listener is null
    • adjustGap

      void adjustGap(int position, int sizeHint, int line)
      Adjusts the gap to accommodate a text change that is occurring.

      Parameters:
      position - the position at which a change is occurring
      sizeHint - the size of the change
      line - the line where the gap will go
    • indexLines

      void indexLines()
      Calculates the indexes of each line in the text store. Assumes no gap exists. Optimized to do less checking.
    • isDelimiter

      boolean isDelimiter(char ch)
      Returns whether or not the given character is a line delimiter. Both CR and LF are valid line delimiters.

      Parameters:
      ch - the character to test
      Returns:
      true if ch is a delimiter, false otherwise
    • isInsideCRLF

      private boolean isInsideCRLF(int pos)
    • validateReplace

      private void validateReplace(int start, int replaceLength)
      Validates the replace operation. DefaultContent will not allow the \r\n line delimiter to be split or partially deleted.

      Parameters:
      start - start offset of text to replace
      replaceLength - start offset of text to replace
    • indexLines

      int[][] indexLines(int offset, int length, int numLines)
      Calculates the indexes of each line of text in the given range.

      Parameters:
      offset - the logical start offset of the text lineate
      length - the length of the text to lineate, includes gap
      numLines - the number of lines to initially allocate for the line index array, passed in for efficiency (the exact number of lines may be known)
      Returns:
      a line indexes array where each line is identified by a start offset and a length
    • insert

      void insert(int position, String text)
      Inserts text.

      Parameters:
      position - the position at which to insert the text
      text - the text to insert
    • moveAndResizeGap

      void moveAndResizeGap(int position, int size, int newGapLine)
      Moves the gap and adjusts its size in anticipation of a text change. The gap is resized to actual size + the specified size and moved to the given position.

      Parameters:
      position - the position at which a change is occurring
      size - the size of the change
      newGapLine - the line where the gap should be put
    • lineCount

      int lineCount(int startOffset, int length)
      Returns the number of lines that are in the specified text.

      Parameters:
      startOffset - the start of the text to lineate
      length - the length of the text to lineate
      Returns:
      number of lines
    • lineCount

      int lineCount(String text)
      Returns the number of lines that are in the specified text.

      Parameters:
      text - the text to lineate
      Returns:
      number of lines in the text
    • getCharCount

      public int getCharCount()
      Description copied from interface: StyledTextContent
      Return the number of characters in the content.
      Specified by:
      getCharCount in interface StyledTextContent
      Returns:
      the logical length of the text store
    • getLine

      public String getLine(int index)
      Returns the line at index without delimiters.

      Specified by:
      getLine in interface StyledTextContent
      Parameters:
      index - the index of the line to return
      Returns:
      the logical line text (i.e., without the gap)
      Throws:
      IllegalArgumentException -
      • ERROR_INVALID_ARGUMENT when index is out of range
    • getLineDelimiter

      public String getLineDelimiter()
      Returns the line delimiter that should be used by the StyledText widget when inserting new lines. This delimiter may be different than the delimiter that is used by the StyledTextContent interface.

      Specified by:
      getLineDelimiter in interface StyledTextContent
      Returns:
      the platform line delimiter as specified in the line.separator system property.
    • getFullLine

      String getFullLine(int index)
      Returns the line at the given index with delimiters.

      Parameters:
      index - the index of the line to return
      Returns:
      the logical line text (i.e., without the gap) with delimiters
    • getPhysicalLine

      String getPhysicalLine(int index)
      Returns the physical line at the given index (i.e., with delimiters and the gap).

      Parameters:
      index - the line index
      Returns:
      the physical line
    • getLineCount

      public int getLineCount()
      Description copied from interface: StyledTextContent
      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 lines in the text store
    • getLineAtOffset

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

      Specified by:
      getLineAtOffset in interface StyledTextContent
      Parameters:
      charPosition - logical character offset (i.e., does not include gap)
      Returns:
      the line index
      Throws:
      IllegalArgumentException -
      • ERROR_INVALID_ARGUMENT when charPosition is out of range
    • getLineAtPhysicalOffset

      int getLineAtPhysicalOffset(int position)
      Returns the line index at the given physical offset.

      Parameters:
      position - physical character offset (i.e., includes gap)
      Returns:
      the line index
    • getOffsetAtLine

      public int getOffsetAtLine(int lineIndex)
      Returns the logical offset of the given line.

      Specified by:
      getOffsetAtLine in interface StyledTextContent
      Parameters:
      lineIndex - index of line
      Returns:
      the logical starting offset of the line. When there are not any lines, getOffsetAtLine(0) is a valid call that should answer 0.
      Throws:
      IllegalArgumentException -
      • ERROR_INVALID_ARGUMENT when lineIndex is out of range
    • expandLinesBy

      void expandLinesBy(int numLines)
      Increases the line indexes array to accommodate more lines.

      Parameters:
      numLines - the number to increase the array by
    • error

      void error(int code)
      Reports an SWT error.

      Parameters:
      code - the error code
    • gapExists

      boolean gapExists()
      Returns whether or not a gap exists in the text store.

      Returns:
      true if gap exists, false otherwise
    • getPhysicalText

      String getPhysicalText(int start, int length)
      Returns a string representing the continuous content of the text store.

      Parameters:
      start - the physical start offset of the text to return
      length - the physical length of the text to return
      Returns:
      the text
    • getTextRange

      public String getTextRange(int start, int length)
      Returns a string representing the logical content of the text store (i.e., gap stripped out).

      Specified by:
      getTextRange in interface StyledTextContent
      Parameters:
      start - the logical start offset of the text to return
      length - the logical length of the text to return
      Returns:
      the text
    • removeTextChangeListener

      public void removeTextChangeListener(TextChangeListener listener)
      Removes the specified TextChangeListener.

      Specified by:
      removeTextChangeListener in interface StyledTextContent
      Parameters:
      listener - the listener which should no longer be notified
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT when listener is null
    • replaceTextRange

      public void replaceTextRange(int start, int replaceLength, String newText)
      Replaces the text with newText starting at position start for a length of replaceLength. Notifies the appropriate listeners.

      When sending the TextChangingEvent, newLineCount is the number of lines that are going to be inserted and replaceLineCount is the number of lines that are going to be deleted, based on the change that occurs visually. For example:

      • (replaceText,newText) ==> (replaceLineCount,newLineCount)
      • ("","\n") ==> (0,1)
      • ("\n\n","a") ==> (2,0)
      Specified by:
      replaceTextRange in interface StyledTextContent
      Parameters:
      start - start offset of text to replace
      replaceLength - start offset of text to replace
      newText - start offset of text to replace
      Throws:
      SWTException -
      • ERROR_INVALID_ARGUMENT when the text change results in a multi byte line delimiter being split or partially deleted. Splitting a line delimiter by inserting text between the CR and LF characters of the \r\n delimiter or deleting part of this line delimiter is not supported
      See Also:
    • sendTextEvent

      void sendTextEvent(StyledTextEvent event)
      Sends the text listeners the TextChanged event.
    • setText

      public void setText(String text)
      Sets the content to text and removes the gap since there are no sensible predictions about where the next change will occur.

      Specified by:
      setText in interface StyledTextContent
      Parameters:
      text - the text
      See Also:
    • delete

      void delete(int position, int length, int numLines)
      Deletes text.

      Parameters:
      position - the position at which the text to delete starts
      length - the length of the text to delete
      numLines - the number of lines that are being deleted