1 25 package org.ofbiz.pos.screen; 26 27 28 import java.awt.AWTEvent ; 29 import java.awt.Frame ; 30 import java.awt.Window ; 31 import java.awt.event.FocusEvent ; 32 import java.awt.event.FocusListener ; 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 61 public class PosScreen extends NavigationHelper implements Runnable , DialogCallback, FocusListener { 62 63 public static final String module = PosScreen.class.getName(); 64 public static final Frame appFrame = XProjectManager.getCurrentProject().getAppFrame(); 65 public static final Window appWin = XProjectManager.getCurrentProject().getAppWindow(); 66 public static final String 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 activityMonitor = null; 74 75 protected ClassLoader 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 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 this.setEnabled(false); 99 this.setVisible(false); 100 101 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 XProjectManager.getPageManager().loadPage(this.getScreenLocation() + "/paypanel"); 114 XProjectManager.getPageManager().loadPage(this.getScreenLocation() + "/mgrpanel"); 115 XProjectManager.getPageManager().loadPage(this.getScreenLocation() + "/promopanel"); 116 117 if (activityMonitor == null) { 119 monitorRunning = true; 120 activityMonitor = new Thread (this); 121 activityMonitor.setDaemon(false); 122 activityMonitor.start(); 123 } 124 125 KeyboardAdaptor.attachComponents(appFrame, false); 127 KeyboardAdaptor.attachComponents(appWin, false); 128 appFrame.addFocusListener(this); 129 appWin.addFocusListener(this); 130 131 SplashLoader.close(); 133 } 134 135 this.buttons = new PosButton(this); 137 138 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 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 } else { 242 output.print(UtilProperties.getMessage("pos","ULOGIN",defaultLocale)); 243 } 245 246 this.repaint(); 247 } 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 getClassLoader() { 290 return classLoader; 291 } 292 293 public synchronized void buttonPressed() { 295 this.setLastActivity(System.currentTimeMillis()); 296 buttons.buttonPressed(this, (AWTEvent )this.getCurrentEvent()); 297 journal.focus(); 298 } 299 300 public PosScreen showPage(String pageName) { 302 return this.showPage(pageName, true); 303 } 304 305 public PosScreen showPage(String 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 pageName) { 330 return showDialog(pageName, this, null); 331 } 332 333 public PosDialog showDialog(String pageName, String text) { 334 return showDialog(pageName, this, text); 335 } 336 337 public PosDialog showDialog(String pageName, DialogCallback cb) { 338 return showDialog(pageName, cb, null); 339 } 340 341 public PosDialog showDialog(String pageName, DialogCallback cb, String 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 public void receiveDialogCb(PosDialog dialog) { 353 Debug.log("Dialog closed; refreshing screen", module); 354 this.refresh(); 355 } 356 357 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 e) { 367 Debug.logError(e, module); 368 } 369 } 370 } 371 372 public void focusGained(FocusEvent event) { 373 if (Debug.verboseOn()) { 374 String 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 event) { 380 if (Debug.verboseOn()) { 381 String 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 getScreenLocation() { 387 if (this.scrLocation == null) { 388 synchronized(this) { 389 if (this.scrLocation == null) { 390 String xuiProps = this.getSession().getContainer().getXuiPropertiesName(); 391 String 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 |