Class UndoStackAttribute

  • All Implemented Interfaces:
    java.lang.Cloneable, Changeable, Debuggable, DebugListener, Derivable, ModelErrorHandler, MoMLExportable, Moveable, Nameable, Singleton
    Direct Known Subclasses:
    GTFrameTools.DelegatedUndoStackAttribute

    public class UndoStackAttribute
    extends SingletonAttribute
    This attribute holds the undo/redo information for a model. This attribute is not persistent, so undo/redo information disappears when the model is closed. It is also a singleton, meaning that it will replace any previous attribute that has the same name and is an instance of the same base class, SingletonAttribute.

    Two stacks of information are maintained - one for undo information and one for redo information. Normally, a push onto this stack puts the undo information in the undo stack. However, if the push occurs during the execution of an undo, then the information is put on the redo stack. The entries on the stack implement the UndoAction interface.

    NOTE: the information in the redo stack is emptied when a new undo action is pushed onto the undo stack that was not the result of a redo being requested. This situation arises when a user requests a series of undo and redo operations, and then performs some normal undoable action. At this point the information in the redo stack is not relevant to the state of the model and so must be cleared.

    Since:
    Ptolemy II 3.1
    Version:
    $Id$
    Author:
    Neil Smyth and Edward A. Lee
    See Also:
    UndoAction
    Pt.AcceptedRating:
    Red (cxh)
    Pt.ProposedRating:
    Yellow (eal)
    • Constructor Detail

      • UndoStackAttribute

        public UndoStackAttribute​(NamedObj container,
                                  java.lang.String name)
                           throws IllegalActionException,
                                  NameDuplicationException
        Construct an attribute with the given name contained by the specified container. The container argument must not be null, or a NullPointerException will be thrown. This attribute will use the workspace of the container for synchronization and version counts. If the name argument is null, then the name is set to the empty string. The object is added to the directory of the workspace if the container is null. Increment the version of the workspace.
        Parameters:
        container - The container.
        name - The name of this attribute.
        Throws:
        IllegalActionException - If the attribute is not of an acceptable class for the container, or if the name contains a period.
        NameDuplicationException - If the name coincides with an attribute already in the container.
    • Method Detail

      • getUndoInfo

        public static UndoStackAttribute getUndoInfo​(NamedObj object)
        Get the UndoStackAttribute associated with the given object. This is done by searching up the containment hierarchy until such an attribute is found. If no such attribute is found, then create and attach a new one to the top level. This method gets read access on the workspace associated with the specified object.
        Parameters:
        object - The model for which an undo stack is required (must not be null or a NullPointerException will the thrown).
        Returns:
        The current undo stack attribute if there is one, or a new one.
      • mergeTopTwo

        public void mergeTopTwo()
        Merge the top two undo entries into a single action, unless we are in either a redo or an undo, in which case the merge happens automatically and need not be explicitly requested by the client. If there are fewer than two entries on the stack, do nothing. Note that when two entries are merged, the one on the top of the stack becomes the first one executed and the one below that on the stack becomes the second one executed. This method gets write access on the workspace.
      • push

        public void push​(UndoAction action)
        Push an action to the undo stack, or if we are executing an undo, onto the redo stack. This method gets write access on the workspace.
        Parameters:
        action - The undo action.
      • redo

        public void redo()
                  throws java.lang.Exception
        Remove the top redo action and execute it. If there are no redo entries, do nothing. This method gets write access on the workspace.
        Throws:
        java.lang.Exception - If something goes wrong.
      • undo

        public void undo()
                  throws java.lang.Exception
        Remove the top undo action and execute it. If there are no undo entries, do nothing. This method gets write access on the workspace.
        Throws:
        java.lang.Exception - If something goes wrong.