UndoManager (Java 2 Platform SE 5.0) (original) (raw)
javax.swing.undo
Class UndoManager
java.lang.Object
javax.swing.undo.AbstractUndoableEdit
javax.swing.undo.CompoundEdit
javax.swing.undo.UndoManager
All Implemented Interfaces:
Serializable, EventListener, UndoableEditListener, UndoableEdit
public class UndoManager
extends CompoundEdit
implements UndoableEditListener
Concrete subclass of CompoundEdit
which can serve as a UndoableEditListener
, consolidating the UndoableEditEvents
from a variety of sources, and undoing or redoing them one at a time. Unlike AbstractUndoableEdit
and CompoundEdit
, the public methods of this class are synchronized, and should be safe to call from multiple threads. This should make UndoManager
a convenient marshall for sets of undoable JavaBeans.
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 JavaBeansTM has been added to the java.beans
package. Please see XMLEncoder.
Field Summary |
---|
Fields inherited from class javax.swing.undo.CompoundEdit |
---|
edits |
Fields inherited from class javax.swing.undo.AbstractUndoableEdit |
---|
RedoName, UndoName |
Constructor Summary |
---|
UndoManager() |
Method Summary | |
---|---|
boolean | addEdit(UndoableEdit anEdit) If inProgress, inserts anEdit at indexOfNextAdd, and removes any old edits that were at indexOfNextAdd or later. |
boolean | canRedo() Overridden to preserve usual semantics: returns true if a redo operation would be successful now, false otherwise |
boolean | canUndo() Overridden to preserve usual semantics: returns true if an undo operation would be successful now, false otherwise |
boolean | canUndoOrRedo() Return true if calling undoOrRedo will undo or redo. |
void | discardAllEdits() Empty the undo manager, sending each edit a die message in the process. |
protected UndoableEdit | editToBeRedone() Returns the the next significant edit to be redone if redo is called. |
protected UndoableEdit | editToBeUndone() Returns the the next significant edit to be undone if undo is called. |
void | end() Sending end() to an UndoManager turns it into a plain old (ended) CompoundEdit. |
int | getLimit() Returns the maximum number of edits this UndoManager will hold. |
String | getRedoPresentationName() If inProgress, returns getRedoPresentationName of the significant edit that will be redone when redo() is invoked. |
String | getUndoOrRedoPresentationName() Return the appropriate name for a command that toggles between undo and redo. |
String | getUndoPresentationName() If inProgress, returns getUndoPresentationName of the significant edit that will be undone when undo() is invoked. |
void | redo() If this UndoManager is inProgress, redoes the last significant UndoableEdit atindexOfNextAdd or after, and all insignificant edits up to it. |
protected void | redoTo(UndoableEdit edit) Redoes all changes from indexOfNextAdd to edit. |
void | setLimit(int l) Set the maximum number of edits this UndoManager will hold. |
String | toString() Returns a string that displays and identifies this object's properties. |
protected void | [trimEdits](../../../javax/swing/undo/UndoManager.html#trimEdits%28int, int%29)(int from, int to) Tell the edits in the given range (inclusive) to die, and remove them from edits. |
protected void | trimForLimit() Reduce the number of queued edits to a range of size limit, centered on indexOfNextAdd. |
void | undo() If this UndoManager is inProgress, undo the last significant UndoableEdit before indexOfNextAdd, and all insignificant edits back to it. |
void | undoableEditHappened(UndoableEditEvent e) Called by the UndoabledEdit sources this UndoManager listens to. |
void | undoOrRedo() Undo or redo as appropriate. |
protected void | undoTo(UndoableEdit edit) Undoes all changes from indexOfNextAdd to edit. |
Methods inherited from class javax.swing.undo.CompoundEdit |
---|
die, getPresentationName, isInProgress, isSignificant, lastEdit |
Methods inherited from class javax.swing.undo.AbstractUndoableEdit |
---|
replaceEdit |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, [wait](../../../java/lang/Object.html#wait%28long, int%29) |
Constructor Detail |
---|
UndoManager
public UndoManager()
Method Detail |
---|
getLimit
public int getLimit()
Returns the maximum number of edits this UndoManager will hold. Default value is 100.
See Also:
addEdit(javax.swing.undo.UndoableEdit), setLimit(int)
discardAllEdits
public void discardAllEdits()
Empty the undo manager, sending each edit a die message in the process.
trimForLimit
protected void trimForLimit()
Reduce the number of queued edits to a range of size limit, centered on indexOfNextAdd.
trimEdits
protected void trimEdits(int from, int to)
Tell the edits in the given range (inclusive) to die, and remove them from edits. from > to is a no-op.
setLimit
public void setLimit(int l)
Set the maximum number of edits this UndoManager will hold. If edits need to be discarded to shrink the limit, they will be told to die in the reverse of the order that they were added.
See Also:
addEdit(javax.swing.undo.UndoableEdit), getLimit()
editToBeUndone
protected UndoableEdit editToBeUndone()
Returns the the next significant edit to be undone if undo is called. May return null
editToBeRedone
protected UndoableEdit editToBeRedone()
Returns the the next significant edit to be redone if redo is called. May return null
undoTo
protected void undoTo(UndoableEdit edit) throws CannotUndoException
Undoes all changes from indexOfNextAdd to edit. Updates indexOfNextAdd accordingly.
Throws:
[CannotUndoException](../../../javax/swing/undo/CannotUndoException.html "class in javax.swing.undo")
redoTo
protected void redoTo(UndoableEdit edit) throws CannotRedoException
Redoes all changes from indexOfNextAdd to edit. Updates indexOfNextAdd accordingly.
Throws:
[CannotRedoException](../../../javax/swing/undo/CannotRedoException.html "class in javax.swing.undo")
undoOrRedo
public void undoOrRedo() throws CannotRedoException, CannotUndoException
Undo or redo as appropriate. Suitable for binding to an action that toggles between these two functions. Only makes sense to send this if limit == 1.
Throws:
[CannotRedoException](../../../javax/swing/undo/CannotRedoException.html "class in javax.swing.undo")
[CannotUndoException](../../../javax/swing/undo/CannotUndoException.html "class in javax.swing.undo")
See Also:
canUndoOrRedo(), getUndoOrRedoPresentationName()
canUndoOrRedo
public boolean canUndoOrRedo()
Return true if calling undoOrRedo will undo or redo. Suitable for deciding to enable a command that toggles between the two functions, which only makes sense to use if limit == 1.
See Also:
undo
public void undo() throws CannotUndoException
If this UndoManager is inProgress, undo the last significant UndoableEdit before indexOfNextAdd, and all insignificant edits back to it. Updates indexOfNextAdd accordingly.
If not inProgress, indexOfNextAdd is ignored and super's routine is called.
Specified by:
[undo](../../../javax/swing/undo/UndoableEdit.html#undo%28%29)
in interface [UndoableEdit](../../../javax/swing/undo/UndoableEdit.html "interface in javax.swing.undo")
Overrides:
[undo](../../../javax/swing/undo/CompoundEdit.html#undo%28%29)
in class [CompoundEdit](../../../javax/swing/undo/CompoundEdit.html "class in javax.swing.undo")
Throws:
[CannotUndoException](../../../javax/swing/undo/CannotUndoException.html "class in javax.swing.undo")
- if canUndo
returns false
See Also:
canUndo
public boolean canUndo()
Overridden to preserve usual semantics: returns true if an undo operation would be successful now, false otherwise
Specified by:
[canUndo](../../../javax/swing/undo/UndoableEdit.html#canUndo%28%29)
in interface [UndoableEdit](../../../javax/swing/undo/UndoableEdit.html "interface in javax.swing.undo")
Overrides:
[canUndo](../../../javax/swing/undo/CompoundEdit.html#canUndo%28%29)
in class [CompoundEdit](../../../javax/swing/undo/CompoundEdit.html "class in javax.swing.undo")
Returns:
true if this edit is alive
and hasBeenDone
is true
See Also:
redo
public void redo() throws CannotRedoException
If this UndoManager
is inProgress
, redoes the last significant UndoableEdit
atindexOfNextAdd
or after, and all insignificant edits up to it. Updates indexOfNextAdd
accordingly.
If not inProgress
, indexOfNextAdd
is ignored and super's routine is called.
Specified by:
[redo](../../../javax/swing/undo/UndoableEdit.html#redo%28%29)
in interface [UndoableEdit](../../../javax/swing/undo/UndoableEdit.html "interface in javax.swing.undo")
Overrides:
[redo](../../../javax/swing/undo/CompoundEdit.html#redo%28%29)
in class [CompoundEdit](../../../javax/swing/undo/CompoundEdit.html "class in javax.swing.undo")
Throws:
[CannotRedoException](../../../javax/swing/undo/CannotRedoException.html "class in javax.swing.undo")
- if canRedo
returns false
See Also:
canRedo
public boolean canRedo()
Overridden to preserve usual semantics: returns true if a redo operation would be successful now, false otherwise
Specified by:
[canRedo](../../../javax/swing/undo/UndoableEdit.html#canRedo%28%29)
in interface [UndoableEdit](../../../javax/swing/undo/UndoableEdit.html "interface in javax.swing.undo")
Overrides:
[canRedo](../../../javax/swing/undo/CompoundEdit.html#canRedo%28%29)
in class [CompoundEdit](../../../javax/swing/undo/CompoundEdit.html "class in javax.swing.undo")
Returns:
true
if this edit is alive
and hasBeenDone
is false
See Also:
addEdit
public boolean addEdit(UndoableEdit anEdit)
If inProgress, inserts anEdit at indexOfNextAdd, and removes any old edits that were at indexOfNextAdd or later. The die method is called on each edit that is removed is sent, in the reverse of the order the edits were added. Updates indexOfNextAdd.
If not inProgress
, acts as aCompoundEdit
.
Specified by:
[addEdit](../../../javax/swing/undo/UndoableEdit.html#addEdit%28javax.swing.undo.UndoableEdit%29)
in interface [UndoableEdit](../../../javax/swing/undo/UndoableEdit.html "interface in javax.swing.undo")
Overrides:
[addEdit](../../../javax/swing/undo/CompoundEdit.html#addEdit%28javax.swing.undo.UndoableEdit%29)
in class [CompoundEdit](../../../javax/swing/undo/CompoundEdit.html "class in javax.swing.undo")
Parameters:
anEdit
- the edit to be added
Returns:
true if the edit is inProgress
; otherwise returns false
See Also:
CompoundEdit.end(), CompoundEdit.addEdit(javax.swing.undo.UndoableEdit)
end
public void end()
Sending end() to an UndoManager turns it into a plain old (ended) CompoundEdit.
Calls super's end() method (making inProgress false), then sends die() to the unreachable edits at indexOfNextAdd and beyond, in the reverse of the order in which they were added.
Overrides:
[end](../../../javax/swing/undo/CompoundEdit.html#end%28%29)
in class [CompoundEdit](../../../javax/swing/undo/CompoundEdit.html "class in javax.swing.undo")
See Also:
getUndoOrRedoPresentationName
public String getUndoOrRedoPresentationName()
Return the appropriate name for a command that toggles between undo and redo. Only makes sense to use such a command if limit == 1 and we're not in progress.
getUndoPresentationName
public String getUndoPresentationName()
If inProgress, returns getUndoPresentationName of the significant edit that will be undone when undo() is invoked. If there is none, returns AbstractUndoableEdit.undoText from the defaults table.
If not inProgress, acts as a CompoundEdit
Specified by:
[getUndoPresentationName](../../../javax/swing/undo/UndoableEdit.html#getUndoPresentationName%28%29)
in interface [UndoableEdit](../../../javax/swing/undo/UndoableEdit.html "interface in javax.swing.undo")
Overrides:
[getUndoPresentationName](../../../javax/swing/undo/CompoundEdit.html#getUndoPresentationName%28%29)
in class [CompoundEdit](../../../javax/swing/undo/CompoundEdit.html "class in javax.swing.undo")
Returns:
the value from the defaults table with keyAbstractUndoableEdit.undoText
, followed by a space, followed by getPresentationName
unless getPresentationName
is "" in which case, the defaults value is returned alone.
See Also:
undo(), CompoundEdit.getUndoPresentationName()
getRedoPresentationName
public String getRedoPresentationName()
If inProgress, returns getRedoPresentationName of the significant edit that will be redone when redo() is invoked. If there is none, returns AbstractUndoableEdit.redoText from the defaults table.
If not inProgress, acts as a CompoundEdit
Specified by:
[getRedoPresentationName](../../../javax/swing/undo/UndoableEdit.html#getRedoPresentationName%28%29)
in interface [UndoableEdit](../../../javax/swing/undo/UndoableEdit.html "interface in javax.swing.undo")
Overrides:
[getRedoPresentationName](../../../javax/swing/undo/CompoundEdit.html#getRedoPresentationName%28%29)
in class [CompoundEdit](../../../javax/swing/undo/CompoundEdit.html "class in javax.swing.undo")
Returns:
the value from the defaults table with keyAbstractUndoableEdit.redoText
, followed by a space, followed by getPresentationName
unless getPresentationName
is "" in which case, the defaults value is returned alone.
See Also:
redo(), CompoundEdit.getUndoPresentationName()
undoableEditHappened
public void undoableEditHappened(UndoableEditEvent e)
Called by the UndoabledEdit sources this UndoManager listens to. Calls addEdit with e.getEdit().
Specified by:
[undoableEditHappened](../../../javax/swing/event/UndoableEditListener.html#undoableEditHappened%28javax.swing.event.UndoableEditEvent%29)
in interface [UndoableEditListener](../../../javax/swing/event/UndoableEditListener.html "interface in javax.swing.event")
See Also:
addEdit(javax.swing.undo.UndoableEdit)
toString
public String toString()
Returns a string that displays and identifies this object's properties.
Overrides:
[toString](../../../javax/swing/undo/CompoundEdit.html#toString%28%29)
in class [CompoundEdit](../../../javax/swing/undo/CompoundEdit.html "class in javax.swing.undo")
Returns:
a String representation of this object
Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.