dsto.dfc.swing.undo
Class MonitoredUndoManager

java.lang.Object
  extended by dsto.dfc.util.BasicPropertyEventSource
      extended by dsto.dfc.swing.undo.MonitoredUndoManager
All Implemented Interfaces:
Copyable, JavaBean, PropertyEventSource, Serializable, Cloneable, EventListener, UndoableEditListener

public class MonitoredUndoManager
extends BasicPropertyEventSource
implements UndoableEditListener

Provides the same functions as the Swing UndoManager, plus provides functionality to skip non-undoable edits in the middle of the history list. Note that the non-undoable edit and subsequent edits are removed if they are skipped because they are rendered invalid.

Version:
$Revision$
See Also:
Serialized Form

Field Summary
static int DEFAULT_LIMIT
          The default limit placed on the number of undo's.
protected  EditListModel edits
          The list of edits.
protected  int indexOfNextAdd
          The index at which to place the next edit added to the manager.
protected  int limit
          The maximum number of edits stored in the undo list.
static UndoableEdit NULL_EDIT
          Null object pattern - the cursors will point to valid edits or NULL_EDIT.
 UndoableEdit redoCursor
          The next edit to be redone in the edit list.
 UndoableEdit undoCursor
          The last edit which can be undone in the edit list.
protected  Object undoRedoSemaphore
          Thread lock.
 
Constructor Summary
MonitoredUndoManager()
           
 
Method Summary
 void addEdit(UndoableEdit edit)
          Add an edit to list at position of undoCursor, and advance cursor.
 void broadcastEditEventsTo(UndoableEditListener newEditEventRecorder)
          By invoking this method, the editEventRecorder will 'hear' every UndoableEdit that the MonitoredUndoManager 'hears'.
 boolean canRedo()
          True if the manager can execute a redo operation.
 boolean canUndo()
          True if the manager can execute an undo operation.
 boolean containsAllEdits(List someEdits)
          Returns true if all edits in someEdits are known to the MonitoredUndoManager and false otherwise.
 boolean containsEdit(UndoableEdit edit)
          Returns true if edit is known to the MonitoredUndoManager.
 void discardAllEdits()
          Removes all edits from the undo stack and calls UndoableEdit.die() for each edit.
protected  void dropEdit(int index)
          Drops the edit at index and handles moving of the cursors.
 void dropEdit(UndoableEdit unwantedEdit)
          Drops the unwantedEdit and handles the moving of cursors.
 void dropEdits(Collection unwantedEdits)
          Drops the collection of unwantedEdits and handles updating of cursors.
protected  void dropEdits(int start, int end)
          Drop all edits between two indices.
 void dropFirstEdit()
          Drops the first edit in the edit list and handles the moving of cursors.
protected  boolean editQueueIsFull()
          Internal test to determine if edits is full and edits need to be dropped before more can be added.
 int getEditCount()
          The number of edits on the undo stack.
 UndoableEdit getEditOrNullEdit(int i)
          Get an edit for a given index into the undo stack.
 EditListModel getEdits()
          Returns the list of edits maintained by the MonitoredUndoManager.
 int getLimit()
          The maximum number of edits that will be kept on the stack.
static MonitoredUndoManager getOrCreateUndoManager(Component c)
          Get or create a monitored undomanager for the window containing a given component.
 String getPresentationName()
          Get the name of the next edit to be undone using its getPresentationName () method.
 UndoableEdit getRedoCursor()
          Returns the next edit in line to be redone.
 String getRedoPresentationName()
          Get the human readable name for the next edit that will be redone by redo ().
 UndoableEdit getUndoCursor()
          Returns the undo cursor which points to the last edit not undone.
 String getUndoPresentationName()
          Get the human readable name for the next edit that will be undone by undo().
protected  void handleMoveEdits(int insertionIndex, List droppedEdits)
          For drop'n'drag support of edits.
protected  void handleSkipUndoRamifications()
          Drops all edits beyond the undo cursor when an undo is skipped.
 int indexOfEdit(UndoableEdit edit)
          Returns the index of edit if it's known, and -1 if it isn't.
 int indexOfRedoCursor()
          Returns the index of the redo cursor.
 int indexOfUndoCursor()
          Returns the index of the undo cursor.
 boolean inRedoList(UndoableEdit edit)
          Returns true if edit is in the redo list and false otherwise.
 boolean insertEdit(int index, UndoableEdit newEdit)
          Insert newEdit at index.
 void insertEdits(int index, Collection newEdits)
          Inserts the edits in newEdits at index.
 boolean inUndoList(UndoableEdit edit)
          Returns true if edit is in the undo list and false otherwise.
 int lastIndex()
          Returns the index of the last edit in the list.
 boolean missingFromRedoList(UndoableEdit edit)
          Returns true if edit is not in the redo list.
 boolean missingFromUndoList(UndoableEdit edit)
          Returns true if edit is not in the undo list.
protected  void moveCursorsBackward()
          Moves the cursors one step backwards.
protected  void moveCursorsForward()
          Moves the cursors one step forward.
 void redo()
          Redo the next available redoable edit.
 boolean redoCursorIsNull()
          Returns true if the redo cursor is the NULL_EDIT.
 void redoTo(UndoableEdit endEdit)
          Redo the edits up to and including endEdit.
 void setLimit(int newLimit)
          Sets the maximum number of edits kept on the stack.
protected  void setRedoCursor(UndoableEdit newCursor)
           
protected  void setUndoCursor(UndoableEdit newCursor)
          Sets the current cursor to newEdit which has not been undone.
 void skipRedo()
          Skip the current redoable edit at move the cursors forward one step.
 void skipUndo()
          Skips a non-undoable edit, moving the undo cursor to the previous edit and removing the non-undoable edit and the invalid edits after it.
 void stopBroadcastingEditEvents()
          Deregisters the currently registered editEventRecorder.
 void undo()
          Undoes the edit at the undo cursor and shifts the cursor backwards one step.
 void undoableEditHappened(UndoableEditEvent e)
          UndoableEditListener interface implementation.
 boolean undoCursorIsNull()
          Returns true if the undo cursor is the NULL_EDIT.
 void undoTo(UndoableEdit endEdit)
          Undoes the edits up to and including endEdit.
 
Methods inherited from class dsto.dfc.util.BasicPropertyEventSource
addPropertyChangeListener, clone, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getPropertyChangeSource, removePropertyChangeListener, setPropertyChangeSource
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LIMIT

public static final int DEFAULT_LIMIT
The default limit placed on the number of undo's.

See Also:
Constant Field Values

NULL_EDIT

public static final UndoableEdit NULL_EDIT
Null object pattern - the cursors will point to valid edits or NULL_EDIT.


limit

protected int limit
The maximum number of edits stored in the undo list.


edits

protected EditListModel edits
The list of edits.


indexOfNextAdd

protected int indexOfNextAdd
The index at which to place the next edit added to the manager.


undoCursor

public UndoableEdit undoCursor
The last edit which can be undone in the edit list.


redoCursor

public UndoableEdit redoCursor
The next edit to be redone in the edit list.


undoRedoSemaphore

protected Object undoRedoSemaphore
Thread lock.

Constructor Detail

MonitoredUndoManager

public MonitoredUndoManager()
Method Detail

getOrCreateUndoManager

public static MonitoredUndoManager getOrCreateUndoManager(Component c)
Get or create a monitored undomanager for the window containing a given component.


getLimit

public int getLimit()
The maximum number of edits that will be kept on the stack.

Returns:
The maximum number of edits.

setLimit

public void setLimit(int newLimit)
Sets the maximum number of edits kept on the stack. Any existing edits are discarded.

Parameters:
newLimit - The new maximum edit limit.

getEdits

public EditListModel getEdits()
Returns the list of edits maintained by the MonitoredUndoManager.


discardAllEdits

public void discardAllEdits()
Removes all edits from the undo stack and calls UndoableEdit.die() for each edit.


canUndo

public boolean canUndo()
True if the manager can execute an undo operation.


canRedo

public boolean canRedo()
True if the manager can execute a redo operation.


getUndoPresentationName

public String getUndoPresentationName()
Get the human readable name for the next edit that will be undone by undo().

Returns:
The human readable name for the next edit to be undone, or "Undo" if no edit can currently be undone.
See Also:
canUndo()

getRedoPresentationName

public String getRedoPresentationName()
Get the human readable name for the next edit that will be redone by redo ().

Returns:
The human readable name for the next edit to be redone, or "Redo" if no edit can currently be redone.
See Also:
canRedo()

getPresentationName

public String getPresentationName()
Get the name of the next edit to be undone using its getPresentationName () method.

Returns:
The result of calling getPresentationName () on the next undo edit or "" if no edit can currently be undone.

getEditOrNullEdit

public UndoableEdit getEditOrNullEdit(int i)
Get an edit for a given index into the undo stack.

Parameters:
i - The index of the edit.
Returns:
The edit at index i or the NULL_EDIT if i is outside the range of the undo stack.
See Also:
getEditCount()

getEditCount

public int getEditCount()
The number of edits on the undo stack.

Returns:
The number of edits.

setUndoCursor

protected void setUndoCursor(UndoableEdit newCursor)
Sets the current cursor to newEdit which has not been undone.


getUndoCursor

public UndoableEdit getUndoCursor()
Returns the undo cursor which points to the last edit not undone. If you call redo () on it, it will barf because it hasn't been undone. You may call undo () on it happily, at which time the cursor will shift to the previous edit.

Returns:
The edit which will be undone next.

setRedoCursor

protected void setRedoCursor(UndoableEdit newCursor)

getRedoCursor

public UndoableEdit getRedoCursor()
Returns the next edit in line to be redone.


containsEdit

public boolean containsEdit(UndoableEdit edit)
Returns true if edit is known to the MonitoredUndoManager.


containsAllEdits

public boolean containsAllEdits(List someEdits)
Returns true if all edits in someEdits are known to the MonitoredUndoManager and false otherwise.


lastIndex

public int lastIndex()
Returns the index of the last edit in the list.


editQueueIsFull

protected boolean editQueueIsFull()
Internal test to determine if edits is full and edits need to be dropped before more can be added.


addEdit

public void addEdit(UndoableEdit edit)
Add an edit to list at position of undoCursor, and advance cursor. Will cause all redoable edits to be discarded. The undoRedoSemaphore ensures that no edits will be added while the undoManager is in the middle of an undo or redo operation.

Parameters:
edit - The edit to be added to the stack.

insertEdit

public boolean insertEdit(int index,
                          UndoableEdit newEdit)
Insert newEdit at index. Handles moving of cursors.


insertEdits

public void insertEdits(int index,
                        Collection newEdits)
Inserts the edits in newEdits at index. It handles moving the cursors appropriately.


moveCursorsForward

protected void moveCursorsForward()
Moves the cursors one step forward.


moveCursorsBackward

protected void moveCursorsBackward()
Moves the cursors one step backwards.


redo

public void redo()
          throws CannotRedoException
Redo the next available redoable edit.

Throws:
CannotRedoException - if there are no redoable edits.

redoTo

public void redoTo(UndoableEdit endEdit)
            throws CannotRedoException
Redo the edits up to and including endEdit.

Throws:
CannotRedoException

undo

public void undo()
          throws CannotUndoException
Undoes the edit at the undo cursor and shifts the cursor backwards one step.

Throws:
CannotUndoException

undoTo

public void undoTo(UndoableEdit endEdit)
            throws CannotUndoException
Undoes the edits up to and including endEdit.

Throws:
CannotUndoException

skipUndo

public void skipUndo()
Skips a non-undoable edit, moving the undo cursor to the previous edit and removing the non-undoable edit and the invalid edits after it.


handleSkipUndoRamifications

protected void handleSkipUndoRamifications()
Drops all edits beyond the undo cursor when an undo is skipped. This is because the edits after the undo cursor are regarded as unstable.


skipRedo

public void skipRedo()
Skip the current redoable edit at move the cursors forward one step.


inUndoList

public boolean inUndoList(UndoableEdit edit)
Returns true if edit is in the undo list and false otherwise.


missingFromUndoList

public boolean missingFromUndoList(UndoableEdit edit)
Returns true if edit is not in the undo list. Convenience method.


inRedoList

public boolean inRedoList(UndoableEdit edit)
Returns true if edit is in the redo list and false otherwise.


missingFromRedoList

public boolean missingFromRedoList(UndoableEdit edit)
Returns true if edit is not in the redo list. Convenience method.


indexOfEdit

public int indexOfEdit(UndoableEdit edit)
Returns the index of edit if it's known, and -1 if it isn't.


indexOfUndoCursor

public int indexOfUndoCursor()
Returns the index of the undo cursor. If it's -1, then the undo list is empty.


indexOfRedoCursor

public int indexOfRedoCursor()
Returns the index of the redo cursor. If it's -1, then the redo list is empty.


undoableEditHappened

public void undoableEditHappened(UndoableEditEvent e)
UndoableEditListener interface implementation. Calls addEdit(javax.swing.undo.UndoableEdit) to add the edit to the stack.

Specified by:
undoableEditHappened in interface UndoableEditListener

dropFirstEdit

public void dropFirstEdit()
Drops the first edit in the edit list and handles the moving of cursors.


dropEdit

protected void dropEdit(int index)
Drops the edit at index and handles moving of the cursors.


dropEdits

protected void dropEdits(int start,
                         int end)
Drop all edits between two indices. Does not adjust index and assumes valid indices.

Parameters:
start - The first index.
end - The second index. start <= end or this method does nothing.

dropEdit

public void dropEdit(UndoableEdit unwantedEdit)
Drops the unwantedEdit and handles the moving of cursors.


dropEdits

public void dropEdits(Collection unwantedEdits)
Drops the collection of unwantedEdits and handles updating of cursors.


broadcastEditEventsTo

public void broadcastEditEventsTo(UndoableEditListener newEditEventRecorder)
By invoking this method, the editEventRecorder will 'hear' every UndoableEdit that the MonitoredUndoManager 'hears'. Only one editEventRecorder can be registered with the MonitoredUndoManager at any one time.


stopBroadcastingEditEvents

public void stopBroadcastingEditEvents()
Deregisters the currently registered editEventRecorder.


undoCursorIsNull

public boolean undoCursorIsNull()
Returns true if the undo cursor is the NULL_EDIT.


redoCursorIsNull

public boolean redoCursorIsNull()
Returns true if the redo cursor is the NULL_EDIT.


handleMoveEdits

protected void handleMoveEdits(int insertionIndex,
                               List droppedEdits)
For drop'n'drag support of edits. This implementation does not support the movement of edits within its list.



Copyright © 2008 Commonwealth of Australia