1 7 8 package javax.swing.undo; 9 10 import java.util.Enumeration ; 11 import java.util.Hashtable ; 12 import java.util.Vector ; 13 14 41 42 public class StateEdit 43 extends AbstractUndoableEdit { 44 45 protected static final String RCSID = "$Id: StateEdit.java,v 1.6 1997/10/01 20:05:51 sandipc Exp $"; 46 47 51 54 protected StateEditable object; 55 56 59 protected Hashtable <Object ,Object > preState; 60 61 64 protected Hashtable <Object ,Object > postState; 65 66 69 protected String undoRedoName; 70 71 75 82 public StateEdit(StateEditable anObject) { 83 super(); 84 init (anObject,null); 85 } 86 87 95 public StateEdit(StateEditable anObject, String name) { 96 super(); 97 init (anObject,name); 98 } 99 100 protected void init (StateEditable anObject, String name) { 101 this.object = anObject; 102 this.preState = new Hashtable (11); 103 this.object.storeState(this.preState); 104 this.postState = null; 105 this.undoRedoName = name; 106 } 107 108 109 113 114 118 public void end() { 119 this.postState = new Hashtable (11); 120 this.object.storeState(this.postState); 121 this.removeRedundantState(); 122 } 123 124 127 public void undo() { 128 super.undo(); 129 this.object.restoreState(preState); 130 } 131 132 135 public void redo() { 136 super.redo(); 137 this.object.restoreState(postState); 138 } 139 140 143 public String getPresentationName() { 144 return this.undoRedoName; 145 } 146 147 148 152 155 protected void removeRedundantState() { 156 Vector uselessKeys = new Vector (); 157 Enumeration myKeys = preState.keys(); 158 159 while (myKeys.hasMoreElements()) { 161 Object myKey = myKeys.nextElement(); 162 if (postState.containsKey(myKey) && 163 postState.get(myKey).equals(preState.get(myKey))) { 164 uselessKeys.addElement(myKey); 165 } 166 } 167 168 for (int i = uselessKeys.size()-1; i >= 0; i--) { 170 Object myKey = uselessKeys.elementAt(i); 171 preState.remove(myKey); 172 postState.remove(myKey); 173 } 174 } 175 176 } | Popular Tags |