Class Undo

java.lang.Object
org.opentrafficsim.editor.Undo
All Implemented Interfaces:
EventListener, EventListener

public class Undo extends Object implements EventListener
Undo unit for the OTS editor. This class stores an internal queue of actions. Changes to XsdTreeNodes should be grouped per single user input in an action. All actions need to be initiated externally using startAction(). This class will itself listen to all relevant changes in the tree and add incoming sub-actions under the started action.

Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Author:
Wouter Schakel
  • Constructor Details

  • Method Details

    • clear

      public void clear()
      Clears the entire queue, suitable for when a new tree is loaded. Also sets ignore changes to false.
    • setIgnoreChanges

      public void setIgnoreChanges(boolean ignore)
      Tells the undo unit to ignore all changes. Reset this by calling clear(). Useful during file loading.
      Parameters:
      ignore - ignore changes.
    • startAction

      public void startAction(Undo.ActionType type, XsdTreeNode node, String attribute)
      Starts a new action, which groups all sub-actions until a new action is started. This method can be called without being sure concrete changes will be made. Internal listeners listen to all changes and will combine them in to one undo action, up to the point the next action is started with this method. If no actual changes were made in between, the former start of an action does not result in anything the user can undo or redo. When the user has stepped back a few undo actions, and then makes a new change, rolled back undo steps can no longer be redone. Clearing rolled back undo steps is performed lazily on the first concrete change by a sub-action. Starting a new action does not clear rolled back undo steps by itself.
      Parameters:
      type - action type.
      node - node on which the action is applied, i.e. node that should be selected on undo/redo.
      attribute - attribute name, may be null for actions that are not an attribute value change.
    • canUndo

      public boolean canUndo()
      Returns whether an undo is available.
      Returns:
      whether an undo is available.
    • canRedo

      public boolean canRedo()
      Returns whether a redo is available.
      Returns:
      whether a redo is available.
    • undo

      public void undo()
      Performs an undo.
    • redo

      public void redo()
      Performs a redo.
    • updateButtons

      public void updateButtons()
      Update the enabled state and text of the undo and redo button.
    • notify

      public void notify(Event event)
      Specified by:
      notify in interface EventListener
    • setPostActionShowNode

      public void setPostActionShowNode(XsdTreeNode node)
      Sets the node to show in the tree after the action. This is for example useful to set the selection on the duplicate of a duplicated node when redoing the duplication. Note that the node of the action that is otherwise shown would be the duplicated node, rather than the duplicate.
      Parameters:
      node - node to show in the tree after the action.