com.nqadmin.swingSet
Class SSTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.nqadmin.swingSet.SSTableModel
All Implemented Interfaces:
java.io.Serializable, javax.swing.table.TableModel

public class SSTableModel
extends javax.swing.table.AbstractTableModel

SSTableModel.java

SwingSet - Open Toolkit For Making Swing Controls Database-Aware

 SSTableModel provides an implementation of the TableModel interface.
 The SSDataGrid uses this class for providing a grid view for a SSRowSet.
 SSTableModel can be used without the SSDataGrid (e.g. in conjunction with a
 JTable), but the cell renderers and hidden columns features of the SSDataGrid
 will not be available.

 SSTableModel can be used with a JTable to get a Grid view of the data.

Version:
$Revision: 1.25 $
Author:
$Author: prasanth $
See Also:
Serialized Form

Field Summary
protected  boolean allowInsertion
          Indicator to determine if insertions are allowed.
protected  SSCellEditing cellEditing
          Implementation of SSCellEditing interface used to determine dynamically if a given cell can be edited and to determine if a given value is valid.
protected  int columnCount
          Number of columns in the SSRowSet.
protected  java.awt.Component component
          Window where messages should be displayed.
protected  SSDataGridHandler dataGridHandler
          Implementation of DataGridHandler interface used to determine dynamically if a given row can be deleted, and what to do before and after a row is added or removed.
protected  SSDataValue dataValue
          Implementation of SSDataValue interface used to determine PK value for new rows.
protected  java.util.HashMap defaultValuesMap
          Map to store the default values of different columns.
protected  java.lang.String[] headers
          JTable headers.
protected  int[] hiddenColumns
          List of hidden columns.
protected  boolean inInsertRow
          Indicator to determine if the SSRowSet is on the insertion row.
protected  int rowCount
          Number of rows in the SSRowSet.
protected  SSRowSet rowset
           
protected  javax.swing.JTable table
          JTable being modeled.
protected  int[] uneditableColumns
          List of uneditable columns.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
SSTableModel()
          Constructs a SSTableModel object.
SSTableModel(SSRowSet _rowset)
          Constructs a SSTableModel object with the given SSRowSet.
 
Method Summary
 boolean deleteRow(int _row)
          Deletes the specified row from the database.
 java.lang.Class getColumnClass(int _column)
          Returns the type for the column specified for the current view.
 int getColumnCount()
          Returns the number of columns in the model.
 java.lang.String getColumnName(int _columnNumber)
          Returns the name of the column appearing in the view at column position column.
 java.lang.Object getDefaultValue(int _columnNumber)
          Returns the default value inforce for the requested column.
 int getRowCount()
          Returns the number of rows in the model.
protected  java.sql.Date getSQLDate(java.lang.String _strDate)
          Returns an SQL date for a string date formatted as "MM/dd/yyyy".
 java.lang.Object getValueAt(int _row, int _column)
          Returns the value for the cell at the specified row & column.
protected  void init()
          Initializes the SSTableModel.
protected  void insertRow(java.lang.Object _value, int _column)
          Inserts a new row into the database.
 boolean isCellEditable(int _row, int _column)
          Returns true if the cell at rowIndex and columnIndex is editable.
protected  void setDefaults()
          This function sets the default values for the present row.
 void setDefaultValues(int[] _columnNumbers, java.lang.Object[] _values)
          Sets the default values for different columns.
 void setHeaders(java.lang.String[] _headers)
          Sets the headers for the JTable.
 void setHiddenColumns(int[] _columnNumbers)
          Sets the column numbers that should be hidden.
 void setInsertion(boolean _insert)
          Sets row insertion indicator.
 void setJTable(javax.swing.JTable _table)
          This sets the JTable to which the table model is bound to.
 void setMessageWindow(java.awt.Component _component)
          Sets the message window.
protected  void setPrimaryColumn()
          Updates the primary key column based on the SSDataValue implementation specified for the SSTableModel and the underlying SQL data type.
 void setPrimaryColumn(int _columnNumber)
          Sets the column number which is the primary column for the table.
 void setRowSet(SSRowSet _rowset)
          Deprecated.  
 void setSSCellEditing(SSCellEditing _cellEditing)
          Used to set an implementation of SSCellEditing interface which can be used to determine dynamically if a given cell can be edited and to determine if a given value is valid.
 void setSSDataGridHandler(SSDataGridHandler _dataGridHandler)
          Used to set an implementation of SSDataGridHandler interface which can be used to determine dynamically if a given row can be deleted, and what should be done after row insertion, and deletion.
 void setSSDataValue(SSDataValue _dataValue)
          Sets the SSDataValue interface implemention.
 void setSSRowSet(SSRowSet _rowset)
          Sets the SSRowSet for SSTableModel to the given SSRowSet.
 void setUneditableColumns(int[] _columnNumbers)
          Sets the uneditable columns.
 void setValueAt(java.lang.Object _value, int _row, int _column)
          Sets the value in the cell at _row and _column to _value.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rowset

protected SSRowSet rowset

rowCount

protected transient int rowCount
Number of rows in the SSRowSet.


columnCount

protected transient int columnCount
Number of columns in the SSRowSet.


defaultValuesMap

protected java.util.HashMap defaultValuesMap
Map to store the default values of different columns.


inInsertRow

protected boolean inInsertRow
Indicator to determine if the SSRowSet is on the insertion row.


component

protected transient java.awt.Component component
Window where messages should be displayed.


table

protected transient javax.swing.JTable table
JTable being modeled.


headers

protected transient java.lang.String[] headers
JTable headers.


dataValue

protected SSDataValue dataValue
Implementation of SSDataValue interface used to determine PK value for new rows.


cellEditing

protected SSCellEditing cellEditing
Implementation of SSCellEditing interface used to determine dynamically if a given cell can be edited and to determine if a given value is valid.


dataGridHandler

protected SSDataGridHandler dataGridHandler
Implementation of DataGridHandler interface used to determine dynamically if a given row can be deleted, and what to do before and after a row is added or removed.


uneditableColumns

protected int[] uneditableColumns
List of uneditable columns.


hiddenColumns

protected int[] hiddenColumns
List of hidden columns.


allowInsertion

protected boolean allowInsertion
Indicator to determine if insertions are allowed.

Constructor Detail

SSTableModel

public SSTableModel()
Constructs a SSTableModel object. If this contructor is used the setSSRowSet() method has to be used to set the SSRowSet before constructing the JTable.


SSTableModel

public SSTableModel(SSRowSet _rowset)
             throws java.sql.SQLException
Constructs a SSTableModel object with the given SSRowSet. This will call the execute method on the given SSRowSet.

Parameters:
_rowset - SSRowSet object whose records has to be displayed in JTable.
Throws:
java.sql.SQLException
Method Detail

setSSRowSet

public void setSSRowSet(SSRowSet _rowset)
                 throws java.sql.SQLException
Sets the SSRowSet for SSTableModel to the given SSRowSet. This SSRowSet will be used to get the data for JTable.

Parameters:
_rowset - SSRowSet object whose records has to be displayed in JTable.
Throws:
java.sql.SQLException

setSSCellEditing

public void setSSCellEditing(SSCellEditing _cellEditing)
Used to set an implementation of SSCellEditing interface which can be used to determine dynamically if a given cell can be edited and to determine if a given value is valid.

Parameters:
_cellEditing - implementation of SSCellEditing interface.

setSSDataGridHandler

public void setSSDataGridHandler(SSDataGridHandler _dataGridHandler)
Used to set an implementation of SSDataGridHandler interface which can be used to determine dynamically if a given row can be deleted, and what should be done after row insertion, and deletion.

Parameters:
_dataGridHandler - implementation of SSDataGridHandler interface.

setInsertion

public void setInsertion(boolean _insert)
Sets row insertion indicator.

Parameters:
_insert - true if user can insert new rows, else false.

init

protected void init()
Initializes the SSTableModel. (Gets the column count and row count for the given SSRowSet.)


getColumnCount

public int getColumnCount()
Returns the number of columns in the model. A JTable uses this method to determine how many columns it should create and display by default.

Returns:
the number of columns in the SSTableModel

getRowCount

public int getRowCount()
Returns the number of rows in the model. A JTable uses this method to determine how many rows it should display.

Returns:
the number of rows in the SSTableModel

isCellEditable

public boolean isCellEditable(int _row,
                              int _column)
Returns true if the cell at rowIndex and columnIndex is editable. Otherwise, a call to setValueAt() on the cell will not change the value of that cell.

Specified by:
isCellEditable in interface javax.swing.table.TableModel
Overrides:
isCellEditable in class javax.swing.table.AbstractTableModel
Parameters:
_row - the row whose value to be queried
_column - the column whose value to be queried
Returns:
editable indicator for cell at row and column specified

getValueAt

public java.lang.Object getValueAt(int _row,
                                   int _column)
Returns the value for the cell at the specified row & column.

Parameters:
_row - the row whose value to be queried.
_column - the column whose value to be queried.
Returns:
value at the requested cell.

setValueAt

public void setValueAt(java.lang.Object _value,
                       int _row,
                       int _column)
Sets the value in the cell at _row and _column to _value.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class javax.swing.table.AbstractTableModel
Parameters:
_value - the new value
_row - the row whose value is to be changed
_column - the column whose value is to be changed

insertRow

protected void insertRow(java.lang.Object _value,
                         int _column)
Inserts a new row into the database. While doing so it inserts all the defaults provided by user and if the primary column is specified along with an SSDataValue implementation then the primary column value will be inserted.

Parameters:
_value - value entererd of a column
_column - the column number for which the value is entered.

setDefaults

protected void setDefaults()
This function sets the default values for the present row.


getColumnClass

public java.lang.Class getColumnClass(int _column)
Returns the type for the column specified for the current view.

Specified by:
getColumnClass in interface javax.swing.table.TableModel
Overrides:
getColumnClass in class javax.swing.table.AbstractTableModel
Parameters:
_column - the column in the view being queried
Returns:
type for the specified column (first column is 0)

deleteRow

public boolean deleteRow(int _row)
Deletes the specified row from the database. The rows are numbered as: 0, 1, ..., n-1

Parameters:
_row - the row number that has to be deleted.
Returns:
returns true on succesful deletion else false.

setDefaultValues

public void setDefaultValues(int[] _columnNumbers,
                             java.lang.Object[] _values)
Sets the default values for different columns. These values will be used while inserting a new row.

Parameters:
_columnNumbers - the column numbers for which defaults are required
_values - the values for all the columns specified in first argument

getDefaultValue

public java.lang.Object getDefaultValue(int _columnNumber)
Returns the default value inforce for the requested column. The type of object is same as returned by getColumnClass in JTable.

Parameters:
_columnNumber - the column number for which default value is needed.
Returns:
returns a object representing the default value.

setMessageWindow

public void setMessageWindow(java.awt.Component _component)
Sets the message window. This is used as parent component for pop up message dialogs.

Parameters:
_component - the component that should be used for message dialogs.

setJTable

public void setJTable(javax.swing.JTable _table)
This sets the JTable to which the table model is bound to. When an insert row has taken place TableModel tries to update the UI.

Parameters:
_table - JTable to which SSTableModel is bound to.

setPrimaryColumn

public void setPrimaryColumn(int _columnNumber)
Sets the column number which is the primary column for the table. This is required if new rows have to be added to the JTable. For this to properly work the SSDataValue object should also be provided SSDataValue is used to get the value for the primary column.

Parameters:
_columnNumber - the column which is the primary column.

setSSDataValue

public void setSSDataValue(SSDataValue _dataValue)
Sets the SSDataValue interface implemention. This interface specifies function to retrieve primary column values for a new row to be added.

Parameters:
_dataValue - implementation of SSDataValue for determining PK

setPrimaryColumn

protected void setPrimaryColumn()
Updates the primary key column based on the SSDataValue implementation specified for the SSTableModel and the underlying SQL data type.


getSQLDate

protected java.sql.Date getSQLDate(java.lang.String _strDate)
Returns an SQL date for a string date formatted as "MM/dd/yyyy".

Parameters:
_strDate - String containing a date in "MM/dd/yyyy" format.
Returns:
String date reformatted as an SQL date

setHeaders

public void setHeaders(java.lang.String[] _headers)
Sets the headers for the JTable. This function has to be called before setting the SSRowSet for SSDataGrid.

Parameters:
_headers - array of string objects representing the header for each column.

getColumnName

public java.lang.String getColumnName(int _columnNumber)
Returns the name of the column appearing in the view at column position column.

Specified by:
getColumnName in interface javax.swing.table.TableModel
Overrides:
getColumnName in class javax.swing.table.AbstractTableModel
Parameters:
_columnNumber - the column in the view being queried
Returns:
the name of the column at the position specified for the current vew where column numbering begins at 0

setUneditableColumns

public void setUneditableColumns(int[] _columnNumbers)
Sets the uneditable columns. The columns specified as uneditable will not be available for user to edit. This overrides the isCellEditable function in SSCellEditing.

Parameters:
_columnNumbers - array specifying the column numbers which should be uneditable.

setHiddenColumns

public void setHiddenColumns(int[] _columnNumbers)
Sets the column numbers that should be hidden. The SSDataGrid sets the column width of these columns to 0. The columns are set to zero width rather than removing the column from the table. Thus preserving the column numbering. If a column is removed then the column numbers for columns after the removed column will change. Even if the column is specified as hidden user will be seeing a tiny strip. Make sure that you specify the hidden column numbers in the uneditable column list.

Parameters:
_columnNumbers - array specifying the column numbers which should be hidden.

setRowSet

public void setRowSet(SSRowSet _rowset)
               throws java.sql.SQLException
Deprecated. 

Sets the SSRowSet for SSTableModel to the given SSRowSet. This SSRowSet will be used to get the data for JTable.

Parameters:
_rowset - SSRowSet object whose records has to be displayed in JTable.
Throws:
java.sql.SQLException
See Also:
setSSRowSet(com.nqadmin.swingSet.datasources.SSRowSet)