public class UndoManager extends CompoundEdit implements UndoableEditListener
Tyipcally, an application will create only one single instance
of UndoManager. When the user performs an undoable action, for
instance changing the color of an object from green to blue, the
application registers an UndoableEdit
object with the
UndoManager
. To implement the “undo” and
“redo” menu commands, the application invokes the
UndoManager’s undo()
and redo()
methods. The
human-readable text of these menu commands is provided by getUndoPresentationName()
and getRedoPresentationName()
,
respectively. To determine whether the menu item should be
selectable or greyed out, use canUndo()
and canRedo()
.
The UndoManager will only keep a specified number of editing
actions, the limit. The value of this parameter can be
retrieved by calling getLimit()
and set with setLimit(int)
. If more UndoableEdits are added to the UndoManager,
the oldest actions will be discarded.
Some applications do not provide separate menu commands for
“undo” and “redo.” Instead, they
have just a single command whose text switches between the two.
Such applications would use an UndoManager with a limit
of 1. The text of this combined menu item is available via
getUndoOrRedoPresentationName()
, and it is implemented
by calling undoOrRedo()
.
Thread Safety: In constrast to the other classes of the
javax.swing.undo
package, the public methods of an
UndoManager
are safe to call from concurrent threads.
The caller does not need to perform external synchronization, and
UndoableEditEvent
sources do not need to
broadcast their events from inside the Swing worker thread.
edits
RedoName, UndoName
Constructor and Description |
---|
UndoManager()
Constructs an UndoManager.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addEdit(UndoableEdit edit)
Registers an undoable editing action with this UndoManager.
|
boolean |
canRedo()
Determines whether it would be possible to redo this editing
action.
|
boolean |
canUndo()
Determines whether it would be possible to undo this editing
action.
|
boolean |
canUndoOrRedo()
Determines whether it would be possible to either undo or redo
this editing action.
|
void |
discardAllEdits()
Discards all editing actions that are currently registered with
this UndoManager.
|
protected UndoableEdit |
editToBeRedone()
Determines which significant edit would be redone if
redo() was called. |
protected UndoableEdit |
editToBeUndone()
Determines which significant edit would be undone if
undo() was called. |
void |
end()
Puts this UndoManager into a state where it acts as a normal
CompoundEdit . |
int |
getLimit()
Returns how many edits this UndoManager can maximally hold.
|
String |
getRedoPresentationName()
Calculates a localized text for presenting the redo action
to the user, for example in the form of a menu command.
|
String |
getUndoOrRedoPresentationName()
Calculates a localized text for presenting the undo or redo
action to the user, for example in the form of a menu command.
|
String |
getUndoPresentationName()
Calculates a localized text for presenting the undo action
to the user, for example in the form of a menu command.
|
void |
redo()
Redoes one significant edit action.
|
protected void |
redoTo(UndoableEdit edit)
Redoes all editing actions in the same order as they were
added to this UndoManager, up to the specified action.
|
void |
setLimit(int limit)
Changes the maximal number of edits that this UndoManager can
process.
|
String |
toString()
Returns a string representation for this UndoManager.
|
protected void |
trimEdits(int from,
int to)
Discards a range of edits.
|
protected void |
trimForLimit()
Called by various internal methods in order to enforce
the
limit value. |
void |
undo()
Undoes one significant edit action.
|
void |
undoableEditHappened(UndoableEditEvent event)
Registers the edit action of an
UndoableEditEvent
with this UndoManager. |
void |
undoOrRedo()
Undoes or redoes the last action.
|
protected void |
undoTo(UndoableEdit edit)
Undoes all editing actions in reverse order of addition,
up to the specified action,
|
die, getPresentationName, isInProgress, isSignificant, lastEdit
replaceEdit
public UndoManager()
The limit
of the freshly constructed UndoManager
is 100.
public String toString()
getUndoPresentationName()
, getRedoPresentationName()
, and getUndoOrRedoPresentationName()
.toString
in class CompoundEdit
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)
public void end()
CompoundEdit
. It is unlikely that an application would
want to do this.end
in class CompoundEdit
public int getLimit()
setLimit(int)
public void setLimit(int limit)
die
message in reverse order of addition.limit
- the new limit.IllegalStateException
- if end()
has already been
called on this UndoManager.public void discardAllEdits()
UndoableEdit
will receive a die message
.protected void trimForLimit()
limit
value.protected void trimEdits(int from, int to)
[from
.. to]
will receive a die
message before being removed from the edits array. If
from
is greater than to
, nothing
happens.from
- the lower bound of the range of edits to be
discarded.to
- the upper bound of the range of edits to be discarded.protected UndoableEdit editToBeUndone()
undo()
was called.null
if no significant edit would be affected by
calling undo()
.protected UndoableEdit editToBeRedone()
redo()
was called.null
if no significant edit would be affected by
calling redo()
.protected void undoTo(UndoableEdit edit) throws CannotUndoException
edit
- the last editing action to be undone.CannotUndoException
protected void redoTo(UndoableEdit edit) throws CannotRedoException
edit
- the last editing action to be redone.CannotRedoException
public void undoOrRedo() throws CannotRedoException, CannotUndoException
This is useful for applications that do not present a separate
undo and redo facility, but just have a single menu item for
undoing and redoing the very last action. Such applications will
use an UndoManager
whose limit
is 1.
public boolean canUndoOrRedo()
This is useful for applications that do not present a separate
undo and redo facility, but just have a single menu item for
undoing and redoing the very last action. Such applications will
use an UndoManager
whose limit
is 1.
true
to indicate that this action can be
undone or redone; false
if neither is possible at
the current time.public void undo() throws CannotUndoException
However, if end()
has been called on this
UndoManager, it will behave like a normal CompoundEdit
. In this case, all actions will be undone in
reverse order of addition. Typical applications will never call
end()
on their UndoManager
.
undo
in interface UndoableEdit
undo
in class CompoundEdit
CannotUndoException
- if no action can be undone.canUndo()
,
redo()
,
undoOrRedo()
public boolean canUndo()
canUndo
in interface UndoableEdit
canUndo
in class CompoundEdit
true
to indicate that this action can be
undone; false
otherwise.undo()
,
canRedo()
,
canUndoOrRedo()
public void redo() throws CannotRedoException
However, if end()
has been called on this
UndoManager, it will behave like a normal CompoundEdit
. In this case, all actions will be redone
in order of addition. Typical applications will never call end()
on their UndoManager
.
redo
in interface UndoableEdit
redo
in class CompoundEdit
CannotRedoException
- if no action can be redone.canRedo()
,
redo()
,
undoOrRedo()
public boolean canRedo()
canRedo
in interface UndoableEdit
canRedo
in class CompoundEdit
true
to indicate that this action can be
redone; false
otherwise.redo()
,
canUndo()
,
canUndoOrRedo()
public boolean addEdit(UndoableEdit edit)
limit
is reached, the oldest action
will be discarded (and receives a die message. Equally, any actions that were undone (but not re-done)
will be discarded, too.addEdit
in interface UndoableEdit
addEdit
in class CompoundEdit
edit
- the editing action that is added to this UndoManager.true
if edit
could be
incorporated; false
if edit
has not
been incorporated because end()
has already been called
on this UndoManager
.public String getUndoOrRedoPresentationName()
This is useful for applications that do not present a separate
undo and redo facility, but just have a single menu item for
undoing and redoing the very last action. Such applications will
use an UndoManager
whose limit
is 1.
getUndoPresentationName()
,
getRedoPresentationName()
public String getUndoPresentationName()
getUndoPresentationName
in interface UndoableEdit
getUndoPresentationName
in class CompoundEdit
public String getRedoPresentationName()
getRedoPresentationName
in interface UndoableEdit
getRedoPresentationName
in class CompoundEdit
public void undoableEditHappened(UndoableEditEvent event)
UndoableEditEvent
with this UndoManager.
Thread Safety: This method may safely be invoked from
concurrent threads. The caller does not need to perform external
synchronization. This means that UndoableEditEvent
sources do not need to broadcast
their events from inside the Swing worker thread.
undoableEditHappened
in interface UndoableEditListener
event
- the event whose edit
will be
passed to addEdit(javax.swing.undo.UndoableEdit)
.UndoableEditEvent.getEdit()
,
addEdit(javax.swing.undo.UndoableEdit)