Class TextEditComponent
- All Implemented Interfaces:
KeyboardInputHandler,MouseInteractionController,ClipboardOwner
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 Summary
FieldsModifier and TypeFieldDescriptionThe current cursor position in the text (row and column).intThe number of characters the view is scrolled horizontally.intThe number of lines the view is scrolled vertically.booleanWhether the user is currently in selection mode (Shift key held during navigation).Selection start and end pointers.Fields inherited from class eu.svjatoslav.sixth.e3d.gui.GuiComponent
viewPanelFields inherited from class eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape
mouseInteractionController -
Constructor Summary
ConstructorsConstructorDescriptionTextEditComponent(Transform transform, ViewPanel viewPanel, Point2D sizeInWorldCoordinates, LookAndFeel lookAndFeel) Creates a new text editor component positioned in 3D space. -
Method Summary
Modifier and TypeMethodDescriptionvoidClears the current text selection by setting the selection end to match the selection start, effectively making the selection empty.voidCopies the currently selected text to the system clipboard.voidCuts the currently selected text to the system clipboard.voidDeletes the currently selected text from the page.voidEnsures thatselectionStartis smaller thanselectionEnd.Retrieves the current text contents of the system clipboard.voidgoToLine(int Line) Scrolls to and positions the cursor at the beginning of the specified line.voidinsertText(String txt) Inserts the given text string at the current cursor position.booleankeyPressed(KeyEvent event, ViewPanel viewPanel) Handles a key press event by routing it through the editor's input processing pipeline.voidlostOwnership(Clipboard aClipboard, Transferable aContents) Called when this editor loses ownership of the system clipboard.voidMarks the current cursor row as dirty, scheduling it for repaint on the next rendering cycle.voidPastes text from the system clipboard at the current cursor position.voidProcesses the Delete key action.voidRepaints the entire visible page area onto the text canvas.voidrepaintRow(int rowNumber) Repaints a single row of the editor.voidscroll(int charactersToScroll, int linesToScroll) Scrolls the visible editor area by the specified number of characters and lines.voidsetClipboardContents(String contents) Places the given string into the system clipboard so that it can be pasted into other applications.voidReplaces the entire editor content with the given text.Methods inherited from class eu.svjatoslav.sixth.e3d.gui.GuiComponent
focusLost, focusReceived, getBorders, getDepth, getHeight, getWidth, hideBorder, keyReleased, mouseClicked, mouseEntered, mouseExitedMethods inherited from class eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape
addShape, addShape, beforeTransformHook, getGroup, getLocation, getOriginalSubShapes, getViewSpaceTracker, hideGroup, removeGroup, setBackfaceCulling, setColor, setGroupForUngrouped, setLightingManager, setMouseInteractionController, setShadingEnabled, setTransform, showGroup, transform
-
Field Details
-
scrolledCharacters
public int scrolledCharactersThe number of characters the view is scrolled horizontally. -
scrolledLines
public int scrolledLinesThe number of lines the view is scrolled vertically. -
selecting
public boolean selectingWhether the user is currently in selection mode (Shift key held during navigation). -
selectionStart
Selection start and end pointers. -
selectionEnd
-
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. ATextCanvasis created internally and added as a child shape.- Parameters:
transform- the position and orientation of the editor in 3D spaceviewPanel- the view panel this editor belongs tosizeInWorldCoordinates- the editor size in world coordinates (width, height); determines the number of visible columns and rowslookAndFeel- 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 thatselectionStartis smaller thanselectionEnd.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
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
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
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 cursorENTER-- splits the current line at the cursorBACKSPACE-- 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;nullvalues are silently ignored
-
keyPressed
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:
keyPressedin interfaceKeyboardInputHandler- Overrides:
keyPressedin classGuiComponent- Parameters:
event- the keyboard eventviewPanel- the view panel that dispatched this event- Returns:
- always
true, indicating the event was consumed
-
lostOwnership
Called when this editor loses ownership of the system clipboard.This is an empty implementation of the
ClipboardOwnerinterface; no action is taken when clipboard ownership is lost.- Specified by:
lostOwnershipin interfaceClipboardOwner- Parameters:
aClipboard- the clipboard that this editor previously ownedaContents- 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
Pagemodel 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
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
-