edu.rice.cs.drjava.model.definitions
Class CompoundUndoManager

java.lang.Object
  extended by javax.swing.undo.AbstractUndoableEdit
      extended by javax.swing.undo.CompoundEdit
          extended by javax.swing.undo.UndoManager
              extended by edu.rice.cs.drjava.model.definitions.CompoundUndoManager
All Implemented Interfaces:
Serializable, EventListener, UndoableEditListener, UndoableEdit

public class CompoundUndoManager
extends UndoManager

Extended UndoManager with increased functionality. Can handle aggregating multiple edits into one for the purposes of undoing and redoing. It exposes editToBeUndone and editToBeRedone (under new names); they are protected methods in UndoManager. The public methods that involve composite state are synchronized, so this manager can be accessed outside of the event thread. The internal data structures _compoundEdits and _keys are not thread safe but they only accessed only by synchronized methods. The synchronization scheme (locking on this) follows UndoManager.

Version:
$Id: CompoundUndoManager.java 5361 2010-08-13 22:39:45Z mgricken $
See Also:
Serialized Form

Field Summary
private  LinkedList<CompoundEdit> _compoundEdits
          The compound edits we are storing.
private  LinkedList<Integer> _keys
          The keys for the CompoundEdits we are storing.
private  int _nextKey
          The next key to use for nested CompoundEdits.
private  GlobalEventNotifier _notifier
          Keeps track of the listeners to this undo manager.
private  UndoableEdit _savePoint
          The last edit that was performed before the last save.
private static int counter
           
private  int id
           
(package private) static Log LOG
           
 
Fields inherited from class javax.swing.undo.CompoundEdit
edits
 
Fields inherited from class javax.swing.undo.AbstractUndoableEdit
RedoName, UndoName
 
Constructor Summary
CompoundUndoManager(GlobalEventNotifier notifier)
          Standard constructor.
 
Method Summary
 boolean _compoundEditInProgress()
          Returns whether or not a compound edit is in progress.
private  void _notifyUndoHappened()
          Helper method to notify the view that an undoable edit has occured.
 boolean addEdit(UndoableEdit e)
          Adds an edit.
 boolean canUndo()
          Returns true when a compound edit is in progress, or when there are valid stored undoable edits
 void documentSaved()
          Informs this undo manager that the document has been saved.
private  void endCompoundEdit()
          Ends the compoundEdit in progress if any.
 void endCompoundEdit(int key)
          Ends a compound edit.
 void endLastCompoundEdit()
          Ends the last compound edit that was created.
 CompoundEdit getLastCompoundEdit()
          Gets the last Compound Edit entered into the list.
 UndoableEdit getNextRedo()
          Gets the next redo.
 UndoableEdit getNextUndo()
          Gets the next undo.
 String getUndoPresentationName()
          Returns the presentation name for this undo, or delegates to super if none is available
 boolean isModified()
          Determines if the document is in the same undo state as it was when it was last saved.
 void redo()
          Overrides redo so that any compound edit in progress is ended before the redo is performed.
 int startCompoundEdit()
          Starts a compound edit.
 String toString()
           
 void undo()
          Undoes the last undoable edit, or compound edit created by the user.
 
Methods inherited from class javax.swing.undo.UndoManager
canRedo, canUndoOrRedo, discardAllEdits, editToBeRedone, editToBeUndone, end, getLimit, getRedoPresentationName, getUndoOrRedoPresentationName, redoTo, setLimit, trimEdits, trimForLimit, undoableEditHappened, undoOrRedo, undoTo
 
Methods inherited from class javax.swing.undo.CompoundEdit
die, getPresentationName, isInProgress, isSignificant, lastEdit
 
Methods inherited from class javax.swing.undo.AbstractUndoableEdit
replaceEdit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOG

static Log LOG

counter

private static volatile int counter

id

private final int id

_compoundEdits

private final LinkedList<CompoundEdit> _compoundEdits
The compound edits we are storing. Not thread safe!


_keys

private final LinkedList<Integer> _keys
The keys for the CompoundEdits we are storing.


_nextKey

private volatile int _nextKey
The next key to use for nested CompoundEdits.


_savePoint

private volatile UndoableEdit _savePoint
The last edit that was performed before the last save.


_notifier

private final GlobalEventNotifier _notifier
Keeps track of the listeners to this undo manager.

Constructor Detail

CompoundUndoManager

public CompoundUndoManager(GlobalEventNotifier notifier)
Standard constructor.

Method Detail

startCompoundEdit

public int startCompoundEdit()
Starts a compound edit.

Returns:
the key for the compound edit

endLastCompoundEdit

public void endLastCompoundEdit()
Ends the last compound edit that was created. Used when a compound edit is created by the _undoListener in DefinitionsPane and the key is not known in DefinitionsDocument.


endCompoundEdit

public void endCompoundEdit(int key)
Ends a compound edit.

Parameters:
key - the key that was returned by startCompoundEdit()

getLastCompoundEdit

public CompoundEdit getLastCompoundEdit()
Gets the last Compound Edit entered into the list. Used in making a Compound edit for granular undo.


getNextUndo

public UndoableEdit getNextUndo()
Gets the next undo.

Returns:
the next undo

getNextRedo

public UndoableEdit getNextRedo()
Gets the next redo.

Returns:
the next redo

addEdit

public boolean addEdit(UndoableEdit e)
Adds an edit. Checks whether or not the current edit is a compound edit.

Specified by:
addEdit in interface UndoableEdit
Overrides:
addEdit in class UndoManager
Parameters:
e - the edit to be added
Returns:
true if the add is successful, false otherwise

_compoundEditInProgress

public boolean _compoundEditInProgress()
Returns whether or not a compound edit is in progress.

Returns:
true iff in progress

canUndo

public boolean canUndo()
Returns true when a compound edit is in progress, or when there are valid stored undoable edits

Specified by:
canUndo in interface UndoableEdit
Overrides:
canUndo in class UndoManager
Returns:
true iff undoing is possible

getUndoPresentationName

public String getUndoPresentationName()
Returns the presentation name for this undo, or delegates to super if none is available

Specified by:
getUndoPresentationName in interface UndoableEdit
Overrides:
getUndoPresentationName in class UndoManager
Returns:
the undo's presentation name

undo

public void undo()
Undoes the last undoable edit, or compound edit created by the user.

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

redo

public void redo()
Overrides redo so that any compound edit in progress is ended before the redo is performed.

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

_notifyUndoHappened

private void _notifyUndoHappened()
Helper method to notify the view that an undoable edit has occured. Note that lock on this is not held by the event thread (even if called from event thread) when notification happens.


endCompoundEdit

private void endCompoundEdit()
Ends the compoundEdit in progress if any. Used by undo(), redo(), documentSaved().


documentSaved

public void documentSaved()
Informs this undo manager that the document has been saved.


isModified

public boolean isModified()
Determines if the document is in the same undo state as it was when it was last saved.

Returns:
true iff all changes have been undone since the last save

toString

public String toString()
Overrides:
toString in class UndoManager