1 33 34 package com.nqadmin.swingSet.formatting; 35 36 import com.nqadmin.swingSet.SSDataNavigator; 37 import com.nqadmin.swingSet.datasources.SSRowSet; 38 39 import java.awt.*; 40 import java.awt.event.*; 41 import javax.swing.*; 42 43 import javax.sql.RowSetListener ; 44 import javax.swing.JFormattedTextField ; 45 import java.util.Set ; 46 import java.util.HashSet ; 47 48 import com.nqadmin.swingSet.SSDataNavigator; 49 import com.nqadmin.swingSet.formatting.helpers.*; 50 51 52 62 public class SSFormattedTextField extends JFormattedTextField implements RowSetListener , KeyListener, FocusListener, MouseListener { 63 64 private JPopupMenu menu = null; 65 private JPopupMenu calculator = null; 66 private JPopupMenu helper = null; 67 68 private java.awt.Color std_color = null; 69 private String columnName = null; 70 private int colType = -99; 71 private SSDataNavigator navigator = null; 72 private SSRowSet rowset = null; 73 74 75 76 public SSFormattedTextField() { 77 super(); 78 79 83 84 Set forwardKeys = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); 85 Set newForwardKeys = new HashSet (forwardKeys); 86 newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)); 87 newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, java.awt.event.InputEvent.SHIFT_MASK )); 88 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,newForwardKeys); 89 90 Set backwardKeys = getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS); 91 Set newBackwardKeys = new HashSet (backwardKeys); 92 newBackwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_UP, java.awt.event.InputEvent.SHIFT_MASK )); 93 setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,newBackwardKeys); 94 95 99 addKeyListener(this); 100 101 105 addFocusListener(this); 106 107 111 addMouseListener(this); 112 113 114 117 menu = new JPopupMenu(); 118 menu.add("Opcion 1"); 119 menu.add("Opcion 2"); 120 121 125 setInputVerifier(new internalVerifier()); 126 } 127 128 public SSFormattedTextField(javax.swing.JFormattedTextField.AbstractFormatterFactory factory) { 129 this(); 130 this.setFormatterFactory(factory); 131 } 132 133 public void setColumnName(String columnName) { 134 this.columnName = columnName; 135 bind(); 136 } 137 138 public String getColumnName() { 139 return columnName; 140 } 141 142 148 public void setRowSet(SSRowSet rowset) { 149 this.rowset = rowset; 150 bind(); 151 } 152 153 public void setSSRowSet(SSRowSet rowset) { 154 this.rowset = rowset; 155 } 156 157 public SSRowSet getSSRowSet() { 158 return rowset; 159 } 160 161 public void setNavigator(SSDataNavigator navigator) { 162 this.navigator = navigator; 163 setSSRowSet(navigator.getSSRowSet()); 164 bind(); 165 } 166 167 public SSDataNavigator getNavigator() { 168 return this.navigator; 169 } 170 171 private void bind() { 172 173 if (columnName == null || rowset == null) return; 174 175 try { 176 colType = rowset.getColumnType(columnName); 177 } catch(java.sql.SQLException sqe) { 178 System.out.println("bind error = " + sqe); 179 } 180 181 rowset.addRowSetListener(this); 182 183 DbToFm(); 184 } 185 186 public void rowSetChanged(javax.sql.RowSetEvent event) { 187 189 } 190 191 public void rowChanged(javax.sql.RowSetEvent event) { 192 194 } 195 196 public void cursorMoved(javax.sql.RowSetEvent event) { 197 201 DbToFm(); 202 } 203 204 public void keyTyped(KeyEvent e) { 205 206 } 207 208 public void keyReleased(KeyEvent e) { 209 210 } 211 212 217 public void keyPressed(KeyEvent e) { 218 219 if (e.getKeyCode() == KeyEvent.VK_F1) { 220 showHelper(e); 221 } 222 223 if (e.getKeyCode() == KeyEvent.VK_F2) { 224 225 } 226 227 if (e.getKeyCode() == KeyEvent.VK_F3) { 228 229 System.out.println("F3 "); 230 calculator = new javax.swing.JPopupMenu (); 231 JFormattedTextField ob = (JFormattedTextField )(e.getSource()); 233 java.awt.Dimension d = ob.getSize(); 234 calculator.show(ob, 0, d.height); 235 } 236 237 if (e.getKeyCode() == KeyEvent.VK_F4) { 238 System.out.println("F4 "); 239 } 240 241 if (e.getKeyCode() == KeyEvent.VK_F5) { 242 System.out.println("F5 = PROCESS"); 243 navigator.doCommitButtonClick(); 244 } 245 246 if (e.getKeyCode() == KeyEvent.VK_F6) { 247 System.out.println("F6 = DELETE"); 248 navigator.doDeleteButtonClick(); 249 } 250 251 if (e.getKeyCode() == KeyEvent.VK_F8) { 252 System.out.println("F8 "); 253 navigator.doUndoButtonClick(); 254 } 255 256 if (e.getKeyCode() == KeyEvent.VK_END) { 257 System.out.println("END "); 258 } 259 260 if (e.getKeyCode() == KeyEvent.VK_DELETE) { 261 System.out.println("DELETE "); 262 } 263 264 if (e.getKeyCode() == KeyEvent.VK_HOME) { 265 System.out.println("HOME "); 266 } 267 } 268 269 public void focusLost(FocusEvent e) { 270 274 setBackground(std_color); 275 } 276 277 public void focusGained(FocusEvent e) { 278 279 283 java.awt.Color col = new java.awt.Color (204,255,255); 284 std_color = getBackground(); 285 setBackground(col); 286 287 288 293 SwingUtilities.invokeLater(new Runnable () { 294 public void run() { 295 selectAll(); 296 } 297 }); 298 } 299 300 305 306 private void DbToFm() { 307 308 try { 309 310 switch(colType) { 311 312 case java.sql.Types.ARRAY: break; 314 315 case java.sql.Types.BINARY: break; 317 318 case java.sql.Types.BIT: case java.sql.Types.BOOLEAN: this.setValue(new Boolean (rowset.getBoolean(columnName))); 321 break; 322 323 case java.sql.Types.BLOB: break; 325 326 case java.sql.Types.CLOB: break; 328 329 case java.sql.Types.DATALINK: break; 331 332 case java.sql.Types.DATE: this.setValue(new java.util.Date (rowset.getDate(columnName).getTime())); 334 break; 335 336 case java.sql.Types.DECIMAL: break; 338 339 case java.sql.Types.DISTINCT: break; 341 342 case java.sql.Types.DOUBLE: break; 344 345 case java.sql.Types.FLOAT: break; 347 348 case java.sql.Types.INTEGER: case java.sql.Types.BIGINT: case java.sql.Types.SMALLINT: case java.sql.Types.TINYINT: this.setValue(new Integer (rowset.getInt(columnName))); 353 break; 354 355 case java.sql.Types.JAVA_OBJECT: break; 357 358 case java.sql.Types.LONGVARBINARY: case java.sql.Types.VARBINARY: break; 361 362 case java.sql.Types.VARCHAR: case java.sql.Types.LONGVARCHAR: case java.sql.Types.CHAR: this.setValue(rowset.getString(columnName)); 366 break; 367 368 case java.sql.Types.NULL: break; 370 371 case java.sql.Types.NUMERIC: break; 373 374 case java.sql.Types.OTHER: break; 376 377 case java.sql.Types.REAL: break; 379 380 case java.sql.Types.REF: break; 382 383 case java.sql.Types.STRUCT: break; 385 386 case java.sql.Types.TIME: break; 388 389 case java.sql.Types.TIMESTAMP: break; 391 392 default: 393 break; 394 } 395 } catch (java.sql.SQLException sqe) { 396 System.out.println("Error in DbToFm() = " + sqe); 397 } 398 } 399 400 public void mouseExited(MouseEvent e) { 401 } 403 404 public void mouseEntered(MouseEvent e) { 405 } 407 408 public void mouseClicked(MouseEvent e) { 409 } 412 413 public void mousePressed(MouseEvent evt) { 414 if (evt.isPopupTrigger()) { 416 menu.show(evt.getComponent(), evt.getX(), evt.getY()); 418 } 419 } 420 421 public void mouseReleased(MouseEvent evt) { 422 if (evt.isPopupTrigger()) { 424 menu.show(evt.getComponent(), evt.getX(), evt.getY()); 426 } 427 } 428 429 public void setHelper(JPopupMenu helper) { 430 this.helper = helper; 431 432 if (helper instanceof HelperPopup) 433 ((HelperPopup)this.helper).setTarget(this); 434 435 if (helper instanceof RowSetHelperPopup) 436 ((RowSetHelperPopup)this.helper).setTarget(this); 437 438 } 439 440 public void showHelper(KeyEvent e) { 441 if (helper == null) return; 442 443 JFormattedTextField ob = (JFormattedTextField )(e.getSource()); 444 java.awt.Dimension d = ob.getSize(); 445 helper.requestFocusInWindow(); 446 helper.show(ob, 0, d.height); 447 } 448 449 454 455 class internalVerifier extends InputVerifier { 456 457 public boolean verify(JComponent input) { 458 459 Object aux = null; 460 boolean passed = true; 461 462 465 466 SSFormattedTextField tf = (SSFormattedTextField) input; 467 aux = tf.getValue(); 468 469 474 478 488 489 493 System.out.println("inputVerifier(): " + columnName); 494 495 499 if (passed == true) { 500 501 setBackground(java.awt.Color.WHITE); 502 503 try { 504 tf.commitEdit(); 505 System.out.println("Committed();"); 506 } catch (java.text.ParseException pe) { 507 System.out.println("ParseException"); 508 } 509 510 try { 511 rowset.removeRowSetListener(tf); 512 513 aux = tf.getValue(); 514 if (aux == null) { 515 return false; 516 } 517 518 switch(colType) { 519 520 case java.sql.Types.ARRAY: break; 522 523 case java.sql.Types.BINARY: break; 525 526 case java.sql.Types.BIT: System.out.println("BIT - Set"); 528 rowset.updateBoolean(columnName, ((Boolean )tf.getValue()).booleanValue()); 529 break; 530 531 case java.sql.Types.BLOB: break; 533 534 case java.sql.Types.BOOLEAN: System.out.println("BOOLEAN - Set"); 536 break; 537 538 case java.sql.Types.CLOB: break; 540 541 case java.sql.Types.DATALINK: break; 543 544 case java.sql.Types.DATE: rowset.updateDate(columnName, new java.sql.Date (((java.util.Date ) aux).getTime())); 546 break; 547 548 case java.sql.Types.DECIMAL: System.out.println("DECIMAL"); 550 break; 551 552 case java.sql.Types.DISTINCT: break; 554 555 case java.sql.Types.FLOAT: case java.sql.Types.DOUBLE: System.out.println("DOUBLE"); 558 if (aux instanceof Double ) { 559 System.out.println("Double = columnName => " + columnName); 560 System.out.println("getValue() = " + aux); 561 rowset.updateDouble(columnName, ((Double )aux).intValue()); 562 } else if (aux instanceof Float ) { 563 System.out.println("Float = columnName => " + columnName); 564 System.out.println("getValue() = " + aux); 565 rowset.updateFloat(columnName, ((Float )aux).intValue()); 566 } else { 567 System.out.println("ELSE ???"); 568 } 569 System.out.println("getValue() = " + aux); 570 break; 571 572 case java.sql.Types.INTEGER: 574 case java.sql.Types.BIGINT: 576 case java.sql.Types.SMALLINT: 578 case java.sql.Types.TINYINT: 580 if (aux instanceof Integer ) { 581 System.out.println("Integer = columnName => " + columnName); 582 System.out.println("getValue() = " + aux); 583 rowset.updateInt(columnName, ((Integer )aux).intValue()); 584 } else if (aux instanceof Long ) { 585 System.out.println("Long = columnName => " + columnName); 586 System.out.println("getValue() = " + aux); 587 rowset.updateLong(columnName, ((Long )aux).intValue()); 588 } else { 589 System.out.println("ELSE ???"); 590 } 591 System.out.println("getValue() = " + aux); 592 break; 593 594 case java.sql.Types.JAVA_OBJECT: break; 596 597 case java.sql.Types.LONGVARBINARY: case java.sql.Types.VARBINARY: break; 600 601 case java.sql.Types.VARCHAR: case java.sql.Types.LONGVARCHAR: case java.sql.Types.CHAR: System.out.println("CHAR = columnName => " + columnName); 605 System.out.println("getValue() = " + aux); 606 rowset.updateString(columnName, aux.toString()); 607 break; 608 609 case java.sql.Types.NULL: break; 611 612 case java.sql.Types.NUMERIC: break; 614 615 case java.sql.Types.OTHER: break; 617 618 case java.sql.Types.REAL: break; 620 621 case java.sql.Types.REF: break; 623 624 case java.sql.Types.STRUCT: break; 626 627 case java.sql.Types.TIME: break; 629 630 case java.sql.Types.TIMESTAMP: break; 632 633 default: 634 System.out.println("============================================================================"); 635 System.out.println("default = " + colType); 636 System.out.println("columnName = " + columnName); 637 System.out.println("============================================================================"); 638 639 if (aux instanceof java.lang.Double 640 && ((java.lang.Double ) aux).doubleValue() < 0.0) { 641 tf.setForeground(Color.RED); 642 } else { 643 tf.setForeground(Color.BLACK); 644 } 645 break; 646 } 647 rowset.addRowSetListener(tf); 648 } catch (java.sql.SQLException se) { 649 System.out.println("---> SQLException -----------> " + se); 650 } catch(java.lang.NullPointerException np) { 651 System.out.println("---> NullPointerException ---> " + np); 652 } 653 return true; 654 } else { 655 659 660 setBackground(java.awt.Color.RED); 661 return false; 662 } 663 } 664 } 665 } 666 667 703 | Popular Tags |