Class UndoManager

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

    public class UndoManager
    extends javax.swing.undo.UndoManager
    A PAMELA UndoManager tracks and record PAMELA atomic edits into aggregates named compound edit.
    It provides a way to undo or redo the appropriate edits.
    To instantiate and enable an UndoManager, use the PamelaModelFactory#createUndoManager() method.
    UndoManager internally manages a list of CompoundEdit, which are aggregates of PAMELA atomic events (AtomicEdit).
    You should use {startRecording(String) and {stopRecording(CompoundEdit) methods to semantically control undo/redo sequences.
    The UndoManager is automatically tracking all PAMELA atomic events (see AtomicEdit). If an AtomicEdit is received outside a declared recording edit, then a new edition (a CompoundEdit) is automatically instantiated. No inconsistency should be raised because of unregistered edits.
    UndoManager maintains 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 if undo() has been invoked it corresponds to the index of the last significant edit that was undone. When undo() is invoked all edits from the index of the next edit to the last significant edit are undone, in reverse order.
    Invoking redo() results in invoking redo() on all edits between the index of the next edit and the next significant edit (or the end of the list).
    Adding an edit to an UndoManager results in removing all edits from the index of the next edit to the end of the list.
    TODO: WARNING: if you are dealing with custom implementation of PAMELA objects with methods involved in setter/adder/remover and declared as final, those calls won't be intercepted by the UndoManager, and thus results will be of undetermined state. Please fix this.
    This class is thread safe.
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ENABLED  
      static java.lang.String REDONE  
      static java.lang.String START_RECORDING  
      static java.lang.String STOP_RECORDING  
      static java.lang.String UNDONE  
      static java.lang.String UNIDENTIFIED_RECORDING  
      • Fields inherited from class javax.swing.undo.CompoundEdit

        edits
      • Fields inherited from class javax.swing.undo.AbstractUndoableEdit

        RedoName, UndoName
    • Constructor Summary

      Constructors 
      Constructor Description
      UndoManager()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean addEdit​(javax.swing.undo.UndoableEdit anEdit)
      Adds an UndoableEdit to this UndoManager, if it's possible.
      boolean canRedo()
      Returns true if edits may be redone.
      boolean canUndo()
      Returns true if edits may be undone.
      boolean canUndoIfStoppingCurrentEdition()
      Returns true if edits may be undone.
      If en edition is in progress, return true if stopping this edition will cause UndoManager to be able to undo
      void debug()  
      void disableAnticipatedRecording()
      Disable 'anticipated' recording, see above
      CompoundEdit editToBeRedone()
      Returns the the next significant edit to be redone if redo is invoked.
      CompoundEdit editToBeUndone()
      Returns the the next significant edit to be undone if undo is invoked.
      void enableAnticipatedRecording()
      This method should be called whenever some edit might be triggered outside of an official edition action, but if those edits
      should be aggregated in the next edition action.
      Calling this method enable this scheme.
      protected void fireAddEdit​(javax.swing.undo.UndoableEdit anEdit)  
      protected void fireRedo()  
      protected void fireUndo()  
      CompoundEdit getCurrentEdition()
      Return current edition
      java.lang.String getDeletedProperty()  
      java.util.List<javax.swing.undo.UndoableEdit> getEdits()  
      java.beans.PropertyChangeSupport getPropertyChangeSupport()  
      int getUndoLevel()  
      java.lang.String getUndoPresentationName()
      Returns a description of the undoable form of this edit.
      boolean isAnticipatedRecording()
      Return flag indicating if UndoManager is currently recording in anticipated mode
      boolean isBeeingRecording()
      Return flag indicating if UndoManager is currently recording.
      Return true if start recording has been called
      boolean isEnabled()  
      boolean isIgnorable​(javax.swing.undo.UndoableEdit edit)
      This should be used as a hook to intercept some edit that we dont't want to be caught during capture of UNDO edits
      This method should be overriden when required.
      Default implementation return false
      boolean isRedoInProgress()
      Return boolean indicating if redo is currently being processed
      boolean isUndoInProgress()
      Return boolean indicating if undo is currently being processed
      protected CompoundEdit makeCompoundEdit​(java.lang.String presentationName)  
      void redo()
      Redoes the appropriate edits.
      Invokes redo on all edits between the index of the next edit and the next significant edit, updating the index of the next edit appropriately.
      void setEnabled​(boolean enabled)  
      void setUndoLevel​(int undoLevel)  
      CompoundEdit startRecording​(java.lang.String presentationName)
      Start a new labelled edit tracking
      All PAMELA atomic events that will be received from now will be aggregated into newly created CompoundEdit
      CompoundEdit stopRecording​(CompoundEdit edit)
      Stops supplied edit tracking
      After this method, this edit will be available for undo/redo
      void undo()
      Undoes the appropriate edits.
      Invokes undo on all edits between the index of the next edit and the last significant edit, updating the index of the next edit appropriately.
      • Methods inherited from class javax.swing.undo.UndoManager

        canUndoOrRedo, discardAllEdits, end, getLimit, getRedoPresentationName, getUndoOrRedoPresentationName, redoTo, setLimit, toString, trimEdits, trimForLimit, undoableEditHappened, undoOrRedo, undoTo
      • 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
    • Constructor Detail

      • UndoManager

        public UndoManager()
    • Method Detail

      • enableAnticipatedRecording

        public void enableAnticipatedRecording()
        This method should be called whenever some edit might be triggered outside of an official edition action, but if those edits
        should be aggregated in the next edition action.
        Calling this method enable this scheme. Next received edits will be stored temporarily in this anticipated recording, which will be turned into next official edition action.
      • disableAnticipatedRecording

        public void disableAnticipatedRecording()
        Disable 'anticipated' recording, see above
      • startRecording

        public CompoundEdit startRecording​(java.lang.String presentationName)
        Start a new labelled edit tracking
        All PAMELA atomic events that will be received from now will be aggregated into newly created CompoundEdit
        Parameters:
        presentationName -
        Returns:
        the newly created CompoundEdit
      • makeCompoundEdit

        protected CompoundEdit makeCompoundEdit​(java.lang.String presentationName)
      • stopRecording

        public CompoundEdit stopRecording​(CompoundEdit edit)
        Stops supplied edit tracking
        After this method, this edit will be available for undo/redo
        Parameters:
        edit -
        Returns:
      • isBeeingRecording

        public boolean isBeeingRecording()
        Return flag indicating if UndoManager is currently recording.
        Return true if start recording has been called
        Returns:
      • isAnticipatedRecording

        public boolean isAnticipatedRecording()
        Return flag indicating if UndoManager is currently recording in anticipated mode
        Returns:
      • canUndo

        public boolean canUndo()
        Returns true if edits may be undone.
        Specified by:
        canUndo in interface javax.swing.undo.UndoableEdit
        Overrides:
        canUndo in class javax.swing.undo.UndoManager
        Returns:
        true if there are edits to be undone
        See Also:
        CompoundEdit.canUndo(), editToBeUndone()
      • canUndoIfStoppingCurrentEdition

        public boolean canUndoIfStoppingCurrentEdition()
        Returns true if edits may be undone.
        If en edition is in progress, return true if stopping this edition will cause UndoManager to be able to undo
        Returns:
        true if there are edits to be undone
        See Also:
        CompoundEdit.canUndo(), editToBeUndone()
      • canRedo

        public boolean canRedo()
        Returns true if edits may be redone.
        Specified by:
        canRedo in interface javax.swing.undo.UndoableEdit
        Overrides:
        canRedo in class javax.swing.undo.UndoManager
        Returns:
        true if there are edits to be redone
        See Also:
        CompoundEdit.canRedo(), editToBeRedone()
      • addEdit

        public boolean addEdit​(javax.swing.undo.UndoableEdit anEdit)
        Adds an UndoableEdit to this UndoManager, if it's possible.
        Specified by:
        addEdit in interface javax.swing.undo.UndoableEdit
        Overrides:
        addEdit in class javax.swing.undo.UndoManager
      • fireAddEdit

        protected void fireAddEdit​(javax.swing.undo.UndoableEdit anEdit)
      • isIgnorable

        public boolean isIgnorable​(javax.swing.undo.UndoableEdit edit)
        This should be used as a hook to intercept some edit that we dont't want to be caught during capture of UNDO edits
        This method should be overriden when required.
        Default implementation return false
        Parameters:
        edit -
        Returns:
      • editToBeUndone

        public CompoundEdit editToBeUndone()
        Returns the the next significant edit to be undone if undo is invoked. This returns null if there are no edits to be undone.
        Overrides:
        editToBeUndone in class javax.swing.undo.UndoManager
        Returns:
        the next significant edit to be undone
      • editToBeRedone

        public CompoundEdit editToBeRedone()
        Returns the the next significant edit to be redone if redo is invoked. This returns null if there are no edits to be redone.
        Overrides:
        editToBeRedone in class javax.swing.undo.UndoManager
        Returns:
        the next significant edit to be redone
      • undo

        public void undo()
                  throws javax.swing.undo.CannotUndoException
        Undoes the appropriate edits.
        Invokes undo on all edits between the index of the next edit and the last significant edit, updating the index of the next edit appropriately.
        Specified by:
        undo in interface javax.swing.undo.UndoableEdit
        Overrides:
        undo in class javax.swing.undo.UndoManager
        Throws:
        javax.swing.undo.CannotUndoException - if one of the edits throws CannotUndoException or there are no edits to be undone
        See Also:
        CompoundEdit.end(), canUndo(), editToBeUndone()
      • fireUndo

        protected void fireUndo()
      • redo

        public void redo()
                  throws javax.swing.undo.CannotUndoException
        Redoes the appropriate edits.
        Invokes redo on all edits between the index of the next edit and the next significant edit, updating the index of the next edit appropriately.
        Specified by:
        redo in interface javax.swing.undo.UndoableEdit
        Overrides:
        redo in class javax.swing.undo.UndoManager
        Throws:
        javax.swing.undo.CannotRedoException - if one of the edits throws CannotRedoException or there are no edits to be redone
        javax.swing.undo.CannotUndoException
        See Also:
        CompoundEdit.end(), canRedo(), editToBeRedone()
      • fireRedo

        protected void fireRedo()
      • getCurrentEdition

        public CompoundEdit getCurrentEdition()
        Return current edition
      • getEdits

        public java.util.List<javax.swing.undo.UndoableEdit> getEdits()
      • debug

        public void debug()
      • getDeletedProperty

        public java.lang.String getDeletedProperty()
      • getPropertyChangeSupport

        public java.beans.PropertyChangeSupport getPropertyChangeSupport()
      • getUndoPresentationName

        public java.lang.String getUndoPresentationName()
        Returns a description of the undoable form of this edit.
        Specified by:
        getUndoPresentationName in interface javax.swing.undo.UndoableEdit
        Overrides:
        getUndoPresentationName in class javax.swing.undo.UndoManager
        Returns:
        a description of the undoable form of this edit
      • isUndoInProgress

        public boolean isUndoInProgress()
        Return boolean indicating if undo is currently being processed
      • isRedoInProgress

        public boolean isRedoInProgress()
        Return boolean indicating if redo is currently being processed
      • getUndoLevel

        public int getUndoLevel()
      • setUndoLevel

        public void setUndoLevel​(int undoLevel)
      • isEnabled

        public boolean isEnabled()
      • setEnabled

        public void setEnabled​(boolean enabled)