javax.swing.undo

Class StateEdit

public class StateEdit extends AbstractUndoableEdit

A helper class, making it easy to support undo and redo.

The following example shows how to use this class.

 Foo foo; // class Foo implements {@link StateEditable}
 StateEdit edit;

 edit = new StateEdit(foo, "Name Change");
 foo.setName("Jane Doe");
 edit.end();
 undoManager.addEdit(edit);
 

If Foo’s implementation of {@link StateEditable} considers the name as part of the editable state, the user can now choose “Undo Name Change” or “Redo Name Change” from the respective menu. No further undo support is needed from the application.

The following explains what happens in the example.

  1. When a StateEdit is created, the associated {@link StateEditable} gets asked to store its state into a hash table, {@link #preState}.
  2. The application will now perform some changes to the edited object. This typically happens by invoking methods on the edited object.
  3. The editing phase is terminated by invoking the {@link #end()} method of the StateEdit. The end() method does two things.
    • The edited object receives a second request for storing its state. This time, it will use a different hash table, {@link #postState}.
    • To increase efficiency, the StateEdit now removes any entries from {@link #preState} and {@link #postState} that have the same key, and whose values are equal. Equality is determined by invoking the equals method inherited from {@link java.lang.Object}.
  4. When the user later chooses to undo the StateEdit, the edited object is asked to {@linkplain StateEditable#restoreState restore its state} from the {@link #preState} table. Similarly, when the user chooses to redo the StateEdit, the edited object gets asked to restore its state from the {@link #postState}.
Field Summary
protected StateEditableobject
The object which is being edited by this StateEdit.
protected Hashtable<Object,Object>postState
The state of object at the time when {@link #end()} was called.
protected Hashtable<Object,Object>preState
The state of object at the time of constructing this StateEdit.
protected static StringRCSID
The ID of the Java source file in Sun’s Revision Control System (RCS).
protected StringundoRedoName
A human-readable name for this edit action.
Constructor Summary
StateEdit(StateEditable obj)
Constructs a StateEdit, specifying the object whose state is being edited.
StateEdit(StateEditable obj, String name)
Constructs a StateEdit, specifying the object whose state is being edited.
Method Summary
voidend()
Informs this StateEdit that all edits are finished.
StringgetPresentationName()
Returns a human-readable, localized name that describes this editing action and can be displayed to the user.
protected voidinit(StateEditable obj, String name)
Initializes this StateEdit.
voidredo()
Redoes this edit operation.
protected voidremoveRedundantState()
Removes all redundant entries from the pre- and post-edit state hash tables.
voidundo()
Undoes this edit operation.

Field Detail

object

protected StateEditable object
The object which is being edited by this StateEdit.

postState

protected Hashtable<Object,Object> postState
The state of object at the time when {@link #end()} was called.

preState

protected Hashtable<Object,Object> preState
The state of object at the time of constructing this StateEdit.

RCSID

protected static final String RCSID
The ID of the Java source file in Sun’s Revision Control System (RCS). This certainly should not be part of the API specification. But in order to be API-compatible with Sun’s reference implementation, GNU Classpath also has to provide this field and match its value. The value used here has been in every JDK release at least from 1.2 to 1.5.

undoRedoName

protected String undoRedoName
A human-readable name for this edit action.

Constructor Detail

StateEdit

public StateEdit(StateEditable obj)
Constructs a StateEdit, specifying the object whose state is being edited.

Parameters: obj the object whose state is being edited by this StateEdit.

StateEdit

public StateEdit(StateEditable obj, String name)
Constructs a StateEdit, specifying the object whose state is being edited.

Parameters: obj the object whose state is being edited by this StateEdit. name the human-readable name of the editing action.

Method Detail

end

public void end()
Informs this StateEdit that all edits are finished. The edited object will be asked to store its state into {@link #postState}, and any redundant entries will get removed from {@link #preState} and {@link #postState}.

getPresentationName

public String getPresentationName()
Returns a human-readable, localized name that describes this editing action and can be displayed to the user.

Returns: the name, or null if no presentation name is available.

init

protected void init(StateEditable obj, String name)
Initializes this StateEdit. The edited object will be asked to store its current state into {@link #preState}.

Parameters: obj the object being edited. name the human-readable name of the editing action.

redo

public void redo()
Redoes this edit operation. The edited object will be asked to {@linkplain StateEditable#restoreState restore its state} from {@link #postState}.

Throws: CannotRedoException if {@link #canRedo()} returns false, for example because this action has not yet been undone.

removeRedundantState

protected void removeRedundantState()
Removes all redundant entries from the pre- and post-edit state hash tables. An entry is considered redundant if it is present both before and after the edit, and if the two values are equal.

undo

public void undo()
Undoes this edit operation. The edited object will be asked to {@linkplain StateEditable#restoreState restore its state} from {@link #preState}.

Throws: CannotUndoException if {@link #canUndo()} returns false, for example because this action has already been undone.