SPT Core API

com.sptci.echo2.table
Class TableModel<D>

java.lang.Object
  extended by nextapp.echo2.app.table.AbstractTableModel
      extended by com.sptci.echo2.table.AbstractTableModel<D>
          extended by com.sptci.echo2.table.TableModel<D>
Type Parameters:
D - The type (class name) of the model object used.
All Implemented Interfaces:
Serializable, TableModel
Direct Known Subclasses:
DefaultPageableTableModel, DefaultSortableTableModel

public class TableModel<D>
extends AbstractTableModel<D>

A genericised TableModel used to hold instances of objects of the type specified.

The following shows sample usage of this class:

   import com.sptci.echo2.table.Table;
   import com.sptci.echo2.table.TableModel;

     ...
     Collection collection = ... // Fetch the collection of data
     TableModel tableModel = new TableModel( collection );
     Table table = new Table( tableModel );
 

© Copyright 2007 Sans Pareil Technologies, Inc.

Version:
$Id: TableModel.java 4553 2008-12-24 10:34:16Z rakesh $
Author:
Rakesh Vidyadharan 2007-09-19
See Also:
Serialized Form

Field Summary
protected  List<ColumnMetaData> columns
          The list of columns displayed in the Table.
protected  List<D> data
          The list of data objects that are being displayed in the Table.
 
Fields inherited from class com.sptci.echo2.table.AbstractTableModel
sortDirection, sortIndex
 
Constructor Summary
protected TableModel()
          Default constructor.
  TableModel(Collection<D> collection)
          Create a new instance of the model using the specified collection of data objects.
 
Method Summary
 void addRow(D row)
          Add the specified object to the end of the model.
 void deleteRow(D rowData)
          Delete the row(s) containing the specified object from the model.
 void deleteRow(int row)
          Delete the specified row from the model.
protected  Map<Field,Column> getColumnAnnotations(Map<String,Field> fields)
          Fetch any Fields in the specified array that have a Column annotation.
 Class getColumnClass(int column)
          Returns the class of the objects displayed in the specified column.
 int getColumnCount()
          Returns the number of columns displayed in the table.
 String getColumnName(int column)
          Returns the name of the specified column.
 D getObjectAt(int row)
          Return the object stored at the specified index in data.
 int getRowCount()
          Return the total number of entries in data.
 Object getValueAt(int column, int row)
          Return the value at the specified coordinate in the table.
 void init()
          Initialise this model.
 void insertRow(int row, D rowData)
          Insert the specified object at the specified row index into the model.
protected  void processColumnAnnotations(Map<Field,Column> fields)
          Process the annotated fields and populate columns as annotated.
protected  void processColumns()
          Process the Fields of the object type included in data and populate columns.
protected  void processColumnsAnnotation(D obj)
          Check for the Columns annotation on the data object and populate columns as appropriate.
protected  void processFields(Map<String,Field> fields)
          Process the Fields in the underlying data object and initialise columns.
 void updateRow(int row, D rowData)
          Replace the row specified with the object specified.
 
Methods inherited from class com.sptci.echo2.table.AbstractTableModel
getSortDirection
 
Methods inherited from class nextapp.echo2.app.table.AbstractTableModel
addTableModelListener, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getEventListenerList, removeTableModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

data

protected List<D> data
The list of data objects that are being displayed in the Table.


columns

protected List<ColumnMetaData> columns
The list of columns displayed in the Table. This is used by the getColumnName(int) and getColumnCount() methods.

Constructor Detail

TableModel

protected TableModel()
Default constructor.


TableModel

public TableModel(Collection<D> collection)
Create a new instance of the model using the specified collection of data objects. Tables must invoke the init() method to initialise this model.

Parameters:
collection - The collection of data objects to display in the Table.
Method Detail

init

public void init()
Initialise this model. Invokes processColumns() if the columns collection is empty. This gives a chance to sub-classes to get around over-riding the processColumns() method and also helps automatic initialisers get around the default processing.

This method is usually invoked from Table constructor before the Table.setModel(nextapp.echo2.app.table.TableModel) method is invoked. It is imperative that the model be initialised before the Table.setModel(nextapp.echo2.app.table.TableModel) method is invoked keeping in mind the implementation of Table.setModel(nextapp.echo2.app.table.TableModel).

Specified by:
init in class AbstractTableModel<D>
See Also:
processColumns()

addRow

public void addRow(D row)
Add the specified object to the end of the model.

Parameters:
row - The data object that is to be added to the model.
See Also:
insertRow(int, D)

insertRow

public void insertRow(int row,
                      D rowData)
Insert the specified object at the specified row index into the model.

Parameters:
row - The index at which the object is to be inserted.
rowData - The object that is to be added to the model.
See Also:
AbstractTableModel.fireTableRowsInserted(int, int)

deleteRow

public void deleteRow(int row)
Delete the specified row from the model.

Parameters:
row - The index of the row that is to be deleted.
See Also:
AbstractTableModel.fireTableRowsDeleted(int, int)

deleteRow

public void deleteRow(D rowData)
Delete the row(s) containing the specified object from the model.

Parameters:
rowData - The object that is to be removed.
See Also:
deleteRow( int )

updateRow

public void updateRow(int row,
                      D rowData)
Replace the row specified with the object specified.

Parameters:
row - The index at which the object is to be updated.
rowData - The new object that is to be used in the model.
See Also:
AbstractTableModel.fireTableRowsUpdated(int, int)

getColumnClass

public Class getColumnClass(int column)
Returns the class of the objects displayed in the specified column.

Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel
Parameters:
column - The column whose class is to be returned.
Returns:
The class of the objects displayed.
See Also:
TableModel.getColumnClass(int)

getColumnCount

public int getColumnCount()
Returns the number of columns displayed in the table.

Returns:
The number of columns displayed.
See Also:
TableModel.getColumnCount()

getColumnName

public String getColumnName(int column)
Returns the name of the specified column. Looks for a localised value with a key <D.class.getName()>.<fieldName>.heading and if found returns the localised value.

Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel
Parameters:
column - The index of the column whose name is to be returned.
Returns:
The name of the specified column.
See Also:
TableModel.getColumnName(int)

getRowCount

public int getRowCount()
Return the total number of entries in data.

Returns:
The total number of rows displayed in the table.
See Also:
TableModel.getRowCount()

getValueAt

public Object getValueAt(int column,
                         int row)
                  throws IllegalArgumentException
Return the value at the specified coordinate in the table. If an unexpected error is encountered, a Level.SEVERE message with stack trace is logged. Throwing a RuntimeException results in Echo framework handling it improperly and ultimately killing the user session with a unrelated message.

Parameters:
column - The column index.
row - The row index.
Returns:
The object that is displayed at the coordinate specified.
Throws:
IllegalArgumentException - If column or row indices are invalid for this model.
See Also:
TableModel.getValueAt(int, int)

getObjectAt

public D getObjectAt(int row)
Return the object stored at the specified index in data.

Parameters:
row - The index of the row whose underlying object is to be returned.
Returns:
The underlying data object represented by the specified row.

processColumns

protected void processColumns()
Process the Fields of the object type included in data and populate columns.

See Also:
processColumnsAnnotation(D), getColumnAnnotations(java.util.Map), processColumnAnnotations(java.util.Map), processFields(java.util.Map)

processColumnsAnnotation

protected void processColumnsAnnotation(D obj)
                                 throws RuntimeException
Check for the Columns annotation on the data object and populate columns as appropriate.

Parameters:
obj - The data object to check for the annotation.
Throws:
RuntimeException - If errors are encountered while fetching the Field from the data object via reflection.

getColumnAnnotations

protected Map<Field,Column> getColumnAnnotations(Map<String,Field> fields)
Fetch any Fields in the specified array that have a Column annotation.

Parameters:
fields - The map of fields that are to be checked for the annotation.
Returns:
A map of the fields and their annotations.

processColumnAnnotations

protected void processColumnAnnotations(Map<Field,Column> fields)
Process the annotated fields and populate columns as annotated.

Parameters:
fields - A map of Field and Column instances to use to initialise columns.

processFields

protected void processFields(Map<String,Field> fields)
                      throws RuntimeException
Process the Fields in the underlying data object and initialise columns. Only process fields with common types such as primitives and their wrappers, String, Calendar and Date. In addition these fields must have a JavaBeans compliant accessor method.

Parameters:
fields - The map of fields that are to be processed.
Throws:
RuntimeException - If errors are encountered while fetching the value of the Field from fields.

SPT Core API