Class UndoManager

java.lang.Object
com.codename1.ui.editor.UndoManager

public class UndoManager extends Object
A compact undo / redo stack for the pure editors. Every mutation is recorded as a replacement of the range [start, start + removed.length) with inserted; the inverse simply swaps removed and inserted. Consecutive single character insertions are coalesced into one undo unit so a burst of typing undoes as a word rather than a keystroke at a time.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Breaks the current coalescing run so the next recorded insert starts a fresh undo unit.
    boolean
    True when there is anything to redo.
    boolean
    True when there is anything to undo.
    void
    Clears all recorded history.
    void
    record(int start, String removed, String inserted)
    Records a mutation for later undo.
    int
    Redoes the most recently undone mutation against the supplied document.
    int
    Undoes the most recent mutation against the supplied document.

    Methods inherited from class Object

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

    • UndoManager

      public UndoManager()
  • Method Details

    • record

      public void record(int start, String removed, String inserted)

      Records a mutation for later undo. Clears the redo stack.

      Parameters
      • start: the offset at which text was removed / inserted

      • removed: the text that was removed (empty for a pure insert)

      • inserted: the text that was inserted (empty for a pure delete)

    • breakRun

      public void breakRun()
      Breaks the current coalescing run so the next recorded insert starts a fresh undo unit. Called on caret jumps, selection changes and structural edits.
    • canUndo

      public boolean canUndo()
      True when there is anything to undo.
    • canRedo

      public boolean canRedo()
      True when there is anything to redo.
    • undo

      public int undo(EditorDocument doc)

      Undoes the most recent mutation against the supplied document.

      Parameters
      • doc: the document to mutate
      Returns

      the caret offset after the undo, or -1 when there was nothing to undo

    • redo

      public int redo(EditorDocument doc)

      Redoes the most recently undone mutation against the supplied document.

      Parameters
      • doc: the document to mutate
      Returns

      the caret offset after the redo, or -1 when there was nothing to redo

    • clear

      public void clear()
      Clears all recorded history.