Search this API

y.view
Class Graph2DUndoManager

java.lang.Object
  extended by y.view.Graph2DUndoManager
All Implemented Interfaces:
java.util.EventListener, GraphListener, Graph2D.BackupRealizersHandler, HierarchyListener

public class Graph2DUndoManager
extends java.lang.Object
implements GraphListener, Graph2D.BackupRealizersHandler, HierarchyListener

This class provides undoability and redoability support for Graph2D changes. It must be registered as a GraphListener and Graph2D.BackupRealizersHandler to make this class aware of Graph2D changes. The backup mechanism of realizers depends on a correct implementation of their createCopy method.

Additionally this class acts as HierarchyListener and must therefore be registered with HierarchyManager if one is being used.

Note that using the non-empty constructor will automatically register the listeners for you on the provided instance (both on the Graph2D and on its HierarchyManager if one is associated with it).

GraphEvents of type GraphEvent.PRE_EVENT and GraphEvent.POST_EVENT serve as hints for the undo manager to decide which commands to group into a single command that can be undone or redone. To group a sequence of graph changes in one undoable command use the following code:

   Graph2D graph = ...
   graph.firePreEvent();
   //block of graph changes follows
   graph.firePostEvent;
 

changes to the realizers of the nodes and edges are handled by backing up realizers that are about to change. Use the Graph2D methods Graph2D.backupRealizers(NodeCursor) and Graph2D.backupRealizers(EdgeCursor) in your code whenever you want to be able to revert to the former state of the realizers at a later time.

 

Nested Class Summary
static interface Graph2DUndoManager.UndoListener
          Implementations of this interface can register with the UndoManager to receive notification whenever a new command is added to the queue of undoable events.
 
Constructor Summary
Graph2DUndoManager()
          Creates a new Instance of UndoManager.
Graph2DUndoManager(Graph2D graph)
          Creates a new instance of this class, that conveniently registers the necessary listeners on the provided graph instance.
 
Method Summary
 void addPropertyChangeListener(java.beans.PropertyChangeListener l)
          Adds a PropertyChangeListener to the listener list.
 void addUndoListener(Graph2DUndoManager.UndoListener listener)
          Registers the given UndoListener with the UndoManager.
 void backupRealizers(Graph2D graph, EdgeCursor ec)
          Graph2D.BackupRealizersHandler implementation.
 void backupRealizers(Graph2D graph, NodeCursor nc)
          Graph2D.BackupRealizersHandler implementation.
 boolean canRedo()
          Returns whether or not there is a command that can be redone.
 boolean canUndo()
          Returns whether or not there is a command that can be undone.
protected  void commandAdded(Command com)
          Calling this method will inform all registered UndoListeners that the given command has been added to the command queue.
 void dispose()
          Disposes this Graph2DUndoManager instance.
 Graph getActiveGraph()
          Getter for property activeGraph.
 java.lang.Object getActiveToken()
          Obtains a token which determines the currently active position in the stream.
 int getMaximumUndoDepth()
          Returns the maximum number of commands stored on the undo stream.
 javax.swing.Action getRedoAction()
          Returns a swing action that can be used to trigger a redo operation.
 javax.swing.Action getUndoAction()
          Returns a swing action that can be used to trigger an undo operation.
 ViewContainer getViewContainer()
          Returns the view container this undo manager is associated with.
 void hierarchyChange(HierarchyEvent ev)
          HierarchyListener implementation.
 boolean isActive()
          Getter for property active.
 boolean isActiveToken(java.lang.Object token)
          Determines, whether the stream is currently at the same position, the time the token was obtained using getActiveToken()
 boolean isSelectionStateAdjusting()
          Determines whether this instance adjust the selection state of the realizers to match that of the currently active realizers upon realizer exchange.
 void onGraphEvent(GraphEvent e)
          Graph2DListener implementation.Structural changes of the graph will be converted to undoable commands and stored in a command stream for later execution.
 void push(Command com)
          This method can be used to push undoable commands on the stream.
 void redo()
          Causes the next command in line to be redone.
 void removePropertyChangeListener(java.beans.PropertyChangeListener l)
          Removes a PropertyChangeListener from the listener list.
 void removeUndoListener(Graph2DUndoManager.UndoListener listener)
          Removed the given UndoListener from the UndoManager.
 void resetQueue()
          Clears the current undo queue, i.e. canUndo() and canRedo() will return false if invoked directly after this call.
protected  void sanatize()
          Performs the suitable number of close bracket operations, to ensure undo() and redo() will work as expected.
 void setActiveGraph(Graph activeGraph)
          Setter for property activeGraph.
 boolean setExchangeRealizersOnBackup()
          Returns the exchangeRealizerOnBackup policy used.
 void setExchangeRealizersOnBackup(boolean exchange)
          Controls whether or not an exchange of the realizers associated with a node or edge is desired when backing them up.
 void setMaximumUndoDepth(int depth)
          Sets the maximum number of commands stored on the undo stream.
 void setSelectionStateAdjusting(boolean selectionStateAdjusting)
          Sets whether this instance should adjust the selection state of the realizers to match that of the currently active realizers upon realizer exchange.
 void setViewContainer(ViewContainer view)
          Sets the view container this undo manager is associated with.
 void undo()
          Causes the next command in line to be undone.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Graph2DUndoManager

public Graph2DUndoManager(Graph2D graph)
Creates a new instance of this class, that conveniently registers the necessary listeners on the provided graph instance. Note that this means that custom code should not register this instance as a graph listener and backup realizers handler if the graph has already been passed to this instance via this constructor. Also if at the time of the invocation, there is a hierarchy manager associated with the provided graph instance, it will be registered with it correctly, so that again this instance should not be add this instance to the HierarchyManager's event listener list.

Parameters:
graph - The graph that should be managed for undoability. This will register this instance as the a listener and backup handler on the graph instance, as well as will try to listen for hierarchy event on the associated hierarchy manager.

Graph2DUndoManager

public Graph2DUndoManager()
Creates a new Instance of UndoManager. If this instance should be used with Graph2D instances, it needs to be registered as a listener with the corresponding graph events.

Method Detail

dispose

public void dispose()
Disposes this Graph2DUndoManager instance. To free resources, this method should be called after a Graph2DUndoManager instance is not needed anymore.

If this Graph2DUndoManager was automatically registered as a GraphListener, HierarchyListener, and BackupRealizersHandler by using the Graph2DUndoManager(Graph2D) constructor, invoking this method will also deregister this manager from the corresponding graph and hierarchy manager.

Note: It is not safe to invoke any methods on a disposed Graph2DUndoManager instance. Doing so may result in NullPointerException being thrown.


isSelectionStateAdjusting

public boolean isSelectionStateAdjusting()
Determines whether this instance adjust the selection state of the realizers to match that of the currently active realizers upon realizer exchange.

Returns:
whether the state is adjusted to match. The default is true.

setSelectionStateAdjusting

public void setSelectionStateAdjusting(boolean selectionStateAdjusting)
Sets whether this instance should adjust the selection state of the realizers to match that of the currently active realizers upon realizer exchange.


setExchangeRealizersOnBackup

public void setExchangeRealizersOnBackup(boolean exchange)
Controls whether or not an exchange of the realizers associated with a node or edge is desired when backing them up. By default this option is set to false.


setExchangeRealizersOnBackup

public boolean setExchangeRealizersOnBackup()
Returns the exchangeRealizerOnBackup policy used.


canUndo

public boolean canUndo()
Returns whether or not there is a command that can be undone.


canRedo

public boolean canRedo()
Returns whether or not there is a command that can be redone.


push

public void push(Command com)
This method can be used to push undoable commands on the stream.


setMaximumUndoDepth

public void setMaximumUndoDepth(int depth)
Sets the maximum number of commands stored on the undo stream. By default this value is set to 40.


resetQueue

public void resetQueue()
Clears the current undo queue, i.e. canUndo() and canRedo() will return false if invoked directly after this call.


getMaximumUndoDepth

public int getMaximumUndoDepth()
Returns the maximum number of commands stored on the undo stream.


undo

public void undo()
Causes the next command in line to be undone.


redo

public void redo()
Causes the next command in line to be redone.


sanatize

protected void sanatize()
Performs the suitable number of close bracket operations, to ensure undo() and redo() will work as expected. This should have an effect if an action crashed and close bracket operations were skipped.


getActiveToken

public java.lang.Object getActiveToken()
Obtains a token which determines the currently active position in the stream. isActiveToken(Object) can be used to determine, whether the current stream position equals the position at the time to token was obtained

Returns:
a token

isActiveToken

public boolean isActiveToken(java.lang.Object token)
Determines, whether the stream is currently at the same position, the time the token was obtained using getActiveToken()

Parameters:
token - the token
Returns:
true if the stream is at the same position

isActive

public boolean isActive()
Getter for property active. This value indicates whether a current undo or redo is in progress, i.e. whether incoming events can and should be ignored.

Returns:
Value of property active.

onGraphEvent

public void onGraphEvent(GraphEvent e)
Graph2DListener implementation.Structural changes of the graph will be converted to undoable commands and stored in a command stream for later execution.

Specified by:
onGraphEvent in interface GraphListener

getUndoAction

public javax.swing.Action getUndoAction()
Returns a swing action that can be used to trigger an undo operation. The action will only be enabled if a command to be undone is available. This action will only update the current view if a valid ViewContainer has been set.

See Also:
setViewContainer(ViewContainer)

getRedoAction

public javax.swing.Action getRedoAction()
Returns a swing action that can be used to trigger a redo operation. The action will only be enabled if a command to be redone is available. This action will only update the current view if a valid ViewContainer has been set.

See Also:
setViewContainer(ViewContainer)

hierarchyChange

public void hierarchyChange(HierarchyEvent ev)
HierarchyListener implementation. This class keeps track of hierarchy changes.

Specified by:
hierarchyChange in interface HierarchyListener

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener l)
Adds a PropertyChangeListener to the listener list. Changes of the "activeGraph" property will be propagated.


removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener l)
Removes a PropertyChangeListener from the listener list.


getActiveGraph

public Graph getActiveGraph()
Getter for property activeGraph.


setActiveGraph

public void setActiveGraph(Graph activeGraph)
Setter for property activeGraph.


addUndoListener

public void addUndoListener(Graph2DUndoManager.UndoListener listener)
Registers the given UndoListener with the UndoManager.


removeUndoListener

public void removeUndoListener(Graph2DUndoManager.UndoListener listener)
Removed the given UndoListener from the UndoManager.


commandAdded

protected void commandAdded(Command com)
Calling this method will inform all registered UndoListeners that the given command has been added to the command queue.


setViewContainer

public void setViewContainer(ViewContainer view)
Sets the view container this undo manager is associated with.


getViewContainer

public ViewContainer getViewContainer()
Returns the view container this undo manager is associated with.


backupRealizers

public void backupRealizers(Graph2D graph,
                            NodeCursor nc)
Graph2D.BackupRealizersHandler implementation. This method creates copies of the given realizer and stores them in undoable commands for later reactivation.

Specified by:
backupRealizers in interface Graph2D.BackupRealizersHandler

backupRealizers

public void backupRealizers(Graph2D graph,
                            EdgeCursor ec)
Graph2D.BackupRealizersHandler implementation. This method creates copies of the given realizer and stores them in undoable commands for later reactivation.

Specified by:
backupRealizers in interface Graph2D.BackupRealizersHandler

© Copyright 2000-2022,
yWorks GmbH.
All rights reserved.