KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > jpos > service > KeyboardService


1 /*
2  * $Id: KeyboardService.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.jpos.service;
26
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.awt.event.KeyListener JavaDoc;
31 import java.awt.event.KeyEvent JavaDoc;
32 import java.lang.reflect.Field JavaDoc;
33
34 import jpos.JposException;
35 import jpos.POSKeyboardConst;
36 import jpos.JposConst;
37 import jpos.events.DataEvent;
38 import jpos.services.EventCallbacks;
39
40 import org.ofbiz.pos.adaptor.KeyboardReceiver;
41 import org.ofbiz.pos.adaptor.KeyboardAdaptor;
42 import org.ofbiz.base.util.Debug;
43 import org.ofbiz.base.util.UtilValidate;
44
45 /**
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @version $Rev: 5462 $
49  * @since 3.2
50  */

51 public class KeyboardService extends BaseService implements jpos.services.POSKeyboardService17, KeyboardReceiver, KeyListener JavaDoc {
52
53     public static final String JavaDoc module = KeyboardService.class.getName();
54
55     protected boolean autoDisable = false;
56     protected boolean received = false;
57
58     protected int eventTypes = POSKeyboardConst.KBD_ET_DOWN;
59     protected int keyEvent = -1;
60     protected int keyData = -1;
61
62     protected KeyEvent JavaDoc lastEvent = null;
63     protected Map JavaDoc keyMapping = null;
64
65     public KeyboardService() {
66         KeyboardAdaptor.getInstance(this, KeyboardAdaptor.KEYBOARD_DATA);
67     }
68
69     public void open(String JavaDoc deviceName, EventCallbacks ecb) throws JposException {
70         super.open(deviceName, ecb);
71
72         // setup the key mapping
73
this.keyMapping = new HashMap JavaDoc();
74         Enumeration JavaDoc props = entry.getPropertyNames();
75         while (props.hasMoreElements()) {
76             String JavaDoc propName = (String JavaDoc) props.nextElement();
77             if (propName.startsWith("key.")) {
78                 String JavaDoc propValue = (String JavaDoc) entry.getPropertyValue(propName);
79                 propName = propName.substring(4);
80
81                 PosKey key = new PosKey(propName, propValue);
82                 keyMapping.put(new Integer JavaDoc(key.hashCode()), key);
83             }
84         }
85     }
86
87     // POSKeyboardService12
88
public boolean getCapKeyUp() throws JposException {
89         // we only support key down events
90
return false;
91     }
92
93     public boolean getAutoDisable() throws JposException {
94         return this.autoDisable;
95     }
96
97     public void setAutoDisable(boolean b) throws JposException {
98         this.autoDisable = b;
99     }
100
101     public int getEventTypes() throws JposException {
102         return this.eventTypes;
103     }
104
105     public void setEventTypes(int i) throws JposException {
106         if (i == POSKeyboardConst.KBD_ET_DOWN)
107             this.eventTypes = i;
108     }
109
110     public int getPOSKeyData() throws JposException {
111         if (!received) {
112             throw new JposException(JposConst.JPOS_PS_UNKNOWN, "No data received");
113         }
114         return keyData;
115     }
116
117     public int getPOSKeyEventType() throws JposException {
118         if (!received) {
119             throw new JposException(JposConst.JPOS_PS_UNKNOWN, "No data received");
120         }
121         return this.keyEvent;
122     }
123
124     public void clearInput() throws JposException {
125         this.keyEvent = -1;
126         this.keyData = -1;
127         this.received = false;
128     }
129
130     // POSKeyboardService13
131
public int getCapPowerReporting() throws JposException {
132         return 0;
133     }
134
135     public int getPowerNotify() throws JposException {
136         return 0;
137     }
138
139     public void setPowerNotify(int i) throws JposException {
140     }
141
142     public int getPowerState() throws JposException {
143         return 0;
144     }
145
146     // KeyboardReceiver
147
public synchronized void receiveData(int[] codes, char[] chars) {
148         if (lastEvent != null) {
149             KeyEvent JavaDoc thisEvent = lastEvent;
150             PosKey thisKey = new PosKey(thisEvent);
151             PosKey mappedKey = (PosKey) keyMapping.get(new Integer JavaDoc(thisKey.hashCode()));
152             if (mappedKey != null && mappedKey.checkModifiers(thisEvent.getModifiersEx())) {
153                 this.received = true;
154                 this.keyData = mappedKey.getMappedCode();
155
156                 // fire off the event notification
157
DataEvent event = new DataEvent(this, 0);
158                 this.fireEvent(event);
159             }
160         } else {
161             Debug.log("Last Event is null??", module);
162         }
163     }
164
165     // KeyListener
166
public void keyPressed(KeyEvent JavaDoc event) {
167         this.keyEvent = POSKeyboardConst.KBD_KET_KEYDOWN;
168         this.lastEvent = event;
169     }
170
171     public void keyTyped(KeyEvent JavaDoc event) {
172     }
173
174     public void keyReleased(KeyEvent JavaDoc event) {
175         // currently this is not enabled
176
if (this.eventTypes == POSKeyboardConst.KBD_ET_DOWN_UP) {
177             this.keyEvent = POSKeyboardConst.KBD_KET_KEYDOWN;
178             this.lastEvent = event;
179         }
180     }
181
182     class PosKey {
183
184         private int keyCode, mappedCode;
185         private boolean alt, ctrl, shift;
186
187         public PosKey(KeyEvent JavaDoc event) {
188             this.keyCode = event.getKeyCode();
189             this.mappedCode = -1;
190
191             int modifiersEx = event.getModifiersEx();
192             this.shift = this.checkShift(modifiersEx);
193             this.ctrl = this.checkCtrl(modifiersEx);
194             this.alt = this.checkAlt(modifiersEx);
195         }
196
197         public PosKey(String JavaDoc keyName, String JavaDoc mappedValue) throws JposException {
198             String JavaDoc keyDef = null;
199             String JavaDoc keyMod = null;
200             if (keyName.indexOf("+") != -1) {
201                 keyDef = keyName.substring (0, keyName.indexOf("+")).trim();
202                 keyMod = keyName.substring(keyName.indexOf("+") + 1);
203             } else {
204                 keyDef = keyName;
205             }
206
207             // set the keycode
208
if (keyDef.startsWith("0x")) {
209                 try {
210                     this.keyCode = Integer.parseInt(keyDef.substring(2), 16);
211                 } catch (Throwable JavaDoc t) {
212                     Debug.logError(t, module);
213                     throw new JposException(JposConst.JPOS_E_ILLEGAL, "Illegal hex code key definition [" + keyName + "]");
214                 }
215             } else if (keyDef.startsWith("VK_")) {
216                 try {
217                     Field JavaDoc kef = KeyEvent JavaDoc.class.getField(keyDef);
218                     this.keyCode = kef.getInt(kef);
219                 } catch (Throwable JavaDoc t) {
220                     Debug.logError(t, module);
221                     throw new JposException(JposConst.JPOS_E_ILLEGAL, "Illegal virtual key definition [" + keyName + "]");
222                 }
223             } else {
224                 try {
225                     this.keyCode = Integer.parseInt(keyDef);
226                 } catch (Throwable JavaDoc t) {
227                     Debug.logError(t, module);
228                     throw new JposException(JposConst.JPOS_E_ILLEGAL, "Illegal key code definition [" + keyName + "]");
229                 }
230             }
231
232             // set the key modifiers
233
String JavaDoc[] modifiers = null;
234             if (keyMod != null && keyMod.length() > 0) {
235                 if (keyMod.indexOf("+") != -1) {
236                     modifiers = keyMod.split("\\+");
237                 } else {
238                     modifiers = new String JavaDoc[1];
239                     modifiers[0] = keyMod;
240                 }
241                 for (int i = 0; i < modifiers.length; i++) {
242                     if ("SHIFT".equalsIgnoreCase(modifiers[i])) {
243                         this.shift = true;
244                     } else {
245                         this.shift = false;
246                     }
247                     if ("CTRL".equalsIgnoreCase(modifiers[i])) {
248                         this.ctrl = true;
249                     } else {
250                         this.ctrl = false;
251                     }
252                     if ("ALT".equalsIgnoreCase(modifiers[i])) {
253                         this.alt = true;
254                         this.alt = false;
255                     }
256                 }
257             }
258
259             // set the mapped value
260
if (UtilValidate.isNotEmpty(mappedValue)) {
261                 try {
262                     this.mappedCode = Integer.parseInt(mappedValue);
263                 } catch (Throwable JavaDoc t) {
264                     Debug.logError(t, module);
265                     throw new JposException(JposConst.JPOS_E_ILLEGAL, "Illegal key code mapping [" + mappedValue + "]");
266                 }
267             } else {
268                 this.mappedCode = keyCode;
269             }
270         }
271
272         public int getKeyCode() {
273             return keyCode;
274         }
275
276         public int getMappedCode() {
277             return mappedCode;
278         }
279
280         public int hashCode() {
281             int code = this.keyCode;
282             if (shift) code += KeyEvent.SHIFT_DOWN_MASK;
283             if (ctrl) code += KeyEvent.CTRL_DOWN_MASK;
284             if (alt) code += KeyEvent.ALT_DOWN_MASK;
285             return code;
286         }
287
288         public boolean checkModifiers(int mod) {
289             if (shift && !checkShift(mod)) {
290                 return false;
291             }
292             if (ctrl && !checkCtrl(mod)) {
293                 return false;
294             }
295             if (alt && !checkAlt(mod)) {
296                 return false;
297             }
298             return true;
299         }
300
301         public boolean checkShift(int mod) {
302             return ((mod & KeyEvent.SHIFT_DOWN_MASK) > 0);
303         }
304
305         public boolean checkCtrl(int mod) {
306             return ((mod & KeyEvent.CTRL_DOWN_MASK) > 0);
307         }
308
309         public boolean checkAlt(int mod) {
310             return ((mod & KeyEvent.ALT_DOWN_MASK) > 0);
311         }
312     }
313 }
314
Popular Tags