Class 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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a listener told whenever what can be undone or redone changes.voidbeginCompound(String name) Starts a group of edits that undo and redo as one step.booleancanRedo()True when an undone edit can be made again.booleancanUndo()True when there is an edit to take back.voidclear()Forgets every edit, for instance after loading another document.voidForgets every edit that is no longer alive (UndoableEdit.isAlive).voidEnds the groupbeginCompoundstarted.static UndoHistoryThe application's history, which every component that supports undo records into unless it is given another.intgetLimit()How many steps are kept; older ones are dropped.intThe number of steps that can be redone.The name of the editredowould make again, or null.intThe number of steps that can be undone.The name of the editundowould take back, or null.booleanTrue betweenbeginCompoundand itsendCompound.booleanisLastEdit(UndoableEdit edit) True wheneditis the stepundowould take back next, and no compound is open.booleanTrue whileundoorredois running an edit.voidrecord(UndoableEdit edit) Records an edit that was just made.booleanredo()Makes the last undone edit again.voidRemoves a listener.voidsetLimit(int limit) Sets how many steps are kept, at least one.booleanundo()Takes back the last edit.
-
Constructor Details
-
UndoHistory
public UndoHistory()
-
-
Method Details
-
getGlobal
The application's history, which every component that supports undo records into unless it is given another. -
record
Records an edit that was just made.
Inside a compound the edit joins it. Otherwise it becomes the next thing
undotakes back, and nothing can be redone any more. -
beginCompound
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 groupbeginCompoundstarted. A group in which nothing was recorded leaves no step behind. -
isCompoundOpen
public boolean isCompoundOpen()True betweenbeginCompoundand itsendCompound. -
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
The name of the editundowould take back, or null. -
getRedoName
The name of the editredowould 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 whileundoorredois 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
True wheneditis the stepundowould 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
Adds a listener told whenever what can be undone or redone changes. -
removeListener
Removes a listener.
-