Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
javax.swing.undo.AbstractUndoableEdit
javax.swing.undo.CompoundEdit
javax.swing.undo.UndoManager
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()
and redo()
methods. The
human-readable text of these menu commands is provided by getUndoPresentationName()
and getRedoPresentationName()
,
respectively. To determine whether the menu item should be
selectable or greyed out, use canUndo()
and canRedo()
.
The UndoManager will only keep a specified number of editing
actions, the limit. The value of this parameter can be
retrieved by calling getLimit()
and set with setLimit(int)
. 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()
, and it is implemented
by calling undoOrRedo()
.
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
UndoableEditEvent
sources do not need to
broadcast their events from inside the Swing worker thread.
Field Summary |
Fields inherited from class javax.swing.undo.CompoundEdit | |
edits |
Fields inherited from class javax.swing.undo.AbstractUndoableEdit | |
RedoName , UndoName |
Constructor Summary | |
|
Method Summary | |
boolean |
|
boolean |
|
boolean |
|
boolean |
|
void |
|
protected UndoableEdit |
|
protected UndoableEdit |
|
void |
|
int |
|
String |
|
String |
|
String |
|
void |
|
protected void |
|
void |
|
String |
|
protected void |
|
protected void |
|
void |
|
void |
|
protected void |
|
void |
|
Methods inherited from class javax.swing.undo.CompoundEdit | |
addEdit , canRedo , canUndo , die , end , getPresentationName , getRedoPresentationName , getUndoPresentationName , isInProgress , isSignificant , lastEdit , redo , toString , undo |
Methods inherited from class javax.swing.undo.AbstractUndoableEdit | |
addEdit , canRedo , canUndo , die , getPresentationName , getRedoPresentationName , getUndoPresentationName , isSignificant , redo , replaceEdit , toString , undo |
Methods inherited from class java.lang.Object | |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
public UndoManager()
Constructs an UndoManager.The
limit
of the freshly constructed UndoManager is 100.
public boolean addEdit(UndoableEdit edit)
Registers an undoable editing action with this UndoManager. If the capacitylimit
is reached, the oldest action will be discarded (and receives a die message. Equally, any actions that were undone (but not re-done) will be discarded, too.
- Specified by:
- addEdit in interface UndoableEdit
- Overrides:
- addEdit in interface CompoundEdit
- Parameters:
edit
- the editing action that is added to this UndoManager.
- Returns:
true
ifedit
could be incorporated;false
ifedit
has not been incorporated becauseend()
has already been called on thisUndoManager
.
public boolean canRedo()
Determines whether it would be possible to redo this editing action.
- Specified by:
- canRedo in interface UndoableEdit
- Overrides:
- canRedo in interface CompoundEdit
- Returns:
true
to indicate that this action can be redone;false
otherwise.
- See Also:
redo()
,canUndo()
,canUndoOrRedo()
public boolean canUndo()
Determines whether it would be possible to undo this editing action.
- Specified by:
- canUndo in interface UndoableEdit
- Overrides:
- canUndo in interface CompoundEdit
- Returns:
true
to indicate that this action can be undone;false
otherwise.
- See Also:
undo()
,canRedo()
,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
whoselimit
is 1.
- Returns:
true
to indicate that this action can be undone or redone;false
if neither is possible at the current time.
public void discardAllEdits()
Discards all editing actions that are currently registered with this UndoManager. EachUndoableEdit
will receive adie message
.
protected UndoableEdit editToBeRedone()
Determines which significant edit would be redone ifredo()
was called.
- Returns:
- the significant edit that would be redone, or
null
if no significant edit would be affected by callingredo()
.
protected UndoableEdit editToBeUndone()
Determines which significant edit would be undone ifundo()
was called.
- Returns:
- the significant edit that would be undone, or
null
if no significant edit would be affected by callingundo()
.
public void end()
Puts this UndoManager into a state where it acts as a normalCompoundEdit
. It is unlikely that an application would want to do this.
- Overrides:
- end in interface CompoundEdit
public int getLimit()
Returns how many edits this UndoManager can maximally hold.
- See Also:
setLimit(int)
public 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 interface CompoundEdit
public 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
whoselimit
is 1.
- Returns:
- the redo presentation name if the last action has already been undone, or the undo presentation name otherwise.
public 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 interface CompoundEdit
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()
has been called on this UndoManager, it will behave like a normalCompoundEdit
. In this case, all actions will be redone in order of addition. Typical applications will never callend()
on theirUndoManager
.
- Specified by:
- redo in interface UndoableEdit
- Overrides:
- redo in interface CompoundEdit
- Throws:
CannotRedoException
- if no action can be redone.
- See Also:
canRedo()
,redo()
,undoOrRedo()
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.
- Parameters:
edit
- the last editing action to be redone.
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 adie
message in reverse order of addition.
- Parameters:
limit
- the new limit.
- Throws:
IllegalStateException
- ifend()
has already been called on this UndoManager.
public String toString()
Returns a string representation for this UndoManager. This may be useful for debugging purposes. For the text of menu items, please refer togetUndoPresentationName()
,getRedoPresentationName()
, andgetUndoOrRedoPresentationName()
.
- Overrides:
- toString in interface CompoundEdit
protected void trimEdits(int from, int to)
Discards a range of edits. All edits in the range[from .. to]
will receive a die message before being removed from the edits array. Iffrom
is greater thanto
, nothing happens.
- Parameters:
from
- the lower bound of the range of edits to be discarded.to
- the upper bound of the range of edits to be discarded.
protected void trimForLimit()
Called by various internal methods in order to enforce thelimit
value.
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()
has been called on this UndoManager, it will behave like a normalCompoundEdit
. In this case, all actions will be undone in reverse order of addition. Typical applications will never callend()
on theirUndoManager
.
- Specified by:
- undo in interface UndoableEdit
- Overrides:
- undo in interface CompoundEdit
- Throws:
CannotUndoException
- if no action can be undone.
- See Also:
canUndo()
,redo()
,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
whoselimit
is 1.
protected void undoTo(UndoableEdit edit) throws CannotUndoException
Undoes all editing actions in reverse order of addition, up to the specified action,
- Parameters:
edit
- the last editing action to be undone.
public void undoableEditHappened(UndoableEditEvent event)
Registers the edit action of anUndoableEditEvent
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
UndoableEditEvent
sources do not need to broadcast their events from inside the Swing worker thread.
- Specified by:
- undoableEditHappened in interface UndoableEditListener
- Parameters:
event
- the event whoseedit
will be passed toaddEdit(UndoableEdit)
.