KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > undo > StateEdit


1 /*
2    SwingWT
3    Copyright(c)2003-2004 Daniel Naab
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: dannaab@users.sourceforge.net
9
10    $Log: StateEdit.java,v $
11    Revision 1.2 2004/04/16 22:45:50 dannaab
12    Add copyright msg
13
14 */

15
16 package swingwtx.swing.undo;
17
18 import java.util.*;
19
20 /**
21  * StateEdit
22  *
23  * @author Naab
24  * @version %I%, %G%
25  */

26 public class StateEdit extends AbstractUndoableEdit
27 {
28     protected StateEditable object;
29
30     /** Track before/after values */
31     protected Hashtable preState = new Hashtable();
32     protected Hashtable postState = new Hashtable();
33
34     protected String JavaDoc undoRedoName;
35     public String JavaDoc getPresentationName() { return undoRedoName; }
36
37     public StateEdit(StateEditable stateEditable)
38     {
39         super();
40         init(stateEditable, null);
41     }
42
43     public StateEdit(StateEditable stateEditable, String JavaDoc undoRedoName)
44     {
45         super();
46         init(stateEditable, undoRedoName);
47     }
48
49     protected void init(StateEditable stateEditable, String JavaDoc 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         // Undo to state saved off on init
61
object.restoreState(preState);
62     }
63
64     public void redo()
65     {
66         super.redo();
67
68         // Redo to state saved off on initial completion
69
object.restoreState(postState);
70     }
71
72     public void end()
73     {
74         object.storeState(postState);
75         removeRedundantState();
76     }
77
78     /** Called by end() to clean up duplicate and unnecessary key/values */
79     protected void removeRedundantState()
80     {
81         ArrayList duplicates = new ArrayList();
82
83         /** Find duplicates - equal values in both preState and postState */
84         Iterator iterator = preState.keySet().iterator();
85         while (iterator.hasNext())
86         {
87             Object JavaDoc 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         /** Remove each duplicate from the postState */
98         iterator = duplicates.iterator();
99         while (iterator.hasNext())
100         {
101             Object JavaDoc key = iterator.next();
102             postState.remove(key);
103             preState.remove(key);
104         }
105     }
106 }
107
Popular Tags