public class DatabaseTableModel extends RowSetTableModel implements PageableTableModel, SortableTableModel
TableModel used to represent
data contained in in a database table.
Note: This class requires quite a lot of configuration prior
to initialisation. Hence no convenient constructor's are provided. You
must configure the model as appropriate and invoke the init()
method. The following shows sample code for initialising this model:
import com.sptci.echo2.table.DatabaseTableModel;
import com.sptci.echo2.table.RowSetTable;
import com.sptci.echo2.table.TableNavigation;
...
List<String> columns = new ArrayList<>();
columns.add( "col1" );
columns.add( "col2" );
List<String> order = new ArrayList<>();
order.add( "col2 desc" );
DatabaseTableModel model = new DatabaseTableModel();
model.setDisplayColumns( columns );
model.setSchema( null );
model.setTable( "table1" );
model.setDataSourceName( "java:/comp/env/jdbc/db" );
model.setPrefix( "table1.headings" );
model.setPageSize( 10 );
model.setOrderBy( order );
try
{
model.init();
}
catch ( Throwable t )
{
// Handle exception
}
© Copyright 2007 Sans Pareil Technologies, Inc.
SortableTableModel.Direction| Modifier and Type | Field and Description |
|---|---|
protected DataSource |
dataSource
The
DataSource to use to fetch data. |
protected String |
dataSourceName
The JNDI name of the
DataSource to use to fetch data. |
protected List<String> |
displayColumns
The list of columns that are to be displayed in the model.
|
protected boolean |
initialised
A flag used to indicate whether the model has been initialised.
|
protected List<String> |
orderBy
The list of columns to order the table data by.
|
protected int |
page
The current page being displayed in the table.
|
protected int |
pageSize
The number of rows to display on one page.
|
protected List<String> |
primaryKeys
The list of primary key columns for the
table. |
protected String |
schema
The name of database schema in which the
table exists. |
protected String |
table
The name of the database table to represent in the model.
|
protected int |
totalRows
The total number of records available in the database.
|
columns, prefix, rowCount, rowSetsortDirection, sortIndex| Constructor and Description |
|---|
DatabaseTableModel()
Default constructor.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
fetchData()
Fetch the data from
table. |
String |
getDataSourceName()
Returns
dataSourceName. |
List<String> |
getDisplayColumns()
Returns
displayColumns. |
List<String> |
getOrderBy()
Returns
orderBy. |
int |
getPage()
Returns
page. |
int |
getPageSize()
Returns
pageSize. |
List<String> |
getPrimaryKeys()
Returns
primaryKeys list of primary key column names. |
String |
getSchema()
Returns
schema. |
String |
getTable()
Returns
table. |
int |
getTotalPages()
Return the total number of pages that may be displayed by fetching
records from
table. |
void |
init()
Intialise this table model with data from the
table. |
protected void |
introspect()
Introspect the
table and determine its primary key columns. |
protected void |
processColumns()
Process the
RowSetMetaData associated with
RowSetTableModel.rowSet and populate RowSetTableModel.columns. |
void |
setDataSourceName(String dataSourceName)
Sets
dataSourceName. |
void |
setDisplayColumns(List<String> columns)
Sets
displayColumns. |
void |
setOrderBy(List<String> order)
Sets
orderBy. |
void |
setPage(int page)
Sets the value of
page. |
void |
setPageSize(int pageSize)
Sets the value of
pageSize. |
void |
setSchema(String schema)
|
void |
setTable(String table)
Sets the name of the
table to represent in this model. |
void |
sort(int column)
Sort the data displayed in the table by the specified column.
|
void |
sort(int column,
SortableTableModel.Direction direction)
Sort the data displayed in the table by the specified column.
|
addRow, deleteRow, dispose, getColumnClass, getColumnCount, getColumnName, getObjectAt, getPrefix, getRowCount, getValueAt, setColumn, setPrefix, setRowCount, setRowCount, updateRowgetSortDirectionaddTableModelListener, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getEventListenerList, removeTableModelListenerclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetSortDirectionprotected String table
protected String dataSourceName
DataSource to use to fetch data.protected DataSource dataSource
DataSource to use to fetch data.protected int totalRows
protected final List<String> displayColumns
protected int page
protected int pageSize
TableNavigation.MINIMUM_PAGE_SIZEprotected boolean initialised
public DatabaseTableModel()
init() method.public void init()
throws RuntimeException
table.init in class RowSetTableModelRuntimeException - If a NamingException or
SQLException are encountered while initialising
dataSource or RowSetTableModel.rowSet.introspect(),
fetchData(),
processColumns(),
RowSetTableModel.setRowCount()protected void introspect()
throws SQLException
table and determine its primary key columns.SQLException - If errors are encountered while introspecting the
table.DatabaseHelper.getPrimaryKeys(javax.sql.DataSource, java.lang.String, java.lang.String)protected void fetchData()
throws SQLException
table. If RowSetTableModel.columns have been
specified, only data from those columns will be retrieved. All
columns in primaryKeys will be retrieved regardless of whether
they are specified in RowSetTableModel.columns or not.SQLException - If errors are encountered while fetching data from
the table.RowSetTableModel.setRowCount(),
DatabaseHelper.getData(java.lang.String, javax.sql.DataSource, java.lang.String, java.util.List<java.lang.String>, int, int),
AbstractTableModel.fireTableDataChanged()protected void processColumns()
throws SQLException
RowSetMetaData associated with
RowSetTableModel.rowSet and populate RowSetTableModel.columns. Over-ridden to process
only columns specified in displayColumns if specified.processColumns in class RowSetTableModelSQLException - If errors are encountered while introspecting the
meta data.public void sort(int column)
throws RuntimeException
table ordered by the specified
column.sort in interface SortableTableModelcolumn - The index of the column in RowSetTableModel.columns by which
to order the results.RuntimeException - If errors are encountered while fetching
the data.SortableTableModel.sort( int ),
AbstractTableModel.getSortDirection(int),
SortableTableModel.sort( int, Direction )public void sort(int column,
SortableTableModel.Direction direction)
throws RuntimeException
table ordered by the specified
column. The page is set to point to the first page of
data since the re-sorted data will usually have no reference to the
original data.sort in interface SortableTableModelcolumn - The index of the column in RowSetTableModel.columns by which
to order the results.direction - The direction in which to sort the data.RuntimeException - If errors are encountered while fetching
the data.SortableTableModel.sort( int, Direction ),
fetchData()public int getPage()
page.getPage in interface PageableTableModelPageableTableModel.getPage()public void setPage(int page)
page.setPage in interface PageableTableModelpage - The current page of data to load in the model.RuntimeException - If SQLException is raised
by fetchData().PageableTableModel.setPage(int),
fetchData()public int getPageSize()
pageSize.getPageSize in interface PageableTableModelPageableTableModel.getPageSize()public void setPageSize(int pageSize)
pageSize.setPageSize in interface PageableTableModelpageSize - The number of rows to display in the model.RuntimeException - If SQLException is raised
by fetchData().PageableTableModel.setPageSize(int),
fetchData()public int getTotalPages()
table.getTotalPages in interface PageableTableModelpublic List<String> getDisplayColumns()
displayColumns.public void setDisplayColumns(List<String> columns)
displayColumns. Note that this cannot be set after the
model is initialised through init().columns - The list of columns to display.public List<String> getPrimaryKeys()
primaryKeys list of primary key column names.public String getDataSourceName()
dataSourceName.public void setDataSourceName(String dataSourceName)
dataSourceName.dataSourceName - The JNDI resource name.public void setSchema(String schema)
schema - The name of the schema.public void setTable(String table)
table to represent in this model.table - The name of the table.public List<String> getOrderBy()
orderBy.public void setOrderBy(List<String> order) throws SQLException
orderBy. This method may be invoked after initialising
the model to resort the results.order - The list of order by clauses.SQLException - If errors are encountered while refetching the
data from the table.