Commit 45cfeb06 by Sascha Brawer Committed by Michael Koch

UndoManager.java: Re-written from scratch.

2004-02-14  Sascha Brawer  <brawer@dandelis.ch>

	* javax/swing/undo/UndoManager.java: Re-written from scratch.

From-SVN: r77827
parent 33421d69
2004-02-14 Sascha Brawer <brawer@dandelis.ch>
* javax/swing/undo/UndoManager.java: Re-written from scratch.
2004-02-14 Per Bothner <per@bothner.com> 2004-02-14 Per Bothner <per@bothner.com>
* java/nio/channels/spi/AbstractInterruptibleChannel.java (close): * java/nio/channels/spi/AbstractInterruptibleChannel.java (close):
......
/* AbstractTableModel.java -- /* AbstractTableModel.java --
Copyright (C) 2002 Free Software Foundation, Inc. Copyright (C) 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
...@@ -38,223 +38,588 @@ exception statement from your version. */ ...@@ -38,223 +38,588 @@ exception statement from your version. */
package javax.swing.undo; package javax.swing.undo;
import javax.swing.UIManager;
import javax.swing.event.UndoableEditEvent; import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener; import javax.swing.event.UndoableEditListener;
/** /**
* UndoManager * A manager for providing an application&#x2019;s undo/redo
* @author Andrew Selkirk * functionality.
*
* <p>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 {@link UndoableEdit} object with the
* <code>UndoManager</code>. To implement the &#x201c;undo&#x201d; and
* &#x201c;redo&#x201d; menu commands, the application invokes the
* UndoManager&#x2019;s {@link #undo} and {@link #redo} methods. The
* human-readable text of these menu commands is provided by {@link
* #getUndoPresentationName} and {@link #getRedoPresentationName},
* respectively. To determine whether the menu item should be
* selectable or greyed out, use {@link #canUndo} and {@link
* #canRedo}.
*
* <p>The UndoManager will only keep a specified number of editing
* actions, the <em>limit</em>. The value of this parameter can be
* retrieved by calling {@link #getLimit} and set with {@link
* #setLimit}. If more UndoableEdits are added to the UndoManager,
* the oldest actions will be discarded.
*
* <p>Some applications do not provide separate menu commands for
* &#x201c;undo&#x201d; and &#x201c;redo.&#x201d; Instead, they
* have just a single command whose text switches between the two.
* Such applications would use an UndoManager with a <code>limit</code>
* of 1. The text of this combined menu item is available via
* {@link #getUndoOrRedoPresentationName}, and it is implemented
* by calling {@link #undoOrRedo}.
*
* <p><b>Thread Safety:</b> In constrast to the other classes of the
* <code>javax.swing.undo</code> package, the public methods of an
* <code>UndoManager</code> are safe to call from concurrent threads.
* The caller does not need to perform external synchronization, and
* {@link javax.swing.event.UndoableEvent} sources do not need to
* broadcast their events from inside the Swing worker thread.
*
* @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a>
*/
public class UndoManager
extends CompoundEdit
implements UndoableEditListener
{
/**
* The unique ID for serializing instances of this class. Determined
* using the <code>serialver</code> tool of Sun JDK 1.4.1_01 on
* GNU/Linux.
*/ */
public class UndoManager extends CompoundEdit implements UndoableEditListener { static final long serialVersionUID = -2077529998244066750L;
//-------------------------------------------------------------
// Variables --------------------------------------------------
//-------------------------------------------------------------
/** /**
* indexOfNextAdd * An index into the inherited {@link #edits} Vector that indicates
* at which position newly added editing actions would get inserted.
*
* <p>Normally, the value of <code>indexOfNextAdd</code> equals
* the number of UndoableEdits stored by this UndoManager, i.e.
* <code>edits.size()</code>. For each call to {@link #undo},
* <code>indexOfNextAdd</code> is decremented by one. For each
* call to {@link #redo}, it is incremented again.
*/ */
int indexOfNextAdd; int indexOfNextAdd;
/** /**
* limit * The maximum number of UndoableEdits stored by this UndoManager.
*/ */
int limit; int limit;
//-------------------------------------------------------------
// Initialization ---------------------------------------------
//-------------------------------------------------------------
/** /**
* Constructor UndoManager * Constructs an UndoManager.
*
* <p>The <code>limit</code> of the freshly constructed UndoManager
* is 100.
*/ */
public UndoManager() { public UndoManager()
// TODO {
} // UndoManager() limit = 100;
}
//-------------------------------------------------------------
// Methods ----------------------------------------------------
//-------------------------------------------------------------
/** /**
* toString * Returns a string representation for this UndoManager. This may be
* @returns String * useful for debugging purposes. For the text of menu items, please
* refer to {@link #getUndoPresentationName}, {@link
* #getRedoPresentationName}, and {@link
* #getUndoOrRedoPresentationName}.
*/ */
public String toString() { public String toString()
return null; // TODO {
} // toString() return super.toString()
+ " limit: " + limit
+ " indexOfNextAdd: " + indexOfNextAdd;
}
/** /**
* end * Puts this UndoManager into a state where it acts as a normal
* {@link CompoundEdit}. It is unlikely that an application would
* want to do this.
*/ */
public synchronized void end() { public synchronized void end()
// TODO {
} // end() super.end();
trimEdits(indexOfNextAdd, edits.size() - 1);
}
/** /**
* getLimit * Returns how many edits this UndoManager can maximally hold.
* @returns int *
* @see #setLimit
*/ */
public synchronized int getLimit() { public synchronized int getLimit()
return 0; // TODO {
} // getLimit() return limit;
}
/** /**
* discardAllEdits * 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 {@link UndoableEdit#die() die}
* message in reverse order of addition.
*
* @param limit the new limit.
*
* @throws IllegalStateException if {@link #end()} has already been
* called on this UndoManager.
*/ */
public synchronized void discardAllEdits() { public synchronized void setLimit(int limit)
// TODO {
} // discardAllEdits() if (!isInProgress())
throw new IllegalStateException();
this.limit = limit;
trimForLimit();
}
/** /**
* trimForLimit * Discards all editing actions that are currently registered with
* this UndoManager. Each {@link UndoableEdit} will receive a {@link
* UndoableEdit#die() die message}.
*/ */
protected void trimForLimit() { public synchronized void discardAllEdits()
// TODO {
} // trimForLimit() int size;
size = edits.size();
for (int i = size - 1; i >= 0; i--)
((UndoableEdit) edits.get(i)).die();
indexOfNextAdd = 0;
edits.clear();
}
/** /**
* trimEdits * Called by various internal methods in order to enforce
* @param value0 TODO * the <code>limit</code> value.
* @param value1 TODO
*/ */
protected void trimEdits(int value0, int value1) { protected void trimForLimit()
// TODO {
} // trimEdits() int high, s;
/** s = edits.size();
* setLimit
* @param value0 TODO /* The Sun J2SE1.4.1_01 implementation can be observed to do
* nothing (instead of throwing an exception) with a negative or
* zero limit. It may be debatable whether this is the best
* behavior, but we replicate it for sake of compatibility.
*/ */
public synchronized void setLimit(int value0) { if (limit <= 0 || s <= limit)
// TODO return;
} // setLimit()
high = Math.min(indexOfNextAdd + limit/2 - 1, s - 1);
trimEdits(high + 1, s - 1);
trimEdits(0, high - limit);
}
/** /**
* editToBeUndone * Discards a range of edits. All edits in the range <code>[from
* @returns UndoableEdit * .. to]</code> will receive a {@linkplain UndoableEdit#die() die
* message} before being removed from the edits array. If
* <code>from</code> is greater than <code>to</code>, nothing
* happens.
*
* @param from the lower bound of the range of edits to be
* discarded.
*
* @param to the upper bound of the range of edits to be discarded.
*/ */
protected UndoableEdit editToBeUndone() { protected void trimEdits(int from, int to)
return null; // TODO {
} // editToBeUndone() if (from > to)
return;
for (int i = to; i >= from; i--)
((UndoableEdit) edits.get(i)).die();
// Remove the range [from .. to] from edits. If from == to, which
// is likely to be a very common case, we can do better than
// creating a sub-list and clearing it.
if (to == from)
edits.remove(from);
else
edits.subList(from, to + 1).clear();
if (indexOfNextAdd > to)
indexOfNextAdd = indexOfNextAdd - to + from - 1;
else if (indexOfNextAdd >= from)
indexOfNextAdd = from;
}
/** /**
* editToBeRedone * Determines which significant edit would be undone if {@link
* @returns UndoableEdit * #undo()} was called.
*
* @returns the significant edit that would be undone, or
* <code>null</code> if no significant edit would be affected by
* calling {@link #undo()}.
*/ */
protected UndoableEdit editToBeRedone() { protected UndoableEdit editToBeUndone()
return null; // TODO {
} // editToBeRedone() UndoableEdit result;
for (int i = indexOfNextAdd - 1; i >= 0; i--)
{
result = (UndoableEdit) edits.get(i);
if (result.isSignificant())
return result;
}
return null;
}
/** /**
* undoTo * Determines which significant edit would be redone if {@link
* @param value0 TODO * #redo()} was called.
* @exception CannotUndoException TODO *
* @returns the significant edit that would be redone, or
* <code>null</code> if no significant edit would be affected by
* calling {@link #redo()}.
*/ */
protected void undoTo(UndoableEdit value0) throws CannotUndoException { protected UndoableEdit editToBeRedone()
// TODO {
} // undoTo() UndoableEdit result;
for (int i = indexOfNextAdd; i < edits.size(); i++)
{
result = (UndoableEdit) edits.get(i);
if (result.isSignificant())
return result;
}
return null;
}
/** /**
* redoTo * Undoes all editing actions in reverse order of addition,
* @param value0 TODO * up to the specified action,
* @exception CannotRedoException TODO *
* @param edit the last editing action to be undone.
*/ */
protected void redoTo(UndoableEdit value0) throws CannotRedoException { protected void undoTo(UndoableEdit edit)
// TODO throws CannotUndoException
} // redoTo() {
UndoableEdit cur;
if (!edits.contains(edit))
throw new CannotUndoException();
while (true)
{
indexOfNextAdd -= 1;
cur = (UndoableEdit) edits.get(indexOfNextAdd);
cur.undo();
if (cur == edit)
return;
}
}
/** /**
* undoOrRedo * Redoes all editing actions in the same order as they were
* @exception CannotRedoException TODO * added to this UndoManager, up to the specified action.
* @exception CannotUndoException TODO *
* @param edit the last editing action to be redone.
*/ */
public synchronized void undoOrRedo() throws CannotRedoException, CannotUndoException { protected void redoTo(UndoableEdit edit)
// TODO throws CannotRedoException
} // undoOrRedo() {
UndoableEdit cur;
if (!edits.contains(edit))
throw new CannotRedoException();
while (true)
{
cur = (UndoableEdit) edits.get(indexOfNextAdd);
indexOfNextAdd += 1;
cur.redo();
if (cur == edit)
return;
}
}
/** /**
* canUndoOrRedo * Undoes or redoes the last action. If the last action has already
* @returns boolean * been undone, it will be re-done, and vice versa.
*
* <p>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 <code>UndoManager</code> whose <code>limit</code> is 1.
*/ */
public synchronized boolean canUndoOrRedo() { public synchronized void undoOrRedo()
return false; // TODO throws CannotRedoException, CannotUndoException
} // canUndoOrRedo() {
if (indexOfNextAdd == edits.size())
undo();
else
redo();
}
/** /**
* undo * Determines whether it would be possible to either undo or redo
* @exception CannotUndoException TODO * this editing action.
*
* <p>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 <code>UndoManager</code> whose <code>limit</code> is 1.
*
* @return <code>true</code> to indicate that this action can be
* undone or redone; <code>false</code> if neither is possible at
* the current time.
*/ */
public synchronized void undo() throws CannotUndoException { public synchronized boolean canUndoOrRedo()
// TODO {
} // undo() return indexOfNextAdd == edits.size() ? canUndo() : canRedo();
}
/** /**
* canUndo * Undoes one significant edit action. If insignificant actions have
* @returns boolean * been posted after the last signficant action, the insignificant
* ones will be undone first.
*
* <p>However, if {@link #end()} has been called on this
* UndoManager, it will behave like a normal {@link
* CompoundEdit}. In this case, all actions will be undone in
* reverse order of addition. Typical applications will never call
* {@link #end()} on their <code>UndoManager</code>.
*
* @throws CannotUndoException if no action can be undone.
*
* @see #canUndo()
* @see #redo()
* @see #undoOrRedo()
*/ */
public synchronized boolean canUndo() { public synchronized void undo()
return false; // TODO throws CannotUndoException
} // canUndo() {
if (!isInProgress())
{
super.undo();
return;
}
UndoableEdit edit = editToBeUndone();
if (edit == null)
throw new CannotUndoException();
undoTo(edit);
}
/** /**
* redo * Determines whether it would be possible to undo this editing
* @exception CannotRedoException TODO * action.
*
* @return <code>true</code> to indicate that this action can be
* undone; <code>false</code> otherwise.
*
* @see #undo()
* @see #canRedo()
* @see #canUndoOrRedo()
*/ */
public synchronized void redo() throws CannotRedoException { public synchronized boolean canUndo()
// TODO {
} // redo() UndoableEdit edit;
if (!isInProgress())
return super.canUndo();
edit = editToBeUndone();
return edit != null && edit.canUndo();
}
/** /**
* canRedo * Redoes one significant edit action. If insignificant actions have
* @returns boolean * been posted in between, the insignificant ones will be redone
* first.
*
* <p>However, if {@link #end()} has been called on this
* UndoManager, it will behave like a normal {@link
* CompoundEdit}. In this case, <em>all</em> actions will be redone
* in order of addition. Typical applications will never call {@link
* #end()} on their <code>UndoManager</code>.
*
* @throws CannotRedoException if no action can be redone.
*
* @see #canRedo()
* @see #redo()
* @see #undoOrRedo()
*/ */
public synchronized boolean canRedo() { public synchronized void redo()
return false; // TODO throws CannotRedoException
} // canRedo() {
if (!isInProgress())
{
super.redo();
return;
}
UndoableEdit edit = editToBeRedone();
if (edit == null)
throw new CannotRedoException();
redoTo(edit);
}
/** /**
* addEdit * Determines whether it would be possible to redo this editing
* @param value0 TODO * action.
* @returns boolean *
* @return <code>true</code> to indicate that this action can be
* redone; <code>false</code> otherwise.
*
* @see #redo()
* @see #canUndo()
* @see #canUndoOrRedo()
*/ */
public synchronized boolean addEdit(UndoableEdit value0) { public synchronized boolean canRedo()
return false; // TODO {
} // addEdit() UndoableEdit edit;
if (!isInProgress())
return super.canRedo();
edit = editToBeRedone();
return edit != null && edit.canRedo();
}
/** /**
* getUndoOrRedoPresentationName * Registers an undoable editing action with this UndoManager. If
* @returns String * the capacity <code>limit</code> is reached, the oldest action
* will be discarded (and receives a {@linkplain UndoableEdit#die()
* die message}. Equally, any actions that were undone (but not re-done)
* will be discarded, too.
*
* @param edit the editing action that is added to this UndoManager.
*
* @return <code>true</code> if <code>edit</code> could be
* incorporated; <code>false</code> if <code>edit</code> has not
* been incorporated because {@link #end()} has already been called
* on this <code>UndoManager</code>.
*/ */
public synchronized String getUndoOrRedoPresentationName() { public synchronized boolean addEdit(UndoableEdit edit)
return null; // TODO {
} // getUndoOrRedoPresentationName() boolean result;
// Discard any edits starting at indexOfNextAdd.
trimEdits(indexOfNextAdd, edits.size() - 1);
result = super.addEdit(edit);
indexOfNextAdd = edits.size();
trimForLimit();
return result;
}
/** /**
* getUndoPresentationName * Calculates a localized text for presenting the undo or redo
* @returns String * action to the user, for example in the form of a menu command.
*
* <p>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 <code>UndoManager</code> whose <code>limit</code> is 1.
*
* @return the redo presentation name if the last action has already
* been undone, or the undo presentation name otherwise.
*
* @see #getUndoPresentationName()
* @see #getRedoPresentationName()
*/ */
public synchronized String getUndoPresentationName() { public synchronized String getUndoOrRedoPresentationName()
return null; // TODO {
} // getUndoPresentationName() if (indexOfNextAdd == edits.size())
return getUndoPresentationName();
else
return getRedoPresentationName();
}
/** /**
* getRedoPresentationName * Calculates a localized text for presenting the undo action
* @returns String * to the user, for example in the form of a menu command.
*/ */
public synchronized String getRedoPresentationName() { public synchronized String getUndoPresentationName()
return null; // TODO {
} // getRedoPresentationName() UndoableEdit edit;
if (!isInProgress())
return super.getUndoPresentationName();
edit = editToBeUndone();
if (edit == null)
return UIManager.getString("AbstractUndoableEdit.undoText");
else
return edit.getUndoPresentationName();
}
/** /**
* undoableEditHappened * Calculates a localized text for presenting the redo action
* @param value0 TODO * to the user, for example in the form of a menu command.
*/ */
public void undoableEditHappened(UndoableEditEvent value0) { public synchronized String getRedoPresentationName()
// TODO {
} // undoableEditHappened() UndoableEdit edit;
if (!isInProgress())
return super.getRedoPresentationName();
edit = editToBeRedone();
if (edit == null)
return UIManager.getString("AbstractUndoableEdit.redoText");
else
return edit.getRedoPresentationName();
}
} // UndoManager
/**
* Registers the edit action of an {@link UndoableEditEvent}
* with this UndoManager.
*
* <p><b>Thread Safety:</b> This method may safely be invoked from
* concurrent threads. The caller does not need to perform external
* synchronization. This means that {@link
* javax.swing.event.UndoableEvent} sources do not need to broadcast
* their events from inside the Swing worker thread.
*
* @param event the event whose <code>edit</code> will be
* passed to {@link #addEdit}.
*
* @see UndoableEditEvent#getEdit()
* @see #addEdit
*/
public void undoableEditHappened(UndoableEditEvent event)
{
// Note that this method does not need to be synchronized,
// because addEdit will obtain and release the mutex.
addEdit(event.getEdit());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment