Class Undo

  • All Implemented Interfaces:
    CleanUpHandler

    public class Undo
    extends Object
    implements CleanUpHandler
    A general Undo/Redo-mechanism: stores objects either in memory or on disk. The objects have to be serializable.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Field Detail

      • COMMENT_MAX_LENGTH

        public static final int COMMENT_MAX_LENGTH
        the maximum length for a comment.
        See Also:
        Constant Field Values
      • DEFAULT_MAX_UNDO

        public static final int DEFAULT_MAX_UNDO
        the default for number of undo steps.
        See Also:
        Constant Field Values
      • m_UndoClass

        protected Class m_UndoClass
        the class for which the undo is.
      • m_OnDisk

        protected boolean m_OnDisk
        whether to store the objects on disk.
      • m_UndoList

        protected List<Undo.UndoPoint> m_UndoList
        the vector for the undo-points objects.
      • m_RedoList

        protected List<Undo.UndoPoint> m_RedoList
        the vector for the redo-points objects.
      • m_Enabled

        protected boolean m_Enabled
        whether undo is currently enabled or not.
      • m_Working

        protected boolean m_Working
        whether the object is currently working, e.g., performing an undo/redo.
      • m_MaxUndo

        protected int m_MaxUndo
        the maximum number of undo points.
      • m_ShorteningType

        protected ShorteningType m_ShorteningType
        where to shorten the comments.
      • m_MaxCommentLength

        protected int m_MaxCommentLength
        the maximum length for comments before shortening.
    • Constructor Detail

      • Undo

        public Undo​(Class undoClass)
        Initializes the undo mechanism for the specified class (in memory). Maximum of DEFAULT_MAX_UNDO undo steps.
        Parameters:
        undoClass - the class for which the undo is setup
        See Also:
        setMaxUndo(int), DEFAULT_MAX_UNDO
      • Undo

        public Undo​(Class undoClass,
                    boolean onDisk)
        Initializes the undo mechanism for the specified class. Maximum of DEFAULT_MAX_UNDO undo steps.
        Parameters:
        undoClass - the class for which the undo is setup
        onDisk - whether to store the objects on disk (when they're serializable)
        See Also:
        setMaxUndo(int), DEFAULT_MAX_UNDO
    • Method Detail

      • getUndoClass

        public Class getUndoClass()
        Returns the class this undo mechanism is for.
        Returns:
        the class this undo is for
      • getOnDisk

        public boolean getOnDisk()
        Returns whether the objects are stored on disk (only possible for serializable ones) or held in memory.
        Returns:
        true if the objects are stored on disk
      • isEnabled

        public boolean isEnabled()
        Returns whether undo is currently enabled or not.
        Returns:
        true if undo is enabled
      • setEnabled

        public void setEnabled​(boolean value)
        Sets the enabled state, i.e., either enables or disables the undo.
        Parameters:
        value - if true then the undo will be enabled.
      • setMaxUndo

        public void setMaxUndo​(int value)
        Sets the maximum number of undo steps to allow. Can only be set before the first undo/redo happens.
        Parameters:
        value - the number of undo steps, use -1 for unlimited
      • getMaxUndo

        public int getMaxUndo()
        Returns the maximum number of undo steps to allow.
        Returns:
        the number of undo steps, -1 if unlimited
      • isWorking

        public boolean isWorking()
        Returns whether undo/redo is currently performed.
        Returns:
        true if an undo/redo is currently performed
      • addUndo

        public boolean addUndo​(Object o,
                               String comment)
        Adds the object to the undo list, throws the Redo list away.
        Parameters:
        o - the object to add to the undo list
        comment - the comment to associate with the undo point
        Returns:
        true if the object was added successfully
      • addUndo

        public boolean addUndo​(Object o,
                               String comment,
                               boolean keepRedo)
        Adds the object to the undo list.
        Parameters:
        o - the object to add to the undo list
        comment - the comment to associate with the undo point
        keepRedo - whether to keep the redo list
        Returns:
        true if the object was added successfully
      • addRedo

        public boolean addRedo​(Object o,
                               String comment)
        Adds the object to the redo list.
        Parameters:
        o - the object to add to the redo list
        comment - the comment to associate with the redo point
        Returns:
        true if the object was added successfully
      • canUndo

        public boolean canUndo()
        Returns whether any undo-steps are left.
        Returns:
        true if at least one undo-step is left
      • undo

        public Undo.UndoPoint undo()
        Returns the object from the next undo step and removes it from its internal list.
        Returns:
        the next undo object
      • peekUndo

        public Undo.UndoPoint peekUndo()
        "Peeks" at the last undo item.
        Returns:
        the last undo item
      • peekUndoComment

        public String peekUndoComment()
        Returns the comment of the last undo point.
        Returns:
        the comment
      • undo

        protected Undo.UndoPoint undo​(boolean notify)
        Returns the object from the next undo step and removes it from its internal list.
        Parameters:
        notify - whether a notify event should be sent
        Returns:
        the next undo object, or null if not successful
      • canRedo

        public boolean canRedo()
        Returns whether any redo-steps are left.
        Returns:
        true if at least one redo-step is left
      • redo

        public Undo.UndoPoint redo()
        Returns the object from the next redo step and removes it from its internal list.
        Returns:
        the next undo object
      • peekRedo

        public Undo.UndoPoint peekRedo()
        "Peeks" at the last redo item.
        Returns:
        the last redo item
      • peekRedoComment

        public String peekRedoComment()
        Returns the comment of the last redo point.
        Returns:
        the comment
      • redo

        protected Undo.UndoPoint redo​(boolean notify)
        Returns the object from the next redo step and removes it from its internal list.
        Parameters:
        notify - whether a notify event should be sent
        Returns:
        the next redo object, or null if not successful
      • cleanUp

        protected void cleanUp​(List<Undo.UndoPoint> list)
        If "onDisk" is active, removes all files from disk, otherwise it only empties the list.
        Parameters:
        list - the undo points to clean up
      • clear

        public void clear()
        Clears the undo and redo list.
      • addUndoListener

        public void addUndoListener​(UndoListener l)
        Adds the listener to the internal list.
        Parameters:
        l - the listener to add
      • removeUndoListener

        public void removeUndoListener​(UndoListener l)
        Removes the listener from the internal list.
        Parameters:
        l - the listener to remove
      • notifyListeners

        protected void notifyListeners​(UndoEvent e)
        notifies all listeners with the specified event.
        Parameters:
        e - the event to send
      • setShorteningType

        public void setShorteningType​(ShorteningType value)
        Sets how to shorten the comments.
        Parameters:
        value - the type
      • getShorteningType

        public ShorteningType getShorteningType()
        Returns how to shorten the comments.
        Returns:
        the type
      • setMaxCommentLength

        public void setMaxCommentLength​(int value)
        Sets the maximum length for comments before shortening them.
        Parameters:
        value - the maximum length
      • getMaxCommentLength

        public int getMaxCommentLength()
        Returns the maximum length for comments before shortening them.
        Returns:
        the maximum length
      • shortenComment

        protected String shortenComment​(String s)
        Shortens the comment.
        Parameters:
        s - the comment to process
        Returns:
        the (potentially) shortened comment
      • cleanUp

        public void cleanUp()
        Cleans up data structures, frees up memory.
        Specified by:
        cleanUp in interface CleanUpHandler