- 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 UndoManagermanages a list ofUndoableEdits, providing a way to undo or redo the appropriate edits. There are two ways to add edits to anUndoManager. Add the edit directly using theaddEditmethod, or add theUndoManagerto a bean that supportsUndoableEditListener. The following examples creates anUndoManagerand adds it as anUndoableEditListenerto aJTextField:UndoManager undoManager = new UndoManager(); JTextField tf = ...; tf.getDocument().addUndoableEditListener(undoManager); UndoManagermaintains 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 ifundohas been invoked it corresponds to the index of the last significant edit that was undone. Whenundois invoked all edits from the index of the next edit to the last significant edit are undone, in reverse order. For example, consider anUndoManagerconsisting 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 undoresults in invokingundoon 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 undoagain invokesundoon 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 redoresults in invokingredoon 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 ifredowere invoked,redowould 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 UndoManagerresults 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 havingdieinvoked 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 endhas been invoked on anUndoManagerthe superclass behavior is used for allUndoableEditmethods. Refer toCompoundEditfor 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.beanspackage. Please seeXMLEncoder.- See Also:
- Serialized Form
 
- 
- 
Field Summary- 
Fields declared in class javax.swing.undo.CompoundEditedits
 - 
Fields declared in class javax.swing.undo.AbstractUndoableEditRedoName, UndoName
 
- 
 - 
Constructor SummaryConstructors Constructor Description UndoManager()Creates a newUndoManager.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddEdit(UndoableEdit anEdit)Adds anUndoableEditto thisUndoManager, if it's possible.booleancanRedo()Returns true if edits may be redone.booleancanUndo()Returns true if edits may be undone.booleancanUndoOrRedo()Returns true if it is possible to invokeundoorredo.voiddiscardAllEdits()Empties the undo manager sending each edit adiemessage in the process.protected UndoableEditeditToBeRedone()Returns the next significant edit to be redone ifredois invoked.protected UndoableEditeditToBeUndone()Returns the next significant edit to be undone ifundois invoked.voidend()Turns thisUndoManagerinto a normalCompoundEdit.intgetLimit()Returns the maximum number of edits thisUndoManagerholds.StringgetRedoPresentationName()Returns a description of the redoable form of this edit.StringgetUndoOrRedoPresentationName()Convenience method that returns eithergetUndoPresentationNameorgetRedoPresentationName.StringgetUndoPresentationName()Returns a description of the undoable form of this edit.voidredo()Redoes the appropriate edits.protected voidredoTo(UndoableEdit edit)Redoes all changes from the index of the next edit toedit, updating the index of the next edit appropriately.voidsetLimit(int l)Sets the maximum number of edits thisUndoManagerholds.StringtoString()Returns a string that displays and identifies this object's properties.protected voidtrimEdits(int from, int to)Removes edits in the specified range.protected voidtrimForLimit()Reduces the number of queued edits to a range of size limit, centered on the index of the next edit.voidundo()Undoes the appropriate edits.voidundoableEditHappened(UndoableEditEvent e)AnUndoableEditListenermethod.voidundoOrRedo()Convenience method that invokes one ofundoorredo.protected voidundoTo(UndoableEdit edit)Undoes all changes from the index of the next edit toedit, updating the index of the next edit appropriately.- 
Methods declared in class javax.swing.undo.CompoundEditdie, getPresentationName, isInProgress, isSignificant, lastEdit
 - 
Methods declared in class javax.swing.undo.AbstractUndoableEditreplaceEdit
 
- 
 
- 
- 
- 
Method Detail- 
getLimitpublic int getLimit() Returns the maximum number of edits thisUndoManagerholds. A value less than 0 indicates the number of edits is not limited.- Returns:
- the maximum number of edits this UndoManagerholds
- See Also:
- addEdit(javax.swing.undo.UndoableEdit),- setLimit(int)
 
 - 
discardAllEditspublic void discardAllEdits() Empties the undo manager sending each edit adiemessage in the process.- See Also:
- AbstractUndoableEdit.die()
 
 - 
trimForLimitprotected void trimForLimit() Reduces the number of queued edits to a range of size limit, centered on the index of the next edit.
 - 
trimEditsprotected void trimEdits(int from, int to)Removes edits in the specified range. All edits in the given range (inclusive, and in reverse order) will havedieinvoked on them and are removed from the list of edits. This has no effect iffrom>to.- Parameters:
- from- the minimum index to remove
- to- the maximum index to remove
 
 - 
setLimitpublic void setLimit(int l) Sets the maximum number of edits thisUndoManagerholds. A value less than 0 indicates the number of edits is not limited. If edits need to be discarded to shrink the limit,diewill be invoked on them in the reverse order they were added. The default is 100.- Parameters:
- l- the new limit
- Throws:
- RuntimeException- if this- UndoManageris not in progress (- endhas been invoked)
- See Also:
- CompoundEdit.isInProgress(),- end(),- addEdit(javax.swing.undo.UndoableEdit),- getLimit()
 
 - 
editToBeUndoneprotected UndoableEdit editToBeUndone() Returns the next significant edit to be undone ifundois invoked. This returnsnullif there are no edits to be undone.- Returns:
- the next significant edit to be undone
 
 - 
editToBeRedoneprotected UndoableEdit editToBeRedone() Returns the next significant edit to be redone ifredois invoked. This returnsnullif there are no edits to be redone.- Returns:
- the next significant edit to be redone
 
 - 
undoToprotected void undoTo(UndoableEdit edit) throws CannotUndoException Undoes all changes from the index of the next edit toedit, updating the index of the next edit appropriately.- Parameters:
- edit- the edit to be undo to
- Throws:
- CannotUndoException- if one of the edits throws- CannotUndoException
 
 - 
redoToprotected void redoTo(UndoableEdit edit) throws CannotRedoException Redoes all changes from the index of the next edit toedit, updating the index of the next edit appropriately.- Parameters:
- edit- the edit to be redo to
- Throws:
- CannotRedoException- if one of the edits throws- CannotRedoException
 
 - 
undoOrRedopublic void undoOrRedo() throws CannotRedoException, CannotUndoExceptionConvenience method that invokes one ofundoorredo. If any edits have been undone (the index of the next edit is less than the length of the edits list) this invokesredo, otherwise it invokesundo.- Throws:
- CannotUndoException- if one of the edits throws- CannotUndoException
- CannotRedoException- if one of the edits throws- CannotRedoException
- See Also:
- canUndoOrRedo(),- getUndoOrRedoPresentationName()
 
 - 
canUndoOrRedopublic boolean canUndoOrRedo() Returns true if it is possible to invokeundoorredo.- Returns:
- true if invoking canUndoOrRedois valid
- See Also:
- undoOrRedo()
 
 - 
undopublic void undo() throws CannotUndoExceptionUndoes the appropriate edits. Ifendhas been invoked this calls through to the superclass, otherwise this invokesundoon all edits between the index of the next edit and the last significant edit, updating the index of the next edit appropriately.- Specified by:
- undoin interface- UndoableEdit
- Overrides:
- undoin class- CompoundEdit
- Throws:
- CannotUndoException- if one of the edits throws- CannotUndoExceptionor there are no edits to be undone
- See Also:
- CompoundEdit.end(),- canUndo(),- editToBeUndone()
 
 - 
canUndopublic boolean canUndo() Returns true if edits may be undone. Ifendhas been invoked, this returns the value from super. Otherwise this returns true if there are any edits to be undone (editToBeUndonereturns non-null).- Specified by:
- canUndoin interface- UndoableEdit
- Overrides:
- canUndoin class- CompoundEdit
- Returns:
- true if there are edits to be undone
- See Also:
- CompoundEdit.canUndo(),- editToBeUndone()
 
 - 
redopublic void redo() throws CannotRedoExceptionRedoes the appropriate edits. Ifendhas been invoked this calls through to the superclass. Otherwise this invokesredoon all edits between the index of the next edit and the next significant edit, updating the index of the next edit appropriately.- Specified by:
- redoin interface- UndoableEdit
- Overrides:
- redoin class- CompoundEdit
- Throws:
- CannotRedoException- if one of the edits throws- CannotRedoExceptionor there are no edits to be redone
- See Also:
- CompoundEdit.end(),- canRedo(),- editToBeRedone()
 
 - 
canRedopublic boolean canRedo() Returns true if edits may be redone. Ifendhas been invoked, this returns the value from super. Otherwise, this returns true if there are any edits to be redone (editToBeRedonereturns non-null).- Specified by:
- canRedoin interface- UndoableEdit
- Overrides:
- canRedoin class- CompoundEdit
- Returns:
- true if there are edits to be redone
- See Also:
- CompoundEdit.canRedo(),- editToBeRedone()
 
 - 
addEditpublic boolean addEdit(UndoableEdit anEdit) Adds anUndoableEditto thisUndoManager, if it's possible. This removes all edits from the index of the next edit to the end of the edits list. Ifendhas been invoked the edit is not added andfalseis returned. Ifendhasn't been invoked this returnstrue.- Specified by:
- addEditin interface- UndoableEdit
- Overrides:
- addEditin class- CompoundEdit
- Parameters:
- anEdit- the edit to be added
- Returns:
- true if anEditcan be incorporated into this edit
- See Also:
- CompoundEdit.end(),- CompoundEdit.addEdit(javax.swing.undo.UndoableEdit)
 
 - 
endpublic void end() Turns thisUndoManagerinto a normalCompoundEdit. This removes all edits that have been undone.- Overrides:
- endin class- CompoundEdit
- See Also:
- CompoundEdit.end()
 
 - 
getUndoOrRedoPresentationNamepublic String getUndoOrRedoPresentationName() Convenience method that returns eithergetUndoPresentationNameorgetRedoPresentationName. If the index of the next edit equals the size of the edits list,getUndoPresentationNameis returned, otherwisegetRedoPresentationNameis returned.- Returns:
- undo or redo name
 
 - 
getUndoPresentationNamepublic String getUndoPresentationName() Returns a description of the undoable form of this edit. Ifendhas been invoked this calls into super. Otherwise if there are edits to be undone, this returns the value from the next significant edit that will be undone. If there are no edits to be undone andendhas not been invoked this returns the value from theUIManagerproperty "AbstractUndoableEdit.undoText".- Specified by:
- getUndoPresentationNamein interface- UndoableEdit
- Overrides:
- getUndoPresentationNamein class- CompoundEdit
- Returns:
- a description of the undoable form of this edit
- See Also:
- undo(),- CompoundEdit.getUndoPresentationName()
 
 - 
getRedoPresentationNamepublic String getRedoPresentationName() Returns a description of the redoable form of this edit. Ifendhas been invoked this calls into super. Otherwise if there are edits to be redone, this returns the value from the next significant edit that will be redone. If there are no edits to be redone andendhas not been invoked this returns the value from theUIManagerproperty "AbstractUndoableEdit.redoText".- Specified by:
- getRedoPresentationNamein interface- UndoableEdit
- Overrides:
- getRedoPresentationNamein class- CompoundEdit
- Returns:
- a description of the redoable form of this edit
- See Also:
- redo(),- CompoundEdit.getRedoPresentationName()
 
 - 
undoableEditHappenedpublic void undoableEditHappened(UndoableEditEvent e) AnUndoableEditListenermethod. This invokesaddEditwithe.getEdit().- Specified by:
- undoableEditHappenedin interface- UndoableEditListener
- Parameters:
- e- the- UndoableEditEventthe- UndoableEditEventwill be added from
- See Also:
- addEdit(javax.swing.undo.UndoableEdit)
 
 - 
toStringpublic String toString() Returns a string that displays and identifies this object's properties.- Overrides:
- toStringin class- CompoundEdit
- Returns:
- a String representation of this object
 
 
- 
 
-