1 25 package org.ofbiz.pos.component; 26 27 import java.awt.Color ; 28 import java.awt.Component ; 29 import java.awt.event.KeyEvent ; 30 import java.awt.event.KeyListener ; 31 import java.util.EmptyStackException ; 32 import java.util.Iterator ; 33 import java.util.Stack ; 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 47 public class Input implements KeyboardReceiver, KeyListener { 48 49 public static final String module = Input.class.getName(); 50 private static final String [] validFunc = { "LOGIN", "OPEN", "CLOSE", "UNLOCK", "MGRLOGIN", "PAID", "TOTAL", 51 "CREDIT", "GIFTCARD", "MSRINFO", "CHECK", "CHECKINFO", "REFNUM", 52 "QTY", "VOID", "SHIFT" }; 53 54 protected Stack functionStack = new Stack (); 55 protected Component [] pageComs = null; 56 protected Color 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 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 if (lock) { 76 } else { 79 } 81 isLocked = lock; 82 } 83 84 public void setFunction(String function, String value) throws IllegalArgumentException { 85 if (isValidFunction(function)) { 86 this.functionStack.push( new String [] { function, value }); 87 input.setText(""); 88 } else { 89 throw new IllegalArgumentException (); 90 } 91 } 92 93 public void setFunction(String function) throws IllegalArgumentException { 94 setFunction(function, input.getText()); 95 } 96 97 private boolean isValidFunction(String 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 [] getLastFunction() { 107 String [] f = null; 108 try { 109 f = (String []) this.functionStack.peek(); 110 } catch (EmptyStackException e) { 111 } 112 return f; 113 } 114 115 public String [] clearLastFunction() { 116 String [] f = null; 117 try { 118 f = (String []) this.functionStack.pop(); 119 } catch (EmptyStackException e) { 120 } 121 return f; 122 } 123 124 public String [] getFunction(String function) { 125 Iterator i = functionStack.iterator(); 126 while (i.hasNext()) { 127 String [] func = (String []) i.next(); 128 if (func[0].equals(function)) { 129 return func; 130 } 131 } 132 return null; 133 } 134 135 public String [] clearFunction(String function) { 136 Iterator i = functionStack.iterator(); 137 while (i.hasNext()) { 138 String [] func = (String []) 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 function) { 148 Iterator i = functionStack.iterator(); 149 while (i.hasNext()) { 150 String func[] = (String []) 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 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 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 public synchronized void receiveData(int[] codes, char[] chars) { 187 if (chars.length > 0 && checkChars(chars)) 188 this.appendString(new String (chars)); 189 } 190 191 public void keyPressed(KeyEvent event) { 193 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 event) { 202 } 203 204 public void keyReleased(KeyEvent 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 |