Package jexer

Class TEditorWidget

  • All Implemented Interfaces:
    java.lang.Comparable<TWidget>, EditMenuUser

    public class TEditorWidget
    extends TWidget
    implements EditMenuUser
    TEditorWidget displays an editable text document. It is unaware of scrolling behavior, but can respond to mouse and keyboard events.
    • Field Detail

      • document

        protected Document document
        The document being edited.
    • Constructor Detail

      • TEditorWidget

        public TEditorWidget​(TWidget parent,
                             java.lang.String text,
                             int x,
                             int y,
                             int width,
                             int height)
        Public constructor.
        Parameters:
        parent - parent widget
        text - text on the screen
        x - column relative to parent
        y - row relative to parent
        width - width of text area
        height - height of text area
    • Method Detail

      • onMouseDown

        public void onMouseDown​(TMouseEvent mouse)
        Handle mouse press events.
        Overrides:
        onMouseDown in class TWidget
        Parameters:
        mouse - mouse button press event
      • onMouseUp

        public void onMouseUp​(TMouseEvent mouse)
        Handle mouse release events.
        Overrides:
        onMouseUp in class TWidget
        Parameters:
        mouse - mouse button release event
      • onMouseMotion

        public void onMouseMotion​(TMouseEvent mouse)
        Handle mouse motion events.
        Overrides:
        onMouseMotion in class TWidget
        Parameters:
        mouse - mouse motion event
      • onKeypress

        public void onKeypress​(TKeypressEvent keypress)
        Handle keystrokes.
        Overrides:
        onKeypress in class TWidget
        Parameters:
        keypress - keystroke event
      • onResize

        public void onResize​(TResizeEvent resize)
        Method that subclasses can override to handle window/screen resize events.
        Overrides:
        onResize in class TWidget
        Parameters:
        resize - resize event
      • onCommand

        public void onCommand​(TCommandEvent command)
        Handle posted command events.
        Overrides:
        onCommand in class TWidget
        Parameters:
        command - command event
      • draw

        public void draw()
        Draw the text box.
        Overrides:
        draw in class TWidget
      • isAutoWrap

        public boolean isAutoWrap()
        Get the wrapping behavior.
        Returns:
        true if the editor automatically wraps text to fit in the margin
      • setAutoWrap

        public void setAutoWrap​(boolean autoWrap)
        Set the wrapping behavior.
        Parameters:
        autoWrap - if true, automatically wrap text to fit in the margin
      • setUndoLevel

        public void setUndoLevel​(int undoLevel)
        Set the undo level.
        Parameters:
        undoLevel - the maximum number of undo operations
      • setMargin

        public void setMargin​(int margin)
        Set the right margin.
        Parameters:
        margin - column number, or 0 to disable
      • getLineCount

        public int getLineCount()
        Get the number of lines in the underlying Document.
        Returns:
        the number of lines
      • getVisibleRowNumber

        public int getVisibleRowNumber()
        Get the current visible top row number. 1-based.
        Returns:
        the visible top row number. Row 1 is the first row.
      • setVisibleRowNumber

        public void setVisibleRowNumber​(int row)
        Set the current visible row number. 1-based.
        Parameters:
        row - the new visible row number. Row 1 is the first row.
      • getEditingRowNumber

        public int getEditingRowNumber()
        Get the current editing row number. 1-based.
        Returns:
        the editing row number. Row 1 is the first row.
      • setEditingRowNumber

        public void setEditingRowNumber​(int row)
        Set the current editing row number. 1-based.
        Parameters:
        row - the new editing row number. Row 1 is the first row.
      • getVisibleColumnNumber

        public int getVisibleColumnNumber()
        Set the current visible column number. 1-based.
        Returns:
        the visible column number. Column 1 is the first column.
      • setVisibleColumnNumber

        public void setVisibleColumnNumber​(int column)
        Set the current visible column number. 1-based.
        Parameters:
        column - the new visible column number. Column 1 is the first column.
      • getEditingColumnNumber

        public int getEditingColumnNumber()
        Get the current editing column number. 1-based.
        Returns:
        the editing column number. Column 1 is the first column.
      • setEditingColumnNumber

        public void setEditingColumnNumber​(int column)
        Set the current editing column number. 1-based.
        Parameters:
        column - the new editing column number. Column 1 is the first column.
      • getMaximumRowNumber

        public int getMaximumRowNumber()
        Get the maximum possible row number. 1-based.
        Returns:
        the maximum row number. Row 1 is the first row.
      • getMaximumColumnNumber

        public int getMaximumColumnNumber()
        Get the maximum possible column number. 1-based.
        Returns:
        the maximum column number. Column 1 is the first column.
      • getEditingRawLine

        public java.lang.String getEditingRawLine​(int row)
        Get the current editing row plain text. 1-based.
        Parameters:
        row - the editing row number. Row 1 is the first row.
        Returns:
        the plain text of the row
      • isDirty

        public boolean isDirty()
        Get the dirty value.
        Returns:
        true if the buffer is dirty
      • setNotDirty

        public void setNotDirty()
        Unset the dirty flag.
      • isOverwrite

        public boolean isOverwrite()
        Get the overwrite value.
        Returns:
        true if new text will overwrite old text
      • saveToFilename

        public void saveToFilename​(java.lang.String filename)
                            throws java.io.IOException
        Save contents to file.
        Parameters:
        filename - file to save to
        Throws:
        java.io.IOException - if a java.io operation throws
      • wrapText

        public void wrapText()
        Reflow the text to fit inside the margin.
      • setSelection

        public void setSelection​(int startRow,
                                 int startColumn,
                                 int endRow,
                                 int endColumn)
        Set the selection.
        Parameters:
        startRow - the starting row number. 0-based: row 0 is the first row.
        startColumn - the starting column number. 0-based: column 0 is the first column.
        endRow - the ending row number. 0-based: row 0 is the first row.
        endColumn - the ending column number. 0-based: column 0 is the first column.
      • getSelection

        public java.lang.String getSelection()
        Copy text within the selection bounds to a string.
        Returns:
        the selection as a string, or null if there is no selection
      • getSelectionStartRow

        public int getSelectionStartRow()
        Get the selection starting row number.
        Returns:
        the starting row number, or -1 if there is no selection. 0-based: row 0 is the first row.
      • getSelectionStartColumn

        public int getSelectionStartColumn()
        Get the selection starting column number.
        Returns:
        the starting column number, or -1 if there is no selection. 0-based: column 0 is the first column.
      • getSelectionEndRow

        public int getSelectionEndRow()
        Get the selection ending row number.
        Returns:
        the ending row number, or -1 if there is no selection. 0-based: row 0 is the first row.
      • getSelectionEndColumn

        public int getSelectionEndColumn()
        Get the selection ending column number.
        Returns:
        the ending column number, or -1 if there is no selection. 0-based: column 0 is the first column.
      • unsetSelection

        public void unsetSelection()
        Unset the selection.
      • replaceSelection

        public void replaceSelection​(java.lang.String text)
        Replace whatever is being selected with new text. If not in selection, nothing is replaced.
        Parameters:
        text - the new replacement text
      • hasSelection

        public boolean hasSelection()
        Check if selection is available.
        Returns:
        true if a selection has been made
      • getText

        public java.lang.String getText()
        Get the entire contents of the editor as one string.
        Returns:
        the editor contents
      • setText

        public void setText​(java.lang.String text)
        Set the entire contents of the editor from one string.
        Parameters:
        text - the new contents
      • isEditMenuCut

        public boolean isEditMenuCut()
        Check if the cut menu item should be enabled.
        Specified by:
        isEditMenuCut in interface EditMenuUser
        Returns:
        true if the cut menu item should be enabled
      • isEditMenuCopy

        public boolean isEditMenuCopy()
        Check if the copy menu item should be enabled.
        Specified by:
        isEditMenuCopy in interface EditMenuUser
        Returns:
        true if the copy menu item should be enabled
      • isEditMenuPaste

        public boolean isEditMenuPaste()
        Check if the paste menu item should be enabled.
        Specified by:
        isEditMenuPaste in interface EditMenuUser
        Returns:
        true if the paste menu item should be enabled
      • isEditMenuClear

        public boolean isEditMenuClear()
        Check if the clear menu item should be enabled.
        Specified by:
        isEditMenuClear in interface EditMenuUser
        Returns:
        true if the clear menu item should be enabled
      • undo

        public void undo()
        Undo an edit.
      • redo

        public void redo()
        Redo an edit.
      • cleanWhitespace

        public void cleanWhitespace()
        Trim trailing whitespace from lines and trailing empty lines from the document.
      • setHighlighting

        public void setHighlighting​(boolean enabled)
        Set keyword highlighting.
        Parameters:
        enabled - if true, enable keyword highlighting