Java Reference
In-Depth Information
Figure 21-2.
UndoableEdit states
The flow between states goes as follows:
Undoable state:
When an
UndoableEdit
command is first created, the operation is in the
Undoable state. The purpose of the
die()
method is to release resources for an
UndoableEdit
before the garbage collector decides to clean things up and to place the command in the
Done state. Calling the
undo()
method either throws a
CannotUndoException
or causes the
command to be undone and the state to change to Redoable. Calling the
redo()
method
either throws a
CannotRedoException
or causes the command to be done again and the
state to stay at Undoable.
Redoable state:
When the operation is in the Redoable state, the command has already
been undone. Calling the
die()
method releases any resources and places the command
in the Done state. Calling the
undo()
method either throws a
CannotUndoException
or
causes the command to be undone again and the state to stay at Redoable. Calling the
redo()
method either throws a
CannotRedoException
or causes the command to be redone,
returning the state to the Undoable state.
Done state:
When the operation is in the Done state, calling any of the
undo()
,
redo()
, or
die()
methods leaves the operation in the Done state.
Some state changes aren't commonplace; however, all state changes are supported.
The specifics are left to the Command that you're using (as described in the preceding “The
Command Design Pattern” section). For instance, Microsoft Word allows you to continuously
repeat the last command if the capabilities make sense—such as when formatting a paragraph
or just typing a phrase.