KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)StateEdit.java 1.14 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.undo;
9
10 import java.util.Enumeration JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 /**
15  * <P>StateEdit is a general edit for objects that change state.
16  * Objects being edited must conform to the StateEditable interface.</P>
17  *
18  * <P>This edit class works by asking an object to store it's state in
19  * Hashtables before and after editing occurs. Upon undo or redo the
20  * object is told to restore it's state from these Hashtables.</P>
21  *
22  * A state edit is used as follows:
23  * <PRE>
24  * // Create the edit during the "before" state of the object
25  * StateEdit newEdit = new StateEdit(myObject);
26  * // Modify the object
27  * myObject.someStateModifyingMethod();
28  * // "end" the edit when you are done modifying the object
29  * newEdit.end();
30  * </PRE>
31  *
32  * <P><EM>Note that when a StateEdit ends, it removes redundant state from
33  * the Hashtables - A state Hashtable is not guaranteed to contain all
34  * keys/values placed into it when the state is stored!</EM></P>
35  *
36  * @see StateEditable
37  *
38  * @version 1.14 05/05/04
39  * @author Ray Ryan
40  */

41
42 public class StateEdit
43     extends AbstractUndoableEdit JavaDoc {
44
45     protected static final String JavaDoc RCSID = "$Id: StateEdit.java,v 1.6 1997/10/01 20:05:51 sandipc Exp $";
46
47     //
48
// Attributes
49
//
50

51     /**
52      * The object being edited
53      */

54     protected StateEditable JavaDoc object;
55
56     /**
57      * The state information prior to the edit
58      */

59     protected Hashtable JavaDoc<Object JavaDoc,Object JavaDoc> preState;
60
61     /**
62      * The state information after the edit
63      */

64     protected Hashtable JavaDoc<Object JavaDoc,Object JavaDoc> postState;
65
66     /**
67      * The undo/redo presentation name
68      */

69     protected String JavaDoc undoRedoName;
70
71     //
72
// Constructors
73
//
74

75     /**
76      * Create and return a new StateEdit.
77      *
78      * @param anObject The object to watch for changing state
79      *
80      * @see StateEdit
81      */

82     public StateEdit(StateEditable JavaDoc anObject) {
83         super();
84     init (anObject,null);
85     }
86
87     /**
88      * Create and return a new StateEdit with a presentation name.
89      *
90      * @param anObject The object to watch for changing state
91      * @param name The presentation name to be used for this edit
92      *
93      * @see StateEdit
94      */

95     public StateEdit(StateEditable JavaDoc anObject, String JavaDoc name) {
96     super();
97     init (anObject,name);
98     }
99
100     protected void init (StateEditable JavaDoc anObject, String JavaDoc name) {
101     this.object = anObject;
102     this.preState = new Hashtable JavaDoc(11);
103     this.object.storeState(this.preState);
104     this.postState = null;
105     this.undoRedoName = name;
106     }
107
108
109     //
110
// Operation
111
//
112

113
114     /**
115      * Gets the post-edit state of the StateEditable object and
116      * ends the edit.
117      */

118     public void end() {
119     this.postState = new Hashtable JavaDoc(11);
120     this.object.storeState(this.postState);
121     this.removeRedundantState();
122     }
123
124     /**
125      * Tells the edited object to apply the state prior to the edit
126      */

127     public void undo() {
128     super.undo();
129     this.object.restoreState(preState);
130     }
131
132     /**
133      * Tells the edited object to apply the state after the edit
134      */

135     public void redo() {
136     super.redo();
137     this.object.restoreState(postState);
138     }
139
140     /**
141      * Gets the presentation name for this edit
142      */

143     public String JavaDoc getPresentationName() {
144     return this.undoRedoName;
145     }
146
147
148     //
149
// Internal support
150
//
151

152     /**
153      * Remove redundant key/values in state hashtables.
154      */

155     protected void removeRedundantState() {
156     Vector JavaDoc uselessKeys = new Vector JavaDoc();
157     Enumeration JavaDoc myKeys = preState.keys();
158
159     // Locate redundant state
160
while (myKeys.hasMoreElements()) {
161         Object JavaDoc myKey = myKeys.nextElement();
162         if (postState.containsKey(myKey) &&
163         postState.get(myKey).equals(preState.get(myKey))) {
164         uselessKeys.addElement(myKey);
165         }
166     }
167
168     // Remove redundant state
169
for (int i = uselessKeys.size()-1; i >= 0; i--) {
170         Object JavaDoc myKey = uselessKeys.elementAt(i);
171         preState.remove(myKey);
172         postState.remove(myKey);
173     }
174     }
175
176 } // End of class StateEdit
177
Popular Tags