1 32 33 package com.nqadmin.swingSet; 34 35 import javax.swing.*; 36 import java.awt.event.*; 37 import java.awt.*; 38 import java.io.ObjectInputStream ; 39 import java.io.IOException ; 40 import java.util.StringTokenizer ; 41 import com.nqadmin.swingSet.datasources.SSRowSet; 42 43 54 public class SSTextField extends JTextField { 55 56 59 public static final int MMDDYYYY = 1; 60 61 64 public static final int DDMMYYYY = 2; 65 66 69 public static final int SSN = 3; 70 71 75 public static final int DECIMAL = 4; 76 77 80 protected SSRowSet sSRowSet; 81 82 85 protected String columnName = ""; 86 87 90 protected int mask = 0; 91 92 95 protected int numberOfDecimalPlaces = 2; 96 97 103 public SSTextField(String _text, int _mask) { 104 super(_text); 105 mask = _mask; 106 init(); 107 } 108 109 114 public SSTextField(int _mask) { 115 super(); 116 mask = _mask; 117 init(); 118 } 119 120 123 public SSTextField() { 124 init(); 125 } 126 127 134 public SSTextField(int _mask, int _numberOfDecimalPlaces) { 135 mask = _mask; 136 numberOfDecimalPlaces = _numberOfDecimalPlaces; 137 init(); 138 } 139 140 159 public SSTextField(int _mask, int _numberOfDecimalPlaces, int _align) { 160 mask = _mask; 161 numberOfDecimalPlaces = _numberOfDecimalPlaces; 162 setHorizontalAlignment(_align); 163 init(); 164 } 165 166 173 public SSTextField(SSRowSet _sSRowSet, String _columnName) { 174 sSRowSet = _sSRowSet; 175 columnName = _columnName; 176 init(); 177 bind(); 178 } 179 180 186 public void setColumnName(String _columnName) { 187 String oldValue = columnName; 188 columnName = _columnName; 189 firePropertyChange("columnName", oldValue, columnName); 190 bind(); 191 } 192 193 198 public String getColumnName() { 199 return columnName; 200 } 201 202 207 public void setSSRowSet(SSRowSet _sSRowSet) { 208 SSRowSet oldValue = sSRowSet; 209 sSRowSet = _sSRowSet; 210 firePropertyChange("sSRowSet", oldValue, sSRowSet); 211 bind(); 212 } 213 214 219 public SSRowSet getSSRowSet() { 220 return sSRowSet; 221 } 222 223 228 public void setMask(int _mask) { 229 int oldValue = mask; 230 mask = _mask; 231 firePropertyChange("mask", oldValue, mask); 232 233 } 235 236 241 public int getMask() { 242 return mask; 243 } 244 245 252 public void setNumberOfDecimalPlaces(int _numberOfDecimalPlaces) { 253 int oldValue = numberOfDecimalPlaces; 254 numberOfDecimalPlaces = _numberOfDecimalPlaces; 255 firePropertyChange("numberOfDecimalPlaces", oldValue, numberOfDecimalPlaces); 256 } 257 258 265 public int getNumberOfDecimalPlaces() { 266 return numberOfDecimalPlaces; 267 } 268 269 275 public void bind(SSRowSet _sSRowSet, String _columnName) { 276 SSRowSet oldValue = sSRowSet; 277 sSRowSet = _sSRowSet; 278 firePropertyChange("sSRowSet", oldValue, sSRowSet); 279 280 String oldValue2 = columnName; 281 columnName = _columnName; 282 firePropertyChange("columnName", oldValue2, columnName); 283 284 bind(); 285 } 286 287 290 protected void init() { 291 292 setPreferredSize(new Dimension(200,20)); 294 295 this.addFocusListener(new FocusAdapter(){ 298 public void focusGained(FocusEvent fe){ 299 SSTextField.this.selectAll(); 300 } 301 }); 302 303 this.addKeyListener(new KeyListener() { 305 306 public void keyReleased(KeyEvent ke) { 307 if(mask == DECIMAL || mask == SSN){ 308 int position = SSTextField.this.getCaretPosition(); 309 mask(ke); 310 SSTextField.this.setCaretPosition(position); 311 } 312 } 313 314 public void keyTyped(KeyEvent ke) { 315 } 316 317 public synchronized void keyPressed(KeyEvent ke) { 318 if (ke.getKeyCode() == KeyEvent.VK_ENTER) { 320 ((Component)ke.getSource()).transferFocus(); 321 } 322 323 if(mask == MMDDYYYY || mask == DDMMYYYY){ 324 mask(ke); 325 } 326 } 327 328 }); 329 330 } 332 335 protected void bind() { 336 337 if (columnName==null || columnName.trim().equals("") || sSRowSet==null) { 339 return; 340 } 341 342 345 setDocument(new SSTextDocument(sSRowSet, columnName)); 347 348 351 } 352 353 358 protected void mask(KeyEvent _ke) { 359 String str = getText(); 361 char ch = _ke.getKeyChar(); 362 363 if (_ke.getKeyCode() == KeyEvent.VK_BACK_SPACE || 365 _ke.getKeyCode() == KeyEvent.VK_DELETE || 366 _ke.getKeyCode() == KeyEvent.VK_END || 367 _ke.getKeyCode() == KeyEvent.VK_ENTER || 368 _ke.getKeyCode() == KeyEvent.VK_ESCAPE) { 369 370 return; 371 } else if ( (_ke.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) == KeyEvent.CTRL_DOWN_MASK || 372 (_ke.getModifiersEx() & KeyEvent.ALT_DOWN_MASK) == KeyEvent.ALT_DOWN_MASK ) { 373 374 return; 375 } else if(!Character.isDefined(ch)) { 376 return; 377 } 378 379 if (getSelectionStart() != getSelectionEnd()) { 380 str = str.substring(0,getSelectionStart()) 381 + str.substring(getSelectionEnd(), str.length()); 382 } 383 384 switch(mask) { 387 case MMDDYYYY: 388 case DDMMYYYY: 389 if (getCaretPosition() < str.length()) { 390 return; 391 } 392 setText(dateMask(str, _ke)); 393 break; 394 case SSN: 395 setText(ssnMask(str, _ke)); 396 break; 397 case DECIMAL: 398 setText(decimalMask(str, numberOfDecimalPlaces)); 399 break; 400 } 402 } 404 413 protected String dateMask(String _str, KeyEvent _ke) { 414 switch(_str.length()) { 415 case 1: 416 if (_ke.getKeyChar() == '/') { 417 _str = "0" + _str ; 418 } 419 break; 420 case 2: 421 if ( _ke.getKeyChar() == '/' ) { 422 } else { 424 _str = _str + "/"; 425 } 426 break; 427 case 4: 428 if ( _ke.getKeyChar() == '/' ){ 429 String newStr = _str.substring(0,3); 430 newStr = newStr + "0" + _str.substring(3,4); 431 _str = newStr; 432 } 433 break; 434 case 5: 435 if ( _ke.getKeyChar() != '/' ) { 436 _str = _str + "/"; 437 } 438 break; 439 } 441 return _str; 442 443 } 445 453 protected String ssnMask(String _str, KeyEvent _ke) { 454 switch(_str.length()) { 455 case 3: 456 case 6: 457 _str = _str + "-"; 458 break; 459 case 5: 460 case 8: 461 if (_ke.getKeyChar() == '-') { 462 _str = _str.substring(0,_str.length()-1); 463 } 464 break; 465 } 466 467 return _str; 468 469 } 470 471 479 protected String decimalMask(String _str, int numberOfDecimalPlaces) { 480 StringTokenizer strtok = new StringTokenizer (_str,".",false); 481 String intPart = ""; 482 String decimalPart = ""; 483 String returnStr = _str; 484 if (strtok.hasMoreTokens()) { 486 intPart = strtok.nextToken(); 487 } 488 if (strtok.hasMoreTokens()) { 489 decimalPart = strtok.nextToken(); 490 } 491 if ( decimalPart.length() > numberOfDecimalPlaces ) { 494 returnStr = intPart +"."+ decimalPart.substring(0,numberOfDecimalPlaces); 495 } 496 497 return returnStr; 498 } 499 500 } 502 503 504 | Popular Tags |