Class UndoHistory

java.lang.Object
com.codename1.util.UndoHistory

public class UndoHistory extends Object

The edits a user made, in order, so they can be undone and redone.

A history knows nothing about what is edited. A component that supports undo records an UndoableEdit for every change the user makes through it. Every such component records into the application's history, getGlobal(), unless it is given another one, so a single "Undo" takes back the last change wherever it was made:

Button undoButton = new Button("Undo");
UndoHistory history = UndoHistory.getGlobal();
undoButton.addActionListener(e -> history.undo());
history.addListener(h -> undoButton.setEnabled(h.canUndo()));

A component given its own history, or null to record nothing, keeps its edits out of the application's.

Edits recorded between beginCompound and endCompound are one step: dragging five selected nodes is a single "Move".

Recording a new edit drops everything that could have been redone. An edit recorded while the history is itself undoing or redoing is ignored, so an edit may freely call the same code a user's action does.

Like the components that use it, a history belongs to the event dispatch thread.

  • Constructor Details

    • UndoHistory

      public UndoHistory()
  • Method Details

    • getGlobal

      public static UndoHistory getGlobal()
      The application's history, which every component that supports undo records into unless it is given another.
    • record

      public void record(UndoableEdit edit)

      Records an edit that was just made.

      Inside a compound the edit joins it. Otherwise it becomes the next thing undo takes back, and nothing can be redone any more.

    • beginCompound

      public void beginCompound(String name)

      Starts a group of edits that undo and redo as one step. Calls nest: only the outermost pair makes a step, named by the outermost call.

      Parameters
      • name: the step's name, or null
    • endCompound

      public void endCompound()
      Ends the group beginCompound started. A group in which nothing was recorded leaves no step behind.
    • isCompoundOpen

      public boolean isCompoundOpen()
      True between beginCompound and its endCompound.
    • canUndo

      public boolean canUndo()
      True when there is an edit to take back.
    • canRedo

      public boolean canRedo()
      True when an undone edit can be made again.
    • getUndoName

      public String getUndoName()
      The name of the edit undo would take back, or null.
    • getRedoName

      public String getRedoName()
      The name of the edit redo would make again, or null.
    • undo

      public boolean undo()

      Takes back the last edit.

      Returns

      false when there was nothing to undo

    • redo

      public boolean redo()

      Makes the last undone edit again.

      Returns

      false when there was nothing to redo

    • isReplaying

      public boolean isReplaying()
      True while undo or redo is running an edit. A component checks it to tell a replayed change from one the user is making.
    • clear

      public void clear()
      Forgets every edit, for instance after loading another document.
    • discardDead

      public void discardDead()
      Forgets every edit that is no longer alive (UndoableEdit.isAlive). A component calls this when it drops what its edits change, such as on being given another document, so the rest of the history stays.
    • isLastEdit

      public boolean isLastEdit(UndoableEdit edit)
      True when edit is the step undo would take back next, and no compound is open. A component uses it to extend its own last edit, such as a run of typing, only while nothing was recorded after it.
    • getLimit

      public int getLimit()
      How many steps are kept; older ones are dropped. 100 by default.
    • setLimit

      public void setLimit(int limit)
      Sets how many steps are kept, at least one.
    • getUndoCount

      public int getUndoCount()
      The number of steps that can be undone.
    • getRedoCount

      public int getRedoCount()
      The number of steps that can be redone.
    • addListener

      public void addListener(UndoHistoryListener l)
      Adds a listener told whenever what can be undone or redone changes.
    • removeListener

      public void removeListener(UndoHistoryListener l)
      Removes a listener.