1 32 33 package com.nqadmin.swingSet; 34 35 import javax.swing.table.*; 36 import javax.swing.JOptionPane ; 37 import javax.swing.JTable ; 38 import java.awt.Component ; 39 import java.sql.*; 40 import java.util.HashMap ; 41 import java.util.Set ; 42 import java.util.Iterator ; 43 import java.util.StringTokenizer ; 44 import com.nqadmin.swingSet.datasources.SSRowSet; 45 46 62 public class SSTableModel extends AbstractTableModel { 63 64 protected SSRowSet rowset = null; 65 66 69 protected transient int rowCount = 0; 70 71 74 protected transient int columnCount = 0; 75 76 79 protected HashMap defaultValuesMap = null; 80 81 84 protected boolean inInsertRow = false; 85 86 89 protected transient Component component = null; 90 91 94 protected transient JTable table = null; 95 96 99 protected transient String [] headers = null; 100 101 104 int primaryColumn = -1; 105 106 110 protected SSDataValue dataValue = null; 111 112 117 protected SSCellEditing cellEditing = null; 118 119 122 protected int[] uneditableColumns = null; 123 124 127 protected int[] hiddenColumns = null; 128 129 132 protected boolean allowInsertion = true; 133 134 139 public SSTableModel() { 140 super(); 141 } 142 143 149 public SSTableModel(SSRowSet _rowset) throws SQLException { 150 super(); 151 rowset = _rowset; 152 init(); 153 } 154 155 161 public void setSSRowSet(SSRowSet _rowset) throws SQLException { 162 rowset = _rowset; 163 init(); 164 } 165 166 173 public void setSSCellEditing(SSCellEditing _cellEditing) { 174 cellEditing = _cellEditing; 175 } 176 177 182 public void setInsertion(boolean _insert){ 183 allowInsertion = _insert; 184 } 185 186 190 protected void init() { 191 try { 192 193 columnCount = rowset.getColumnCount(); 194 rowset.last(); 195 rowCount = rowset.getRow(); 198 rowset.first(); 199 200 this.fireTableDataChanged(); 203 205 } catch(SQLException se) { 206 se.printStackTrace(); 207 } 208 } 209 210 216 public int getColumnCount() { 217 return columnCount ; 218 } 219 220 226 public int getRowCount() { 227 if(allowInsertion) 230 return rowCount +1; 231 return rowCount; 233 } 234 235 244 public boolean isCellEditable(int _row, int _column) { 245 246 if (uneditableColumns != null) { 252 for (int i=0; i<uneditableColumns.length;i++) { 253 if (_column == uneditableColumns[i]) { 254 return false; 255 } 256 } 257 } 258 if (cellEditing != null) { 259 return cellEditing.isCellEditable(_row, _column); 260 } 261 262 return true; 263 264 } 266 274 public Object getValueAt(int _row, int _column) { 275 276 Object value = null; 277 if (_row == rowCount) { 278 value = getDefaultValue(_column); 279 return value; 280 } 281 282 try { 283 rowset.absolute(_row + 1); 285 int type = rowset.getColumnType(_column + 1); 287 switch(type) { 288 case Types.INTEGER: 289 case Types.SMALLINT: 290 case Types.TINYINT: 291 value = new Integer (rowset.getInt(_column+1)); 292 break; 293 case Types.BIGINT: 294 value = new Long (rowset.getLong(_column+1)); 295 break; 296 case Types.FLOAT: 297 value = new Float (rowset.getFloat(_column+1)); 298 break; 299 case Types.DOUBLE: 300 case Types.NUMERIC: 301 value = new Double (rowset.getDouble(_column+1)); 302 break; 303 case Types.BOOLEAN: 304 case Types.BIT: 305 value = new Boolean (rowset.getBoolean(_column+1)); 306 break; 307 case Types.DATE: 308 case Types.TIMESTAMP: 309 value = rowset.getDate(_column+1); 310 break; 311 case Types.CHAR: 312 case Types.VARCHAR: 313 case Types.LONGVARCHAR: 314 value = rowset.getString(_column+1); 315 break; 316 default: 317 System.out.println("SSTableModel.getValueAt(): Unknown data type"); 318 } 319 } catch(SQLException se) { 320 se.printStackTrace(); 321 if (component != null) { 322 JOptionPane.showMessageDialog(component,"Error while retrieving value.\n" + se.getMessage()); 323 } 324 325 } 326 327 return value; 328 329 } 331 338 public void setValueAt(Object _value, int _row, int _column) { 339 340 if (cellEditing != null) { 343 boolean allowEdit; 346 347 if (_row == rowCount) { 349 allowEdit = cellEditing.cellUpdateRequested(_row, _column, null, _value); 350 } else { 351 allowEdit = cellEditing.cellUpdateRequested(_row, _column, getValueAt(_row,_column), _value); 352 } 353 354 if (!allowEdit) { 357 return; 358 } 359 } 360 361 if ( _row == rowCount ) { 364 insertRow(_value,_column); 365 return; 366 } 367 368 try{ 370 if (rowset.getRow() != _row +1) { 372 rowset.absolute(_row +1); 373 } 374 if ( _value == null) { 375 rowset.updateNull(_column+1); 376 return; 377 } 378 379 int type = rowset.getColumnType(_column + 1); 380 switch(type) { 381 case Types.INTEGER: 382 case Types.SMALLINT: 383 case Types.TINYINT: 384 rowset.updateInt(_column+1, ((Integer )_value).intValue()); 385 break; 386 case Types.BIGINT: 387 rowset.updateLong(_column+1,((Long )_value).longValue()); 389 break; 390 case Types.FLOAT: 391 rowset.updateFloat(_column+1, ((Float )_value).floatValue()); 392 break; 393 case Types.DOUBLE: 394 case Types.NUMERIC: 395 rowset.updateDouble(_column+1, ((Double )_value).doubleValue()); 396 break; 397 case Types.BOOLEAN: 398 case Types.BIT: 399 rowset.updateBoolean(_column+1, ((Boolean )_value).booleanValue()); 400 break; 401 case Types.DATE: 402 case Types.TIMESTAMP: 403 if (_value instanceof String ) { 407 if (getSQLDate((String )_value) == null) { 408 rowset.updateNull(_column+1); 409 } else { 410 rowset.updateDate(_column+1,getSQLDate((String )_value)); 411 } 412 } else { 413 rowset.updateDate(_column+1,(Date)_value); 414 } 415 break; 416 case Types.CHAR: 417 case Types.VARCHAR: 418 case Types.LONGVARCHAR: 419 rowset.updateString(_column+1, (String )_value); 420 break; 421 default: 422 System.out.println("SSTableModel.setValueAt(): Unknown data type"); 423 } 424 rowset.updateRow(); 425 } catch(SQLException se) { 427 se.printStackTrace(); 428 if (component != null) { 429 JOptionPane.showMessageDialog(component,"Error while updating the value.\n" + se.getMessage()); 430 } 431 } 432 433 } 435 444 protected void insertRow(Object _value, int _column) { 445 if (_value == null) { 446 return; 447 } 448 449 try { 450 if (!inInsertRow) { 452 rowset.moveToInsertRow(); 453 setDefaults(); 455 inInsertRow = true; 456 if (dataValue != null) { 458 setPrimaryColumn(); 459 } 460 461 } 462 463 int type = rowset.getColumnType(_column + 1); 464 465 switch(type) { 466 case Types.INTEGER: 467 case Types.SMALLINT: 468 case Types.TINYINT: 469 rowset.updateInt(_column+1, ((Integer )_value).intValue()); 470 break; 471 case Types.BIGINT: 472 rowset.updateLong(_column+1,((Long )_value).longValue()); 474 break; 475 case Types.FLOAT: 476 rowset.updateFloat(_column+1, ((Float )_value).floatValue()); 477 break; 478 case Types.DOUBLE: 479 case Types.NUMERIC: 480 rowset.updateDouble(_column+1, ((Double )_value).doubleValue()); 481 break; 482 case Types.BOOLEAN: 483 case Types.BIT: 484 rowset.updateBoolean(_column+1, ((Boolean )_value).booleanValue()); 485 break; 486 case Types.DATE: 487 if (_value instanceof String ) { 488 rowset.updateDate(_column+1,getSQLDate((String )_value)); 489 } else { 490 rowset.updateDate(_column+1,(Date)_value); 491 } 492 break; 493 case Types.CHAR: 494 case Types.VARCHAR: 495 case Types.LONGVARCHAR: 496 rowset.updateString(_column+1, (String )_value); 497 break; 498 default: 499 System.out.println("SSTableModel.setValueAt(): Unknown data type"); 500 } 501 502 rowset.insertRow(); 503 if (rowCount != 0) { 504 rowset.moveToCurrentRow(); 505 } else { 506 rowset.first(); 507 } 508 rowset.refreshRow(); 509 if (table != null) { 511 table.updateUI(); 512 } 513 inInsertRow = false; 514 rowCount++; 515 516 } catch(SQLException se) { 517 se.printStackTrace(); 518 if (component != null) { 519 JOptionPane.showMessageDialog(component,"Error while trying to insert row.\n" + se.getMessage()); 520 } 521 } 522 524 } 526 529 protected void setDefaults() { 530 if (defaultValuesMap == null) { 531 return; 532 } 533 534 Set keySet = defaultValuesMap.keySet(); 535 Iterator iterator = keySet.iterator(); 536 try { 537 while (iterator.hasNext()) { 538 Integer column = (Integer )iterator.next(); 539 int type = rowset.getColumnType(column.intValue() +1 ); 542 switch(type) { 543 case Types.INTEGER: 544 case Types.SMALLINT: 545 case Types.TINYINT: 546 rowset.updateInt(column.intValue()+1, ((Integer )defaultValuesMap.get(column)).intValue()); 547 break; 548 case Types.BIGINT: 549 rowset.updateLong(column.intValue()+1, ((Long )defaultValuesMap.get(column)).longValue()); 550 break; 551 case Types.FLOAT: 552 rowset.updateFloat(column.intValue()+1, ((Float )defaultValuesMap.get(column)).floatValue()); 553 break; 554 case Types.DOUBLE: 555 case Types.NUMERIC: 556 rowset.updateDouble(column.intValue()+1, ((Double )defaultValuesMap.get(column)).doubleValue()); 557 break; 558 case Types.BOOLEAN: 559 case Types.BIT: 560 rowset.updateBoolean(column.intValue()+1, ((Boolean )defaultValuesMap.get(column)).booleanValue()); 561 break; 562 case Types.DATE: 563 rowset.updateDate(column.intValue()+1, (Date)defaultValuesMap.get(column)); 564 break; 565 case Types.CHAR: 566 case Types.VARCHAR: 567 case Types.LONGVARCHAR: 568 rowset.updateString(column.intValue()+1, (String )defaultValuesMap.get(column)); 569 break; 570 default: 571 System.out.println("SSTableModel.setValueAt(): Unknown data type"); 572 } 574 } 576 } catch(SQLException se) { 577 se.printStackTrace(); 578 if (component != null) { 579 JOptionPane.showMessageDialog(component,"Error while inserting row.\n" + se.getMessage()); 580 } 581 } 582 } 584 591 public Class getColumnClass(int _column) { 592 int type; 593 try { 594 type = rowset.getColumnType(_column+1); 595 } catch (SQLException e) { 596 return super.getColumnClass(_column); 597 } 598 599 switch(type) { 600 case Types.INTEGER: 601 case Types.SMALLINT: 602 case Types.TINYINT: 603 return Integer .class; 604 605 case Types.BIGINT: 606 return Long .class; 607 608 case Types.FLOAT: 609 return Float .class; 610 611 case Types.DOUBLE: 612 case Types.NUMERIC: 613 return Double .class; 614 615 case Types.BOOLEAN: 616 case Types.BIT: 617 return Boolean .class; 618 619 case Types.DATE: 620 return java.sql.Date .class; 621 622 case Types.CHAR: 623 case Types.VARCHAR: 624 case Types.LONGVARCHAR: 625 return String .class; 626 627 default: 628 return Object .class; 629 } 630 631 } 633 641 public boolean deleteRow(int _row) { 642 643 if (_row < rowCount) { 644 try { 645 rowset.absolute(_row +1); 646 rowset.deleteRow(); 647 rowCount--; 648 return true; 649 } catch(SQLException se) { 650 se.printStackTrace(); 651 if (component != null) { 652 JOptionPane.showMessageDialog(component,"Error while deleting row.\n" + se.getMessage()); 653 } 654 655 } 656 } 657 658 return false; 659 660 } 662 669 public void setDefaultValues(int[] _columnNumbers, Object [] _values) { 670 if (_columnNumbers == null || _values == null) { 671 defaultValuesMap = null; 672 } 673 674 if (defaultValuesMap == null) { 675 defaultValuesMap = new HashMap (); 676 } else { 677 defaultValuesMap.clear(); 678 } 679 680 for (int i=0;i<_columnNumbers.length;i++) { 681 defaultValuesMap.put(new Integer (_columnNumbers[i]), _values[i]); 682 } 683 } 684 685 694 public Object getDefaultValue(int _columnNumber) { 695 Object value = null; 696 if (defaultValuesMap != null) { 697 value = defaultValuesMap.get(new Integer (_columnNumber)); 698 } 699 return value; 700 } 701 702 708 public void setMessageWindow(Component _component) { 709 component = _component; 710 } 711 712 718 public void setJTable(JTable _table) { 719 table = _table; 720 } 721 722 730 public void setPrimaryColumn(int _columnNumber) { 731 primaryColumn = _columnNumber; 732 } 733 734 740 public void setSSDataValue(SSDataValue _dataValue) { 741 dataValue = _dataValue; 742 } 743 744 748 protected void setPrimaryColumn() { 749 try { 750 751 int type = rowset.getColumnType(primaryColumn +1); 752 753 switch(type) { 754 case Types.INTEGER: 755 case Types.SMALLINT: 756 case Types.TINYINT: 757 rowset.updateInt(primaryColumn+1,((Integer )dataValue.getPrimaryColumnValue()).intValue()); 758 break; 759 case Types.BIGINT: 760 rowset.updateLong(primaryColumn+1,((Long )dataValue.getPrimaryColumnValue()).longValue()); 761 break; 762 case Types.FLOAT: 763 rowset.updateFloat(primaryColumn+1,((Float )dataValue.getPrimaryColumnValue()).floatValue()); 764 break; 765 case Types.DOUBLE: 766 case Types.NUMERIC: 767 rowset.updateDouble(primaryColumn+1,((Double )dataValue.getPrimaryColumnValue()).doubleValue()); 768 break; 769 case Types.BOOLEAN: 770 case Types.BIT: 771 rowset.updateBoolean(primaryColumn+1,((Boolean )dataValue.getPrimaryColumnValue()).booleanValue()); 772 break; 773 case Types.DATE: 774 rowset.updateDate(primaryColumn+1,(Date)dataValue.getPrimaryColumnValue()); 775 break; 776 case Types.CHAR: 777 case Types.VARCHAR: 778 case Types.LONGVARCHAR: 779 rowset.updateString(primaryColumn+1,(String )dataValue.getPrimaryColumnValue()); 780 break; 781 default: 782 System.out.println("SSTableModel.setPrimaryColumn(): Unknown data type"); 783 } 784 } catch(SQLException se) { 785 se.printStackTrace(); 786 if (component != null) { 787 JOptionPane.showMessageDialog(component,"Error while inserting Primary Key value.\n" + se.getMessage()); 788 } 789 } 790 } 792 793 800 protected Date getSQLDate(String _strDate) { 801 if (_strDate.trim().equals("")) { 802 return null; 803 } 804 String newStrDate = _strDate; 805 if(_strDate.indexOf("/") != -1){ 806 StringTokenizer strtok = new StringTokenizer (_strDate,"/",false); 807 String month = strtok.nextToken(); 808 String day = strtok.nextToken(); 809 newStrDate = strtok.nextToken() + "-" + month + "-" + day; 810 } 811 return Date.valueOf(newStrDate); 812 } 813 814 820 public void setHeaders(String [] _headers) { 821 headers = _headers; 822 } 823 824 832 public String getColumnName(int _columnNumber) { 833 if (headers != null) { 834 if (_columnNumber < headers.length) { 835 return headers[_columnNumber]; 837 } 838 } 839 return ""; 841 } 842 843 851 public void setUneditableColumns(int[] _columnNumbers) { 852 uneditableColumns = _columnNumbers; 853 } 854 855 868 public void setHiddenColumns(int[] _columnNumbers) { 869 hiddenColumns = _columnNumbers; 870 } 871 872 873 874 876 885 public void setRowSet(SSRowSet _rowset) throws SQLException { 886 rowset = _rowset; 887 init(); 888 } 889 890 } 892 893 894 | Popular Tags |