KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > component > Input


1 /*
2  * $Id: Input.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.component;
26
27 import java.awt.Color JavaDoc;
28 import java.awt.Component JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.awt.event.KeyListener JavaDoc;
31 import java.util.EmptyStackException JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Stack JavaDoc;
34
35 import net.xoetrope.swing.XEdit;
36
37 import org.ofbiz.pos.adaptor.KeyboardAdaptor;
38 import org.ofbiz.pos.adaptor.KeyboardReceiver;
39 import org.ofbiz.pos.screen.PosScreen;
40
41 /**
42  *
43  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44  * @version $Rev: 7207 $
45  * @since 3.1
46  */

47 public class Input implements KeyboardReceiver, KeyListener JavaDoc {
48
49     public static final String JavaDoc module = Input.class.getName();
50     private static final String JavaDoc[] validFunc = { "LOGIN", "OPEN", "CLOSE", "UNLOCK", "MGRLOGIN", "PAID", "TOTAL",
51                                                 "CREDIT", "GIFTCARD", "MSRINFO", "CHECK", "CHECKINFO", "REFNUM",
52                                                 "QTY", "VOID", "SHIFT" };
53
54     protected Stack JavaDoc functionStack = new Stack JavaDoc();
55     protected Component JavaDoc[] pageComs = null;
56     protected Color JavaDoc lastColor = null;
57     protected XEdit input = null;
58     protected boolean isLocked = false;
59
60     public Input(PosScreen page) {
61         this.input = (XEdit) page.findComponent("pos_input");
62         this.input.setVisible(true);
63         this.input.setFocusable(false);
64
65         // initialize the KeyboardAdaptor
66
KeyboardAdaptor.getInstance(this, KeyboardAdaptor.KEYBOARD_DATA);
67     }
68
69     public void focus() {
70         this.input.requestFocus();
71     }
72
73     public void setLock(boolean lock) {
74         // hide the input text
75
if (lock) {
76             //lastColor = this.input.getForeground();
77
//input.setForeground(this.input.getBackground());
78
} else {
79             //input.setForeground(this.lastColor);
80
}
81         isLocked = lock;
82     }
83
84     public void setFunction(String JavaDoc function, String JavaDoc value) throws IllegalArgumentException JavaDoc {
85         if (isValidFunction(function)) {
86             this.functionStack.push( new String JavaDoc[] { function, value });
87             input.setText("");
88         } else {
89             throw new IllegalArgumentException JavaDoc();
90         }
91     }
92
93     public void setFunction(String JavaDoc function) throws IllegalArgumentException JavaDoc {
94         setFunction(function, input.getText());
95     }
96
97     private boolean isValidFunction(String JavaDoc function) {
98         for (int i = 0; i < validFunc.length; i++) {
99             if (validFunc[i].equals(function)) {
100                 return true;
101             }
102         }
103         return false;
104     }
105
106     public String JavaDoc[] getLastFunction() {
107         String JavaDoc[] f = null;
108         try {
109             f = (String JavaDoc[]) this.functionStack.peek();
110         } catch (EmptyStackException JavaDoc e) {
111         }
112         return f;
113     }
114
115     public String JavaDoc[] clearLastFunction() {
116         String JavaDoc[] f = null;
117         try {
118             f = (String JavaDoc[]) this.functionStack.pop();
119         } catch (EmptyStackException JavaDoc e) {
120         }
121         return f;
122     }
123
124     public String JavaDoc[] getFunction(String JavaDoc function) {
125         Iterator JavaDoc i = functionStack.iterator();
126         while (i.hasNext()) {
127             String JavaDoc[] func = (String JavaDoc[]) i.next();
128             if (func[0].equals(function)) {
129                 return func;
130             }
131         }
132         return null;
133     }
134
135     public String JavaDoc[] clearFunction(String JavaDoc function) {
136         Iterator JavaDoc i = functionStack.iterator();
137         while (i.hasNext()) {
138             String JavaDoc[] func = (String JavaDoc[]) i.next();
139             if (func[0].equals(function)) {
140                 i.remove();
141                 return func;
142             }
143         }
144         return null;
145     }
146
147     public boolean isFunctionSet(String JavaDoc function) {
148         Iterator JavaDoc i = functionStack.iterator();
149         while (i.hasNext()) {
150             String JavaDoc func[] = (String JavaDoc[]) i.next();
151             if (func[0].equals(function)) {
152                 return true;
153             }
154         }
155         return false;
156     }
157
158     public void clearInput() {
159         input.setText("");
160     }
161
162     public void clear() {
163         input.setText("");
164         functionStack.clear();
165     }
166
167     public String JavaDoc value() {
168         return input.getText();
169     }
170
171     public void appendChar(char c) {
172         input.setText(this.input.getText() + c);
173     }
174
175     public void appendString(String JavaDoc str) {
176         input.setText(this.input.getText() + str);
177     }
178
179     public void stripLastChar() {
180         if (this.value().length() > 0) {
181             this.input.setText(this.value().substring(0, this.value().length() - 1));
182         }
183     }
184
185     // KeyboardReceiver
186
public synchronized void receiveData(int[] codes, char[] chars) {
187         if (chars.length > 0 && checkChars(chars))
188             this.appendString(new String JavaDoc(chars));
189     }
190
191     // KeyListener
192
public void keyPressed(KeyEvent JavaDoc event) {
193         // implements to handle backspacing only
194
if (event.getKeyCode() == 8 && this.value().length() > 0) {
195             this.input.setText(this.value().substring(0, this.value().length() - 1));
196         } else if (event.getKeyCode() == 27 && this.value().length() > 0) {
197             this.input.setText("");
198         }
199     }
200
201     public void keyTyped(KeyEvent JavaDoc event) {
202     }
203
204     public void keyReleased(KeyEvent JavaDoc event) {
205     }
206
207     private boolean checkChars(char[] chars) {
208         int[] idxToRemove = new int[chars.length];
209         boolean process = false;
210         int remIdx = 0;
211         for (int i = 0; i < chars.length; i++) {
212             if (((int) chars[i]) == 10 || ((int) chars[i]) == 8 || ((int) chars[i] == 27)) {
213                 idxToRemove[remIdx++] = i+1;
214             } else {
215                 process = true;
216             }
217         }
218
219         if (chars.length == 1) {
220             return process;
221         }
222
223         int toRemove = 0;
224         for (int i = 0; i < idxToRemove.length; i++) {
225             if (idxToRemove[i] > 0) {
226                 toRemove++;
227             }
228         }
229
230         if (toRemove > 0) {
231             if (chars.length - toRemove < 1) {
232                 return false;
233             }
234
235             char[] newChars = new char[chars.length - toRemove];
236             int currentIndex = 0;
237             for (int i = 0; i < chars.length; i++) {
238                 boolean appendChar = true;
239                 for (int x = 0; x < idxToRemove.length; x++) {
240                     if ((idxToRemove[x] - 1) == i) {
241                         appendChar = false;
242                         continue;
243                     } else {
244                     }
245                 }
246                 if (appendChar) {
247                     newChars[currentIndex] = chars[i];
248                     currentIndex++;
249                 }
250             }
251         }
252
253         return process;
254     }
255 }
256
Popular Tags