KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > examples > patterns > Calculator1


1
2 package Jt.examples.patterns;
3
4 import Jt.*;
5 import java.io.*;
6 import java.util.*;
7 import Jt.xml.*;
8 import java.beans.*;
9
10 /**
11  * Calculator implementation based on Command and Memento.
12  * Creating a subclass of JtCommand and implementing the processMessage
13  * method is basically all that is needed. Command requests are logged via
14  * the inherited LogMessage method.
15  */

16
17 public class Calculator1 extends JtCommand {
18
19   transient Object JavaDoc state = null; // Object state
20
private int total = 0; // Calculator Total
21
private JtMemento memento = null; // Contains the state of the object
22
// needed to implement the undo operation
23

24
25   public Calculator1 () {
26   }
27
28   public void setTotal (int total) {
29     this.total = total;
30   }
31
32   public int getTotal () {
33     return (total);
34   }
35
36   private void saveState () {
37
38     JtMessage msg = new JtMessage ("JtENCODE_OBJECT");
39
40     JtObject tmp = new JtObject();
41
42     msg.setMsgContent (this);
43
44
45     state = tmp.processMessage (msg);
46     
47     if (state == null) {
48       handleWarning ("saveSate: Unable to save the current state"); //check
49
return;
50     }
51
52     if (memento == null)
53       memento = new JtMemento ();
54
55     memento.setState (state);
56
57     handleTrace ("saveState: saving state ...\n" + state);
58
59   }
60
61
62   private void restoreState () {
63
64     JtMessage msg = new JtMessage ("JtDECODE_OBJECT");
65     Calculator1 tmp;
66     JtObject copier = new JtObject ();
67
68
69     if (memento == null) {
70       handleWarning ("restoreState: nothing to undo ....");
71       return;
72     }
73
74     state = memento.getState ();
75     memento = null;
76  
77     if (state == null) {
78       handleWarning ("restoreState: nothing to undo ....");
79       return;
80     }
81
82     handleTrace ("restoreState (state) ...\n" + state);
83
84     msg.setMsgContent (state);
85     //tmp = (Calculator1) xmlHelper.processMessage (msg);
86
tmp = (Calculator1) copier.processMessage (msg);
87
88     msg = new JtMessage ("JtENCODE_OBJECT");
89     msg.setMsgContent (tmp);
90
91     //System.out.println ("...\n" + copier.processMessage (msg));
92
//System.out.println ("src...\n" + tmp.getMessageLog ());
93

94             
95     msg = new JtMessage ("JtCOPY_OBJECT");
96
97     if (tmp == null) {
98       handleTrace ("restoreState failed: unable to convert object from XML");
99       return;
100     }
101     
102     msg.setMsgContent (tmp);
103     msg.setMsgData (this);
104     sendMessage (copier, msg);
105
106     msg = new JtMessage ("JtENCODE_OBJECT");
107     msg.setMsgContent (this);
108
109
110   }
111
112
113
114   /**
115     * Process object messages (Command requests).
116     * <ul>
117     * <li>JtADD - Add a number (msgContent) to the total
118     * </ul>
119     */

120
121   public Object JavaDoc processMessage (Object JavaDoc message) {
122
123    String JavaDoc msgid = null;
124    JtMessage msg = (JtMessage) message;
125    Object JavaDoc content;
126
127      if (msg == null)
128     return null;
129
130      // Retrieve Message ID and content
131

132      msgid = (String JavaDoc) msg.getMsgId ();
133
134      if (msgid == null)
135     return null;
136
137      content = msg.getMsgContent();
138
139      // Add a number to the total
140

141      if (msgid.equals ("ADD")) {
142
143
144         saveState ();
145
146         // Log the message
147
logMessage (msg);
148
149         total += ((Integer JavaDoc) content).intValue ();
150
151          
152
153         return (new Integer JavaDoc (total));
154      }
155
156
157      if (msgid.equals ("UNDO")) {
158
159  
160
161         restoreState ();
162         
163         // Log the message
164
// logMessage (msg);
165
return (new Integer JavaDoc (total));
166      }
167
168      // JtRemove message (Remove Object)
169

170      if (msgid.equals ("JtREMOVE")) {
171        return (null);
172      }
173
174      handleError ("Calculator1.processMessage: invalid message id:" + msgid);
175      return (null);
176   }
177
178
179  
180   /**
181    * Calculator implementation (main)
182    */

183
184   public static void main(String JavaDoc[] args) {
185
186     JtObject main = new JtFactory ();
187     JtMessage msg, msg1;
188     Object JavaDoc total;
189     JtKeyboard keyboard;
190     String JavaDoc input;
191     int num = 0;
192     Calculator1 calculator;
193
194     // Create calculator and Keyboard
195

196     calculator = (Calculator1) main.createObject ("Jt.examples.patterns.Calculator1", "calculator");
197     keyboard = (JtKeyboard) main.createObject ("Jt.JtKeyboard", "keyboard");
198
199     System.out.println
200 ("Enter a number to be added to the total or 'U' to undo the last operation (or <CR> to exit):");
201
202     for (;;) {
203
204       // Read input (number) from the keyboard (JtACTIVATE message)
205

206       input = (String JavaDoc) main.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
207
208       if (input != null)
209         input = input.trim ();
210
211
212       if ("".equals (input))
213         break;
214
215
216       if ("U".equals (input) || "u".equals (input)) {
217         msg = new JtMessage ("UNDO");
218         main.sendMessage ("calculator", msg);
219         System.out.println ("Total:" + main.getValue ("calculator", "total"));
220         continue;
221       }
222
223
224       try {
225         num = Integer.parseInt (input);
226       } catch (Exception JavaDoc e) {
227
228         System.err.println (e);
229       }
230
231
232       // Add the number to the Total (ADD Message)
233
msg = new JtMessage ("ADD");
234       msg.setMsgContent (new Integer JavaDoc (num));
235       total = main.sendMessage ("calculator", msg);
236
237       System.out.println ("Total:" + total);
238
239     }
240
241     // Print the log (list of requests executed by the Calculator)
242
// Use main to convert the log information (messageLog)
243
// into XML.
244

245     msg = new JtMessage ("JtENCODE_OBJECT");
246     msg.setMsgContent (main.getValue ("calculator", "messageLog"));
247
248     System.out.println ("Log:\n" +
249       main.sendMessage (main, msg));
250
251
252     // Remove object
253

254     main.removeObject ("calculator");
255
256   }
257
258 }
259
260
261
262
Popular Tags