1 15 16 package swingwtx.swing.undo; 17 18 import java.util.*; 19 20 26 public class StateEdit extends AbstractUndoableEdit 27 { 28 protected StateEditable object; 29 30 31 protected Hashtable preState = new Hashtable(); 32 protected Hashtable postState = new Hashtable(); 33 34 protected String undoRedoName; 35 public String getPresentationName() { return undoRedoName; } 36 37 public StateEdit(StateEditable stateEditable) 38 { 39 super(); 40 init(stateEditable, null); 41 } 42 43 public StateEdit(StateEditable stateEditable, String undoRedoName) 44 { 45 super(); 46 init(stateEditable, undoRedoName); 47 } 48 49 protected void init(StateEditable stateEditable, String undoRedoName) 50 { 51 object = stateEditable; 52 this.undoRedoName = undoRedoName; 53 object.storeState(preState); 54 } 55 56 public void undo() 57 { 58 super.undo(); 59 60 object.restoreState(preState); 62 } 63 64 public void redo() 65 { 66 super.redo(); 67 68 object.restoreState(postState); 70 } 71 72 public void end() 73 { 74 object.storeState(postState); 75 removeRedundantState(); 76 } 77 78 79 protected void removeRedundantState() 80 { 81 ArrayList duplicates = new ArrayList(); 82 83 84 Iterator iterator = preState.keySet().iterator(); 85 while (iterator.hasNext()) 86 { 87 Object key = iterator.next(); 88 if (postState.containsKey(key)) 89 { 90 if (preState.get(key).equals(postState.get(key))) 91 { 92 duplicates.add(key); 93 } 94 } 95 } 96 97 98 iterator = duplicates.iterator(); 99 while (iterator.hasNext()) 100 { 101 Object key = iterator.next(); 102 postState.remove(key); 103 preState.remove(key); 104 } 105 } 106 } 107 | Popular Tags |