javax.swing.undo
public class: StateEdit [javadoc |
source]
java.lang.Object
javax.swing.undo.AbstractUndoableEdit
javax.swing.undo.StateEdit
All Implemented Interfaces:
UndoableEdit, Serializable
StateEdit is a general edit for objects that change state.
Objects being edited must conform to the StateEditable interface.
This edit class works by asking an object to store it's state in
Hashtables before and after editing occurs. Upon undo or redo the
object is told to restore it's state from these Hashtables.
A state edit is used as follows:
// Create the edit during the "before" state of the object
StateEdit newEdit = new StateEdit(myObject);
// Modify the object
myObject.someStateModifyingMethod();
// "end" the edit when you are done modifying the object
newEdit.end();
Note that when a StateEdit ends, it removes redundant state from
the Hashtables - A state Hashtable is not guaranteed to contain all
keys/values placed into it when the state is stored!
Field Summary |
---|
protected static final String | RCSID | |
protected StateEditable | object | The object being edited |
protected Hashtable<Object, Object> | preState | The state information prior to the edit |
protected Hashtable<Object, Object> | postState | The state information after the edit |
protected String | undoRedoName | The undo/redo presentation name |
Constructor: |
public StateEdit(StateEditable anObject) {
super();
init (anObject,null);
}
Create and return a new StateEdit. Parameters:
anObject - The object to watch for changing state
Also see:
- StateEdit
|
public StateEdit(StateEditable anObject,
String name) {
super();
init (anObject,name);
}
Create and return a new StateEdit with a presentation name. Parameters:
anObject - The object to watch for changing state
name - The presentation name to be used for this edit
Also see:
- StateEdit
|
Methods from javax.swing.undo.AbstractUndoableEdit: |
---|
addEdit, canRedo, canUndo, die, getPresentationName, getRedoPresentationName, getUndoPresentationName, isSignificant, redo, replaceEdit, toString, undo |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from javax.swing.undo.StateEdit Detail: |
public void end() {
this.postState = new Hashtable< Object, Object >(11);
this.object.storeState(this.postState);
this.removeRedundantState();
}
Gets the post-edit state of the StateEditable object and
ends the edit. |
public String getPresentationName() {
return this.undoRedoName;
}
Gets the presentation name for this edit |
protected void init(StateEditable anObject,
String name) {
this.object = anObject;
this.preState = new Hashtable< Object, Object >(11);
this.object.storeState(this.preState);
this.postState = null;
this.undoRedoName = name;
}
|
public void redo() {
super.redo();
this.object.restoreState(postState);
}
Tells the edited object to apply the state after the edit |
protected void removeRedundantState() {
Vector< Object > uselessKeys = new Vector< Object >();
Enumeration myKeys = preState.keys();
// Locate redundant state
while (myKeys.hasMoreElements()) {
Object myKey = myKeys.nextElement();
if (postState.containsKey(myKey) &&
postState.get(myKey).equals(preState.get(myKey))) {
uselessKeys.addElement(myKey);
}
}
// Remove redundant state
for (int i = uselessKeys.size()-1; i >= 0; i--) {
Object myKey = uselessKeys.elementAt(i);
preState.remove(myKey);
postState.remove(myKey);
}
}
Remove redundant key/values in state hashtables. |
public void undo() {
super.undo();
this.object.restoreState(preState);
}
Tells the edited object to apply the state prior to the edit |