UndoManager
manages a list of UndoableEdits
,
providing a way to undo or redo the appropriate edits. There are
two ways to add edits to an UndoManager
. Add the edit
directly using the addEdit
method, or add the
UndoManager
to a bean that supports
UndoableEditListener
. The following examples creates
an UndoManager
and adds it as an
UndoableEditListener
to a JTextField
:
UndoManager undoManager = new UndoManager(); JTextField tf = ...; tf.getDocument().addUndoableEditListener(undoManager);
UndoManager
maintains an ordered list of edits and the
index of the next edit in that list. The index of the next edit is
either the size of the current list of edits, or if
undo
has been invoked it corresponds to the index
of the last significant edit that was undone. When
undo
is invoked all edits from the index of the next
edit to the last significant edit are undone, in reverse order.
For example, consider an UndoManager
consisting of the
following edits: A b c D. Edits with a
upper-case letter in bold are significant, those in lower-case
and italicized are insignificant.
Figure 1 |
As shown in figure 1, if D was just added, the
index of the next edit will be 4. Invoking undo
results in invoking undo
on D and setting the
index of the next edit to 3 (edit c), as shown in the following
figure.
Figure 2 |
The last significant edit is A, so that invoking
undo
again invokes undo
on c,
b, and A, in that order, setting the index of the
next edit to 0, as shown in the following figure.
Figure 3 |
Invoking redo
results in invoking redo
on
all edits between the index of the next edit and the next
significant edit (or the end of the list). Continuing with the previous
example if redo
were invoked, redo
would in
turn be invoked on A, b and c. In addition
the index of the next edit is set to 3 (as shown in figure 2).
Adding an edit to an UndoManager
results in
removing all edits from the index of the next edit to the end of
the list. Continuing with the previous example, if a new edit,
e, is added the edit D is removed from the list
(after having die
invoked on it). If c is not
incorporated by the next edit
(c.addEdit(e)
returns true), or replaced
by it (e.replaceEdit(c)
returns true),
the new edit is added after c, as shown in the following
figure.
Figure 4 |
Once end
has been invoked on an UndoManager
the superclass behavior is used for all UndoableEdit
methods. Refer to CompoundEdit
for more details on its
behavior.
Unlike the rest of Swing, this class is thread safe.
Warning
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeans
has been added to the java.beans
package.
Please see java.
.
Modifier and Type | Class and Description |
---|---|
private static enum |
Modifier and Type | Field and Description |
---|---|
pack-priv int | |
pack-priv int |
Access | Constructor and Description |
---|---|
public |
Modifier and Type | Method and Description |
---|---|
public synchronized boolean | Returns: true ifanEdit can be incorporated into this
editthe edit to be added anEdit)Overrides javax. Implements javax. Adds an |
public synchronized boolean | Returns: true if there are edits to be redoneOverrides javax. Implements javax. Returns true if edits may be redone. |
public synchronized boolean | Returns: true if there are edits to be undoneOverrides javax. Implements javax. Returns true if edits may be undone. |
public synchronized boolean | Returns: true if invokingcanUndoOrRedo is validReturns true if it is possible to invoke |
public synchronized void | |
protected UndoableEdit | Returns: the next significant edit to be redoneReturns the next significant edit to be redone if |
protected UndoableEdit | Returns: the next significant edit to be undoneReturns the next significant edit to be undone if |
public synchronized void | end()
Overrides javax. Turns this |
private UndoableEditLockSupport | |
public synchronized int | Returns: the maximum number of edits thisUndoManager holdsReturns the maximum number of edits this |
public synchronized String | Returns: a description of the redoable form of this editOverrides javax. Implements javax. Returns a description of the redoable form of this edit. |
public synchronized String | Returns: undo or redo nameConvenience method that returns either
|
public synchronized String | Returns: a description of the undoable form of this editOverrides javax. Implements javax. Returns a description of the undoable form of this edit. |
public void | redo()
Overrides javax. Implements javax. Redoes the appropriate edits. |
protected void | redoTo(UndoableEdit
the edit to be redo to edit)Redoes all changes from the index of the next edit to
|
public synchronized void | |
public String | Returns: a String representation of this objectOverrides javax. Returns a string that displays and identifies this object's properties. |
protected void | trimEdits(int
the minimum index to remove from, int the maximum index to remove to)Removes edits in the specified range. |
protected void | trimForLimit()
Reduces the number of queued edits to a range of size limit, centered on the index of the next edit. |
private void | |
public void | undo()
Overrides javax. Implements javax. Undoes the appropriate edits. |
public void | undoableEditHappened(UndoableEditEvent
the e)UndoableEditEvent the
UndoableEditEvent will be added fromImplements javax. An |
public void | |
protected void | undoTo(UndoableEdit
the edit to be undo to edit)Undoes all changes from the index of the next edit to
|
indexOfNextAdd | back to summary |
---|---|
pack-priv int indexOfNextAdd |
limit | back to summary |
---|---|
pack-priv int limit |
UndoManager | back to summary |
---|---|
public UndoManager() Creates a new |
addEdit | back to summary |
---|---|
public synchronized boolean addEdit(UndoableEdit anEdit) Overrides javax. Implements javax. Adds an
|
canRedo | back to summary |
---|---|
public synchronized boolean canRedo() Overrides javax. Implements javax. Returns true if edits may be redone. If
|
canUndo | back to summary |
---|---|
public synchronized boolean canUndo() Overrides javax. Implements javax. Returns true if edits may be undone. If
|
canUndoOrRedo | back to summary |
---|---|
public synchronized boolean canUndoOrRedo() Returns true if it is possible to invoke
|
discardAllEdits | back to summary |
---|---|
public synchronized void discardAllEdits() Empties the undo manager sending each edit a
|
editToBeRedone | back to summary |
---|---|
protected UndoableEdit editToBeRedone() Returns the next significant edit to be redone if
|
editToBeUndone | back to summary |
---|---|
protected UndoableEdit editToBeUndone() Returns the next significant edit to be undone if
|
end | back to summary |
---|---|
public synchronized void end() Overrides javax. Turns this
|
getEditLockSupport | back to summary |
---|---|
private UndoableEditLockSupport getEditLockSupport(UndoableEdit anEdit) |
getLimit | back to summary |
---|---|
public synchronized int getLimit() Returns the maximum number of edits this |
getRedoPresentationName | back to summary |
---|---|
public synchronized String getRedoPresentationName() Overrides javax. Implements javax. Returns a description of the redoable form of this edit.
If
|
getUndoOrRedoPresentationName | back to summary |
---|---|
public synchronized String getUndoOrRedoPresentationName() Convenience method that returns either
|
getUndoPresentationName | back to summary |
---|---|
public synchronized String getUndoPresentationName() Overrides javax. Implements javax. Returns a description of the undoable form of this edit.
If
|
redo | back to summary |
---|---|
public void redo() throws CannotRedoException Overrides javax. Implements javax. Redoes the appropriate edits. If
|
redoTo | back to summary |
---|---|
protected void redoTo(UndoableEdit edit) throws CannotRedoException Redoes all changes from the index of the next edit to
|
setLimit | back to summary |
---|---|
public synchronized void setLimit(int l) Sets the maximum number of edits this
|
toString | back to summary |
---|---|
public String toString() Overrides javax. Returns a string that displays and identifies this object's properties.
|
trimEdits | back to summary |
---|---|
protected void trimEdits(int from, int to) Removes edits in the specified range.
All edits in the given range (inclusive, and in reverse order)
will have
|
trimForLimit | back to summary |
---|---|
protected void trimForLimit() Reduces the number of queued edits to a range of size limit, centered on the index of the next edit. |
tryUndoOrRedo | back to summary |
---|---|
private void tryUndoOrRedo(UndoManager. |
undo | back to summary |
---|---|
public void undo() throws CannotUndoException Overrides javax. Implements javax. Undoes the appropriate edits. If
|
undoableEditHappened | back to summary |
---|---|
public void undoableEditHappened(UndoableEditEvent e) Implements javax. An
|
undoOrRedo | back to summary |
---|---|
public void undoOrRedo() throws CannotRedoException, CannotUndoException Convenience method that invokes one of
|
undoTo | back to summary |
---|---|
protected void undoTo(UndoableEdit edit) throws CannotUndoException Undoes all changes from the index of the next edit to
|
Modifier and Type | Field and Description |
---|---|
public static final UndoManager. | |
public static final UndoManager. | |
public static final UndoManager. |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static UndoManager. | |
public static UndoManager. |
ANY | back to summary |
---|---|
public static final UndoManager. |
REDO | back to summary |
---|---|
public static final UndoManager. |
UNDO | back to summary |
---|---|
public static final UndoManager. |
Action | back to summary |
---|---|
private Action() |
valueOf | back to summary |
---|---|
public static UndoManager. |
values | back to summary |
---|---|
public static UndoManager. |