Copyright © 2005 Scientific Applications. All Rights Reserved.

com.sciapp.table
Class ObjectTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.sciapp.table.ObjectTableModel
All Implemented Interfaces:
ListTableModel, ReorderModel, Serializable, TableModel

public abstract class ObjectTableModel
extends AbstractTableModel
implements ListTableModel, ReorderModel

ObjectTableModel uses an internal ArrayList structure to store the rows of a JTable. The elements in the list can be any Java object. Subclasses should implement getValueAt(int, int) and setValueAt(java.lang.Object, int, int), which define how objects at a column index are retrieved/set respectively. The column names are specified in the constructor, or with setColumns(java.lang.String[])

See Also:
Serialized Form

Field Summary
protected  String[] columns
          A String array holding the columns of the TableModel
protected  List rows
          The actual table rows stored in a List.
protected  boolean sendReorderEvent
          boolean indicating if a reorder event should be sent when needed
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
ObjectTableModel(String[] columns)
          Constructs an ObjectTableModel with columns as the table columns.
 
Method Summary
 void addReorderListener(ReorderListener l)
          Adds a ReorderListener that is notified each time a change to the data model occurs so that the table updates the selected rows accordingly.
 void addRow(Object o)
          Adds a row to the end of the model.
 void addRows(List addedRows)
          Adds a list of rows to the end of the model.
 void clear()
          Clears the model of any data.
 void fireRowsMapped(int[] changedIndices)
          Notifies listeners that the rows of the table have been reordered according to the changedIndices map.
 void fireRowsReordered(ReorderEvent e)
          Notifies listeners that the rows of the table have been reordered in some way.
 Object getCellValue(Object o, int index)
          Returns the Object found at index of row passed as a parameter.
 Class getColumnClass(int c)
          Returns the class of the object at column c.
 int getColumnCount()
          Returns the number of columns in the model.
 String getColumnName(int column)
          Returns the name of the column at column, which depends on the String array passed in the constructor.
 String[] getColumns()
          Returns a clone of the String array of table column names.
 int getRowCount()
          Returns the number of rows in the model.
 List getRows()
          Returns the data of this tablemodel
 boolean getSendReorderEvent()
          Determines if a reorder event should be sent when needed.
 Object getValueAt(int rowIndex, int columnIndex)
          Returns the value for the cell at columnIndex and rowIndex.
abstract  Object getValueAt(Object o, int index)
          Returns the value for the object o at index.
 void insertRow(int row, Object o)
          Inserts a row at row in the model.
 void insertRows(int row, List insertedRows)
          Inserts a list of rows at row in the model.
 boolean isCellEditable(int rowIndex, int columnIndex)
          Returns true by default.
 void removeReorderListener(ReorderListener l)
          Removes a listener from the list that is notified each time a reorder event is generated.
 void removeRow(int row)
          Removes a row from the data model
 void removeRows(int[] deletedRows)
          Removes a few rows from the data model
 void setColumns(String[] columns)
          Sets the table columns.
 void setRows(List rows)
          Assigns the 'row' data.
 void setSendReorderEvent(boolean sendReorderEvent)
          Determines if a reorder event should be sent when needed.
 void setValueAt(Object aValue, int rowIndex, int columnIndex)
          Assigns aValue to the object at columnIndex and rowIndex.
abstract  void setValueAt(Object o, Object aValue, int index)
          Sets the value of Object o at index to aValue
 
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 com.sciapp.table.ListTableModel
fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, removeTableModelListener
 

Field Detail

rows

protected List rows
The actual table rows stored in a List.


columns

protected String[] columns
A String array holding the columns of the TableModel


sendReorderEvent

protected boolean sendReorderEvent
boolean indicating if a reorder event should be sent when needed

Constructor Detail

ObjectTableModel

public ObjectTableModel(String[] columns)
Constructs an ObjectTableModel with columns as the table columns.

Method Detail

addReorderListener

public void addReorderListener(ReorderListener l)
Adds a ReorderListener that is notified each time a change to the data model occurs so that the table updates the selected rows accordingly.

Specified by:
addReorderListener in interface ReorderModel
Parameters:
l - the ReorderListener

addRow

public void addRow(Object o)
Adds a row to the end of the model.

Specified by:
addRow in interface ListTableModel
Parameters:
o - the row being added

addRows

public void addRows(List addedRows)
Adds a list of rows to the end of the model.

addedRows contains objects representing the actual rows being added. e.g. java.util.Vector, for DefaultTableModel.

Specified by:
addRows in interface ListTableModel
Parameters:
addedRows - the rows being added.

clear

public void clear()
Clears the model of any data.

Specified by:
clear in interface ListTableModel

fireRowsMapped

public void fireRowsMapped(int[] changedIndices)
Notifies listeners that the rows of the table have been reordered according to the changedIndices map.

Specified by:
fireRowsMapped in interface ReorderModel
Parameters:
changedIndices - int array indicating how the rows have been reordered.
See Also:
ReorderEvent

fireRowsReordered

public void fireRowsReordered(ReorderEvent e)
Notifies listeners that the rows of the table have been reordered in some way.

Specified by:
fireRowsReordered in interface ReorderModel
Parameters:
e - the reorder event.
See Also:
ReorderEvent

getCellValue

public Object getCellValue(Object o,
                           int index)
Returns the Object found at index of row passed as a parameter.

Specified by:
getCellValue in interface ListTableModel
Parameters:
o - the row data
index - an index
Returns:
the object found at column index of the row data row

getColumnClass

public Class getColumnClass(int c)
Returns the class of the object at column c. This method first fetches the object at row 0 and column c and then returns getClass() on that object.
If the object is null, Object.class is returned. Do not call this method, if there are no data in the tablemodel, since an OutOfIndexException will be thrown.

Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel
Parameters:
c - the column
Returns:
the class of the object at columnc.

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.

Specified by:
getColumnCount in interface TableModel
Returns:
the number of columns in the model
See Also:
TableModel.getRowCount()

getColumnName

public String getColumnName(int column)
Returns the name of the column at column, which depends on the String array passed in the constructor.

Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel
Parameters:
column - the column being queried
Returns:
the name of the column at column.

getColumns

public String[] getColumns()
Returns a clone of the String array of table column names.

Returns:
the table columns

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. This method should be quick, as it is called frequently during rendering.

Specified by:
getRowCount in interface TableModel
Returns:
the number of rows in the model
See Also:
TableModel.getColumnCount()

getRows

public List getRows()
Returns the data of this tablemodel

Specified by:
getRows in interface ListTableModel
Returns:
the rows of the datamodel as a java.util.List

getSendReorderEvent

public boolean getSendReorderEvent()
Determines if a reorder event should be sent when needed. ObjectTableModel's default behaviour is to send a reorder event when non-consecutive rows are removed from the model.

Returns:
true if a reorder event should be sent, false otherwise.

getValueAt

public Object getValueAt(int rowIndex,
                         int columnIndex)
Returns the value for the cell at columnIndex and rowIndex.

Specified by:
getValueAt in interface TableModel
Parameters:
rowIndex - the row whose value is to be queried
columnIndex - the column whose value is to be queried
Returns:
the value Object at the specified cell

getValueAt

public abstract Object getValueAt(Object o,
                                  int index)
Returns the value for the object o at index.

Parameters:
o - the object whose value is to be queried
index - the column whose value is to be queried
Returns:
the value of the Object at the specified index

insertRow

public void insertRow(int row,
                      Object o)
Inserts a row at row in the model.

Parameters:
row - the row index of the row to be inserted
o - the row being added

insertRows

public void insertRows(int row,
                       List insertedRows)
Inserts a list of rows at row in the model.

addedRows contains objects representing the actual rows being added. e.g. java.util.Vector, for DefaultTableModel.

Parameters:
row - the row index of the rows to be inserted
insertedRows - the rows being inserted.

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
Returns true by default.

Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in class AbstractTableModel
Parameters:
rowIndex - the row being queried
columnIndex - the column being queried
Returns:
true
See Also:
TableModel.setValueAt(java.lang.Object, int, int)

removeReorderListener

public void removeReorderListener(ReorderListener l)
Removes a listener from the list that is notified each time a reorder event is generated.

Specified by:
removeReorderListener in interface ReorderModel
Parameters:
l - the RearrangeListener

removeRow

public void removeRow(int row)
Removes a row from the data model

Specified by:
removeRow in interface ListTableModel
Parameters:
row - the index of the row being removed

removeRows

public void removeRows(int[] deletedRows)
Removes a few rows from the data model

Specified by:
removeRows in interface ListTableModel
Parameters:
deletedRows - an integer array that contains indexes of the rows being deleted

setColumns

public void setColumns(String[] columns)
Sets the table columns.

Parameters:
columns - the table columns as an array of Strings

setSendReorderEvent

public void setSendReorderEvent(boolean sendReorderEvent)
Determines if a reorder event should be sent when needed.

Parameters:
sendReorderEvent - true if a reorder event should be sent, false otherwise.

setValueAt

public void setValueAt(Object aValue,
                       int rowIndex,
                       int columnIndex)
Assigns aValue to the object at columnIndex and rowIndex.

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class AbstractTableModel
Parameters:
aValue - value to assign to the cell
rowIndex - row of the cell
columnIndex - column of the cell
See Also:
TableModel.getValueAt(int, int), TableModel.isCellEditable(int, int)

setValueAt

public abstract void setValueAt(Object o,
                                Object aValue,
                                int index)
Sets the value of Object o at index to aValue

Parameters:
o - the object whose value is to be changed
aValue - the new value
index - the index whose value is to be changed

setRows

public void setRows(List rows)
Assigns the 'row' data.

Parameters:
rows - the row data to assign
Since:
3.4.1

Copyright © 2005 Scientific Applications. All Rights Reserved.