kiwi.ui.model
Class TableSorter

java.lang.Object
  |
  +--javax.swing.table.AbstractTableModel
        |
        +--kiwi.ui.model.TableMap
              |
              +--kiwi.ui.model.TableSorter

public class TableSorter
extends TableMap

A sorter for TableModels. The sorter has a model (conforming to TableModel) and itself implements TableModel. TableSorter does not store or copy the data in the TableModel; instead it maintains an array of integers which it keeps the same size as the number of rows in its model. When the model changes it notifies the sorter that something has changed eg. "rowsAdded" so that its internal array of integers can be reallocated. As requests are made of the sorter (like getValueAt(row, col)) it redirects them to its model via the mapping array. That way the TableSorter appears to hold another copy of the table with the rows in a different order. The sorting algorthm used is stable which means that it does not move around rows when its comparison function returns 0 to denote that they are equivalent.

Version:
1.5 (12/97)
Author:
Philip Milne, Mark Lindner
See Also:
Serialized Form

Fields inherited from class kiwi.ui.model.TableMap
model
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TableSorter()
          Construct a new TableSorter.
TableSorter(TableModel model)
          Construct a new TableSorter with a specified data model..
 
Method Summary
 int getRowTranslation(int row)
          Return the index of the given row in the unsorted model that this model wraps.
 Object getValueAt(int row, int col)
          Get the value at the given row and column of the unsorted table.
 void registerTableHeaderListener(JTable table)
          Add a mouse listener to the JTable to trigger a table sort when a column heading is clicked.
 void setModel(TableModel model)
          Set the TableModel for this table sorter.
 void setValueAt(Object value, int row, int col)
          Set the value at the given row and column of the unsorted table.
 void sortByColumn(int column)
          Sort a column in the table in ascending order.
 void sortByColumn(int column, boolean ascending)
          Sort a column in the table.
 void tableChanged(TableModelEvent e)
          Handle table changed events.
 
Methods inherited from class kiwi.ui.model.TableMap
getColumnClass, getColumnCount, getColumnName, getModel, getRowCount, isCellEditable
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, removeTableModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TableSorter

public TableSorter()
Construct a new TableSorter.

TableSorter

public TableSorter(TableModel model)
Construct a new TableSorter with a specified data model..
Parameters:
model - The TableModel to use.
Method Detail

getRowTranslation

public int getRowTranslation(int row)
Return the index of the given row in the unsorted model that this model wraps. This method is useful for determining which row in the actual model is mapped to the currently selected row in the sorted TableMap.

setModel

public void setModel(TableModel model)
Set the TableModel for this table sorter.
Parameters:
model - The TableModel to use.
Overrides:
setModel in class TableMap

getValueAt

public Object getValueAt(int row,
                         int col)
Get the value at the given row and column of the unsorted table.
Parameters:
row - The row.
col - The column.
Returns:
The object at the given position in the unsorted model.
Overrides:
getValueAt in class TableMap

setValueAt

public void setValueAt(Object value,
                       int row,
                       int col)
Set the value at the given row and column of the unsorted table.
Parameters:
row - The row.
col - The column.
value - The new value.
Overrides:
setValueAt in class TableMap

sortByColumn

public void sortByColumn(int column)
Sort a column in the table in ascending order.
Parameters:
column - The index of the column to sort.

sortByColumn

public void sortByColumn(int column,
                         boolean ascending)
Sort a column in the table.
Parameters:
column - The index of the column to sort.
ascending - If true, sorts in ascending order; otherwise, sorts in descending order.

tableChanged

public void tableChanged(TableModelEvent e)
Handle table changed events.
Overrides:
tableChanged in class TableMap

registerTableHeaderListener

public void registerTableHeaderListener(JTable table)
Add a mouse listener to the JTable to trigger a table sort when a column heading is clicked.
Parameters:
table - The JTable to listen for events on.