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 16 17 public class Calculator1 extends JtCommand { 18 19 transient Object state = null; private int total = 0; private JtMemento memento = null; 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"); 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) copier.processMessage (msg); 87 88 msg = new JtMessage ("JtENCODE_OBJECT"); 89 msg.setMsgContent (tmp); 90 91 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 120 121 public Object processMessage (Object message) { 122 123 String msgid = null; 124 JtMessage msg = (JtMessage) message; 125 Object content; 126 127 if (msg == null) 128 return null; 129 130 132 msgid = (String ) msg.getMsgId (); 133 134 if (msgid == null) 135 return null; 136 137 content = msg.getMsgContent(); 138 139 141 if (msgid.equals ("ADD")) { 142 143 144 saveState (); 145 146 logMessage (msg); 148 149 total += ((Integer ) content).intValue (); 150 151 152 153 return (new Integer (total)); 154 } 155 156 157 if (msgid.equals ("UNDO")) { 158 159 160 161 restoreState (); 162 163 return (new Integer (total)); 166 } 167 168 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 183 184 public static void main(String [] args) { 185 186 JtObject main = new JtFactory (); 187 JtMessage msg, msg1; 188 Object total; 189 JtKeyboard keyboard; 190 String input; 191 int num = 0; 192 Calculator1 calculator; 193 194 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 206 input = (String ) 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 e) { 227 228 System.err.println (e); 229 } 230 231 232 msg = new JtMessage ("ADD"); 234 msg.setMsgContent (new Integer (num)); 235 total = main.sendMessage ("calculator", msg); 236 237 System.out.println ("Total:" + total); 238 239 } 240 241 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 254 main.removeObject ("calculator"); 255 256 } 257 258 } 259 260 261 262 | Popular Tags |