FileDocCategorySizeDatePackage
StateEdit.javaAPI DocJava SE 5 API4144Fri Aug 26 14:58:22 BST 2005javax.swing.undo

StateEdit

public class StateEdit extends AbstractUndoableEdit

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!

see
StateEditable
version
1.14 05/05/04
author
Ray Ryan

Fields Summary
protected static final String
RCSID
protected StateEditable
object
The object being edited
protected Hashtable
preState
The state information prior to the edit
protected Hashtable
postState
The state information after the edit
protected String
undoRedoName
The undo/redo presentation name
Constructors Summary
public StateEdit(StateEditable anObject)
Create and return a new StateEdit.

param
anObject The object to watch for changing state
see
StateEdit


    //
    // Constructors
    //
    
                          
       
        super();
	init (anObject,null);
    
public StateEdit(StateEditable anObject, String name)
Create and return a new StateEdit with a presentation name.

param
anObject The object to watch for changing state
param
name The presentation name to be used for this edit
see
StateEdit

	super();
	init (anObject,name);
    
Methods Summary
public voidend()
Gets the post-edit state of the StateEditable object and ends the edit.

	this.postState = new Hashtable(11);
	this.object.storeState(this.postState);
	this.removeRedundantState();
    
public java.lang.StringgetPresentationName()
Gets the presentation name for this edit

	return this.undoRedoName;
    
protected voidinit(javax.swing.undo.StateEditable anObject, java.lang.String name)

	this.object = anObject;
	this.preState = new Hashtable(11);
	this.object.storeState(this.preState);
	this.postState = null;
	this.undoRedoName = name;
    
public voidredo()
Tells the edited object to apply the state after the edit

	super.redo();
	this.object.restoreState(postState);
    
protected voidremoveRedundantState()
Remove redundant key/values in state hashtables.

	Vector uselessKeys = new Vector();
	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);
	}
    
public voidundo()
Tells the edited object to apply the state prior to the edit

	super.undo();
	this.object.restoreState(preState);