Source code: com/globalretailtech/pos/context/PosEventStack.java
1 /*
2 * Copyright (C) 2001 Global Retail Technology, LLC
3 * <http://www.globalretailtech.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.globalretailtech.pos.context;
21
22 import com.globalretailtech.util.Log;
23 import com.globalretailtech.util.Loader;
24 import com.globalretailtech.pos.events.*;
25 import com.globalretailtech.data.Dialog;
26 import com.globalretailtech.data.DialogEvent;
27
28 /**
29 * Implements a stack of POS events by extending PosStack. Manages
30 * the current and queued event for a PosContext. This class included the more
31 * complex operations for loading and executing events/dialogs
32 * (nextEvent () and loadDialog ()).
33 *
34 * @author Quentin Olson
35 */
36 public class PosEventStack extends PosStack
37 {
38
39 /**
40 * Simple constructor calls super ().
41 */
42 public PosEventStack ()
43 {
44 super ();
45 }
46
47 /**
48 * Return the current event type.
49 */
50 public PosEvent event ()
51 {
52 PosEvent event = (PosEvent) super.peek ();
53 return event;
54 }
55
56 /**
57 * Pop the pending, push it on the processed and return it.
58 */
59 /**
60 * Remove the top of the pending stack and return it. Invoke
61 * the push operation to move it to the pending stack. If there
62 * is only one event on the stack leave it and just peek.
63 */
64 public PosEvent popEvent ()
65 {
66
67 Object o;
68
69 if (pendingSize () > 1)
70 {
71 o = popPending ();
72 }
73 else
74 {
75 o = peek ();
76 }
77 return (PosEvent) o;
78 }
79
80 /**
81 * Add an object to the pending stack.
82 */
83 public void pushEvent (PosEvent value)
84 {
85 pushPending (value);
86 }
87
88 /**
89 * Add an object to the processed stack.
90 */
91 public void pushProcessed (Object value)
92 {
93 pushProcessed (value);
94 }
95
96 /**
97 * Set the fist event in the pending stack
98 * to the object.
99 */
100 public void setEvent (PosEvent event)
101 {
102
103 super.clear ();
104 pushPending (event);
105 }
106
107 /**
108 * Set the fist event in the pending stack
109 * to the object.
110 */
111 public void insertElementAt (PosEvent event, int pos)
112 {
113 pendingInsertElementAt (event, pos);
114 }
115
116
117 /**
118 * The size of the pending stack.
119 */
120 public int pendingSize ()
121 {
122 return super.pendingSize ();
123 }
124
125 /**
126 * The size of the processed stack.
127 */
128 public int processedSize ()
129 {
130 return super.processedSize ();
131 }
132
133 /**
134 * Removes all events from the proccesed and puts them back
135 * on the pending stack.
136 */
137 public void restore ()
138 {
139 super.restore ();
140 }
141
142 /**
143 * Removes all events from the pending stack.
144 */
145 public void clearPending ()
146 {
147 super.clearPending ();
148 }
149
150 /**
151 * Removes all events from the processed stack.
152 */
153 public void clearProcessed ()
154 {
155 super.clearProcessed ();
156 }
157
158 /**
159 * Pops and executes the next event on the stack.
160 */
161 public void nextEvent ()
162 {
163
164 PosEvent event = null;
165
166 // Developers: uncomment this if you want to see what's on the stack
167
168 // dump ();
169 // Log.stackTrace ("nextEvent ()");
170
171 try
172 {
173
174 // if this is a dialog pop it's state stack and invoke
175 // the current event
176
177 if (event ().isDialog ())
178 {
179
180 PosDialogEvent dialog = (PosDialogEvent) event ();
181
182 // if there are state events pop
183
184 if (dialog.states ().pendingSize () > 0)
185 {
186 event = event ();
187 }
188 else
189 {
190 // if there are no state events in the dialog
191 // pop the entire event, and engage the next one
192 popEvent ();
193 event = popEvent ();
194 }
195 }
196 else
197 {
198 event = popEvent ();
199 }
200
201 event.engage (0);
202 }
203 catch (PosException pe)
204 {
205 Log.warning ("Failed in nextEvent ()");
206 Log.warning (pe.toString ());
207 }
208 }
209
210 /**
211 * Finds a dialog set in PosConfig and loads it.
212 */
213 public void loadDialog (String dialog_name, PosContext context)
214 {
215
216 // get the dialog from pos config
217
218 Dialog dialog = (Dialog) context.config ().dialogs ().get (dialog_name);
219 PosEvent event = null;
220
221 if (dialog != null)
222 { // found!
223
224 int insertPos = pendingSize () == 1 ? 1 : 0; // leave the first one on the stack
225
226 for (int i=0;i<dialog.dialogEvents().size ();i++)
227 { // load each event
228
229 DialogEvent dialogEvent = (DialogEvent) dialog.dialogEvents ().elementAt (i);
230
231 if (dialogEvent.eventClass () != null)
232 {
233
234 try
235 { // try to load the event class
236
237 event = (PosEvent) Loader.getInstance (dialogEvent.eventClass ());
238
239 if (event.isDialog ())
240 {
241 ((PosDialogEvent) event).pushState (dialogEvent.state ());
242 }
243 }
244 catch (java.lang.ClassNotFoundException e)
245 {
246 Log.warning ("Class not found : " + dialogEvent.eventClass ());
247 return;
248 }
249 catch (java.lang.Exception e)
250 {
251 Log.warning ("Class load error : " + e.toString () + "/" + dialogEvent.eventClass ());
252 return;
253 }
254
255 // push the event on the stack if found and is required or enabled.
256 if (dialogEvent.eventRequired () || dialogEvent.eventEnabled ())
257 {
258
259 if ( (event != null) && (dialogEvent.eventRequired () || dialogEvent.eventEnabled ()) )
260 {
261
262 event.setContext (context); // it was created using the default constructor, so set up the context
263
264 // This tells it what to do. If a non-zero event type was specified in the dialog
265 // definition, use it, else use that set by the default constructor.
266
267 pushEvent (event);
268 }
269 }
270 }
271 else
272 { // use the event on the top of the stack.
273
274 event = event ();
275 if (event.isDialog () && ((dialogEvent.eventRequired () || dialogEvent.eventEnabled ())) )
276 {
277 ((PosDialogEvent) event).states ().pushState (dialogEvent.state ());
278 }
279 }
280 }
281 }
282 else
283 {
284 Log.warning ("Dialog not found " + dialog_name);
285 }
286 }
287 }
288
289 /**
290 * $Log: PosEventStack.java,v $
291 * Revision 1.1.1.1 2001/08/13 22:18:09 qolson
292 * Initial Checkin 0.2-2
293 *
294 *
295 */