Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class UndoHistory

  1. Object
  2. UndoHistory

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.

Constructors

public UndoHistory()

Methods

public static UndoHistory getGlobal()The application’s history, which every component that supports undo records into unless it is given another.
public void record(UndoableEdit edit)Records an edit that was just made.
public void beginCompound(String name)Starts a group of edits that undo and redo as one step.
public void endCompound()Ends the group beginCompound started.
public boolean isCompoundOpen()True between beginCompound and its endCompound.
public boolean canUndo()True when there is an edit to take back.
public boolean canRedo()True when an undone edit can be made again.
public String getUndoName()The name of the edit undo would take back, or null.
public String getRedoName()The name of the edit redo would make again, or null.
public boolean undo()Takes back the last edit.
public boolean redo()Makes the last undone edit again.
public boolean isReplaying()True while undo or redo is running an edit.
public void clear()Forgets every edit, for instance after loading another document.
public void discardDead()Forgets every edit that is no longer alive (UndoableEdit.isAlive).
public boolean isLastEdit(UndoableEdit edit)True when edit is the step undo would take back next, and no compound is open.
public int getLimit()How many steps are kept; older ones are dropped.
public void setLimit(int limit)Sets how many steps are kept, at least one.
public int getUndoCount()The number of steps that can be undone.
public int getRedoCount()The number of steps that can be redone.
public void addListener(UndoHistoryListener l)Adds a listener told whenever what can be undone or redone changes.
public void removeListener(UndoHistoryListener l)Removes a listener.

Inherited methods

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 String
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.