KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > device > impl > Scanner


1 /*
2  * $Id: Scanner.java 5462 2005-08-05 18:35:48Z 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.device.impl;
26
27 import jpos.JposException;
28 import jpos.ScannerConst;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.pos.adaptor.DataEventAdaptor;
32 import org.ofbiz.pos.device.GenericDevice;
33 import org.ofbiz.pos.screen.PosScreen;
34 import org.ofbiz.pos.event.MenuEvents;
35
36 /**
37  *
38  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
39  * @version $Rev: 5462 $
40  * @since 3.2
41  */

42 public class Scanner extends GenericDevice {
43
44     public static final String JavaDoc module = Scanner.class.getName();
45
46     protected String JavaDoc deviceName = null;
47     protected int timeout = -1;
48
49     public Scanner(String JavaDoc deviceName, int timeout) {
50         super(deviceName, timeout);
51         this.control = new jpos.Scanner();
52     }
53
54     protected void initialize() throws JposException {
55         Debug.logInfo("Scanner [" + control.getPhysicalDeviceName() + "] Claimed : " + control.getClaimed(), module);
56         final jpos.Scanner scanner = (jpos.Scanner) control;
57
58         // tell the driver to decode the scanned data
59
scanner.setDecodeData(true);
60
61         // create the new listner
62
scanner.addDataListener(new DataEventAdaptor() {
63
64             public void dataOccurred(jpos.events.DataEvent event) {
65                 byte[] scanData = null;
66                 int dataType = ScannerConst.SCAN_SDT_UNKNOWN;
67
68                 try {
69                     dataType = scanner.getScanDataType();
70                     scanData = scanner.getScanDataLabel();
71                     if (scanData == null || scanData.length == 0) {
72                         Debug.logWarning("Scanner driver does not support decoding data; the raw result is used instead", module);
73                         scanData = scanner.getScanData();
74                     }
75                     
76                     scanner.clearInput();
77                 } catch (jpos.JposException e) {
78                     Debug.logError(e, module);
79                 }
80
81                 processScanData(scanData, dataType);
82             }
83         });
84     }
85
86     protected void processScanData(byte[] data, int dataType) {
87         if (data != null) {
88             // make sure we are on the main POS screen
89
if (!"main/pospanel".equals(PosScreen.currentScreen.getName())) {
90                 PosScreen.currentScreen.showPage("pospanel");
91             }
92             
93             // we can add some type checking here if needed (i.e. type of barcode; type of SKU, etc)
94
if (dataType == ScannerConst.SCAN_SDT_UNKNOWN) {
95                 Debug.logWarning("Scanner type checking problems - check scanner driver", module);
96             }
97
98             // stuff the data to the Input component
99
PosScreen.currentScreen.getInput().clearInput();
100             PosScreen.currentScreen.getInput().appendString(new String JavaDoc(data));
101
102             // call the ENTER event
103
//this.callEnter();
104
MenuEvents.addItem(PosScreen.currentScreen, null);
105         }
106     }
107 }
108
109
Popular Tags