java.lang.Object
eu.svjatoslav.sixth.e3d.gui.textEditorComponent.TextLine

public class TextLine extends Object
Represents a single line of text in the text editor.

Internally stores a mutable list of Character objects, one per character in the line. Provides operations for inserting, cutting, and copying substrings, as well as indentation manipulation (adding or removing leading spaces).

Lines automatically trim trailing whitespace via the internal pack() method, which is invoked after most mutating operations. This ensures that lines never store unnecessary trailing space characters.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty text line with no characters.
    Creates a text line initialized with the given string.
    Creates a text line from an existing list of Character objects.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addIdent(int amount)
    Adds indentation (leading spaces) to the beginning of this line.
    copySubString(int from, int until)
    Removes characters from the specified range and returns them as a string.
    void
    cutFromBeginning(int charactersToCut)
    Removes the specified number of characters from the beginning of this line.
    cutSubString(int from, int until)
    Extracts a substring from this line, removing those characters and returning them.
    void
    cutUntilEnd(int col)
    Truncates this line at the specified column, discarding all characters from that position to the end.
    char
    Returns the character at the specified column position.
    Returns the internal list of Character objects backing this line.
    int
    Returns the indentation level of this line, measured as the number of leading space characters before the first non-space character.
    int
    Returns the length of this line (number of characters, excluding trimmed trailing whitespace).
    getSubLine(int from, int until)
    Returns a new TextLine containing the characters from this line in the range [from, until).
    getSubString(int from, int until)
    Returns a substring of this line from column from (inclusive) to column until (exclusive).
    void
    insertCharacter(int col, char value)
    Inserts a single character at the specified column position.
    void
    insertString(int col, String value)
    Inserts a string at the specified column position.
    void
    insertTextLine(int col, TextLine textLine)
    Inserts all characters from another TextLine at the specified column.
    boolean
    Returns whether this line contains no characters.
    void
    removeCharacter(int col)
    Removes the character at the specified column position.
    void
    setValue(String string)
    Replaces the entire contents of this line with the given string.
    Returns the string representation of this line by concatenating all character values.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TextLine

      public TextLine()
      Creates an empty text line with no characters.
    • TextLine

      public TextLine(List<Character> value)
      Creates a text line from an existing list of Character objects.

      Trailing whitespace is automatically trimmed via pack().

      Parameters:
      value - the list of characters to initialize this line with
    • TextLine

      public TextLine(String value)
      Creates a text line initialized with the given string.

      Each character in the string is converted to a Character object. Trailing whitespace is automatically trimmed.

      Parameters:
      value - the string to initialize this line with
  • Method Details

    • addIdent

      public void addIdent(int amount)
      Adds indentation (leading spaces) to the beginning of this line.

      If the line is empty, no indentation is added. Otherwise, the specified number of space characters are prepended to the line.

      Parameters:
      amount - the number of space characters to prepend
    • copySubString

      public String copySubString(int from, int until)
      Removes characters from the specified range and returns them as a string.

      This is a destructive operation: the characters in the range [from, until) are removed from this line. If the line is shorter than until, it is padded with spaces before extraction. Trailing whitespace is trimmed after removal.

      Parameters:
      from - the start index (inclusive) of the range to extract
      until - the end index (exclusive) of the range to extract
      Returns:
      the extracted characters as a string
    • cutFromBeginning

      public void cutFromBeginning(int charactersToCut)
      Removes the specified number of characters from the beginning of this line.

      If charactersToCut exceeds the line length, the entire line is cleared. If charactersToCut is zero, no changes are made.

      Parameters:
      charactersToCut - the number of leading characters to remove
    • cutSubString

      public String cutSubString(int from, int until)
      Extracts a substring from this line, removing those characters and returning them.

      Characters in the range [from, until) are removed from this line and returned as a string. Characters outside the range are retained. If the line is shorter than until, it is padded with spaces before extraction. Trailing whitespace is trimmed after the cut.

      Parameters:
      from - the start index (inclusive) of the range to cut
      until - the end index (exclusive) of the range to cut
      Returns:
      the cut characters as a string
    • cutUntilEnd

      public void cutUntilEnd(int col)
      Truncates this line at the specified column, discarding all characters from that position to the end.

      If col is greater than or equal to the current line length, no changes are made.

      Parameters:
      col - the column index at which to truncate (exclusive; characters at indices 0 through col - 1 are kept)
    • getCharForLocation

      public char getCharForLocation(int col)
      Returns the character at the specified column position.

      If the column is beyond the end of this line, a space character is returned.

      Parameters:
      col - the zero-based column index
      Returns:
      the character at the given column, or ' ' if out of bounds
    • getChars

      public List<Character> getChars()
      Returns the internal list of Character objects backing this line.

      Note: the returned list is the live internal list. Modifications to the returned list will directly affect this line.

      Returns:
      the mutable list of characters in this line
    • getIdent

      public int getIdent()
      Returns the indentation level of this line, measured as the number of leading space characters before the first non-space character.

      If the line is empty, returns 0.

      Returns:
      the number of leading space characters
      Throws:
      RuntimeException - if the line is non-empty but contains only spaces (should not occur due to trailing whitespace trimming by pack())
    • getLength

      public int getLength()
      Returns the length of this line (number of characters, excluding trimmed trailing whitespace).
      Returns:
      the number of characters in this line
    • getSubLine

      public TextLine getSubLine(int from, int until)
      Returns a new TextLine containing the characters from this line in the range [from, until).

      If until exceeds the line length, only the available characters are included. The returned line is an independent copy.

      Parameters:
      from - the start index (inclusive)
      until - the end index (exclusive)
      Returns:
      a new TextLine with the specified sub-range of characters
    • getSubString

      public String getSubString(int from, int until)
      Returns a substring of this line from column from (inclusive) to column until (exclusive).

      If the requested range extends beyond the line length, space characters are used for positions past the end of the line.

      Parameters:
      from - the start column (inclusive)
      until - the end column (exclusive)
      Returns:
      the substring in the specified range
    • insertCharacter

      public void insertCharacter(int col, char value)
      Inserts a single character at the specified column position.

      If the column is beyond the current line length, the line is padded with spaces up to that position. Trailing whitespace is trimmed after insertion.

      Parameters:
      col - the zero-based column at which to insert
      value - the character to insert
    • insertString

      public void insertString(int col, String value)
      Inserts a string at the specified column position.

      Each character in the string is inserted sequentially starting at col. If the column is beyond the current line length, the line is padded with spaces. Trailing whitespace is trimmed after insertion.

      Parameters:
      col - the zero-based column at which to start inserting
      value - the string to insert
    • insertTextLine

      public void insertTextLine(int col, TextLine textLine)
      Inserts all characters from another TextLine at the specified column.

      If the column is beyond the current line length, the line is padded with spaces. Trailing whitespace is trimmed after insertion.

      Parameters:
      col - the zero-based column at which to start inserting
      textLine - the text line whose characters will be inserted
    • isEmpty

      public boolean isEmpty()
      Returns whether this line contains no characters.

      Because trailing whitespace is trimmed, an empty line means there are no visible characters on this line.

      Returns:
      true if the line has no characters, false otherwise
    • removeCharacter

      public void removeCharacter(int col)
      Removes the character at the specified column position.

      If the column is beyond the end of the line, no changes are made.

      Parameters:
      col - the zero-based column of the character to remove
    • setValue

      public void setValue(String string)
      Replaces the entire contents of this line with the given string.

      The existing characters are cleared, and each character from the string is added as a new Character object. Trailing whitespace is trimmed.

      Parameters:
      string - the new text content for this line
    • toString

      public String toString()
      Returns the string representation of this line by concatenating all character values.
      Overrides:
      toString in class Object
      Returns:
      the text content of this line as a String