001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.pos.component;
019:
020: import java.awt.Color;
021: import java.awt.Component;
022: import java.awt.event.KeyEvent;
023: import java.awt.event.KeyListener;
024: import java.util.EmptyStackException;
025: import java.util.Iterator;
026: import java.util.Stack;
027:
028: import net.xoetrope.swing.XEdit;
029:
030: import org.ofbiz.pos.adaptor.KeyboardAdaptor;
031: import org.ofbiz.pos.adaptor.KeyboardReceiver;
032: import org.ofbiz.pos.screen.PosScreen;
033:
034: public class Input implements KeyboardReceiver, KeyListener {
035:
036: public static final String module = Input.class.getName();
037: private static final String[] validFunc = { "LOGIN", "OPEN",
038: "CLOSE", "UNLOCK", "MGRLOGIN", "PAID", "TOTAL", "CREDIT",
039: "GIFTCARD", "MSRINFO", "CHECK", "CHECKINFO", "REFNUM",
040: "QTY", "VOID", "SHIFT", "PAID_IN", "PAID_OUT" };
041:
042: protected Stack functionStack = new Stack();
043: protected Component[] pageComs = null;
044: protected Color lastColor = null;
045: protected javax.swing.JTextField input = null;
046: protected boolean isLocked = false;
047:
048: public Input(PosScreen page) {
049: this .input = (XEdit) page.findComponent("pos_input");
050: this .input.setVisible(true);
051: this .input.setFocusable(false);
052:
053: // initialize the KeyboardAdaptor
054: KeyboardAdaptor
055: .getInstance(this , KeyboardAdaptor.KEYBOARD_DATA);
056: }
057:
058: public void focus() {
059: this .input.requestFocus();
060: }
061:
062: public void setLock(boolean lock) {
063: // hide the input text
064: if (lock) {
065: //lastColor = this.input.getForeground();
066: //input.setForeground(this.input.getBackground());
067: } else {
068: //input.setForeground(this.lastColor);
069: }
070: isLocked = lock;
071: }
072:
073: public void setFunction(String function, String value)
074: throws IllegalArgumentException {
075: if (isValidFunction(function)) {
076: this .functionStack.push(new String[] { function, value });
077: input.setText("");
078: } else {
079: throw new IllegalArgumentException();
080: }
081: }
082:
083: public void setFunction(String function)
084: throws IllegalArgumentException {
085: setFunction(function, input.getText());
086: }
087:
088: private boolean isValidFunction(String function) {
089: for (int i = 0; i < validFunc.length; i++) {
090: if (validFunc[i].equals(function)) {
091: return true;
092: }
093: }
094: return false;
095: }
096:
097: public String[] getLastFunction() {
098: String[] f = null;
099: try {
100: f = (String[]) this .functionStack.peek();
101: } catch (EmptyStackException e) {
102: }
103: return f;
104: }
105:
106: public String[] clearLastFunction() {
107: String[] f = null;
108: try {
109: f = (String[]) this .functionStack.pop();
110: } catch (EmptyStackException e) {
111: }
112: return f;
113: }
114:
115: public String[] getFunction(String function) {
116: Iterator i = functionStack.iterator();
117: while (i.hasNext()) {
118: String[] func = (String[]) i.next();
119: if (func[0].equals(function)) {
120: return func;
121: }
122: }
123: return null;
124: }
125:
126: public String[] clearFunction(String function) {
127: Iterator i = functionStack.iterator();
128: while (i.hasNext()) {
129: String[] func = (String[]) i.next();
130: if (func[0].equals(function)) {
131: i.remove();
132: return func;
133: }
134: }
135: return null;
136: }
137:
138: public boolean isFunctionSet(String function) {
139: Iterator i = functionStack.iterator();
140: while (i.hasNext()) {
141: String func[] = (String[]) i.next();
142: if (func[0].equals(function)) {
143: return true;
144: }
145: }
146: return false;
147: }
148:
149: public void clearInput() {
150: input.setText("");
151: }
152:
153: public void clear() {
154: input.setText("");
155: functionStack.clear();
156: }
157:
158: public String value() {
159: return input.getText();
160: }
161:
162: public void appendChar(char c) {
163: input.setText(this .input.getText() + c);
164: }
165:
166: public void appendString(String str) {
167: input.setText(this .input.getText() + str);
168: }
169:
170: public void stripLastChar() {
171: if (this .value().length() > 0) {
172: this .input.setText(this .value().substring(0,
173: this .value().length() - 1));
174: }
175: }
176:
177: // KeyboardReceiver
178: public synchronized void receiveData(int[] codes, char[] chars) {
179: if (chars.length > 0 && checkChars(chars))
180: this .appendString(new String(chars));
181: }
182:
183: // KeyListener
184: public void keyPressed(KeyEvent event) {
185: // implements to handle backspacing only
186: if (event.getKeyCode() == 8 && this .value().length() > 0) {
187: this .input.setText(this .value().substring(0,
188: this .value().length() - 1));
189: } else if (event.getKeyCode() == 27
190: && this .value().length() > 0) {
191: this .input.setText("");
192: }
193: }
194:
195: public void keyTyped(KeyEvent event) {
196: }
197:
198: public void keyReleased(KeyEvent event) {
199: }
200:
201: private boolean checkChars(char[] chars) {
202: int[] idxToRemove = new int[chars.length];
203: boolean process = false;
204: int remIdx = 0;
205: for (int i = 0; i < chars.length; i++) {
206: if (((int) chars[i]) == 10 || ((int) chars[i]) == 8
207: || ((int) chars[i] == 27)) {
208: idxToRemove[remIdx++] = i + 1;
209: } else {
210: process = true;
211: }
212: }
213:
214: if (chars.length == 1) {
215: return process;
216: }
217:
218: int toRemove = 0;
219: for (int i = 0; i < idxToRemove.length; i++) {
220: if (idxToRemove[i] > 0) {
221: toRemove++;
222: }
223: }
224:
225: if (toRemove > 0) {
226: if (chars.length - toRemove < 1) {
227: return false;
228: }
229:
230: char[] newChars = new char[chars.length - toRemove];
231: int currentIndex = 0;
232: for (int i = 0; i < chars.length; i++) {
233: boolean appendChar = true;
234: for (int x = 0; x < idxToRemove.length; x++) {
235: if ((idxToRemove[x] - 1) == i) {
236: appendChar = false;
237: continue;
238: } else {
239: }
240: }
241: if (appendChar) {
242: newChars[currentIndex] = chars[i];
243: currentIndex++;
244: }
245: }
246: }
247:
248: return process;
249: }
250: }
|