KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > adaptor > KeyboardAdaptor


1 /*
2  * $Id: KeyboardAdaptor.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.adaptor;
26
27 import java.awt.Component JavaDoc;
28 import java.awt.Container JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.awt.event.KeyListener JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import org.ofbiz.base.util.Debug;
37
38 import org.apache.commons.collections.map.LinkedMap;
39
40
41 /**
42  * KeyboardAdaptor - Handles reading keyboard input
43  *
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @version $Rev: 5462 $
46  * @since 3.2
47  */

48 public class KeyboardAdaptor {
49
50     public static final String JavaDoc module = KeyboardAdaptor.class.getName();
51
52     public static final int EVENT_RELEASED = 2;
53     public static final int EVENT_PRESSED = 1;
54     public static final int EVENT_TYPED = 3;
55
56     public static final int KEYBOARD_DATA = 100;
57     public static final int SCANNER_DATA = 101;
58     public static final int MSR_DATA = 102;
59     public static final int ALL_DATA = 999;
60
61     protected static List JavaDoc loadedComponents = new LinkedList JavaDoc();
62     protected static Map JavaDoc receivers = new LinkedMap();
63     protected static KeyboardAdaptor adaptor = null;
64     protected static boolean running = true;
65
66     protected KeyboardListener listener = null;
67
68     public static KeyboardAdaptor getInstance(KeyboardReceiver receiver, int dataType) {
69         if (adaptor == null) {
70             synchronized(KeyboardAdaptor.class) {
71                 if (adaptor == null) {
72                     adaptor = new KeyboardAdaptor();
73                 }
74             }
75         }
76
77         if (receiver != null && dataType > -1) {
78             receivers.put(receiver, new Integer JavaDoc(dataType));
79         }
80         return adaptor;
81     }
82
83     public static KeyboardAdaptor getInstance() {
84         return getInstance(null, -1);
85     }
86
87     public static void attachComponents(Component JavaDoc[] coms, boolean recurse) {
88         // check the adaptor
89
if (adaptor == null) {
90             KeyboardAdaptor.getInstance();
91         }
92
93         // add the new ones to listen on
94
if (adaptor != null && coms != null) {
95             adaptor.addComponents(coms, recurse);
96         }
97     }
98
99     public static void attachComponents(Component JavaDoc[] coms) {
100         KeyboardAdaptor.attachComponents(coms, true);
101     }
102
103     public static void attachComponents(Container JavaDoc parent, boolean recurse) {
104         KeyboardAdaptor.attachComponents(new Component JavaDoc[] { parent }, recurse);
105     }
106
107     public static void attachComponents(Container JavaDoc parent) {
108         KeyboardAdaptor.attachComponents(parent, true);
109     }
110
111     public static void stop() {
112         running = false;
113     }
114
115     private KeyboardAdaptor() {
116         this.listener = new KeyboardListener();
117         this.listener.setDaemon(false);
118         this.listener.setName(listener.toString());
119         this.listener.start();
120         KeyboardAdaptor.adaptor = this;
121     }
122
123     private void addComponents(Component JavaDoc[] coms, boolean recurse) {
124         listener.reader.configureComponents(coms, recurse);
125     }
126
127     private class KeyboardListener extends Thread JavaDoc {
128
129         private static final long MAX_WAIT = 100;
130         private List JavaDoc keyCodeData = new LinkedList JavaDoc();
131         private List JavaDoc keyCharData = new LinkedList JavaDoc();
132         private long lastKey = -1;
133         private KeyReader reader = null;
134
135         public KeyboardListener() {
136             this.reader = new KeyReader(this);
137         }
138
139         private int checkDataType(char[] chars) {
140             if (chars.length == 0) {
141                 // non-character data from keyboard interface (i.e. FN keys, enter, esc, etc)
142
return KEYBOARD_DATA;
143             } else if (((int) chars[0]) == 2 && ((int) chars[chars.length - 1]) == 10) {
144                 // test for scanner data
145
return SCANNER_DATA;
146             } else if (((int) chars[0]) == 37 && ((int) chars[chars.length - 1]) == 10) {
147                 // test for MSR data
148
return MSR_DATA;
149             } else {
150                 // otherwise it's keyboard data
151
return KEYBOARD_DATA;
152             }
153         }
154
155         protected synchronized void receiveCode(int keycode) {
156             keyCodeData.add(new Integer JavaDoc(keycode));
157         }
158
159         protected synchronized void receiveChar(char keychar) {
160             keyCharData.add(new Character JavaDoc(keychar));
161         }
162
163         protected synchronized void sendData() {
164             if (KeyboardAdaptor.receivers.size() > 0) {
165                 if (keyCharData.size() > 0 || keyCodeData.size() > 0) {
166                     char[] chars = new char[keyCharData.size()];
167                     int[] codes = new int[keyCodeData.size()];
168
169                     for (int i = 0; i < codes.length; i++) {
170                         Integer JavaDoc itg = (Integer JavaDoc) keyCodeData.get(i);
171                         codes[i] = itg.intValue();
172                     }
173
174                     for (int i = 0; i < chars.length; i++) {
175                         Character JavaDoc ch = (Character JavaDoc) keyCharData.get(i);
176                         chars[i] = ch.charValue();
177                     }
178
179                     Iterator JavaDoc ri = KeyboardAdaptor.receivers.keySet().iterator();
180                     while (ri.hasNext()) {
181                         KeyboardReceiver receiver = (KeyboardReceiver) ri.next();
182                         int receiverType = ((Integer JavaDoc) receivers.get(receiver)).intValue();
183                         int thisDataType = this.checkDataType(chars);
184                         if (receiverType == ALL_DATA || receiverType == thisDataType) {
185                             receiver.receiveData(codes, chars);
186                         }
187                     }
188
189                     keyCharData = new LinkedList JavaDoc();
190                     keyCodeData = new LinkedList JavaDoc();
191                     lastKey = -1;
192                 }
193             } else {
194                 Debug.logWarning("No receivers configured for key input", module);
195             }
196         }
197
198         protected synchronized void sendEvent(int eventType, KeyEvent JavaDoc event) {
199             lastKey = System.currentTimeMillis();
200             if (KeyboardAdaptor.receivers.size() > 0) {
201                 Iterator JavaDoc ri = KeyboardAdaptor.receivers.keySet().iterator();
202                 while (ri.hasNext()) {
203                     KeyboardReceiver receiver = (KeyboardReceiver) ri.next();
204                     if (receiver instanceof KeyListener JavaDoc) {
205                         switch (eventType) {
206                             case 1:
207                                 ((KeyListener JavaDoc) receiver).keyPressed(event);
208                                 break;
209                             case 2:
210                                 ((KeyListener JavaDoc) receiver).keyTyped(event);
211                                 break;
212                             case 3:
213                                 ((KeyListener JavaDoc) receiver).keyReleased(event);
214                                 break;
215                             default:
216                                 break;
217                         }
218                     }
219                 }
220             }
221         }
222
223         public void run() {
224             while (running) {
225                 long now = System.currentTimeMillis();
226                 if ((lastKey > -1) && (now - lastKey) >= MAX_WAIT) {
227                     this.sendData();
228                 }
229
230                 if (!running) {
231                     break;
232                 } else {
233                     try {
234                         Thread.sleep(MAX_WAIT);
235                     } catch (InterruptedException JavaDoc e) {
236                     }
237                 }
238             }
239         }
240     }
241
242     class KeyReader implements KeyListener JavaDoc {
243
244         private KeyboardListener k;
245
246         public KeyReader(KeyboardListener k) {
247             this.k = k;
248         }
249
250         private void configureComponents(Component JavaDoc[] coms, boolean recurse) {
251             for (int i = 0; i < coms.length; i++) {
252                 if (!loadedComponents.contains(coms[i])) {
253                     coms[i].addKeyListener(this);
254                     Debug.logInfo("Added [" + coms[i].getName() + "] to KeyboardAdaptor", module);
255                 }
256                 if (recurse && coms[i] instanceof Container JavaDoc) {
257                     Component JavaDoc[] nextComs = ((Container JavaDoc) coms[i]).getComponents();
258                     configureComponents(nextComs, true);
259                 }
260             }
261         }
262
263         public void keyTyped(KeyEvent JavaDoc e) {
264             k.receiveChar(e.getKeyChar());
265             k.sendEvent(EVENT_TYPED, e);
266         }
267
268         public void keyPressed(KeyEvent JavaDoc e) {
269             k.receiveCode(e.getKeyCode());
270             k.sendEvent(EVENT_PRESSED, e);
271         }
272
273         public void keyReleased(KeyEvent JavaDoc e) {
274             k.sendEvent(EVENT_RELEASED, e);
275         }
276     }
277 }
278
Popular Tags