KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Keyboard.java 7207 2006-04-05 20:21:31Z les7arts $
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 java.util.List JavaDoc;
28
29 import jpos.JposException;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.pos.adaptor.DataEventAdaptor;
33 import org.ofbiz.pos.config.ButtonEventConfig;
34 import org.ofbiz.pos.device.GenericDevice;
35 import org.ofbiz.pos.screen.PosScreen;
36
37 /**
38  * Keyboard Key -> Button Mapping Tool
39  *
40  * This class will invoke button events based on a key press.
41  * The key -> code mapping is handled in the jpos.xml file.
42  * The code -> button mapping is handled in the buttonevents.xml file.
43  * It is advised to map to key codes > 200.
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 7207 $
47  * @since 3.2
48  */

49 public class Keyboard extends GenericDevice {
50
51     public static final String JavaDoc module = CashDrawer.class.getName();
52
53     public Keyboard(String JavaDoc deviceName, int timeout) {
54         super(deviceName, timeout);
55         this.control = new jpos.POSKeyboard();
56     }
57
58     protected void initialize() throws JposException {
59         Debug.logInfo("Keyboard [" + control.getPhysicalDeviceName() + "] Claimed : " + control.getClaimed(), module);
60         final jpos.POSKeyboard keyboard = (jpos.POSKeyboard) control;
61
62         keyboard.addDataListener(new DataEventAdaptor() {
63             public void dataOccurred(jpos.events.DataEvent event) {
64                 Debug.log("POSKeyboard DataEvent - " + event.getWhen(), module);
65                 try {
66                     int keyCode = keyboard.getPOSKeyData();
67                     Debug.log("Received KeyCode From POSKeyboard DataEvent : " + keyCode, module);
68
69                     // -1 is not valid
70
if (keyCode == -1) {
71                         return;
72                     }
73
74                     // check for button mapping
75
if (PosScreen.currentScreen.isLocked()) {
76                         Debug.log("PosScreen is locked; not running POSKeyboard Event!", module);
77                         return;
78                     }
79
80                     List JavaDoc buttonEvents = ButtonEventConfig.findButtonKeyAssign(keyCode);
81                     if (buttonEvents != null && buttonEvents.size() > 0) {
82
83                         Debug.log("Key -> Button Mapping(s) Found [" + keyCode + "]", module);
84                         try {
85                             ButtonEventConfig.invokeButtonEvents(buttonEvents, PosScreen.currentScreen);
86                         } catch (ButtonEventConfig.ButtonEventNotFound e) {
87                             Debug.logError(e, module);
88                         } catch (ButtonEventConfig.ButtonEventException e) {
89                             Debug.logError(e, module);
90                         }
91                     } else {
92                         Debug.logWarning("No key-code button mappings found for key-code [" + keyCode + "]", module);
93                     }
94                 } catch (JposException e) {
95                     Debug.logError(e, module);
96                 }
97             }
98         });
99     }
100 }
101
Popular Tags