y.view
Class Graph2DUndoManager

java.lang.Object
  |
  +--y.view.Graph2DUndoManager
All Implemented Interfaces:
EventListener, Graph2D.BackupRealizersHandler, GraphListener
Direct Known Subclasses:
Graph2DHierarchyUndoManager

public class Graph2DUndoManager
extends Object
implements GraphListener, Graph2D.BackupRealizersHandler

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.

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.


Constructor Summary
Graph2DUndoManager()
          Creates a new Instance of UndoManager.
 
Method Summary
 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.
 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.
 Action getRedoAction()
          Returns a swing action that can be used to trigger a redo operation.
 Action getUndoAction()
          Returns a swing action that can be used to trigger an undo operation.
 ViewContainer getViewContainer()
          Returns the view container this undomanager is associated with.
 boolean isActive()
          Getter for property active.
 boolean isActiveToken(Object token)
          Determines, whether the stream is currently at the same position, the time the token was obtained using Graph2DUndoManager.getActiveToken()
 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 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.
 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 setViewContainer(ViewContainer view)
          Sets the view container this undomanager 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()
Creates a new Instance of UndoManager.

Method Detail

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 Object getActiveToken()
Obtains a token which determines the currently active position in the stream. Graph2DUndoManager.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(Object token)
Determines, whether the stream is currently at the same position, the time the token was obtained using Graph2DUndoManager.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 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:
Graph2DUndoManager.setViewContainer(ViewContainer)

getRedoAction

public 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:
Graph2DUndoManager.setViewContainer(ViewContainer)

setViewContainer

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


getViewContainer

public ViewContainer getViewContainer()
Returns the view container this undomanager 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 reactviation.

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 reactviation.

Specified by:
backupRealizers in interface Graph2D.BackupRealizersHandler

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

2003