Class EditorView

java.lang.Object
com.codename1.ui.Component
com.codename1.ui.editor.EditorView
All Implemented Interfaces:
Animation, Editable, StyleListener, TextInputClient
Direct Known Subclasses:
CodeView, RichView

public class EditorView extends Component implements TextInputClient

The pure Codename One text editing surface. It renders a plain text EditorDocument with its own Graphics code, owns the caret and selection, handles pointer and keyboard interaction, and captures text through the low level TextInputClient contract (soft keyboard / IME) when the platform supports it, falling back to raw keyPressed capture on physical keyboard platforms.

This class provides the shared editing behavior. Feature layers (syntax highlighting, the gutter, styled rich text) subclass it and override #paintContent(Graphics, int, int, int, int) and the document/measurement hooks.

  • Constructor Details

    • EditorView

      public EditorView(EditorHost host, boolean codeMode)

      Creates an editor view.

      Parameters
      • host: the bridge to the owning editor component / platform input source

      • codeMode: true for a code editor (autocorrect and autocapitalize disabled)

  • Method Details

    • scrollByPixels

      protected void scrollByPixels(int deltaX, int deltaY)
      Scrolls the content by the given pixel deltas (as delivered by the mouse wheel gesture).
    • getDocument

      public EditorDocument getDocument()
      Returns the backing document.
    • getUndoManager

      public UndoManager getUndoManager()
      Returns the undo manager.
    • getCaretOffset

      public int getCaretOffset()
      Returns the current caret offset.
    • setBackgroundColor

      public void setBackgroundColor(int color)
      Sets the surface background color.
    • setTextColor

      public void setTextColor(int color)
      Sets the default text color.
    • setSelectionColor

      public void setSelectionColor(int color)
      Sets the selection highlight color.
    • getTextColor

      protected int getTextColor()
      Returns the default text color used by the base renderer.
    • setText

      public void setText(String text)
      Replaces the whole document text and resets the caret and history.
    • getText

      public String getText()
      Returns the whole document text.
    • setEditableState

      public void setEditableState(boolean editable)
      Enables or disables editing.
    • isEditableState

      public boolean isEditableState()
      True when editing is enabled.
    • blur

      public void blur()
      Relinquishes focus and stops the active platform text-input session.
    • baseFontSizePixels

      protected int baseFontSizePixels()
      Returns the base editor font size in pixels.
    • setFontSizeDips

      public void setFontSizeDips(int dips)
      Optionally overrides the editor font size in device independent pixels. Pass 0 to use the font supplied by the component's style (the default, which is correctly sized for the current skin).
    • getEditorFont

      protected Font getEditorFont()
      Returns the font used to render text. By default this is the component's style font (managed and correctly sized by Codename One, and refreshed when the skin / theme reloads, which avoids stale native font handles rendering as squares). Subclasses may override to supply styled variants.
    • getLineHeight

      protected int getLineHeight()
      Returns the default height of a single visual line in pixels.
    • lineHeightAt

      protected int lineHeightAt(int line)
      Returns the height of the given line. The default is uniform (#getLineHeight()); the rich text editor overrides this for headings and font size runs.
    • uniformLineHeight

      protected boolean uniformLineHeight()
      True when every line has the same height (the common case, and the fast path). Feature layers with variable line heights override this to return false.
    • invalidateLayout

      protected void invalidateLayout()
      Invalidates the cached line layout (used by feature layers when line heights change).
    • lineTopContent

      protected int lineTopContent(int line)
      Returns the top y pixel of the given line relative to the content top (before scroll).
    • contentTotalHeight

      protected int contentTotalHeight()
      Returns the total content height in pixels.
    • lineAtContentY

      protected int lineAtContentY(int y)
      Returns the line index at the given content relative y pixel.
    • getContentLeftInset

      protected int getContentLeftInset()
      Returns the left inset (padding plus any gutter) at which text starts.
    • contentWidth

      protected int contentWidth()
      Returns the width available for text (component width minus padding and any gutter).
    • hasSelection

      public boolean hasSelection()
      True when a non empty selection exists.
    • getSelectionStart

      public int getSelectionStart()
      Returns the inclusive start offset of the selection (or the caret when none).
    • getSelectionEnd

      public int getSelectionEnd()
      Returns the exclusive end offset of the selection (or the caret when none).
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Component
    • longPointerPress

      public void longPointerPress(int x, int y)
      Description copied from class: Component
      If this Component is focused this method is invoked when the user presses and holds the pointer on the Component
      Overrides:
      longPointerPress in class Component
    • paintContent

      protected void paintContent(Graphics g, int originX, int originY, int width, int height)

      Paints the document content. originY is already offset by the scroll position, so line n is at originY + n * getLineHeight(). Subclasses override to add highlighting, a gutter or styled runs but should call through for the shared caret / selection rendering, or reimplement it.

      Parameters
      • g: the graphics context

      • originX: the x pixel at which line text starts

      • originY: the y pixel of the first line (scroll adjusted)

      • width: the content width

      • height: the content height

    • paintComposingUnderline

      protected void paintComposingUnderline(Graphics g, int line, String text, int lineStart, int lineEnd, int tx, int ly, int lineHeight, Font f)
      Draws the IME composition (marked text) underline for the portion of the active composing range that falls on the given line. The composing range is set by #setComposingText(String, int) while an input method is composing (e.g. CJK) and cleared by #finishComposing().
    • paintBeforeContent

      protected void paintBeforeContent(Graphics g, int originX, int originY, int firstLine, int lastLine, int lineHeight, Font f)
      Hook invoked before the visible lines are drawn, used by the code editor to paint the line number gutter. originY is scroll adjusted so line n sits at originY + n * lineHeight.
    • paintLineDecorations

      protected void paintLineDecorations(Graphics g, int line, String text, int x, int y, int lineHeight, Font f)
      Hook invoked after a line's text is drawn, used by the code editor to paint diagnostic squiggles.
    • lineTop

      protected int lineTop(int line)
      Returns the y pixel of the top of the given line in component local coordinates (scroll adjusted).
    • firstVisibleLine

      protected int firstVisibleLine()
      Returns the first fully or partially visible line index.
    • getHorizontalScroll

      protected int getHorizontalScroll()
      Returns the current horizontal scroll offset in pixels.
    • getVerticalScroll

      protected int getVerticalScroll()
      Returns the current vertical scroll offset in pixels.
    • getMaximumVerticalScroll

      protected int getMaximumVerticalScroll()
      Returns the largest valid vertical content offset.
    • paintLine

      protected void paintLine(Graphics g, int line, String text, int x, int y, int lineHeight, Font f)

      Paints a single line of text. Subclasses override to add syntax coloring; the base implementation draws the whole line in the default text color.

      Parameters
      • g: the graphics context

      • line: the zero based line index

      • text: the line text

      • x: the x pixel at which the line text starts

      • y: the top y pixel of the line

      • lineHeight: the line height

      • f: the font

    • offsetAtPoint

      public int offsetAtPoint(int absX, int absY)

      Returns the document offset nearest the given absolute screen point.

      Parameters
      • absX: absolute x

      • absY: absolute y

      Specified by:
      offsetAtPoint in interface TextInputClient
    • replaceRange

      protected void replaceRange(int start, int end, String text, boolean record)

      Replaces the range [start, end) with text, updating the caret, history and platform input state.

      Parameters
      • start: inclusive start offset

      • end: exclusive end offset

      • text: the replacement text (may be empty)

      • record: true to record the mutation for undo

    • documentEdited

      protected void documentEdited(int fromOffset)
      Hook invoked when the document is mutated at fromOffset, used by feature layers (e.g. the code editor) to invalidate cached tokenization from the affected line onward.
    • documentReplaced

      protected void documentReplaced(int start, String removed, String inserted)

      Hook invoked when a range is replaced, carrying the exact removed and inserted text so feature layers (e.g. the rich text editor) can keep a parallel style / block model in sync. The default is a no-op.

      Parameters
      • start: the offset at which the replacement happened

      • removed: the removed text

      • inserted: the inserted text

    • documentReset

      protected void documentReset()
      Hook invoked when the whole document is reset (e.g. by #setText(String)), so feature layers can rebuild their parallel models. The default is a no-op.
    • captureDocumentState

      protected Object captureDocumentState()
      Captures feature-layer state associated with the document for undo/redo. Plain and code editors return null; rich editors override this to snapshot formatting, blocks, links and images.
    • restoreDocumentState

      protected void restoreDocumentState(Object state)
      Restores a feature-layer snapshot captured by #captureDocumentState().
    • refreshUndoAfterState

      protected final void refreshUndoAfterState()
      Refreshes the most recent undo unit after a feature layer finishes decorating an inserted range.
    • insertText

      public void insertText(String text)
      Inserts text at the caret, replacing any active selection.
    • handleTypedText

      protected boolean handleTypedText(String text)

      Hook giving feature layers a chance to intercept typed text before the default insertion (used by the code editor for bracket / quote auto close, tab expansion and auto indent). Return true when the text was consumed. The default returns false.

      Parameters
      • text: the text being typed / committed
    • host

      protected EditorHost host()
      Returns the host bridge, for feature layers that need to fire editor events.
    • moveCaret

      public void moveCaret(int newCaret, boolean extend)

      Moves the caret to a new offset, optionally extending the selection.

      Parameters
      • newCaret: the target offset (clamped to the document)

      • extend: true to extend the selection from the current anchor

    • selectAll

      public void selectAll()
      Selects the entire document.
    • copySelection

      protected void copySelection()
    • pasteClipboardData

      protected void pasteClipboardData(Object data)
      Inserts clipboard data. Rich editor subclasses may override this to preserve formatting.
    • lineContentOffsetX

      protected int lineContentOffsetX(int line)
      Returns the extra left offset of a line's text within the content area (0 for plain text and code; the rich text editor overrides this for list bullets, indentation, block quotes and alignment so the caret and selection line up with the drawn text).
    • measureColumnX

      protected int measureColumnX(int line, String text, int col, Font f)
      Returns the visual pixel x (from the start of a line) of the caret before logical column col. For plain left-to-right text this is simply the substring width; for lines containing right-to-left or mixed (bidirectional) text it maps the logical column through the resolved visual order so the caret lines up with the reordered glyphs. The rich text editor overrides this to measure per styled run.
    • columnAtX

      protected int columnAtX(int line, String text, int localX, Font f)
      Returns the column nearest a given content-local x pixel on a line. Bidi aware: it locates the visual run containing the pixel and maps back to a logical column. The rich text editor overrides it to be styled-run aware.
    • lineVisualSegments

      protected int[] lineVisualSegments(int line, String text, int from, int to, Font f)
      Returns the visual selection segments for the logical range [from, to) on a line as a flat array of {x, width, x, width, ...} in content-local pixels. A bidi line can yield several disjoint segments; a plain line yields a single segment.
    • laidOut

      protected void laidOut()
      Description copied from class: Component
      This is a callback method to inform the Component when it's been laidout on the parent Container
      Overrides:
      laidOut in class Component
    • focusGained

      protected void focusGained()
      Description copied from class: Component
      Callback allowing a developer to track when the component gains focus
      Overrides:
      focusGained in class Component
    • focusLost

      protected void focusLost()
      Description copied from class: Component
      Callback allowing a developer to track wheh the component loses focus
      Overrides:
      focusLost in class Component
    • animate

      public boolean animate()
      Description copied from class: Component

      Allows the animation to reduce "repaint" calls when it returns false. It is called once for every frame. Frames are defined by the com.codename1.ui.Display class.

      Returns

      true if a repaint is desired or false if no repaint is necessary

      Specified by:
      animate in interface Animation
      Overrides:
      animate in class Component
    • pointerPressed

      public void pointerPressed(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer pressed event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerPressed in class Component
    • pointerDragged

      public void pointerDragged(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer dragged event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerDragged in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component
    • keyPressed

      public void keyPressed(int keyCode)
      Description copied from class: Component

      If this Component is focused, the key pressed event will call this method

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyPressed in class Component
    • keyRepeated

      public void keyRepeated(int keyCode)
      Description copied from class: Component

      If this Component is focused, the key repeat event will call this method.

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyRepeated in class Component
    • keyReleased

      public void keyReleased(int keyCode)
      Description copied from class: Component

      If this Component is focused, the key released event will call this method

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyReleased in class Component
    • commitText

      public void commitText(String text)
      Description copied from interface: TextInputClient

      Commits final text at the caret, replacing the active selection. This is the normal path for a typed character, an accepted autocorrect suggestion, dictation and paste.

      Parameters
      • text: the committed text
      Specified by:
      commitText in interface TextInputClient
    • setComposingText

      public void setComposingText(String text, int relativeCaret)
      Description copied from interface: TextInputClient

      Updates the in progress IME composition (marked text) at the caret. The composing text is not yet final and is typically rendered underlined; it is replaced on the next #setComposingText and finalized by #finishComposing.

      Parameters
      • text: the current marked text

      • relativeCaret: caret position relative to the marked text (platform convention)

      Specified by:
      setComposingText in interface TextInputClient
    • finishComposing

      public void finishComposing()
      Description copied from interface: TextInputClient
      Finalizes any active IME composition, committing the current marked text.
      Specified by:
      finishComposing in interface TextInputClient
    • deleteSurroundingText

      public void deleteSurroundingText(int before, int after)
      Description copied from interface: TextInputClient

      Deletes text around the caret, the primary deletion path used by soft keyboards.

      Parameters
      • before: number of UTF-16 units to delete before the caret

      • after: number of UTF-16 units to delete after the caret

      Specified by:
      deleteSurroundingText in interface TextInputClient
    • onKeyCommand

      public void onKeyCommand(int command, int modifiers)
      Description copied from interface: TextInputClient

      Delivers a non text key command (navigation, deletion, clipboard, undo). Text producing keys are delivered through #commitText instead.

      Parameters
      • command: one of the KEY_* constants

      • modifiers: a bit mask of the MOD_* constants

      Specified by:
      onKeyCommand in interface TextInputClient
    • performUndo

      public void performUndo()
      Undoes the last edit.
    • performRedo

      public void performRedo()
      Redoes the last undone edit.
    • onEditorAction

      public void onEditorAction(int action)
      Description copied from interface: TextInputClient

      Delivers the keyboard return key action (done / next / search / send) configured through TextInputConfig.

      Parameters
      • action: one of the TextInputConfig ACTION_* constants
      Specified by:
      onEditorAction in interface TextInputClient
    • getEditingState

      public TextInputState getEditingState()
      Description copied from interface: TextInputClient
      Returns the current editing state (surrounding text, selection and composition) so the platform can seed autocorrect and prediction. May return a window around the caret for very large documents.
      Specified by:
      getEditingState in interface TextInputClient
    • getCaretRect

      public int[] getCaretRect()
      Description copied from interface: TextInputClient
      Returns the caret rectangle in absolute screen pixels {x, y, width, height}, used by the platform to anchor the IME candidate window and the selection loupe near the rendered caret.
      Specified by:
      getCaretRect in interface TextInputClient
    • rectForOffset

      public int[] rectForOffset(int offset)
      Description copied from interface: TextInputClient
      Returns the caret rectangle for an arbitrary offset in absolute screen pixels {x, y, w, h}.
      Specified by:
      rectForOffset in interface TextInputClient
    • getTextLength

      public int getTextLength()
      Description copied from interface: TextInputClient
      Returns the total number of UTF-16 characters in the document.
      Specified by:
      getTextLength in interface TextInputClient
    • getTextRange

      public String getTextRange(int start, int end)
      Description copied from interface: TextInputClient
      Returns the text in [start, end).
      Specified by:
      getTextRange in interface TextInputClient
    • selectionRects

      public int[] selectionRects(int start, int end)
      Description copied from interface: TextInputClient
      Returns the selection rectangles covering [start, end) as a flat array of absolute-pixel rectangles {x, y, w, h, x, y, w, h, ...} (one entry per visual line the range spans).
      Specified by:
      selectionRects in interface TextInputClient
    • replaceRange

      public void replaceRange(int start, int end, String text)
      Description copied from interface: TextInputClient
      Replaces the range [start, end) with text (a direct, range based edit from the platform).
      Specified by:
      replaceRange in interface TextInputClient
    • setSelectionRange

      public void setSelectionRange(int start, int end)
      Description copied from interface: TextInputClient
      Sets the selection to [start, end) (a caret when equal).
      Specified by:
      setSelectionRange in interface TextInputClient
    • getConfig

      public TextInputConfig getConfig()
      Description copied from interface: TextInputClient
      Returns the configuration describing the desired keyboard type and input behavior.
      Specified by:
      getConfig in interface TextInputClient
    • inputFocusGained

      public void inputFocusGained()
      Description copied from interface: TextInputClient
      Notifies the client that it gained the platform input focus (became first responder).
      Specified by:
      inputFocusGained in interface TextInputClient
    • inputFocusLost

      public void inputFocusLost()
      Description copied from interface: TextInputClient
      Notifies the client that it lost the platform input focus.
      Specified by:
      inputFocusLost in interface TextInputClient
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Component