KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nqadmin > swingSet > SSTableModel


1 /* $Id: SSTableModel.java,v 1.19 2005/03/09 21:45:26 prasanth Exp $
2  *
3  * Tab Spacing = 4
4  *
5  * Copyright (c) 2003-2005, The Pangburn Company and Prasanth R. Pasala
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer. Redistributions in binary
13  * form must reproduce the above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or other materials
15  * provided with the distribution. The names of its contributors may not be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package com.nqadmin.swingSet;
34
35 import javax.swing.table.*;
36 import javax.swing.JOptionPane JavaDoc;
37 import javax.swing.JTable JavaDoc;
38 import java.awt.Component JavaDoc;
39 import java.sql.*;
40 import java.util.HashMap JavaDoc;
41 import java.util.Set JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.StringTokenizer JavaDoc;
44 import com.nqadmin.swingSet.datasources.SSRowSet;
45
46 /**
47  * SSTableModel.java
48  *<p>
49  * SwingSet - Open Toolkit For Making Swing Controls Database-Aware
50  *<p><pre>
51  * SSTableModel provides an implementation of the TableModel interface.
52  * The SSDataGrid uses this class for providing a grid view for a SSRowSet.
53  * SSTableModel can be used without the SSDataGrid (e.g. in conjunction with a
54  * JTable), but the cell renderers and hidden columns features of the SSDataGrid
55  * will not be available.
56  *
57  * SSTableModel can be used with a JTable to get a Grid view of the data.
58  *</pre><p>
59  * @author $Author: prasanth $
60  * @version $Revision: 1.19 $
61  */

62 public class SSTableModel extends AbstractTableModel {
63
64     protected SSRowSet rowset = null;
65
66     /**
67      * Number of rows in the SSRowSet.
68      */

69     protected transient int rowCount = 0;
70     
71     /**
72      * Number of columns in the SSRowSet.
73      */

74     protected transient int columnCount = 0;
75
76     /**
77      * Map to store the default values of different columns.
78      */

79     protected HashMap JavaDoc defaultValuesMap = null;
80
81     /**
82      * Indicator to determine if the SSRowSet is on the insertion row.
83      */

84     protected boolean inInsertRow = false;
85
86     /**
87      * Window where messages should be displayed.
88      */

89     protected transient Component JavaDoc component = null;
90
91     /**
92      * JTable being modeled.
93      */

94     protected transient JTable JavaDoc table = null;
95
96     /**
97      * JTable headers.
98      */

99     protected transient String JavaDoc[] headers = null;
100
101     /**
102      * Column containing primary key.
103      */

104     int primaryColumn = -1;
105
106     /**
107      * Implementation of SSDataValue interface used to determine PK value for
108      * new rows.
109      */

110     protected SSDataValue dataValue = null;
111     
112     /**
113      * Implementation of SSCellEditing interface used to determine dynamically
114      * if a given cell can be edited and to determine if a given value is
115      * valid.
116      */

117     protected SSCellEditing cellEditing = null;
118
119     /**
120      * List of uneditable columns.
121      */

122     protected int[] uneditableColumns = null;
123
124     /**
125      * List of hidden columns.
126      */

127     protected int[] hiddenColumns = null;
128
129     /**
130      * Indicator to determine if insertions are allowed.
131      */

132     protected boolean allowInsertion = true;
133
134     /**
135      * Constructs a SSTableModel object.
136      * If this contructor is used the setSSRowSet() method has to be used to set the SSRowSet
137      * before constructing the JTable.
138      */

139     public SSTableModel() {
140         super();
141     }
142
143     /**
144      * Constructs a SSTableModel object with the given SSRowSet.
145      * This will call the execute method on the given SSRowSet.
146      *
147      * @param _rowset SSRowSet object whose records has to be displayed in JTable.
148      */

149     public SSTableModel(SSRowSet _rowset) throws SQLException {
150         super();
151         rowset = _rowset;
152         init();
153     }
154
155     /**
156      * Sets the SSRowSet for SSTableModel to the given SSRowSet.
157      * This SSRowSet will be used to get the data for JTable.
158      *
159      * @param _rowset SSRowSet object whose records has to be displayed in JTable.
160      */

161     public void setSSRowSet(SSRowSet _rowset) throws SQLException {
162         rowset = _rowset;
163         init();
164     }
165
166     /**
167      * Used to set an implementation of SSCellEditing interface which can be
168      * used to determine dynamically if a given cell can be edited and to
169      * determine if a given value is valid.
170      *
171      * @param _cellEditing implementation of SSCellEditing interface.
172      */

173      public void setSSCellEditing(SSCellEditing _cellEditing) {
174         cellEditing = _cellEditing;
175      }
176
177      /**
178       * Sets row insertion indicator.
179       *
180       *@param _insert true if user can insert new rows, else false.
181       */

182      public void setInsertion(boolean _insert){
183         allowInsertion = _insert;
184      }
185
186     /**
187      * Initializes the SSTableModel. (Gets the column count and row count for the
188      * given SSRowSet.)
189      */

190     protected void init() {
191         try {
192
193             columnCount = rowset.getColumnCount();
194             rowset.last();
195             // ROWS IN THE SSROWSET ARE NUMBERED FROM 1, SO LAST ROW NUMBER GIVES THE
196
// ROW COUNT
197
rowCount = rowset.getRow();
198             rowset.first();
199
200 // following code added 11-01-2004 per forum suggestion from Diego Gil (dags)
201
// IF DATA CHANGES, ALERT LISTENERS
202
this.fireTableDataChanged();
203 // end additions
204

205         } catch(SQLException se) {
206             se.printStackTrace();
207         }
208     }
209
210     /**
211      * Returns the number of columns in the model. A JTable uses this method to
212      * determine how many columns it should create and display by default.
213      *
214      * @return the number of columns in the SSTableModel
215      */

216     public int getColumnCount() {
217         return columnCount ;
218     }
219
220     /**
221      * Returns the number of rows in the model. A JTable uses this method to determine
222      * how many rows it should display.
223      *
224      * @return the number of rows in the SSTableModel
225      */

226     public int getRowCount() {
227         // RETURN THE NUMBER OF ROWS AS ONE GREATER THAN THOSE IN DATABASE
228
// ITS USED FOR INSERTING NEW ROWS
229
if(allowInsertion)
230             return rowCount +1;
231         // IF INSERTION IS NOT ALLOWED THEN RETURN THE ACTUAL ROW COUNT
232
return rowCount;
233     }
234
235     /**
236      * Returns true if the cell at rowIndex and columnIndex is editable. Otherwise,
237      * a call to setValueAt() on the cell will not change the value of that cell.
238      *
239      * @param _row the row whose value to be queried
240      * @param _column the column whose value to be queried
241      *
242      * @return editable indicator for cell at row and column specified
243      */

244     public boolean isCellEditable(int _row, int _column) {
245
246 // if(rowset.isReadOnly()){
247
// System.out.println("Is Cell Editable : false");
248
// return false;
249
// }
250
// System.out.println("Is Cell Editable : true");
251
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     } // end public boolean isCellEditable(int _row, int _column) {
265

266     /**
267      * Returns the value for the cell at the specified row & column.
268      *
269      * @param _row the row whose value to be queried.
270      * @param _column the column whose value to be queried.
271      *
272      * @return value at the requested cell.
273      */

274     public Object JavaDoc getValueAt(int _row, int _column) {
275
276         Object JavaDoc value = null;
277         if (_row == rowCount) {
278             value = getDefaultValue(_column);
279             return value;
280         }
281
282         try {
283             // ROW NUMBERS IN SSROWSET START FROM 1 WHERE AS ROW NUMBERING FOR JTABLE START FROM 0
284
rowset.absolute(_row + 1);
285             // COLUMN NUMBERS IN SSROWSET START FROM 1 WHERE AS COLUMN NUMBERING FOR JTABLE START FROM 0
286
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 JavaDoc(rowset.getInt(_column+1));
292                     break;
293                 case Types.BIGINT:
294                     value = new Long JavaDoc(rowset.getLong(_column+1));
295                     break;
296                 case Types.FLOAT:
297                     value = new Float JavaDoc(rowset.getFloat(_column+1));
298                     break;
299                 case Types.DOUBLE:
300                 case Types.NUMERIC:
301                     value = new Double JavaDoc(rowset.getDouble(_column+1));
302                     break;
303                 case Types.BOOLEAN:
304                 case Types.BIT:
305                     value = new Boolean JavaDoc(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     } // end public Object getValueAt(int _row, int _column) {
330

331     /**
332      * Sets the value in the cell at _row and _column to _value.
333      *
334      * @param _value the new value
335      * @param _row the row whose value is to be changed
336      * @param _column the column whose value is to be changed
337      */

338     public void setValueAt(Object JavaDoc _value, int _row, int _column) {
339
340         // IF CELL EDITING INTERFACE IMPLEMENTATION IS PROVIDED INFO THE USER
341
// THAT AN UPDATE FOR CELL HAS BEEN REQUESTED.
342
if (cellEditing != null) {
343             // THE ROW AND COLUMN NUMBERING STARTS FROM 0 FOR JTABLE BUT THE COLUMNS AND
344
// ROWS ARE NUMBERED FROM 1 FOR SSROWSET.
345
boolean allowEdit;
346
347             // IF ITS NEW ROW SEND A NULL FOR THE OLD VALUE
348
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 THE USER DOES NOT PERMIT THE UPDATE RETURN ELSE GO AHEAD AND UPDATE THE
355
// DATABASE.
356
if (!allowEdit) {
357                 return;
358             }
359         }
360
361         // IF CHANGE IS MADE IN INSERT ROW ADD ROW TO THE DATABASE
362
// INSERTROW FUNCTION ALSO INCREMENTS THE ROW COUNT
363
if ( _row == rowCount ) {
364             insertRow(_value,_column);
365             return;
366         }
367
368 // System.out.println("Set value at "+ _row + " " + _column + " with "+ _value);
369
try{
370             // YOU SHOULD BE ON THE RIGHT ROW IN THE SSROWSET
371
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 JavaDoc)_value).intValue());
385                     break;
386                 case Types.BIGINT:
387 // adding update long support 11-01-2004
388
rowset.updateLong(_column+1,((Long JavaDoc)_value).longValue());
389                     break;
390                 case Types.FLOAT:
391                     rowset.updateFloat(_column+1, ((Float JavaDoc)_value).floatValue());
392                     break;
393                 case Types.DOUBLE:
394                 case Types.NUMERIC:
395                     rowset.updateDouble(_column+1, ((Double JavaDoc)_value).doubleValue());
396                     break;
397                 case Types.BOOLEAN:
398                 case Types.BIT:
399                     rowset.updateBoolean(_column+1, ((Boolean JavaDoc)_value).booleanValue());
400                     break;
401                 case Types.DATE:
402                 case Types.TIMESTAMP:
403                 // IF A DATE RENDERER AND EDITOR IS USED THE DATE IS DISPLAYED AS STRING.
404
// SO A STRING WILL BE SENT FOR UPDATE
405
// IN SUCH A CASE CHECK IS THE STRING IS EMPTY IF SO UPDATE NULL FOR THE FEILD
406
if (_value instanceof String JavaDoc) {
407                         if (getSQLDate((String JavaDoc)_value) == null) {
408                             rowset.updateNull(_column+1);
409                         } else {
410                             rowset.updateDate(_column+1,getSQLDate((String JavaDoc)_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 JavaDoc)_value);
420                     break;
421                 default:
422                     System.out.println("SSTableModel.setValueAt(): Unknown data type");
423             }
424             rowset.updateRow();
425 // System.out.println("Updated value: " + getValueAt(_row,_column));
426
} 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     } // end public void setValueAt(Object _value, int _row, int _column) {
434

435     /**
436      * Inserts a new row into the database.
437      * While doing so it inserts all the defaults provided by user and if the
438      * primary column is specified along with an SSDataValue implementation
439      * then the primary column value will be inserted.
440      *
441      * @param _value value entererd of a column
442      * @param _column the column number for which the value is entered.
443      */

444     protected void insertRow(Object JavaDoc _value, int _column) {
445         if (_value == null) {
446             return;
447         }
448
449         try {
450             // IF NOT ON INSERT ROW MOVE TO INSERT ROW.
451
if (!inInsertRow) {
452                 rowset.moveToInsertRow();
453                 // SET THE DEFAULTS
454
setDefaults();
455                 inInsertRow = true;
456                 // IS SSDATAVALUE IS PROVIDED SET PRIMARY KEY VALUE
457
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 JavaDoc)_value).intValue());
470                     break;
471                 case Types.BIGINT:
472 // adding update long support 11-01-2004
473
rowset.updateLong(_column+1,((Long JavaDoc)_value).longValue());
474                     break;
475                 case Types.FLOAT:
476                     rowset.updateFloat(_column+1, ((Float JavaDoc)_value).floatValue());
477                     break;
478                 case Types.DOUBLE:
479                 case Types.NUMERIC:
480                     rowset.updateDouble(_column+1, ((Double JavaDoc)_value).doubleValue());
481                     break;
482                 case Types.BOOLEAN:
483                 case Types.BIT:
484                     rowset.updateBoolean(_column+1, ((Boolean JavaDoc)_value).booleanValue());
485                     break;
486                 case Types.DATE:
487                     if (_value instanceof String JavaDoc) {
488                         rowset.updateDate(_column+1,getSQLDate((String JavaDoc)_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 JavaDoc)_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 // System.out.println("Row number of inserted row : "+ rowset.getRow());
510
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 // System.out.println("Successfully added row");
523

524     } // end protected void insertRow(Object _value, int _column) {
525

526     /**
527      * This function sets the default values for the present row.
528      */

529     protected void setDefaults() {
530         if (defaultValuesMap == null) {
531             return;
532         }
533
534         Set JavaDoc keySet = defaultValuesMap.keySet();
535         Iterator JavaDoc iterator = keySet.iterator();
536         try {
537             while (iterator.hasNext()) {
538                 Integer JavaDoc column = (Integer JavaDoc)iterator.next();
539 // System.out.println("Column number is:" + column);
540
// COLUMNS SPECIFIED START FROM 0 BUT FOR SSROWSET THEY START FROM 1
541
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 JavaDoc)defaultValuesMap.get(column)).intValue());
547                         break;
548                     case Types.BIGINT:
549                         rowset.updateLong(column.intValue()+1, ((Long JavaDoc)defaultValuesMap.get(column)).longValue());
550                         break;
551                     case Types.FLOAT:
552                         rowset.updateFloat(column.intValue()+1, ((Float JavaDoc)defaultValuesMap.get(column)).floatValue());
553                         break;
554                     case Types.DOUBLE:
555                     case Types.NUMERIC:
556                         rowset.updateDouble(column.intValue()+1, ((Double JavaDoc)defaultValuesMap.get(column)).doubleValue());
557                         break;
558                     case Types.BOOLEAN:
559                     case Types.BIT:
560                         rowset.updateBoolean(column.intValue()+1, ((Boolean JavaDoc)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 JavaDoc)defaultValuesMap.get(column));
569                         break;
570                     default:
571                         System.out.println("SSTableModel.setValueAt(): Unknown data type");
572                 } // END OF SWITCH
573

574             } //END OF WHILE
575

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     } // end protected void setDefaults() {
583

584     /**
585      * Returns the type for the column specified for the current view.
586      *
587      * @param _column the column in the view being queried
588      *
589      * @return type for the specified column (first column is 0)
590      */

591     public Class JavaDoc 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 JavaDoc.class;
604
605             case Types.BIGINT:
606                 return Long JavaDoc.class;
607
608             case Types.FLOAT:
609                 return Float JavaDoc.class;
610
611             case Types.DOUBLE:
612             case Types.NUMERIC:
613                 return Double JavaDoc.class;
614
615             case Types.BOOLEAN:
616             case Types.BIT:
617                 return Boolean JavaDoc.class;
618
619             case Types.DATE:
620                 return java.sql.Date JavaDoc.class;
621
622             case Types.CHAR:
623             case Types.VARCHAR:
624             case Types.LONGVARCHAR:
625                 return String JavaDoc.class;
626
627             default:
628                 return Object JavaDoc.class;
629         }
630
631     } // end public Class getColumnClass(int _column) {
632

633     /**
634      * Deletes the specified row from the database.
635      * The rows are numbered as: 0, 1, ..., n-1
636      *
637      * @param _row the row number that has to be deleted.
638      *
639      * @return returns true on succesful deletion else false.
640      */

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     } // end public boolean deleteRow(int _row) {
661

662     /**
663      * Sets the default values for different columns.
664      * These values will be used while inserting a new row.
665      *
666      * @param _columnNumbers the column numbers for which defaults are required
667      * @param _values the values for all the columns specified in first argument
668      */

669     public void setDefaultValues(int[] _columnNumbers, Object JavaDoc[] _values) {
670         if (_columnNumbers == null || _values == null) {
671             defaultValuesMap = null;
672         }
673
674         if (defaultValuesMap == null) {
675             defaultValuesMap = new HashMap JavaDoc();
676         } else {
677             defaultValuesMap.clear();
678         }
679
680         for (int i=0;i<_columnNumbers.length;i++) {
681             defaultValuesMap.put(new Integer JavaDoc(_columnNumbers[i]), _values[i]);
682         }
683     }
684
685     /**
686      * Returns the default value inforce for the requested column.
687      *
688      * The type of object is same as returned by getColumnClass in JTable.
689      *
690      * @param _columnNumber the column number for which default value is needed.
691      *
692      * @return returns a object representing the default value.
693      */

694     public Object JavaDoc getDefaultValue(int _columnNumber) {
695         Object JavaDoc value = null;
696         if (defaultValuesMap != null) {
697             value = defaultValuesMap.get(new Integer JavaDoc(_columnNumber));
698         }
699         return value;
700     }
701
702     /**
703      * Sets the message window.
704      * This is used as parent component for pop up message dialogs.
705      *
706      *@param _component the component that should be used for message dialogs.
707      */

708     public void setMessageWindow(Component JavaDoc _component) {
709         component = _component;
710     }
711
712     /**
713      * This sets the JTable to which the table model is bound to.
714      * When an insert row has taken place TableModel tries to update the UI.
715      *
716      * @param _table JTable to which SSTableModel is bound to.
717      */

718     public void setJTable(JTable JavaDoc _table) {
719         table = _table;
720     }
721
722     /**
723      * Sets the column number which is the primary column for the table.
724      * This is required if new rows have to be added to the JTable.
725      * For this to properly work the SSDataValue object should also be provided
726      * SSDataValue is used to get the value for the primary column.
727      *
728      * @param _columnNumber the column which is the primary column.
729      */

730     public void setPrimaryColumn(int _columnNumber) {
731         primaryColumn = _columnNumber;
732     }
733
734      /**
735      * Sets the SSDataValue interface implemention. This interface specifies
736      * function to retrieve primary column values for a new row to be added.
737      *
738      * @param _dataValue implementation of SSDataValue for determining PK
739      */

740     public void setSSDataValue(SSDataValue _dataValue) {
741         dataValue = _dataValue;
742     }
743
744     /**
745      * Updates the primary key column based on the SSDataValue implementation
746      * specified for the SSTableModel and the underlying SQL data type.
747      */

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 JavaDoc)dataValue.getPrimaryColumnValue()).intValue());
758                     break;
759                 case Types.BIGINT:
760                     rowset.updateLong(primaryColumn+1,((Long JavaDoc)dataValue.getPrimaryColumnValue()).longValue());
761                     break;
762                 case Types.FLOAT:
763                     rowset.updateFloat(primaryColumn+1,((Float JavaDoc)dataValue.getPrimaryColumnValue()).floatValue());
764                     break;
765                 case Types.DOUBLE:
766                 case Types.NUMERIC:
767                     rowset.updateDouble(primaryColumn+1,((Double JavaDoc)dataValue.getPrimaryColumnValue()).doubleValue());
768                     break;
769                 case Types.BOOLEAN:
770                 case Types.BIT:
771                     rowset.updateBoolean(primaryColumn+1,((Boolean JavaDoc)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 JavaDoc)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     } // end protected void setPrimaryColumn() {
791

792
793     /**
794      * Returns an SQL date for a string date formatted as "MM/dd/yyyy".
795      *
796      * @param _strDate String containing a date in "MM/dd/yyyy" format.
797      *
798      * @return String date reformatted as an SQL date
799      */

800     protected Date getSQLDate(String JavaDoc _strDate) {
801         if (_strDate.trim().equals("")) {
802             return null;
803         }
804         String JavaDoc newStrDate = _strDate;
805         if(_strDate.indexOf("/") != -1){
806             StringTokenizer JavaDoc strtok = new StringTokenizer JavaDoc(_strDate,"/",false);
807             String JavaDoc month = strtok.nextToken();
808             String JavaDoc day = strtok.nextToken();
809             newStrDate = strtok.nextToken() + "-" + month + "-" + day;
810         }
811         return Date.valueOf(newStrDate);
812     }
813
814     /**
815      * Sets the headers for the JTable.
816      * This function has to be called before setting the SSRowSet for SSDataGrid.
817      *
818      * @param _headers array of string objects representing the header for each column.
819      */

820     public void setHeaders(String JavaDoc[] _headers) {
821         headers = _headers;
822     }
823
824     /**
825      * Returns the name of the column appearing in the view at column position column.
826      *
827      * @param _columnNumber the column in the view being queried
828      *
829      * @return the name of the column at the position specified for the current
830      * vew where column numbering begins at 0
831      */

832     public String JavaDoc getColumnName(int _columnNumber) {
833         if (headers != null) {
834             if (_columnNumber < headers.length) {
835 // System.out.println("sending header " + headers[_columnNumber]);
836
return headers[_columnNumber];
837             }
838         }
839 // System.out.println(" Not able to supply header name");
840
return "";
841     }
842
843     /**
844      * Sets the uneditable columns.
845      * The columns specified as uneditable will not be available for user to edit.
846      * This overrides the isCellEditable function in SSCellEditing.
847      *
848      * @param _columnNumbers array specifying the column numbers which should be
849      * uneditable.
850      */

851     public void setUneditableColumns(int[] _columnNumbers) {
852         uneditableColumns = _columnNumbers;
853     }
854
855     /**
856      * Sets the column numbers that should be hidden.
857      * The SSDataGrid sets the column width of these columns to 0.
858      * The columns are set to zero width rather than removing the column from the table.
859      * Thus preserving the column numbering. If a column is removed then the column numbers
860      * for columns after the removed column will change.
861      * Even if the column is specified as hidden user will be seeing a tiny strip.
862      * Make sure that you specify the hidden column numbers in the uneditable column
863      * list.
864      *
865      * @param _columnNumbers array specifying the column numbers which should be
866      * hidden.
867      */

868     public void setHiddenColumns(int[] _columnNumbers) {
869         hiddenColumns = _columnNumbers;
870     }
871     
872     
873     
874 // DEPRECATED STUFF....................
875

876     /**
877      * Sets the SSRowSet for SSTableModel to the given SSRowSet.
878      * This SSRowSet will be used to get the data for JTable.
879      *
880      * @param _rowset SSRowSet object whose records has to be displayed in JTable.
881      *
882      * @deprecated
883      * @see #setSSRowSet
884      */

885     public void setRowSet(SSRowSet _rowset) throws SQLException {
886         rowset = _rowset;
887         init();
888     }
889     
890 } // end public class SSTableModel extends AbstractTableModel {
891

892
893
894 /*
895  * $Log: SSTableModel.java,v $
896  * Revision 1.19 2005/03/09 21:45:26 prasanth
897  * Added TIMESTAMP column type in setValueAt & getValueAt functions.
898  *
899  * Revision 1.18 2005/02/09 22:20:05 yoda2
900  * JavaDoc cleanup.
901  *
902  * Revision 1.17 2005/02/07 22:55:34 yoda2
903  * Fixed accidental renaming in deprecated method.
904  *
905  * Revision 1.16 2005/02/07 22:46:29 yoda2
906  * Deprecated setRowSet().
907  *
908  * Revision 1.15 2005/02/04 23:05:10 yoda2
909  * no message
910  *
911  * Revision 1.14 2005/02/04 22:48:54 yoda2
912  * API cleanup & updated Copyright info.
913  *
914  * Revision 1.13 2004/11/11 14:45:48 yoda2
915  * Using TextPad, converted all tabs to "soft" tabs comprised of four actual spaces.
916  *
917  * Revision 1.12 2004/11/01 15:48:39 yoda2
918  * Added support for NUMERIC. Made type support consistent across SwingSet: INTEGER, SMALLINT, TINYINT (Integer); BIGINT (Long); FLOAT (Float); DOUBLE, NUMERIC (Double); BOOLEAN, BIT (Boolean); DATE (Date); CHAR, VARCHAR, LONGVARCHAR (String).
919  *
920  * Revision 1.11 2004/10/25 22:13:43 yoda2
921  * Updated JavaDoc for new datasource abstraction layer in 0.9.0 release.
922  *
923  * Revision 1.10 2004/10/25 19:51:03 prasanth
924  * Modified to use the new SSRowSet instead of RowSet.
925  *
926  * Revision 1.9 2004/10/19 21:13:07 prasanth
927  * In getSQLDate function. checking if a / occurs in the string.
928  * If not assuming that its in standard format yyyy-mm-dd and trying to convert
929  * in to a date.
930  *
931  * Revision 1.8 2004/09/27 15:48:17 prasanth
932  * Added function to disable insertions.
933  *
934  * Revision 1.7 2004/08/10 22:06:58 yoda2
935  * Added/edited JavaDoc, made code layout more uniform across classes, made various small coding improvements suggested by PMD.
936  *
937  * Revision 1.6 2004/08/02 15:27:15 prasanth
938  * Made all variabled protected.
939  *
940  * Revision 1.5 2004/03/08 16:43:37 prasanth
941  * Updated copy right year.
942  *
943  * Revision 1.4 2003/12/18 20:12:20 prasanth
944  * Update class description.
945  *
946  * Revision 1.3 2003/12/16 18:01:40 prasanth
947  * Documented versions for release 0.6.0
948  *
949  */
Popular Tags