1 25 package org.ofbiz.pos.jpos.service; 26 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.Enumeration ; 30 import java.awt.event.KeyListener ; 31 import java.awt.event.KeyEvent ; 32 import java.lang.reflect.Field ; 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 51 public class KeyboardService extends BaseService implements jpos.services.POSKeyboardService17, KeyboardReceiver, KeyListener { 52 53 public static final String 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 lastEvent = null; 63 protected Map keyMapping = null; 64 65 public KeyboardService() { 66 KeyboardAdaptor.getInstance(this, KeyboardAdaptor.KEYBOARD_DATA); 67 } 68 69 public void open(String deviceName, EventCallbacks ecb) throws JposException { 70 super.open(deviceName, ecb); 71 72 this.keyMapping = new HashMap (); 74 Enumeration props = entry.getPropertyNames(); 75 while (props.hasMoreElements()) { 76 String propName = (String ) props.nextElement(); 77 if (propName.startsWith("key.")) { 78 String propValue = (String ) entry.getPropertyValue(propName); 79 propName = propName.substring(4); 80 81 PosKey key = new PosKey(propName, propValue); 82 keyMapping.put(new Integer (key.hashCode()), key); 83 } 84 } 85 } 86 87 public boolean getCapKeyUp() throws JposException { 89 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 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 public synchronized void receiveData(int[] codes, char[] chars) { 148 if (lastEvent != null) { 149 KeyEvent thisEvent = lastEvent; 150 PosKey thisKey = new PosKey(thisEvent); 151 PosKey mappedKey = (PosKey) keyMapping.get(new Integer (thisKey.hashCode())); 152 if (mappedKey != null && mappedKey.checkModifiers(thisEvent.getModifiersEx())) { 153 this.received = true; 154 this.keyData = mappedKey.getMappedCode(); 155 156 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 public void keyPressed(KeyEvent event) { 167 this.keyEvent = POSKeyboardConst.KBD_KET_KEYDOWN; 168 this.lastEvent = event; 169 } 170 171 public void keyTyped(KeyEvent event) { 172 } 173 174 public void keyReleased(KeyEvent event) { 175 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 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 keyName, String mappedValue) throws JposException { 198 String keyDef = null; 199 String 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 if (keyDef.startsWith("0x")) { 209 try { 210 this.keyCode = Integer.parseInt(keyDef.substring(2), 16); 211 } catch (Throwable 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 kef = KeyEvent .class.getField(keyDef); 218 this.keyCode = kef.getInt(kef); 219 } catch (Throwable 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 t) { 227 Debug.logError(t, module); 228 throw new JposException(JposConst.JPOS_E_ILLEGAL, "Illegal key code definition [" + keyName + "]"); 229 } 230 } 231 232 String [] modifiers = null; 234 if (keyMod != null && keyMod.length() > 0) { 235 if (keyMod.indexOf("+") != -1) { 236 modifiers = keyMod.split("\\+"); 237 } else { 238 modifiers = new String [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 if (UtilValidate.isNotEmpty(mappedValue)) { 261 try { 262 this.mappedCode = Integer.parseInt(mappedValue); 263 } catch (Throwable 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 |