Methods Summary |
---|
public synchronized boolean | addEdit(javax.swing.undo.UndoableEdit anEdit)Adds an UndoableEdit to this
UndoManager , if it's possible. This removes all
edits from the index of the next edit to the end of the edits
list. If end has been invoked the edit is not added
and false is returned. If end hasn't
been invoked this returns true .
boolean retVal;
// Trim from the indexOfNextAdd to the end, as we'll
// never reach these edits once the new one is added.
trimEdits(indexOfNextAdd, edits.size()-1);
retVal = super.addEdit(anEdit);
if (inProgress) {
retVal = true;
}
// Maybe super added this edit, maybe it didn't (perhaps
// an in progress compound edit took it instead. Or perhaps
// this UndoManager is no longer in progress). So make sure
// the indexOfNextAdd is pointed at the right place.
indexOfNextAdd = edits.size();
// Enforce the limit
trimForLimit();
return retVal;
|
public synchronized boolean | canRedo()Returns true if edits may be redone. If end has
been invoked, this returns the value from super. Otherwise,
this returns true if there are any edits to be redone
(editToBeRedone returns non-null ).
if (inProgress) {
UndoableEdit edit = editToBeRedone();
return edit != null && edit.canRedo();
} else {
return super.canRedo();
}
|
public synchronized boolean | canUndo()Returns true if edits may be undone. If end has
been invoked, this returns the value from super. Otherwise
this returns true if there are any edits to be undone
(editToBeUndone returns non-null ).
if (inProgress) {
UndoableEdit edit = editToBeUndone();
return edit != null && edit.canUndo();
} else {
return super.canUndo();
}
|
public synchronized boolean | canUndoOrRedo()Returns true if it is possible to invoke undo or
redo .
if (indexOfNextAdd == edits.size()) {
return canUndo();
} else {
return canRedo();
}
|
public synchronized void | discardAllEdits()Empties the undo manager sending each edit a die message
in the process.
Enumeration cursor = edits.elements();
while (cursor.hasMoreElements()) {
UndoableEdit e = (UndoableEdit)cursor.nextElement();
e.die();
}
edits = new Vector();
indexOfNextAdd = 0;
// PENDING(rjrjr) when vector grows a removeRange() method
// (expected in JDK 1.2), trimEdits() will be nice and
// efficient, and this method can call that instead.
|
protected javax.swing.undo.UndoableEdit | editToBeRedone()Returns the the next significant edit to be redone if redo
is invoked. This returns null if there are no edits
to be redone.
int count = edits.size();
int i = indexOfNextAdd;
while (i < count) {
UndoableEdit edit = (UndoableEdit)edits.elementAt(i++);
if (edit.isSignificant()) {
return edit;
}
}
return null;
|
protected javax.swing.undo.UndoableEdit | editToBeUndone()Returns the the next significant edit to be undone if undo
is invoked. This returns null if there are no edits
to be undone.
int i = indexOfNextAdd;
while (i > 0) {
UndoableEdit edit = (UndoableEdit)edits.elementAt(--i);
if (edit.isSignificant()) {
return edit;
}
}
return null;
|
public synchronized void | end()Turns this UndoManager into a normal
CompoundEdit . This removes all edits that have
been undone.
super.end();
this.trimEdits(indexOfNextAdd, edits.size()-1);
|
public synchronized int | getLimit()Returns the maximum number of edits this {@code UndoManager}
holds. A value less than 0 indicates the number of edits is not
limited.
return limit;
|
public synchronized java.lang.String | getRedoPresentationName()Returns a description of the redoable form of this edit.
If end has been invoked this calls into super.
Otherwise if there are edits to be redone, this returns
the value from the next significant edit that will be redone.
If there are no edits to be redone and end has not
been invoked this returns the value from the UIManager
property "AbstractUndoableEdit.redoText".
if (inProgress) {
if (canRedo()) {
return editToBeRedone().getRedoPresentationName();
} else {
return UIManager.getString("AbstractUndoableEdit.redoText");
}
} else {
return super.getRedoPresentationName();
}
|
public synchronized java.lang.String | getUndoOrRedoPresentationName()Convenience method that returns either
getUndoPresentationName or
getRedoPresentationName . If the index of the next
edit equals the size of the edits list,
getUndoPresentationName is returned, otherwise
getRedoPresentationName is returned.
if (indexOfNextAdd == edits.size()) {
return getUndoPresentationName();
} else {
return getRedoPresentationName();
}
|
public synchronized java.lang.String | getUndoPresentationName()Returns a description of the undoable form of this edit.
If end has been invoked this calls into super.
Otherwise if there are edits to be undone, this returns
the value from the next significant edit that will be undone.
If there are no edits to be undone and end has not
been invoked this returns the value from the UIManager
property "AbstractUndoableEdit.undoText".
if (inProgress) {
if (canUndo()) {
return editToBeUndone().getUndoPresentationName();
} else {
return UIManager.getString("AbstractUndoableEdit.undoText");
}
} else {
return super.getUndoPresentationName();
}
|
public synchronized void | redo()Redoes the appropriate edits. If end has been
invoked this calls through to the superclass. Otherwise
this invokes redo on all edits between the
index of the next edit and the next significant edit, updating
the index of the next edit appropriately.
if (inProgress) {
UndoableEdit edit = editToBeRedone();
if (edit == null) {
throw new CannotRedoException();
}
redoTo(edit);
} else {
super.redo();
}
|
protected void | redoTo(javax.swing.undo.UndoableEdit edit)Redoes all changes from the index of the next edit to
edit , updating the index of the next edit appropriately.
boolean done = false;
while (!done) {
UndoableEdit next = (UndoableEdit)edits.elementAt(indexOfNextAdd++);
next.redo();
done = next == edit;
}
|
public synchronized void | setLimit(int l)Sets the maximum number of edits this UndoManager
holds. A value less than 0 indicates the number of edits is not
limited. If edits need to be discarded to shrink the limit,
die will be invoked on them in the reverse
order they were added. The default is 100.
if (!inProgress) throw new RuntimeException("Attempt to call UndoManager.setLimit() after UndoManager.end() has been called");
limit = l;
trimForLimit();
|
public java.lang.String | toString()Returns a string that displays and identifies this
object's properties.
return super.toString() + " limit: " + limit +
" indexOfNextAdd: " + indexOfNextAdd;
|
protected void | trimEdits(int from, int to)Removes edits in the specified range.
All edits in the given range (inclusive, and in reverse order)
will have die invoked on them and are removed from
the list of edits. This has no effect if
from > to .
if (from <= to) {
// System.out.println("Trimming " + from + " " + to + " with index " +
// indexOfNextAdd);
for (int i = to; from <= i; i--) {
UndoableEdit e = (UndoableEdit)edits.elementAt(i);
// System.out.println("JUM: Discarding " +
// e.getUndoPresentationName());
e.die();
// PENDING(rjrjr) when Vector supports range deletion (JDK
// 1.2) , we can optimize the next line considerably.
edits.removeElementAt(i);
}
if (indexOfNextAdd > to) {
// System.out.print("...right...");
indexOfNextAdd -= to-from+1;
} else if (indexOfNextAdd >= from) {
// System.out.println("...mid...");
indexOfNextAdd = from;
}
// System.out.println("new index " + indexOfNextAdd);
}
|
protected void | trimForLimit()Reduces the number of queued edits to a range of size limit,
centered on the index of the next edit.
if (limit >= 0) {
int size = edits.size();
// System.out.print("limit: " + limit +
// " size: " + size +
// " indexOfNextAdd: " + indexOfNextAdd +
// "\n");
if (size > limit) {
int halfLimit = limit/2;
int keepFrom = indexOfNextAdd - 1 - halfLimit;
int keepTo = indexOfNextAdd - 1 + halfLimit;
// These are ints we're playing with, so dividing by two
// rounds down for odd numbers, so make sure the limit was
// honored properly. Note that the keep range is
// inclusive.
if (keepTo - keepFrom + 1 > limit) {
keepFrom++;
}
// The keep range is centered on indexOfNextAdd,
// but odds are good that the actual edits Vector
// isn't. Move the keep range to keep it legal.
if (keepFrom < 0) {
keepTo -= keepFrom;
keepFrom = 0;
}
if (keepTo >= size) {
int delta = size - keepTo - 1;
keepTo += delta;
keepFrom += delta;
}
// System.out.println("Keeping " + keepFrom + " " + keepTo);
trimEdits(keepTo+1, size-1);
trimEdits(0, keepFrom-1);
}
}
|
public synchronized void | undo()Undoes the appropriate edits. If end has been
invoked this calls through to the superclass, otherwise
this invokes undo on all edits between the
index of the next edit and the last significant edit, updating
the index of the next edit appropriately.
if (inProgress) {
UndoableEdit edit = editToBeUndone();
if (edit == null) {
throw new CannotUndoException();
}
undoTo(edit);
} else {
super.undo();
}
|
public synchronized void | undoOrRedo()Convenience method that invokes one of undo or
redo . If any edits have been undone (the index of
the next edit is less than the length of the edits list) this
invokes redo , otherwise it invokes undo .
if (indexOfNextAdd == edits.size()) {
undo();
} else {
redo();
}
|
protected void | undoTo(javax.swing.undo.UndoableEdit edit)Undoes all changes from the index of the next edit to
edit , updating the index of the next edit appropriately.
boolean done = false;
while (!done) {
UndoableEdit next = (UndoableEdit)edits.elementAt(--indexOfNextAdd);
next.undo();
done = next == edit;
}
|
public void | undoableEditHappened(javax.swing.event.UndoableEditEvent e)An UndoableEditListener method. This invokes
addEdit with e.getEdit() .
addEdit(e.getEdit());
|