com.gargoylesoftware.base.gui

Class ReflectedTableModel

public class ReflectedTableModel extends AbstractTableModel

A table model that uses reflection to retrieve values out of the row objects.

The sample below will create a JTable with one row of data and one column per property in the Date class (Date has 10 properties in JDK1.3).

 final JTable table = new JTable();
 final ReflectedTableModel model = new ReflectedTableModel(Date.class);
 model.getRows().add( new Date() );
 table.setModel(model);
 
This sample will only provide columns for month and year.
 final JTable table = new JTable();
 final ReflectedTableModel model = new ReflectedTableModel();
 model.getRows().add( new Date() );
 model.getColumns().add( new ReflectedTableModel.ColumnInfo("month") );
 model.getColumns().add( new ReflectedTableModel.ColumnInfo("year") );
 table.setModel(model);
 
Tip: To enable debugging information call setTraceChannel with a non-null TraceChannel.
 model.setTraceChannel(Trace.out)
 

Version: $Revision: 1.7 $

Author: Mike Bowler

Nested Class Summary
static classReflectedTableModel.ColumnInfo
This class contains information about one specific column in the table.
classReflectedTableModel.RowElementControlData
Field Summary
NotificationListListenercolumnListener_
If any change occurs to the columns then fire a structure changed.
Listcolumns_
static Object[]EMPTY_OBJECT_ARRAY
PropertyChangeListenerpropertyChangeListener_
MaprowElementControlDatas_
NotificationListListenerrowListener_
If the row list changes then fire the appropriate table event.
Listrows_
static longserialVersionUID
TraceChanneltraceChannel_
Constructor Summary
ReflectedTableModel()
Create an empty model with no columns and no rows.
ReflectedTableModel(Class clazz)
Create an empty model with no rows but the columns preset to match the properties in the given class.
Method Summary
voidaddRowElement(Object object)
Add a row element
voidaddRowElements(List list)
protected voidassertNotNull(String fieldName, Object object)
Throw an exception if the specified object is null
intgetColumnCount()
Return the number of columns.
StringgetColumnName(int index)
Return the name of the column at the specified index.
ListgetColumns()
Return a list containing the ColumnInfo objects that are used to define each column.
intgetRowCount()
Return the number of rows.
ListgetRows()
Return a list containing the objects that are used to create each row.
TraceChannelgetTraceChannel()
Return the channel currently being used for tracing or null if tracing is disabled.
ObjectgetValueAt(int rowIndex, int columnIndex)
Return the specified object.
voidremoveRowElement(Object object)
Remove one row
voidremoveRowElements(List list)
voidsetTraceChannel(TraceChannel channel)
Set the channel to be used for tracing.

Field Detail

columnListener_

private NotificationListListener columnListener_
If any change occurs to the columns then fire a structure changed.

columns_

private final List columns_

EMPTY_OBJECT_ARRAY

private static final Object[] EMPTY_OBJECT_ARRAY

propertyChangeListener_

private PropertyChangeListener propertyChangeListener_

rowElementControlDatas_

private final Map rowElementControlDatas_

rowListener_

private NotificationListListener rowListener_
If the row list changes then fire the appropriate table event.

rows_

private final List rows_

serialVersionUID

private static final long serialVersionUID

traceChannel_

private TraceChannel traceChannel_

Constructor Detail

ReflectedTableModel

public ReflectedTableModel()
Create an empty model with no columns and no rows.

ReflectedTableModel

public ReflectedTableModel(Class clazz)
Create an empty model with no rows but the columns preset to match the properties in the given class.

Parameters: clazz The class to get properties from.

Throws: IntrospectionException If the Introspector is unable to get the properties for this class.

Method Detail

addRowElement

private void addRowElement(Object object)
Add a row element

Parameters: object the object that will be used to populate this row

addRowElements

private void addRowElements(List list)

Parameters: list The list of objects that will be used to create the rows.

assertNotNull

protected final void assertNotNull(String fieldName, Object object)
Throw an exception if the specified object is null

Parameters: fieldName The name of the paremeter we are checking object The value of the parameter we are checking

getColumnCount

public int getColumnCount()
Return the number of columns.

Returns: the number of columns.

getColumnName

public String getColumnName(int index)
Return the name of the column at the specified index.

Parameters: index The index of the column.

Returns: The name of the column at the specified index.

getColumns

public List getColumns()
Return a list containing the ColumnInfo objects that are used to define each column. This list is backed by the original store such that changes to this list will be reflected in the table model.

Returns: The columns.

getRowCount

public int getRowCount()
Return the number of rows.

Returns: The number of rows.

getRows

public List getRows()
Return a list containing the objects that are used to create each row. This list is backed by the original store such that changes to this list will be reflected in the table model.

Returns: The rows.

getTraceChannel

public TraceChannel getTraceChannel()
Return the channel currently being used for tracing or null if tracing is disabled.

Returns: The trace channel or null if a channel hasn't been set.

getValueAt

public Object getValueAt(int rowIndex, int columnIndex)
Return the specified object.

Parameters: rowIndex The row index columnIndex The columnIndex

Returns: The object at the specified row and column.

removeRowElement

private void removeRowElement(Object object)
Remove one row

Parameters: object The object that was used to create this row.

removeRowElements

private void removeRowElements(List list)

Parameters: list The list of object that will be removed from the model

setTraceChannel

public void setTraceChannel(TraceChannel channel)
Set the channel to be used for tracing.

Parameters: channel The channel to be used for tracing or null if tracing is to be disabled.