org.springframework.richclient.table
Class BaseTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by org.springframework.richclient.table.BaseTableModel
All Implemented Interfaces:
Serializable, TableModel, MutableTableModel
Direct Known Subclasses:
BeanTableModel, ListTableModel

public abstract class BaseTableModel
extends AbstractTableModel
implements MutableTableModel

A skeleton TableModel implementation that adds to the AbstractTableModel class from the core Java API by providing the functionality to manage the underlying collection of data for the table.

Author:
Keith Donald
See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
BaseTableModel()
          Creates a new uninitialized BaseTableModel.
BaseTableModel(List rows)
          Creates a new BaseTableModel containing the given collection of rows.
 
Method Summary
 void addRow(Object row)
          Adds the given row to the end of the list of existing rows in the table model.
 void addRows(List newRows)
          Adds the given rows to the end of the list of existing rows in the table model.
 void clear()
          Removes all rows from the table model.
protected abstract  Class[] createColumnClasses()
          Subclasses must implement this method to return the array of class types for the columns displayed by this table model.
protected  void createColumnInfo()
          Creates the required column information based on the value provided by the createColumnClasses() and createColumnNames() methods.
protected abstract  String[] createColumnNames()
          Subclasses must implement this method to return the array of column headers for the columns to be displayed by this table model.
 Class getColumnClass(int columnIndex)
          Returns the type of the object to be displayed in the given column.
protected  Class[] getColumnClasses()
          Returns the array of class types for the columns displayed by this table model.
 int getColumnCount()
          
 List getColumnData(int column)
          Returns the collection of data from the given column of each row in the table model.
 String[] getColumnHeaders()
          Returns the array of column headers.
 String getColumnName(int columnIndex)
          Returns the name to be displayed in the header for the column at the given position.
protected  String[] getColumnNames()
          Returns the array of column headers for the columns displayed by this table model.
 int getDataColumnCount()
          Returns the number of columns, excluding the column that displays row numbers if present.
 String[] getDataColumnHeaders()
          Returns the array of column headers other than the column displaying the row numbers, if present.
 Object getRow(int rowIndex)
          Returns the object representing the row at the given zero-based index.
 int getRowCount()
          
 List getRows()
          Returns the collection of all the rows in this table model.
 Object getValueAt(int rowIndex, int columnIndex)
          
protected abstract  Object getValueAtInternal(Object row, int columnIndex)
          Subclasses must implement this method to return the value at the given column index for the given object.
 boolean hasRowNumbers()
          Returns true if row numbers are to appear in the first column of the displayed table (default is true).
 boolean isCellEditable(int rowIndex, int columnIndex)
          
protected  boolean isCellEditableInternal(Object row, int columnIndex)
          Subclasses may override this method to determine if the cell at the specified row and column position can be edited.
 void remove(int index)
          Removes from the table model the row at the given position in the list of rows.
 void remove(int[] indexes)
          Removes the rows at each of the given positions.
 void remove(int firstIndex, int lastIndex)
          Removes all of the rows from firstIndex to lastIndex inclusive.
 int rowOf(Object obj)
          Returns the index of the first row containing the specified element, or -1 if this table model does not contain this element.
 void setRowNumbers(boolean rowNumbers)
          Sets the flag that indicates whether or not row numbers are to appear in the first column of the displayed table.
 void setRows(List rows)
          Overwrites the existing table rows with the given collection and fires an appropriate event to all registered listeners.
 void setValueAt(Object value, int rowIndex, int columnIndex)
          
protected  void setValueAtInternal(Object value, Object row, int columnIndex)
          Subclasses may implement this method to set the given value on the property at the given column index of the given row object.
 
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
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, removeTableModelListener
 

Constructor Detail

BaseTableModel

public BaseTableModel()
Creates a new uninitialized BaseTableModel.


BaseTableModel

public BaseTableModel(List rows)
Creates a new BaseTableModel containing the given collection of rows.

Parameters:
rows - The rows of the table model. May be null or empty.
Method Detail

setRows

public void setRows(List rows)
Overwrites the existing table rows with the given collection and fires an appropriate event to all registered listeners.

Parameters:
rows - The collection of rows that will overwrite the existing collection. May be null or empty.

setRowNumbers

public void setRowNumbers(boolean rowNumbers)
Sets the flag that indicates whether or not row numbers are to appear in the first column of the displayed table.

Parameters:
rowNumbers - The flag to display row numbers in the first column.

hasRowNumbers

public boolean hasRowNumbers()
Returns true if row numbers are to appear in the first column of the displayed table (default is true).

Returns:
The flag to show row numbers in the first column of the displayed table.

createColumnInfo

protected void createColumnInfo()
Creates the required column information based on the value provided by the createColumnClasses() and createColumnNames() methods.


getRowCount

public int getRowCount()

Specified by:
getRowCount in interface TableModel

getColumnCount

public int getColumnCount()

Specified by:
getColumnCount in interface TableModel

getDataColumnCount

public int getDataColumnCount()
Returns the number of columns, excluding the column that displays row numbers if present.

Returns:
The number of columns, not counting the row number column if present.

getColumnClass

public Class getColumnClass(int columnIndex)
Returns the type of the object to be displayed in the given column.

Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel
Parameters:
columnIndex - The zero-based index of the column whose type will be returned.
Throws:
IndexOutOfBoundsException - if columnIndex is not within the bounds of the column range for a row of the table.

getColumnName

public String getColumnName(int columnIndex)
Returns the name to be displayed in the header for the column at the given position.

Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel
Parameters:
columnIndex - The zero-based index of the column whose name will be returned.
Throws:
IndexOutOfBoundsException - if columnInde is not within the bounds of the column range for a row of the table.

getColumnHeaders

public String[] getColumnHeaders()
Returns the array of column headers.

Returns:
The array of column headers, never null.

getDataColumnHeaders

public String[] getDataColumnHeaders()
Returns the array of column headers other than the column displaying the row numbers, if present.

Returns:
The column headers, not including the row number column. Never null.

getValueAt

public Object getValueAt(int rowIndex,
                         int columnIndex)

Specified by:
getValueAt in interface TableModel

getValueAtInternal

protected abstract Object getValueAtInternal(Object row,
                                             int columnIndex)
Subclasses must implement this method to return the value at the given column index for the given object.

Parameters:
row - The object representing a row of data from the table.
columnIndex - The column index of the value to be returned.
Returns:
The value at the given index for the given object. May be null.

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)

Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in class AbstractTableModel

isCellEditableInternal

protected boolean isCellEditableInternal(Object row,
                                         int columnIndex)
Subclasses may override this method to determine if the cell at the specified row and column position can be edited. Default behaviour is to always return false.

Parameters:
row - The object representing the table row.
columnIndex - The zero-based index of the column to be checked.
Returns:
true if the given cell is editable, false otherwise.

setValueAt

public void setValueAt(Object value,
                       int rowIndex,
                       int columnIndex)

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class AbstractTableModel

setValueAtInternal

protected void setValueAtInternal(Object value,
                                  Object row,
                                  int columnIndex)
Subclasses may implement this method to set the given value on the property at the given column index of the given row object. The default implementation is to do nothing.

Parameters:
value - The value to be set.
row - The object representing the row in the table.
columnIndex - The column position of the property on the given row object.

getRow

public Object getRow(int rowIndex)
Returns the object representing the row at the given zero-based index.

Parameters:
rowIndex - The zero-based index of the row whose object should be returned.
Returns:
The object for the given row.
Throws:
IndexOutOfBoundsException - if rowIndex is not within the bounds of the collection of rows for this table model.

getRows

public List getRows()
Returns the collection of all the rows in this table model.

Returns:
The collection rows. The collection may be empty but will never be null.

getColumnData

public List getColumnData(int column)
Returns the collection of data from the given column of each row in the table model.

Parameters:
column - The zero-based index of the column whose data should be returned.
Returns:
The collection of data from the given column.
Throws:
IndexOutOfBoundsException - if the given column is not within the bounds of the number of columns for the rows in this table model.

rowOf

public int rowOf(Object obj)
Returns the index of the first row containing the specified element, or -1 if this table model does not contain this element.

Parameters:
obj - The object whose row number will be returned.
Returns:
The index of the first row containing the given object, or -1 if the table model does not contain the object.

addRow

public void addRow(Object row)
Adds the given row to the end of the list of existing rows in the table model. All registered table model listeners will be notified of the additional row.

Specified by:
addRow in interface MutableTableModel
Parameters:
row - The row to be added. Must not be null.

addRows

public void addRows(List newRows)
Adds the given rows to the end of the list of existing rows in the table model. All registered table model listeners will be notified of the additional rows.

Specified by:
addRows in interface MutableTableModel
Parameters:
newRows - The rows to be added. May be empty but must not be null.

remove

public void remove(int index)
Removes from the table model the row at the given position in the list of rows. All registered table model listeners will be notified of the removal of the row.

Specified by:
remove in interface MutableTableModel
Parameters:
index - The zero-based position of the row to be removed.

remove

public void remove(int firstIndex,
                   int lastIndex)
Removes all of the rows from firstIndex to lastIndex inclusive. All registered table model listeners will be notified of the removal of the rows.

Specified by:
remove in interface MutableTableModel
Parameters:
firstIndex - The zero-based position of the first row to be removed.
lastIndex - The zero-based position of the last row to be removed.

remove

public void remove(int[] indexes)
Removes the rows at each of the given positions. All registered table model listeners will be notified of the removal of the rows.

Specified by:
remove in interface MutableTableModel
Parameters:
indexes - The array of zero-based indexes of the rows to be removed. May be empty but must not be null.

clear

public void clear()
Removes all rows from the table model. All registered table model listeners will be notified of the removal of the rows.

Specified by:
clear in interface MutableTableModel

getColumnClasses

protected Class[] getColumnClasses()
Returns the array of class types for the columns displayed by this table model.

Returns:
The array of column class types, never null.

getColumnNames

protected String[] getColumnNames()
Returns the array of column headers for the columns displayed by this table model.

Returns:
The array of column headers, never null.

createColumnClasses

protected abstract Class[] createColumnClasses()
Subclasses must implement this method to return the array of class types for the columns displayed by this table model.

Returns:
The array of column class types, never null.

createColumnNames

protected abstract String[] createColumnNames()
Subclasses must implement this method to return the array of column headers for the columns to be displayed by this table model.

Returns:
The array of column headers, never null.


Copyright © 2004-2008 The Spring Framework. All Rights Reserved.