com.nwoods.jgo
Class JGoUndoManager

java.lang.Object
  extended bycom.nwoods.jgo.AbstractUndoableEdit
      extended bycom.nwoods.jgo.CompoundEdit
          extended bycom.nwoods.jgo.UndoManager
              extended bycom.nwoods.jgo.JGoUndoManager
All Implemented Interfaces:
java.util.EventListener, JGoDocumentListener, java.io.Serializable, UndoableEdit

public class JGoUndoManager
extends UndoManager
implements JGoDocumentListener

This class is an extension of UndoManager that also keeps track of JGoDocumentEvents and produces JGoDocumentChangedEdits that get added to CompoundEdits that this undo manager manages.

See Also:
JGoDocumentChangedEdit, JGoDocument, JGoDocumentEvent, Serialized Form

Nested Class Summary
 class JGoUndoManager.JGoCompoundEdit
          This CompoundEdit keeps track of the presentation name for this group of JGoDocumentChangedEdits that form a transaction.
 
Field Summary
 java.lang.String[] myViewEditPresentationNames
          An array of user-visible JGoView action presentation names.
 
Fields inherited from class com.nwoods.jgo.CompoundEdit
edits
 
Constructor Summary
JGoUndoManager()
          Construct a JGoUndoManager.
 
Method Summary
 void discardAllEdits()
          This override of UndoManager.discardAllEdits makes sure any ongoing changes are thrown away first.
 void documentChanged(JGoDocumentEvent e)
          Implement the JGoDocumentListener interface.
 void endTransaction(boolean commit)
          Call this method after making a set of changes to a document.
 void endTransaction(java.lang.String pname)
          Call this method after making a set of changes to a document.
 java.util.Vector getAllEdits()
          Return the vector of all CompoundEdits being managed by this UndoManager.
 JGoUndoManager.JGoCompoundEdit getCurrentEdit()
          Return the current CompoundEdit, if any.
 java.util.Vector getCurrentEditVector()
          Return the edits vector of JGoDocumentChangedEdits in the current CompoundEdit.
 int getTransactionLevel()
          Return the number of nested transactions currently in progress.
 java.lang.String getViewEditPresentationName(int act)
          Return a name for each of the different kinds of actions that the default JGoView provides, suitable for an UndoableEdit's presentation name.
 boolean isRedoing()
          This predicate returns true when we are performing an redo.
 boolean isUndoing()
          This predicate returns true when we are performing an undo.
 void redo()
          This override of UndoManager.redo makes sure any ongoing changes are thrown away and then calls super.redo such that isRedoing() is true so that any changes that happen because of the undo operation are ignored by this document listener.
 void setCurrentEdit(JGoUndoManager.JGoCompoundEdit e)
          Set the current CompoundEdit.
 boolean skipEvent(JGoDocumentEvent e)
          This predicate is called by the default document listener implementation to decide whether the event is worth recording for the purposes of undo and redo.
 void startTransaction()
          Call this method before making a set of changes to a document.
 void undo()
          This override of UndoManager.undo makes sure any ongoing changes are bundled up into a transaction and then calls super.undo such that isUndoing() is true so that any changes that happen because of the undo operation are ignored by this document listener.
 
Methods inherited from class com.nwoods.jgo.UndoManager
addEdit, canRedo, canUndo, editToBeRedone, editToBeUndone, end, getLimit, getRedoPresentationName, getUndoPresentationName, redoTo, setLimit, trimEdits, trimForLimit, undoTo
 
Methods inherited from class com.nwoods.jgo.CompoundEdit
die, getPresentationName, isInProgress, lastEdit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

myViewEditPresentationNames

public java.lang.String[] myViewEditPresentationNames
An array of user-visible JGoView action presentation names. If you are using undo/redo and are showing these presentation names, you will want to substitute your own strings when localizing your application.

Constructor Detail

JGoUndoManager

public JGoUndoManager()
Construct a JGoUndoManager.

Method Detail

undo

public void undo()
          throws CannotUndoException
This override of UndoManager.undo makes sure any ongoing changes are bundled up into a transaction and then calls super.undo such that isUndoing() is true so that any changes that happen because of the undo operation are ignored by this document listener.

Specified by:
undo in interface UndoableEdit
Overrides:
undo in class UndoManager
Throws:
CannotUndoException

redo

public void redo()
          throws CannotRedoException
This override of UndoManager.redo makes sure any ongoing changes are thrown away and then calls super.redo such that isRedoing() is true so that any changes that happen because of the undo operation are ignored by this document listener.

Specified by:
redo in interface UndoableEdit
Overrides:
redo in class UndoManager
Throws:
CannotRedoException

discardAllEdits

public void discardAllEdits()
This override of UndoManager.discardAllEdits makes sure any ongoing changes are thrown away first.

Overrides:
discardAllEdits in class UndoManager

documentChanged

public void documentChanged(JGoDocumentEvent e)
Implement the JGoDocumentListener interface. This decides if the document event is interesting to this undo manager by calling the skipEvent predicate. If skipEvent returned false, this creates a JGoDocumentChangedEdit and adds it to the current CompoundEdit for this transaction. It creates a new CompoundEdit if needed.

This method must be a no-op if isUndoing() or isRedoing() is true, because we must ignore any notifications from actually performing an undo or redo.

Specified by:
documentChanged in interface JGoDocumentListener
Parameters:
e - is a document event describing the change

skipEvent

public boolean skipEvent(JGoDocumentEvent e)
This predicate is called by the default document listener implementation to decide whether the event is worth recording for the purposes of undo and redo. Most applications will find that they are really working on an abstract model that encompasses much simpler information than that embodied in the GUI-oriented details of a JGoDocument and its JGoObjects. Thus most applications should use a subclass of JGoUndoManager that overrides skipEvent and only returns true for the events that matter to their model, rather than all events on all objects.

Parameters:
e - an event produced by the document
Returns:
true if the given JGoDocumentEvent is not an interesting change to the document

getTransactionLevel

public int getTransactionLevel()
Return the number of nested transactions currently in progress.

This should be zero between transactions. Calling startTransaction() will increment this value. Calling endTransaction will decrement this value, but not below zero. When the value is greater than zero, canUndo() and canRedo() should be false.


startTransaction

public void startTransaction()
Call this method before making a set of changes to a document. This starts a CompoundEdit. This method is called by JGoView in a few contexts before any changes might happen to the document.

By default this method does nothing except keep track of nested transactions, because the default implementation assumes all detected changes to the document should be bundled into the current CompoundEdit until a call to endTransaction.


endTransaction

public void endTransaction(boolean commit)
Call this method after making a set of changes to a document. This ends a CompoundEdit and adds it as the last UndoableEdit for this UndoManager. This method is called by JGoView in a few contexts after any changes may have happened to the document.

Normally one calls endTransaction(true) to indicate that we're done with a logical set of changes. Call this method with a false value to abort the ongoing changes--this will throw away the current CompoundEdit.

Note that calling endTransaction(false) does not actually undo any or all of the changes that have happened since the last call to startTransaction. Instead, only the record of those changes will be thrown away. This behavior is reasonable for the normal case because JGoView has to restore the "before" state even if no undo/redo functionality is present.

The default implementation permits calling endTransaction without a preceding call to startTransaction. In effect each endTransaction implicitly starts a new transaction after committing any ongoing JGoDocumentChangedEdits.

However, calling startTransaction without calling endTransaction may cause some surprising behavior when using undo and redo. You should be careful to make sure endTransaction is called after every logical user action for each call to startTransaction.

Parameters:
commit - if true, ends the current CompoundEdit and adds it to this UndoManager; if false, throws away any current CompoundEdit

endTransaction

public void endTransaction(java.lang.String pname)
Call this method after making a set of changes to a document. This ends a CompoundEdit and adds it as the last UndoableEdit for this UndoManager. This method is called by JGoView in a few contexts after any changes may have happened to the document.

Unlike endTransaction(boolean), this method assumes the transaction is being committed, and furthermore gives it a presentation name.

Parameters:
pname - the presentation name for the current CompoundEdit being ended

getAllEdits

public java.util.Vector getAllEdits()
Return the vector of all CompoundEdits being managed by this UndoManager.


getCurrentEditVector

public java.util.Vector getCurrentEditVector()
Return the edits vector of JGoDocumentChangedEdits in the current CompoundEdit.


getCurrentEdit

public JGoUndoManager.JGoCompoundEdit getCurrentEdit()
Return the current CompoundEdit, if any. All JGoDocumentChangedEdits will be added to this CompoundEdit for the duration of this transaction.


setCurrentEdit

public void setCurrentEdit(JGoUndoManager.JGoCompoundEdit e)
Set the current CompoundEdit.


isUndoing

public boolean isUndoing()
This predicate returns true when we are performing an undo. This is important for ignoring document events caused by the undo.


isRedoing

public boolean isRedoing()
This predicate returns true when we are performing an redo. This is important for ignoring document events caused by the redo.


getViewEditPresentationName

public java.lang.String getViewEditPresentationName(int act)
Return a name for each of the different kinds of actions that the default JGoView provides, suitable for an UndoableEdit's presentation name. Change the myViewEditPresentationNames array of Strings to substitute your own user-visible presentation names, or override this method. Return null to use a default name.