Package editor.undo

Class AtomicUndoManager

All Implemented Interfaces:
Serializable, EventListener, UndoableEditListener, UndoableEdit

public class AtomicUndoManager extends UndoManager
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:
  • Field Details

    • _undoAtomNest

      A stack used for managing nested undo atoms. Whenever an atomic undo atom begins a new DisplayableCompoundEdit is constructed on it's behalf and pushed onto the stack. When an undo atom ends its DisplayableCompoundEdit 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

      private List<ChangeListener> _changeListeners
    • _lLastUndoTime

      private long _lLastUndoTime
  • Constructor Details

    • AtomicUndoManager

      public AtomicUndoManager()
      An AtomicUndoManager 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