Package ptolemy.moml

Class UndoContext


  • public class UndoContext
    extends java.lang.Object
    Holds information about the current undo context. It is used while parsing an incremental model change to hold the following information:
    • Whether or not undo MoML should be generated for the current context.
    • Whether or not undo MoML should be generated for any child nodes.
    • The undo MoML to start the undo entry for this context.
    • The undo MoML for any child elements, pushed onto a stack.
    • The closing undo MoML for this context, if any. This is appended after the undo MoML for the child nodes.

    At the end of an element, if the context is undoable, then the undo MoML is generated by taking the main undo MoML StringBuffer, appending the undo MoML for children in the reverse order to the order they were parsed, and finally appending any closing undo MoML that is present.

    Since:
    Ptolemy II 2.1
    Version:
    $Id$
    Author:
    Neil Smyth
    Pt.AcceptedRating:
    Red (cxh)
    Pt.ProposedRating:
    Red (cxh)
    • Constructor Summary

      Constructors 
      Constructor Description
      UndoContext​(boolean undoableContext)
      Create a new UndoContext which may or may not need undo MoML generated.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void appendClosingUndoMoML​(java.lang.String undoMoML)
      Append some MoML to be appended after the undo MoML for child nodes.
      void appendUndoMoML​(java.lang.String undoMoML)
      Append some MoML to the current buffer.
      void applyRename​(java.lang.String newName)
      Used to handle the "rename" element.
      java.lang.String generateUndoEntry()
      Generate the undo entry by processing children entries and the closing undo MoML.
      java.lang.String getUndoMoML()
      Get the undo MoML for this element as it currently stands.
      boolean hasUndoableChildren()
      Return whether or not child nodes need to generate MoML.
      boolean hasUndoMoML()
      Return whether or not this context has any undo MoML to be processed.
      boolean isUndoable()
      Tells if the current context is undoable or not.
      static java.lang.String moveContextEnd​(NamedObj context, NamedObj containee)
      Return the closing element corresponding to the starting element returned by moveContextStart(), or an empty string if none is needed.
      static java.lang.String moveContextStart​(NamedObj context, NamedObj containee)
      Return the MoML start element to put us in the context of the immediate container of the containee, assuming the current context is as given by the context argument.
      void pushUndoEntry​(java.lang.String entry)
      Push the passed in MoML onto the stack of element undo entries.
      void setChildrenUndoable​(boolean isUndoable)
      Set whether or not the child contexts are undoable.
      void setUndoable​(boolean isUndoable)
      Set whether or not the current context is undoable.
      java.lang.String toString()
      Return a string representation of this object.
      • Methods inherited from class java.lang.Object

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

      • UndoContext

        public UndoContext​(boolean undoableContext)
        Create a new UndoContext which may or may not need undo MoML generated.
        Parameters:
        undoableContext - Whether or not undo MoML is required for this context
    • Method Detail

      • appendClosingUndoMoML

        public void appendClosingUndoMoML​(java.lang.String undoMoML)
        Append some MoML to be appended after the undo MoML for child nodes. Note that these will appear in the reverse order in which this method is called, which makes sense since generally these are nested calls.
        Parameters:
        undoMoML - The MoMl to be appended after any child nodes
      • appendUndoMoML

        public void appendUndoMoML​(java.lang.String undoMoML)
        Append some MoML to the current buffer.
        Parameters:
        undoMoML - The undo MoML to append to the current buffer.
      • applyRename

        public void applyRename​(java.lang.String newName)
                         throws IllegalActionException
        Used to handle the "rename" element. Replace the value of the "name" attribute the given value. This is a bit of a hack as ideally a child context should not modify the parent context, but with rename that is exactly what is required.
        Parameters:
        newName - the value to give to the value of the name attribute.
        Throws:
        IllegalActionException - if there is currently no undo MoML at this level, or if there is no name attribute present.
      • generateUndoEntry

        public java.lang.String generateUndoEntry()
        Generate the undo entry by processing children entries and the closing undo MoML. First appends the contents of the children undo entry stack to this elements undo MoML, followed by any closing MoML. Note that child elements have their undo MoML appended in reverse order to that in which they were parsed.
        Returns:
        the generated undo entry.
      • getUndoMoML

        public java.lang.String getUndoMoML()
        Get the undo MoML for this element as it currently stands. Note that this method does not append the MoML for children or any closing MoML that has been set.
        Returns:
        The UndoMoML value.
      • hasUndoMoML

        public boolean hasUndoMoML()
        Return whether or not this context has any undo MoML to be processed.
        Returns:
        true if this context has any undo MoML to be processed.
      • hasUndoableChildren

        public boolean hasUndoableChildren()
        Return whether or not child nodes need to generate MoML.
        Returns:
        true if this context has undoable children.
      • isUndoable

        public boolean isUndoable()
        Tells if the current context is undoable or not.
        Returns:
        true if this context is undoable.
      • moveContextEnd

        public static java.lang.String moveContextEnd​(NamedObj context,
                                                      NamedObj containee)
        Return the closing element corresponding to the starting element returned by moveContextStart(), or an empty string if none is needed.

        For example, if the containee is not already immediately contained, and the container is an entity, the </entity> is appended to the model.

        Parameters:
        context - The current context.
        containee - The containee whose immediate context we want.
        Returns:
        The MoML that closes the MoML returned by moveContextStart(), or an empty string if none is needed.
        See Also:
        moveContextStart(NamedObj, NamedObj)
      • moveContextStart

        public static java.lang.String moveContextStart​(NamedObj context,
                                                        NamedObj containee)
        Return the MoML start element to put us in the context of the immediate container of the containee, assuming the current context is as given by the context argument. Return an empty string if the specified context is the immediate container of the specified containee.

        For example, if the context has full name ".top" and the containee has full name ".top.a.b.c.d", then MoML to move down the model such as the following is returned: <entity name="a.b.c" >

        Parameters:
        context - The current context.
        containee - The containee whose immediate context we want.
        Returns:
        The MoML to put us in the right context from the current context, or an empty string if we are already in that context or if either argument is null.
        See Also:
        moveContextEnd(NamedObj, NamedObj)
      • pushUndoEntry

        public void pushUndoEntry​(java.lang.String entry)
        Push the passed in MoML onto the stack of element undo entries. Note that undo entries are pushed onto a stack so that at the end of the element they can be aggregated to form the full undo MoML - in the reverse order to that in which the elements originally appeared.
        Parameters:
        entry - Description of Parameter
      • setChildrenUndoable

        public void setChildrenUndoable​(boolean isUndoable)
        Set whether or not the child contexts are undoable.
        Parameters:
        isUndoable - the new state
      • setUndoable

        public void setUndoable​(boolean isUndoable)
        Set whether or not the current context is undoable.
        Parameters:
        isUndoable - the new state
      • toString

        public java.lang.String toString()
        Return a string representation of this object.
        Overrides:
        toString in class java.lang.Object