Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

javax.swing.undo
Class UndoManager  view UndoManager download UndoManager.java

java.lang.Object
  extended byjavax.swing.undo.AbstractUndoableEdit
      extended byjavax.swing.undo.CompoundEdit
          extended byjavax.swing.undo.UndoManager
All Implemented Interfaces:
java.util.EventListener, java.io.Serializable, UndoableEdit, javax.swing.event.UndoableEditListener

public class UndoManager
extends CompoundEdit
implements javax.swing.event.UndoableEditListener

A manager for providing an application’s undo/redo functionality.

Tyipcally, an application will create only one single instance of UndoManager. When the user performs an undoable action, for instance changing the color of an object from green to blue, the application registers an UndoableEdit object with the UndoManager. To implement the “undo” and “redo” menu commands, the application invokes the UndoManager’s undo() 55 and redo() 55 methods. The human-readable text of these menu commands is provided by getUndoPresentationName() 55 and getRedoPresentationName() 55 , respectively. To determine whether the menu item should be selectable or greyed out, use canUndo() 55 and canRedo() 55 .

The UndoManager will only keep a specified number of editing actions, the limit. The value of this parameter can be retrieved by calling getLimit() 55 and set with setLimit(int) 55 . If more UndoableEdits are added to the UndoManager, the oldest actions will be discarded.

Some applications do not provide separate menu commands for “undo” and “redo.” Instead, they have just a single command whose text switches between the two. Such applications would use an UndoManager with a limit of 1. The text of this combined menu item is available via getUndoOrRedoPresentationName() 55 , and it is implemented by calling undoOrRedo() 55 .

Thread Safety: In constrast to the other classes of the javax.swing.undo package, the public methods of an UndoManager are safe to call from concurrent threads. The caller does not need to perform external synchronization, and javax.swing.event.UndoableEditEvent sources do not need to broadcast their events from inside the Swing worker thread.


Field Summary
(package private)  int indexOfNextAdd
          An index into the inherited CompoundEdit.edits 55 Vector that indicates at which position newly added editing actions would get inserted.
(package private)  int limit
          The maximum number of UndoableEdits stored by this UndoManager.
(package private) static long serialVersionUID
          The unique ID for serializing instances of this class.
 
Fields inherited from class javax.swing.undo.CompoundEdit
edits
 
Fields inherited from class javax.swing.undo.AbstractUndoableEdit
RedoName, UndoName
 
Constructor Summary
UndoManager()
          Constructs an UndoManager.
 
Method Summary
 boolean addEdit(UndoableEdit edit)
          Registers an undoable editing action with this UndoManager.
 boolean canRedo()
          Determines whether it would be possible to redo this editing action.
 boolean canUndo()
          Determines whether it would be possible to undo this editing action.
 boolean canUndoOrRedo()
          Determines whether it would be possible to either undo or redo this editing action.
 void discardAllEdits()
          Discards all editing actions that are currently registered with this UndoManager.
protected  UndoableEdit editToBeRedone()
          Determines which significant edit would be redone if redo() 55 was called.
protected  UndoableEdit editToBeUndone()
          Determines which significant edit would be undone if undo() 55 was called.
 void end()
          Puts this UndoManager into a state where it acts as a normal CompoundEdit.
 int getLimit()
          Returns how many edits this UndoManager can maximally hold.
 java.lang.String getRedoPresentationName()
          Calculates a localized text for presenting the redo action to the user, for example in the form of a menu command.
 java.lang.String getUndoOrRedoPresentationName()
          Calculates a localized text for presenting the undo or redo action to the user, for example in the form of a menu command.
 java.lang.String getUndoPresentationName()
          Calculates a localized text for presenting the undo action to the user, for example in the form of a menu command.
 void redo()
          Redoes one significant edit action.
protected  void redoTo(UndoableEdit edit)
          Redoes all editing actions in the same order as they were added to this UndoManager, up to the specified action.
 void setLimit(int limit)
          Changes the maximal number of edits that this UndoManager can process.
 java.lang.String toString()
          Returns a string representation for this UndoManager.
protected  void trimEdits(int from, int to)
          Discards a range of edits.
protected  void trimForLimit()
          Called by various internal methods in order to enforce the limit value.
 void undo()
          Undoes one significant edit action.
 void undoableEditHappened(javax.swing.event.UndoableEditEvent event)
          Registers the edit action of an javax.swing.event.UndoableEditEvent with this UndoManager.
 void undoOrRedo()
          Undoes or redoes the last action.
protected  void undoTo(UndoableEdit edit)
          Undoes all editing actions in reverse order of addition, up to the specified action,
 
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

serialVersionUID

static final long serialVersionUID
The unique ID for serializing instances of this class. Determined using the serialver tool of Sun JDK 1.4.1_01 on GNU/Linux.

See Also:
Constant Field Values

indexOfNextAdd

int indexOfNextAdd
An index into the inherited CompoundEdit.edits 55 Vector that indicates at which position newly added editing actions would get inserted.

Normally, the value of indexOfNextAdd equals the number of UndoableEdits stored by this UndoManager, i.e. edits.size(). For each call to undo() 55 , indexOfNextAdd is decremented by one. For each call to redo() 55 , it is incremented again.


limit

int limit
The maximum number of UndoableEdits stored by this UndoManager.

Constructor Detail

UndoManager

public UndoManager()
Constructs an UndoManager.

The limit of the freshly constructed UndoManager is 100.

Method Detail

toString

public java.lang.String toString()
Returns a string representation for this UndoManager. This may be useful for debugging purposes. For the text of menu items, please refer to getUndoPresentationName() 55 , getRedoPresentationName() 55 , and getUndoOrRedoPresentationName() 55 .

Overrides:
toString in class CompoundEdit

end

public void end()
Puts this UndoManager into a state where it acts as a normal CompoundEdit. It is unlikely that an application would want to do this.

Overrides:
end in class CompoundEdit

getLimit

public int getLimit()
Returns how many edits this UndoManager can maximally hold.


setLimit

public void setLimit(int limit)
Changes the maximal number of edits that this UndoManager can process. If there are currently more edits than the new limit allows, they will receive a die 55 message in reverse order of addition.


discardAllEdits

public void discardAllEdits()
Discards all editing actions that are currently registered with this UndoManager. Each UndoableEdit will receive a die message 55 .


trimForLimit

protected void trimForLimit()
Called by various internal methods in order to enforce the limit value.


trimEdits

protected void trimEdits(int from,
                         int to)
Discards a range of edits. All edits in the range [from .. to] will receive a die message 55 before being removed from the edits array. If from is greater than to, nothing happens.


editToBeUndone

protected UndoableEdit editToBeUndone()
Determines which significant edit would be undone if undo() 55 was called.


editToBeRedone

protected UndoableEdit editToBeRedone()
Determines which significant edit would be redone if redo() 55 was called.


undoTo

protected void undoTo(UndoableEdit edit)
               throws CannotUndoException
Undoes all editing actions in reverse order of addition, up to the specified action,


redoTo

protected void redoTo(UndoableEdit edit)
               throws CannotRedoException
Redoes all editing actions in the same order as they were added to this UndoManager, up to the specified action.


undoOrRedo

public void undoOrRedo()
                throws CannotRedoException,
                       CannotUndoException
Undoes or redoes the last action. If the last action has already been undone, it will be re-done, and vice versa.

This is useful for applications that do not present a separate undo and redo facility, but just have a single menu item for undoing and redoing the very last action. Such applications will use an UndoManager whose limit is 1.


canUndoOrRedo

public boolean canUndoOrRedo()
Determines whether it would be possible to either undo or redo this editing action.

This is useful for applications that do not present a separate undo and redo facility, but just have a single menu item for undoing and redoing the very last action. Such applications will use an UndoManager whose limit is 1.


undo

public void undo()
          throws CannotUndoException
Undoes one significant edit action. If insignificant actions have been posted after the last signficant action, the insignificant ones will be undone first.

However, if end() 55 has been called on this UndoManager, it will behave like a normal CompoundEdit. In this case, all actions will be undone in reverse order of addition. Typical applications will never call end() 55 on their UndoManager.

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

canUndo

public boolean canUndo()
Determines whether it would be possible to undo this editing action.

Specified by:
canUndo in interface UndoableEdit
Overrides:
canUndo in class CompoundEdit

redo

public void redo()
          throws CannotRedoException
Redoes one significant edit action. If insignificant actions have been posted in between, the insignificant ones will be redone first.

However, if end() 55 has been called on this UndoManager, it will behave like a normal CompoundEdit. In this case, all actions will be redone in order of addition. Typical applications will never call end() 55 on their UndoManager.

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

canRedo

public boolean canRedo()
Determines whether it would be possible to redo this editing action.

Specified by:
canRedo in interface UndoableEdit
Overrides:
canRedo in class CompoundEdit

addEdit

public boolean addEdit(UndoableEdit edit)
Registers an undoable editing action with this UndoManager. If the capacity limit is reached, the oldest action will be discarded (and receives a die message 55 . Equally, any actions that were undone (but not re-done) will be discarded, too.

Specified by:
addEdit in interface UndoableEdit
Overrides:
addEdit in class CompoundEdit

getUndoOrRedoPresentationName

public java.lang.String getUndoOrRedoPresentationName()
Calculates a localized text for presenting the undo or redo action to the user, for example in the form of a menu command.

This is useful for applications that do not present a separate undo and redo facility, but just have a single menu item for undoing and redoing the very last action. Such applications will use an UndoManager whose limit is 1.


getUndoPresentationName

public java.lang.String getUndoPresentationName()
Calculates a localized text for presenting the undo action to the user, for example in the form of a menu command.

Specified by:
getUndoPresentationName in interface UndoableEdit
Overrides:
getUndoPresentationName in class CompoundEdit

getRedoPresentationName

public java.lang.String getRedoPresentationName()
Calculates a localized text for presenting the redo action to the user, for example in the form of a menu command.

Specified by:
getRedoPresentationName in interface UndoableEdit
Overrides:
getRedoPresentationName in class CompoundEdit

undoableEditHappened

public void undoableEditHappened(javax.swing.event.UndoableEditEvent event)
Registers the edit action of an javax.swing.event.UndoableEditEvent with this UndoManager.

Thread Safety: This method may safely be invoked from concurrent threads. The caller does not need to perform external synchronization. This means that javax.swing.event.UndoableEditEvent sources do not need to broadcast their events from inside the Swing worker thread.

Specified by:
undoableEditHappened in interface javax.swing.event.UndoableEditListener