KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > golfShop > business > cart > UndoRedoStack


1 /*
2  * Enhydra Java Application Server
3  * The Initial Developer of the Original Code is Lutris Technologies Inc.
4  * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
5  * Inc.
6  * All Rights Reserved.
7  *
8  * The contents of this file are subject to the Enhydra Public License Version
9  * 1.0 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.enhydra.org/software/license/epl.html
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  * License for the specific language governing rights and limitations under the
16  * License.
17  *
18  *
19  */

20
21 package golfShop.business.cart;
22
23 import java.util.Stack JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import golfShop.business.cart.CartItemPairImpl;
27
28
29 /*
30     Only the cart object needs to use this.
31 */

32 public class UndoRedoStack implements java.io.Serializable JavaDoc {
33
34     private Hashtable JavaDoc currentVersion;
35     private Stack JavaDoc undoStack;
36     private Stack JavaDoc redoStack;
37
38     /**
39      * Create a new UndoRedoStack. To avoid uninitialized states, you
40      * must pass in the initial version of the hashtable you want to track.
41      * Every time you change this object, call StoreNewVersion().
42      * If you want to undo, call UndoToPreviousVersion(). This returns
43      * the previous version of the hashtable; you should use this in your
44      * program as the new value of the hashtable. If you want to redo,
45      * call RedoToNewerVersion(). This returns a newer version of the
46      * hashtable.
47      *
48      * @author Andrew John
49      * @param initialVersion The starting version of the hashtable to be
50      * tracked.
51      * @version $Revision: 1.1
52      */

53     protected UndoRedoStack (Hashtable JavaDoc initialVersion) {
54         undoStack = new Stack JavaDoc();
55         redoStack = new Stack JavaDoc();
56         currentVersion = copyHT(initialVersion);
57     }
58
59     /**
60      * After you make a change to your object, call this function to
61      * remember the current version. The previous version is saved for
62      * so that you can do an undo.
63      *
64      * @param newVersion The current version of the object to
65      * be tracked.
66      */

67     protected void storeNewVersion (Hashtable JavaDoc newVersion) {
68         // Save the current version for later.
69
undoStack.push(currentVersion);
70         // Throw out the whole redo stack.
71
redoStack = new Stack JavaDoc();
72         // This is now the current version.
73
currentVersion = copyHT(newVersion);
74     }
75
76     protected boolean canUndo() {
77         return !undoStack.empty();
78     }
79
80     protected Hashtable JavaDoc undoToPreviousVersion () {
81         if (undoStack.empty())
82             // Error condition.
83
return copyHT(currentVersion);
84         else {
85             // Save the current version in case they redo.
86
redoStack.push(currentVersion);
87             // Go back to the previous version.
88
currentVersion = (Hashtable JavaDoc) undoStack.pop();
89             return copyHT(currentVersion);
90         }
91     }
92
93     protected boolean canRedo() {
94         return !redoStack.empty();
95     }
96
97     protected Hashtable JavaDoc redoToNewerVersion () {
98         if (redoStack.empty())
99             return copyHT(currentVersion);
100         else {
101             undoStack.push(currentVersion);
102             currentVersion = (Hashtable JavaDoc) redoStack.pop();
103             return copyHT(currentVersion);
104         }
105     }
106
107     protected Object JavaDoc getCurrentVersion () {
108         return copyHT(currentVersion);
109     }
110
111     /*
112         Make a copy of the hashtable. If you call clone() on a hashtable
113         it gives you a new hashtable, but it contains pointers to the
114         same key objects and value objects. If you change the
115         value objects, they will be changed in both coppies of the
116         hashtable. This is not what we want for undo functionality.
117         So this function creates a new hashtable and populates it
118         with clones of the value objects. The key objects are not cloned.
119         
120         All the versions stored in this object must be insulated from
121         being modified by the rest of the program. So make coppies of the
122         input, and only give out coppies.
123     */

124     private Hashtable JavaDoc copyHT(Hashtable JavaDoc ht) {
125         Hashtable JavaDoc newHT = new Hashtable JavaDoc();
126         Enumeration JavaDoc e = ht.keys();
127         while (e.hasMoreElements()) {
128             Object JavaDoc key = e.nextElement();
129             CartItemPairImpl value = (CartItemPairImpl) ht.get(key);
130             CartItemPairImpl valueCopy = value.copy();
131             newHT.put(key, valueCopy);
132         }
133         return newHT;
134     }
135
136     // For debugging only.
137
private void printHT(Hashtable JavaDoc ht) {
138         Enumeration JavaDoc e = ht.keys();
139         System.out.println("Hashtable contents:");
140         while (e.hasMoreElements()) {
141             Object JavaDoc key = e.nextElement();
142             CartItemPairImpl value = (CartItemPairImpl) ht.get(key);
143             System.out.println("ObjectId " + value.getItem().getObjectId() +
144                                             " x " + value.getQuantity());
145         }
146         System.out.println("\n");
147     }
148 }
149
150         
151
152
Popular Tags