|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package javax.swing.undo; |
|
|
|
import java.util.Enumeration; |
|
import java.util.Hashtable; |
|
import java.util.Vector; |
|
|
|
/** |
|
* <P>StateEdit is a general edit for objects that change state. |
|
* Objects being edited must conform to the StateEditable interface.</P> |
|
* |
|
* <P>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.</P> |
|
* |
|
* A state edit is used as follows: |
|
* <PRE> |
|
* // 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(); |
|
* </PRE> |
|
* |
|
* <P><EM>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!</EM></P> |
|
* |
|
* @see StateEditable |
|
* |
|
* @author Ray Ryan |
|
*/ |
|
|
|
public class StateEdit |
|
extends AbstractUndoableEdit { |
|
|
|
protected static final String RCSID = "$Id: StateEdit.java,v 1.6 1997/10/01 20:05:51 sandipc Exp $"; |
|
|
|
// |
|
// Attributes |
|
// |
|
|
|
|
|
|
|
*/ |
|
protected StateEditable object; |
|
|
|
|
|
|
|
*/ |
|
protected Hashtable<Object,Object> preState; |
|
|
|
|
|
|
|
*/ |
|
protected Hashtable<Object,Object> postState; |
|
|
|
|
|
|
|
*/ |
|
protected String undoRedoName; |
|
|
|
// |
|
// Constructors |
|
// |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public StateEdit(StateEditable anObject) { |
|
super(); |
|
init (anObject,null); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public StateEdit(StateEditable anObject, String name) { |
|
super(); |
|
init (anObject,name); |
|
} |
|
|
|
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; |
|
} |
|
|
|
|
|
// |
|
// Operation |
|
// |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void end() { |
|
this.postState = new Hashtable<Object, Object>(11); |
|
this.object.storeState(this.postState); |
|
this.removeRedundantState(); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void undo() { |
|
super.undo(); |
|
this.object.restoreState(preState); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void redo() { |
|
super.redo(); |
|
this.object.restoreState(postState); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String getPresentationName() { |
|
return this.undoRedoName; |
|
} |
|
|
|
|
|
// |
|
// Internal support |
|
// |
|
|
|
|
|
|
|
*/ |
|
protected void removeRedundantState() { |
|
Vector<Object> uselessKeys = new Vector<Object>(); |
|
Enumeration myKeys = preState.keys(); |
|
|
|
|
|
while (myKeys.hasMoreElements()) { |
|
Object myKey = myKeys.nextElement(); |
|
if (postState.containsKey(myKey) && |
|
postState.get(myKey).equals(preState.get(myKey))) { |
|
uselessKeys.addElement(myKey); |
|
} |
|
} |
|
|
|
|
|
for (int i = uselessKeys.size()-1; i >= 0; i--) { |
|
Object myKey = uselessKeys.elementAt(i); |
|
preState.remove(myKey); |
|
postState.remove(myKey); |
|
} |
|
} |
|
|
|
} // End of class StateEdit |