Package editor.undo

Class AtomicUndoManager

  • All Implemented Interfaces:
    java.io.Serializable, java.util.EventListener, javax.swing.event.UndoableEditListener, javax.swing.undo.UndoableEdit

    public class AtomicUndoManager
    extends javax.swing.undo.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:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      AtomicUndoManager()
      An AtomicUndoManager with a default limit of 1000 edits.
      AtomicUndoManager​(int iUndoLimit)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addChangeListener​(javax.swing.event.ChangeListener listener)  
      boolean addEdit​(javax.swing.undo.UndoableEdit edit)  
      void beginUndoAtom()
      Begin an atomic undo boundary.
      javax.swing.undo.CompoundEdit beginUndoAtom​(java.lang.String strDisplayName)
      Begin an atomic undo boundary.
      void discardAllEdits()  
      void endUndoAtom()
      Ends the active atomic undo boundary.
      void endUndoAtom​(javax.swing.undo.CompoundEdit undoAtom)  
      private void fireChangeEvent​(UndoChangeEvent.ChangeType type, javax.swing.undo.UndoableEdit edit)  
      long getLastUndoTime()  
      AtomicUndoManager.DisplayableCompoundEdit getUndoAtom()  
      boolean isPaused()  
      void recordChange​(javax.swing.undo.StateEditable change)
      Utility method that records a change that can be undone/redone.
      void redo()  
      protected void redoTo​(javax.swing.undo.UndoableEdit edit)  
      void removeChangeListener​(javax.swing.event.ChangeListener listener)  
      void removeEdit​(javax.swing.undo.UndoableEdit edit)  
      private void setLastUndoTime()  
      void setPaused​(boolean bPaused)
      Sets the paused state of the undo manager.
      void undo()  
      void undoOrRedo()  
      protected void undoTo​(javax.swing.undo.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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • _undoAtomNest

        private java.util.Stack<AtomicUndoManager.DisplayableCompoundEdit> _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 java.util.List<javax.swing.event.ChangeListener> _changeListeners
      • _lLastUndoTime

        private long _lLastUndoTime
    • Constructor Detail

      • 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 Detail

      • 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

        public boolean addEdit​(javax.swing.undo.UndoableEdit edit)
        Specified by:
        addEdit in interface javax.swing.undo.UndoableEdit
        Overrides:
        addEdit in class javax.swing.undo.UndoManager
        Parameters:
        edit - The edit to be added. Note nested edits are supported.
      • removeEdit

        public void removeEdit​(javax.swing.undo.UndoableEdit edit)
        Parameters:
        edit - The edit to be removed.
      • discardAllEdits

        public void discardAllEdits()
        Overrides:
        discardAllEdits in class javax.swing.undo.UndoManager
      • 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 to endUndoAtom().

        Note undo atoms can contain other undo atoms allowing for unlimited nesting of atomic undo operations.

      • beginUndoAtom

        public javax.swing.undo.CompoundEdit beginUndoAtom​(java.lang.String strDisplayName)
        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 to endUndoAtom().

        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:
        UndoManager.getUndoPresentationName()
      • endUndoAtom

        public void endUndoAtom()
        Ends the active atomic undo boundary.
        See Also:
        beginUndoAtom()
      • endUndoAtom

        public void endUndoAtom​(javax.swing.undo.CompoundEdit undoAtom)
      • redo

        public void redo()
                  throws javax.swing.undo.CannotRedoException
        Specified by:
        redo in interface javax.swing.undo.UndoableEdit
        Overrides:
        redo in class javax.swing.undo.UndoManager
        Throws:
        javax.swing.undo.CannotRedoException
      • undo

        public void undo()
                  throws javax.swing.undo.CannotUndoException
        Specified by:
        undo in interface javax.swing.undo.UndoableEdit
        Overrides:
        undo in class javax.swing.undo.UndoManager
        Throws:
        javax.swing.undo.CannotUndoException
      • getLastUndoTime

        public long getLastUndoTime()
      • setLastUndoTime

        private void setLastUndoTime()
      • undoOrRedo

        public void undoOrRedo()
                        throws javax.swing.undo.CannotRedoException,
                               javax.swing.undo.CannotUndoException
        Overrides:
        undoOrRedo in class javax.swing.undo.UndoManager
        Throws:
        javax.swing.undo.CannotRedoException
        javax.swing.undo.CannotUndoException
      • undoTo

        protected void undoTo​(javax.swing.undo.UndoableEdit edit)
                       throws javax.swing.undo.CannotUndoException
        Overrides:
        undoTo in class javax.swing.undo.UndoManager
        Throws:
        javax.swing.undo.CannotUndoException
      • redoTo

        protected void redoTo​(javax.swing.undo.UndoableEdit edit)
                       throws javax.swing.undo.CannotRedoException
        Overrides:
        redoTo in class javax.swing.undo.UndoManager
        Throws:
        javax.swing.undo.CannotRedoException
      • addChangeListener

        public void addChangeListener​(javax.swing.event.ChangeListener listener)
      • removeChangeListener

        public void removeChangeListener​(javax.swing.event.ChangeListener listener)
      • recordChange

        public void recordChange​(javax.swing.undo.StateEditable change)
        Utility method that records a change that can be undone/redone.