public class UndoHistory extends java.lang.Object implements javax.swing.event.UndoableEditListener, Resetable
javax.swing.undo.CompoundEdit
. This mechanism is
based on the assumption that UndoableEdits are actually instances of
javax.swing.text.AbstractDocument.DefaultDocumentEvent
.
To bind this UndoHistory to a text component, call the method
registerUndoHistory()
. It will
automatically be available through the standard shortcuts
ctrl + z
and ctrl + y
, and through this
classes API.
Alternatively (or in parallel) you can register the text component using
the method registerSnapshotHistory()
.
A "snapshot" of the text will be created every time the method
reset()
is called on this object. The snapshot history will
automatically be available through the shortcuts
alt + ←
and ctrl + →
(arrows from cursor block),
and through this classes API.
The difference between the two types of history is the following: While the undo history
is recorded automatically the snapshot history requires explicite recording through
the reset()
method. The snapshot history is always retained and only grows
with more snapshots being added. The undo history on the other hand may grow shrink and
change. If text modifications are undone, then continued from an earlyer point in history,
the undone modifications are discarded causing the undo history to shrink.
Every text component must have its own instance of UndoHistory, that is only one text component may be registered per UndoHistory.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ACTION_KEY_NEXT
Key for Action in action map of text component
|
static java.lang.String |
ACTION_KEY_PREVIOUS
Key for Action in action map of text component
|
static java.lang.String |
ACTION_KEY_REDO
Key for Action in action map of text component
|
static java.lang.String |
ACTION_KEY_UNDO
Key for Action in action map of text component
|
Constructor and Description |
---|
UndoHistory() |
Modifier and Type | Method and Description |
---|---|
boolean |
addEdit(javax.swing.undo.UndoableEdit anEdit)
Adds an UndoableEdit object (undo) to the list of undoable modifications.
|
boolean |
canRedo()
Returns true if there is a tex modification that can be "redone".
|
boolean |
canUndo()
Returns true if there is a tex modification that can be "undone".
|
boolean |
hasNext()
Returns true if there is a newer snapshot in the history
than the one currently displayed.
|
boolean |
hasPrevious()
Returns true if there is an older snapshot in the history
than the one currently displayed.
|
void |
next()
Sets the text component to the next (newer) snapshot in the
snapshot history if there is any.
|
void |
previous()
Sets the text component to the previous snapshot in the
snapshot history if there is any.
|
void |
redo()
"Redoes" the last undone text modification if any is available.
|
void |
registerSnapshotHistory(javax.swing.text.JTextComponent component)
Registers the specified text component for the snapshot history.
|
void |
registerUndoHistory(javax.swing.text.JTextComponent component)
Registers the specified text component for the undo history.
|
void |
reset()
Forces the start of a new run of undos, and discards
any open redoes.
|
void |
undo()
"Undoes" the last registered text modification if any is available.
|
void |
undoableEditHappened(javax.swing.event.UndoableEditEvent event)
Method from interface
UndoableEditListener . |
public static java.lang.String ACTION_KEY_UNDO
public static java.lang.String ACTION_KEY_REDO
public static java.lang.String ACTION_KEY_PREVIOUS
public static java.lang.String ACTION_KEY_NEXT
public boolean addEdit(javax.swing.undo.UndoableEdit anEdit)
anEdit
- to be added to the undo historypublic boolean canRedo()
public boolean canUndo()
public void redo()
public void undo()
public void previous()
public void next()
public boolean hasPrevious()
previous()
would have any effect.public boolean hasNext()
next()
would have any effect.public void undoableEditHappened(javax.swing.event.UndoableEditEvent event)
UndoableEditListener
.
Interfaces with the text component (pecisely: its Document object)
to record text modifications.undoableEditHappened
in interface javax.swing.event.UndoableEditListener
event
- containing an UndoableEdit obejctpublic void reset()
public void registerUndoHistory(javax.swing.text.JTextComponent component)
ctrl + z
and ctrl + y
shortcuts.component
- to be registered for undos.public void registerSnapshotHistory(javax.swing.text.JTextComponent component)
alt + ←
and ctrl + →
shortcuts.component
- to be registered for snapshots.