KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > screen > PosScreen


1 /*
2  * $Id: PosScreen.java 6465 2006-01-04 06:42:36Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.pos.screen;
26
27
28 import java.awt.AWTEvent JavaDoc;
29 import java.awt.Frame JavaDoc;
30 import java.awt.Window JavaDoc;
31 import java.awt.event.FocusEvent JavaDoc;
32 import java.awt.event.FocusListener JavaDoc;
33
34 import java.util.*;
35
36 import net.xoetrope.builder.NavigationHelper;
37 import net.xoetrope.xui.XPage;
38 import net.xoetrope.xui.XProjectManager;
39
40 import org.ofbiz.base.splash.SplashLoader;
41 import org.ofbiz.base.util.Debug;
42 import org.ofbiz.base.util.UtilFormatOut;
43 import org.ofbiz.base.util.UtilProperties;
44 import org.ofbiz.guiapp.xui.XuiContainer;
45 import org.ofbiz.guiapp.xui.XuiSession;
46 import org.ofbiz.pos.PosTransaction;
47 import org.ofbiz.pos.adaptor.KeyboardAdaptor;
48 import org.ofbiz.pos.component.Input;
49 import org.ofbiz.pos.component.Journal;
50 import org.ofbiz.pos.component.Operator;
51 import org.ofbiz.pos.component.Output;
52 import org.ofbiz.pos.component.PosButton;
53 import org.ofbiz.pos.device.DeviceLoader;
54
55 /**
56  *
57  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
58  * @version $Rev: 6465 $
59  * @since 3.1
60  */

61 public class PosScreen extends NavigationHelper implements Runnable JavaDoc, DialogCallback, FocusListener JavaDoc {
62
63     public static final String JavaDoc module = PosScreen.class.getName();
64     public static final Frame JavaDoc appFrame = XProjectManager.getCurrentProject().getAppFrame();
65     public static final Window JavaDoc appWin = XProjectManager.getCurrentProject().getAppWindow();
66     public static final String JavaDoc BUTTON_ACTION_METHOD = "buttonPressed";
67     public static final long MAX_INACTIVITY = 1800000;
68     public static PosScreen currentScreen;
69
70     protected static boolean monitorRunning = false;
71     protected static boolean firstInit = false;
72     protected static long lastActivity = 0;
73     protected static Thread JavaDoc activityMonitor = null;
74
75     protected ClassLoader JavaDoc classLoader = null;
76     protected XuiSession session = null;
77     protected Output output = null;
78     protected Input input = null;
79     protected Journal journal = null;
80     protected Operator operator = null;
81     protected PosButton buttons = null;
82     protected String JavaDoc scrLocation = null;
83     protected boolean isLocked = false;
84     protected boolean inDialog = false;
85
86     private Locale defaultLocale = Locale.getDefault();
87     
88     public PosScreen() {
89         super();
90         this.classLoader = Thread.currentThread().getContextClassLoader();
91         this.addFocusListener(this);
92     }
93
94     public void pageCreated() {
95         super.pageCreated();
96
97         // initial settings
98
this.setEnabled(false);
99         this.setVisible(false);
100
101         // setup the shared components
102
this.session = XuiContainer.getSession();
103         this.output = new Output(this);
104         this.input = new Input(this);
105         this.journal = new Journal(this);
106         this.operator = new Operator(this);
107         this.setLastActivity(System.currentTimeMillis());
108
109         if (!firstInit) {
110             firstInit = true;
111             
112             // pre-load a few screens
113
XProjectManager.getPageManager().loadPage(this.getScreenLocation() + "/paypanel");
114             XProjectManager.getPageManager().loadPage(this.getScreenLocation() + "/mgrpanel");
115             XProjectManager.getPageManager().loadPage(this.getScreenLocation() + "/promopanel");
116
117             // start the shared monitor thread
118
if (activityMonitor == null) {
119                 monitorRunning = true;
120                 activityMonitor = new Thread JavaDoc(this);
121                 activityMonitor.setDaemon(false);
122                 activityMonitor.start();
123             }
124
125             // configure the frame/window listeners
126
KeyboardAdaptor.attachComponents(appFrame, false);
127             KeyboardAdaptor.attachComponents(appWin, false);
128             appFrame.addFocusListener(this);
129             appWin.addFocusListener(this);
130
131             // close the splash screen
132
SplashLoader.close();
133         }
134
135         // buttons are different per screen
136
this.buttons = new PosButton(this);
137
138         // make sure all components have the keyboard set
139
KeyboardAdaptor.attachComponents(this);
140     }
141
142     public void pageActivated() {
143         super.pageActivated();
144
145         this.setLastActivity(System.currentTimeMillis());
146
147         if (session.getUserLogin() == null) {
148             this.setLock(true);
149         } else {
150             this.setLock(isLocked);
151         }
152
153         currentScreen = this;
154         this.refresh();
155     }
156
157     public void pageDeactivated() {
158         super.pageDeactivated();
159
160         if (Debug.verboseOn()) {
161             this.logInfo();
162         }
163     }
164
165     public void logInfo() {
166         Debug.log("App Frame :", module);
167         Debug.log("name - " + appFrame.getName(), module);
168         Debug.log("title - " + appFrame.getTitle(), module);
169         Debug.log("active - " + appFrame.isActive(), module);
170         Debug.log("enabled - " + appFrame.isEnabled(), module);
171         Debug.log("visible - " + appFrame.isVisible(), module);
172         Debug.log("showing - " + appFrame.isShowing(), module);
173         Debug.log("opaque - " + appFrame.isOpaque(), module);
174         Debug.log("focusable - " + appFrame.isFocusable(), module);
175         Debug.log("focused - " + appFrame.isFocused(), module);
176         Debug.log("hasFocus - " + appFrame.hasFocus(), module);
177
178         Debug.log("", module);
179         Debug.log("App Window :", module);
180         Debug.log("name - " + appWin.getName(), module);
181         Debug.log("active - " + appWin.isActive(), module);
182         Debug.log("enabled - " + appWin.isEnabled(), module);
183         Debug.log("visible - " + appWin.isVisible(), module);
184         Debug.log("showing - " + appWin.isShowing(), module);
185         Debug.log("opaque - " + appWin.isOpaque(), module);
186         Debug.log("focusable - " + appWin.isFocusable(), module);
187         Debug.log("focused - " + appWin.isFocused(), module);
188         Debug.log("hasFocus - " + appWin.hasFocus(), module);
189
190         Debug.log("", module);
191
192         Debug.log("POS Screen :", module);
193         Debug.log("name - " + this.getName(), module);
194         Debug.log("enabled - " + this.isEnabled(), module);
195         Debug.log("visible - " + this.isVisible(), module);
196         Debug.log("showing - " + this.isShowing(), module);
197         Debug.log("opaque - " + this.isOpaque(), module);
198         Debug.log("focusable - " + this.isFocusable(), module);
199         Debug.log("focused - " + this.hasFocus(), module);
200     }
201
202     public void refresh() {
203         this.refresh(true);
204     }
205
206     public void refresh(boolean updateOutput) {
207         PosTransaction trans = PosTransaction.getCurrentTx(this.getSession());
208         if (trans == null) {
209             updateOutput = false;
210         }
211
212         appFrame.requestFocus();
213         this.lockScreenButton(this);
214         //this.requestFocus();
215

216         if (!isLocked) {
217             this.setEnabled(true);
218             this.setVisible(true);
219             journal.refresh(this);
220             input.clearInput();
221             operator.refresh();
222             if (updateOutput) {
223                 if (input.isFunctionSet("PAID")) {
224                     output.print(UtilProperties.getMessage("pos","ULOGIN",defaultLocale)
225                             + UtilFormatOut.formatPrice(trans.getTotalDue() * -1));
226                 } else if (input.isFunctionSet("TOTAL")) {
227                     if (trans.getTotalDue() > 0) {
228                         output.print(UtilProperties.getMessage("pos","TOTALD",defaultLocale) + " " + UtilFormatOut.formatPrice(trans.getTotalDue()));
229                     } else {
230                         output.print(UtilProperties.getMessage("pos","PAYFIN",defaultLocale));
231                     }
232                 } else {
233                     if (PosTransaction.getCurrentTx(session).isOpen()) {
234                         output.print(UtilProperties.getMessage("pos","ISOPEN",defaultLocale));
235                     } else {
236                         output.print(UtilProperties.getMessage("pos","ISCLOSED",defaultLocale));
237                     }
238                 }
239             }
240             //journal.focus();
241
} else {
242             output.print(UtilProperties.getMessage("pos","ULOGIN",defaultLocale));
243             //input.focus();
244
}
245
246         this.repaint();
247         //this.logInfo();
248
}
249
250     public boolean isLocked() {
251         return isLocked;
252     }
253
254     public void setLock(boolean lock) {
255         this.buttons.setLock(lock);
256         this.input.setLock(lock);
257         this.output.setLock(lock);
258         this.journal.setLock(lock);
259         this.operator.setLock(lock);
260         this.isLocked = lock;
261         this.input.setFunction("LOGIN");
262         DeviceLoader.enable(!lock);
263     }
264
265     public XuiSession getSession() {
266         return this.session;
267     }
268
269     public Input getInput() {
270         return this.input;
271     }
272
273     public Output getOutput() {
274         return this.output;
275     }
276
277     public Journal getJournal() {
278         return this.journal;
279     }
280
281     public PosButton getButtons() {
282         return this.buttons;
283     }
284
285     public void setLastActivity(long l) {
286         lastActivity = l;
287     }
288
289     public ClassLoader JavaDoc getClassLoader() {
290         return classLoader;
291     }
292
293     // generic button XUI event calls into PosButton to lookup the external reference
294
public synchronized void buttonPressed() {
295         this.setLastActivity(System.currentTimeMillis());
296         buttons.buttonPressed(this, (AWTEvent JavaDoc)this.getCurrentEvent());
297         journal.focus();
298     }
299
300     // generic page display methods - extends those in XPage
301
public PosScreen showPage(String JavaDoc pageName) {
302         return this.showPage(pageName, true);
303     }
304
305     public PosScreen showPage(String JavaDoc pageName, boolean refresh) {
306         if (pageName.startsWith("/")) {
307             pageName = pageName.substring(1);
308         }
309         XPage newPage = (XPage)XProjectManager.getPageManager().showPage(this.getScreenLocation() + "/" + pageName);
310         if (newPage instanceof PosScreen) {
311             if (refresh) ((PosScreen) newPage).refresh();
312             return (PosScreen) newPage;
313         }
314         return null;
315     }
316
317     public void lockScreenButton(PosScreen pos) {
318         if ((this.getScreenLocation() + "/pospanel").equals(pos.getName())) {
319             pos.getButtons().setLock("menuMain", true);
320         } else if ((this.getScreenLocation() + "/mgrpanel").equals(pos.getName())) {
321             pos.getButtons().setLock("menuMgr", true);
322         } else if ((this.getScreenLocation() + "/paypanel").equals(pos.getName())) {
323             pos.getButtons().setLock("menuPay", true);
324         } else if ((this.getScreenLocation() + "/promopanel").equals(pos.getName())) {
325             pos.getButtons().setLock("menuPromo", true);
326         }
327     }
328
329     public PosDialog showDialog(String JavaDoc pageName) {
330         return showDialog(pageName, this, null);
331     }
332
333     public PosDialog showDialog(String JavaDoc pageName, String JavaDoc text) {
334         return showDialog(pageName, this, text);
335     }
336
337     public PosDialog showDialog(String JavaDoc pageName, DialogCallback cb) {
338         return showDialog(pageName, cb, null);
339     }
340
341     public PosDialog showDialog(String JavaDoc pageName, DialogCallback cb, String JavaDoc text) {
342         if (pageName.startsWith("/")) {
343             pageName = pageName.substring(1);
344         }
345         XPage dialogPage = (XPage)XProjectManager.getPageManager().loadPage(this.getScreenLocation() + "/" + pageName);
346         PosDialog dialog = PosDialog.getInstance(dialogPage, true, 0);
347         dialog.showDialog(this, cb, text);
348         return dialog;
349     }
350
351     // PosDialog Callback method
352
public void receiveDialogCb(PosDialog dialog) {
353         Debug.log("Dialog closed; refreshing screen", module);
354         this.refresh();
355     }
356
357     // run method for auto-locking POS on inactivity
358
public void run() {
359         while (monitorRunning) {
360             if (!isLocked && (System.currentTimeMillis() - lastActivity) > MAX_INACTIVITY) {
361                 Debug.log("POS terminal auto-lock activated", module);
362                 PosScreen.currentScreen.showPage("pospanel").setLock(true);
363             }
364             try {
365                 Thread.sleep(5000);
366             } catch (InterruptedException JavaDoc e) {
367                 Debug.logError(e, module);
368             }
369         }
370     }
371
372     public void focusGained(FocusEvent JavaDoc event) {
373         if (Debug.verboseOn()) {
374             String JavaDoc from = event != null && event.getOppositeComponent() != null ? event.getOppositeComponent().getName() : "??";
375             Debug.log(event.getSource() + " focus gained from " + from, module);
376         }
377     }
378
379     public void focusLost(FocusEvent JavaDoc event) {
380         if (Debug.verboseOn()) {
381             String JavaDoc to = event != null && event.getOppositeComponent() != null ? event.getOppositeComponent().getName() : "??";
382             Debug.log(event.getSource() + " focus lost to " + to, module);
383         }
384     }
385
386     private String JavaDoc getScreenLocation() {
387         if (this.scrLocation == null) {
388             synchronized(this) {
389                 if (this.scrLocation == null) {
390                     String JavaDoc xuiProps = this.getSession().getContainer().getXuiPropertiesName();
391                     String JavaDoc startClass = UtilProperties.getPropertyValue(xuiProps, "StartClass", "default/pospanel");
392                     this.scrLocation = startClass.substring(0, startClass.indexOf("/"));
393                 }
394             }
395         }
396         return this.scrLocation;
397     }
398 }
Popular Tags