Class TextEditComponent

All Implemented Interfaces:
KeyboardInputHandler, MouseInteractionController, ClipboardOwner

public class TextEditComponent extends GuiComponent implements ClipboardOwner
A full-featured text editor component rendered in 3D space.

Extends GuiComponent to integrate keyboard focus management and mouse interaction with a multi-line text editing surface. The editor is backed by a Page model containing TextLine instances and rendered via a TextCanvas.

Supported editing features:

  • Cursor navigation with arrow keys, Home, End, Page Up, and Page Down
  • Text selection via Shift + arrow keys
  • Clipboard operations: Ctrl+C (copy), Ctrl+X (cut), Ctrl+V (paste), Ctrl+A (select all)
  • Word-level cursor movement with Ctrl+Left and Ctrl+Right
  • Tab indentation and Shift+Tab dedentation for single lines and block selections
  • Backspace dedentation of selected blocks (removes 4 spaces of indentation)
  • Automatic scrolling when the cursor moves beyond the visible area

Usage example:


 // Create a look and feel (or use defaults)
 LookAndFeel lookAndFeel = new LookAndFeel();

 // Create the text editor at a position in 3D space
 TextEditComponent editor = new TextEditComponent(
     new Transform(new Point3D(0, 0, 500)),  // position in world
     viewPanel,                                // the active ViewPanel
     new Point2D(800, 600),                    // size in world coordinates
     lookAndFeel
 );

 // Set initial content
 editor.setText("Hello, World!\nSecond line of text.");

 // Add to the scene
 viewPanel.getRootShapeCollection().addShape(editor);
 
See Also:
  • Field Details

    • scrolledCharacters

      public int scrolledCharacters
      The number of characters the view is scrolled horizontally.
    • scrolledLines

      public int scrolledLines
      The number of lines the view is scrolled vertically.
    • selecting

      public boolean selecting
      Whether the user is currently in selection mode (Shift key held during navigation).
    • selectionStart

      public TextPointer selectionStart
      Selection start and end pointers.
    • selectionEnd

      public TextPointer selectionEnd
    • cursorLocation

      public TextPointer cursorLocation
      The current cursor position in the text (row and column).
  • Constructor Details

    • TextEditComponent

      public TextEditComponent(Transform transform, ViewPanel viewPanel, Point2D sizeInWorldCoordinates, LookAndFeel lookAndFeel)
      Creates a new text editor component positioned in 3D space.

      The editor dimensions in rows and columns are computed from the given world-coordinate size and the font character dimensions defined in TextCanvas. A TextCanvas is created internally and added as a child shape.

      Parameters:
      transform - the position and orientation of the editor in 3D space
      viewPanel - the view panel this editor belongs to
      sizeInWorldCoordinates - the editor size in world coordinates (width, height); determines the number of visible columns and rows
      lookAndFeel - the color configuration for the editor's visual appearance
  • Method Details

    • clearSelection

      public void clearSelection()
      Clears the current text selection by setting the selection end to match the selection start, effectively making the selection empty.

      A full page repaint is scheduled to remove the visual selection highlight.

    • copyToClipboard

      public void copyToClipboard()
      Copies the currently selected text to the system clipboard.

      If no text is selected (i.e., selection start equals selection end), this method does nothing. Multi-line selections are joined with newline characters.

      See Also:
    • cutToClipboard

      public void cutToClipboard()
      Cuts the currently selected text to the system clipboard.

      This copies the selected text to the clipboard via copyToClipboard(), then deletes the selection from the page and triggers a full repaint.

      See Also:
    • deleteSelection

      public void deleteSelection()
      Deletes the currently selected text from the page.

      After deletion, the selection is cleared and the cursor is moved to the position where the selection started.

      See Also:
    • ensureSelectionOrder

      public void ensureSelectionOrder()
      Ensures that selectionStart is smaller than selectionEnd.

      If the start pointer is after the end pointer (e.g., when the user selected text backwards), the two pointers are swapped so that subsequent operations can iterate from start to end.

    • getClipboardContents

      public String getClipboardContents()
      Retrieves the current text contents of the system clipboard.
      Returns:
      the clipboard text content, or an empty string if the clipboard is empty or does not contain text
    • setClipboardContents

      public void setClipboardContents(String contents)
      Places the given string into the system clipboard so that it can be pasted into other applications.
      Parameters:
      contents - the text to place on the clipboard
      See Also:
    • goToLine

      public void goToLine(int Line)
      Scrolls to and positions the cursor at the beginning of the specified line.

      The view is scrolled so the target line is visible, the cursor is placed at the start of that line (column 0), and a full repaint is triggered.

      Parameters:
      Line - the zero-based line number to navigate to
    • insertText

      public void insertText(String txt)
      Inserts the given text string at the current cursor position.

      The text is processed character by character. Special characters are handled as editing operations:

      • DEL -- deletes the character at the cursor
      • ENTER -- splits the current line at the cursor
      • BACKSPACE -- deletes the character before the cursor

      All other printable characters are inserted at the cursor position, advancing the cursor column by one for each character.

      Parameters:
      txt - the text to insert; null values are silently ignored
    • keyPressed

      public boolean keyPressed(KeyEvent event, ViewPanel viewPanel)
      Handles a key press event by routing it through the editor's input processing pipeline.

      This method delegates to the parent GuiComponent.keyPressed(KeyEvent, ViewPanel) (which handles ESC for focus release), then processes the key event for text editing, marks the affected row as dirty, adjusts scroll boundaries, and repaints as needed.

      Specified by:
      keyPressed in interface KeyboardInputHandler
      Overrides:
      keyPressed in class GuiComponent
      Parameters:
      event - the keyboard event
      viewPanel - the view panel that dispatched this event
      Returns:
      always true, indicating the event was consumed
    • lostOwnership

      public void lostOwnership(Clipboard aClipboard, Transferable aContents)
      Called when this editor loses ownership of the system clipboard.

      This is an empty implementation of the ClipboardOwner interface; no action is taken when clipboard ownership is lost.

      Specified by:
      lostOwnership in interface ClipboardOwner
      Parameters:
      aClipboard - the clipboard that this editor previously owned
      aContents - the contents that were previously placed on the clipboard
    • markRowDirty

      public void markRowDirty()
      Marks the current cursor row as dirty, scheduling it for repaint on the next rendering cycle.
    • pasteFromClipboard

      public void pasteFromClipboard()
      Pastes text from the system clipboard at the current cursor position.
      See Also:
    • processDel

      public void processDel()
      Processes the Delete key action.

      If there is no active selection, deletes the character at the cursor position. If the cursor is at the end of the line, the next line is merged into the current one. If there is an active selection, the entire selection is deleted.

    • repaintPage

      public void repaintPage()
      Repaints the entire visible page area onto the text canvas.

      Iterates over every visible cell (row and column), applying the appropriate foreground and background colors based on whether the cell is the cursor position, part of a selection, or a tab stop margin. Characters are read from the underlying Page model with scroll offsets applied.

    • repaintRow

      public void repaintRow(int rowNumber)
      Repaints a single row of the editor.

      Note: the current implementation delegates to repaintPage() and repaints the entire page. This is a candidate for optimization.

      Parameters:
      rowNumber - the zero-based row index to repaint
    • scroll

      public void scroll(int charactersToScroll, int linesToScroll)
      Scrolls the visible editor area by the specified number of characters and lines.

      Scroll offsets are clamped so they never go below zero. A full page repaint is scheduled after scrolling.

      Parameters:
      charactersToScroll - the number of characters to scroll horizontally (positive = right, negative = left)
      linesToScroll - the number of lines to scroll vertically (positive = down, negative = up)
    • setText

      public void setText(String text)
      Replaces the entire editor content with the given text.

      Resets the cursor to position (0, 0), clears all scroll offsets and selections, creates a fresh Page, inserts the text, and triggers a full repaint.

      Parameters:
      text - the new text content for the editor; may contain newline characters to create multiple lines