Package editor.undo
Class AtomicUndoManager
java.lang.Object
javax.swing.undo.AbstractUndoableEdit
javax.swing.undo.CompoundEdit
javax.swing.undo.UndoManager
editor.undo.AtomicUndoManager
- All Implemented Interfaces:
Serializable
,EventListener
,UndoableEditListener
,UndoableEdit
This class extends
UndoManager
providing nested atomic (or scoped) undo/redo
operations. Note undo/redo operations are captured in StateEditable
implementations.
A generic application of this class follows:
...
_undoMgr = new AtomicUndoManager( 1000 );
...
//
// This method decorates a typical change operation with undo/redo capability.
// Notice the XXXOperationUndoItem class. You'll need to create one of these
// for each undo operation. Basically these UndoItem classes are extensions
// of StateEditable
. They are responsible for capturing the state
// before and after an operation and also for restoring the state as directed
// by this undo manager.
//
void performUndoableXXXOperation( ... )
{
XXXOperationUndoItem undoItem = new XXXOperationUndoItem( ... );
StateEdit transaction = new StateEdit( undoItem, "XXX Operation" );
performXXXOperation( ... );
transaction.end();
_undoMgr.addEdit( transaction );
}
// Your typical change operation.
void performXXXOperation( ... )
{
// do whatever it is you do...
}
//
// This method exercises the atomic and nested features of AtomicUndoManager.
// The atomic boundaries should be enforced with the try/finally idiom so
// a failed atomic operation can be safely (and optionally) "rolled back".
//
void performUndoableZZZOperation
{
try
{
_undoMgr.beginUndoAtom( "ZZZ Operation" );
performUndoableXXXOperation( ... );
performUndoableYYYOperation( ... );
...
}
finally
{
_undoMgr.endUndoAtom();
}
}
//
// Create ui components using Actions such as these:
//
class UndoAction extends AbstractAction
{
UndoAction( String strName, Icon icon )
{
super( strName, icon );
}
public void actionPerformed( ActionEvent e )
{
_undoMgr.undo();
}
public boolean isEnabled()
{
return _undoMgr.canUndo();
}
}
class RedoAction extends AbstractAction
{
RedoAction( String strName, Icon icon )
{
super( strName, icon );
}
public void actionPerformed( ActionEvent e )
{
_undoMgr.redo();
}
public boolean isEnabled()
{
return _undoMgr.canRedo();
}
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private List<ChangeListener>
private long
A stack used for managing nested undo atoms.Fields inherited from class javax.swing.undo.CompoundEdit
edits
Fields inherited from class javax.swing.undo.AbstractUndoableEdit
RedoName, UndoName
-
Constructor Summary
ConstructorsConstructorDescriptionAnAtomicUndoManager
with a default limit of 1000 edits.AtomicUndoManager
(int iUndoLimit) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addChangeListener
(ChangeListener listener) boolean
addEdit
(UndoableEdit edit) void
Begin an atomic undo boundary.beginUndoAtom
(String strDisplayName) Begin an atomic undo boundary.void
void
Ends the active atomic undo boundary.void
endUndoAtom
(CompoundEdit undoAtom) private void
fireChangeEvent
(UndoChangeEvent.ChangeType type, UndoableEdit edit) long
boolean
isPaused()
void
recordChange
(StateEditable change) Utility method that records a change that can be undone/redone.void
redo()
protected void
redoTo
(UndoableEdit edit) void
removeChangeListener
(ChangeListener listener) void
removeEdit
(UndoableEdit edit) private void
void
setPaused
(boolean bPaused) Sets the paused state of the undo manager.void
undo()
void
protected void
undoTo
(UndoableEdit edit) Methods inherited from class javax.swing.undo.UndoManager
canRedo, canUndo, canUndoOrRedo, editToBeRedone, editToBeUndone, end, getLimit, getRedoPresentationName, getUndoOrRedoPresentationName, getUndoPresentationName, setLimit, toString, trimEdits, trimForLimit, undoableEditHappened
Methods inherited from class javax.swing.undo.CompoundEdit
die, getPresentationName, isInProgress, isSignificant, lastEdit
Methods inherited from class javax.swing.undo.AbstractUndoableEdit
replaceEdit
-
Field Details
-
_undoAtomNest
A stack used for managing nested undo atoms. Whenever an atomic undo atom begins a newDisplayableCompoundEdit
is constructed on it's behalf and pushed onto the stack. When an undo atom ends itsDisplayableCompoundEdit
is popped off the stack and added to the next atom's edit list in the stack or, if the stack is empty, it is directly added to this undo manager's edit list. -
_bPaused
private boolean _bPaused -
_changeListeners
-
_lLastUndoTime
private long _lLastUndoTime
-
-
Constructor Details
-
AtomicUndoManager
public AtomicUndoManager()AnAtomicUndoManager
with a default limit of 1000 edits. -
AtomicUndoManager
public AtomicUndoManager(int iUndoLimit) - Parameters:
iUndoLimit
- The maximum number of edits this undo manager will hold.
-
-
Method Details
-
isPaused
public boolean isPaused()- Returns:
- Is this undo manager in a Paused state? i.e., are edits being ignored?
-
setPaused
public void setPaused(boolean bPaused) Sets the paused state of the undo manager. If paused, undo operations are ignored- Parameters:
bPaused
- The paused state.
-
addEdit
- Specified by:
addEdit
in interfaceUndoableEdit
- Overrides:
addEdit
in classUndoManager
- Parameters:
edit
- The edit to be added. Note nested edits are supported.
-
removeEdit
- Parameters:
edit
- The edit to be removed.
-
discardAllEdits
public void discardAllEdits()- Overrides:
discardAllEdits
in classUndoManager
-
beginUndoAtom
public void beginUndoAtom()Begin an atomic undo boundary. Edits added to this undo mananger after this call are considered "subatomic" -- they become part of the atom defined by the boundary. The atom's boundary ends with a call toendUndoAtom()
. Note undo atoms can contain other undo atoms allowing for unlimited nesting of atomic undo operations. -
beginUndoAtom
Begin an atomic undo boundary. Edits added to this undo mananger after this call are considered "subatomic" -- they become part of the atom defined by the boundary. The atom's boundary ends with a call toendUndoAtom()
. Note undo atoms can contain other undo atoms allowing for unlimited nesting of atomic undo operations.- Parameters:
strDisplayName
- The name associated with the undo atom. A user interface typically displays this name in Undo/Redo menu items or toolbar button text.- See Also:
-
endUndoAtom
public void endUndoAtom()Ends the active atomic undo boundary.- See Also:
-
endUndoAtom
-
getUndoAtom
- Returns:
- The undo atom currently in progress.
-
redo
- Specified by:
redo
in interfaceUndoableEdit
- Overrides:
redo
in classUndoManager
- Throws:
CannotRedoException
-
undo
- Specified by:
undo
in interfaceUndoableEdit
- Overrides:
undo
in classUndoManager
- Throws:
CannotUndoException
-
getLastUndoTime
public long getLastUndoTime() -
setLastUndoTime
private void setLastUndoTime() -
undoOrRedo
- Overrides:
undoOrRedo
in classUndoManager
- Throws:
CannotRedoException
CannotUndoException
-
undoTo
- Overrides:
undoTo
in classUndoManager
- Throws:
CannotUndoException
-
redoTo
- Overrides:
redoTo
in classUndoManager
- Throws:
CannotRedoException
-
addChangeListener
-
removeChangeListener
-
recordChange
Utility method that records a change that can be undone/redone. -
fireChangeEvent
-