SalesPoint Framework v3.1

util.swing
Class TableSorter

java.lang.Object
  extended byjavax.swing.table.AbstractTableModel
      extended byutil.swing.AbstractTableModel
          extended byutil.swing.TableMap
              extended byutil.swing.TableSorter
All Implemented Interfaces:
EventListener, Serializable, TableModel, TableModelListener

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.

Author:
Philip Milne, Thomas Medack, Andreas Bartho
See Also:
Serialized Form

Field Summary
 
Fields inherited from class util.swing.TableMap
model
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TableSorter()
          Constructor.
TableSorter(AbstractTableModel model)
          Constructor.
 
Method Summary
 void addMouseListenerToHeaderInTable(JTable table, Object[] ao)
          Adds a mouse listener to the table header.
 int compare(int row1, int row2)
          Comparison of two rows.
 int compareRowsByColumn(int row1, int row2, int column)
          Compares columns of two specific rows.
 Object getRecord(int row)
          Gets the record.
 Object getValueAt(int aRow, int aColumn)
          Changes the value of a table cell.
 void setModel(AbstractTableModel model)
          Sets the TableModel.
 void setValueAt(Object aValue, int aRow, int aColumn)
          Changes the value of a table cell.
 void sortByColumn(int column)
          Sorts the table ascending by a column.
 void sortByColumn(int column, boolean ascending)
          Sorts the table by a column.
 void tableChanged(TableModelEvent e)
          Reacts on TableChangeEvents, either converts them as needed or passes them to the model.
 
Methods inherited from class util.swing.TableMap
getColumnClass, getColumnCount, getColumnName, getModel, getRowCount, isCellEditable, setData
 
Methods inherited from class util.swing.AbstractTableModel
getEntryDescriptor, orderByColumn, reOrderBy
 
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
 

Constructor Detail

TableSorter

public TableSorter()
Constructor.


TableSorter

public TableSorter(AbstractTableModel model)
Constructor.

Parameters:
model - the wrapped TableModel
Method Detail

setModel

public void setModel(AbstractTableModel model)
Sets the TableModel.

Overrides:
setModel in class TableMap
Parameters:
model - the model.

compareRowsByColumn

public int compareRowsByColumn(int row1,
                               int row2,
                               int column)
Compares columns of two specific rows.

Parameters:
row1 - first row.
row2 - second row.
column - the column index.
Returns:
a comparative value.

compare

public int compare(int row1,
                   int row2)
Comparison of two rows.

Parameters:
row1 - first row.
row2 - second row.
Returns:
a comparative value.

tableChanged

public void tableChanged(TableModelEvent e)
Reacts on TableChangeEvents, either converts them as needed or passes them to the model.

Specified by:
tableChanged in interface TableModelListener
Overrides:
tableChanged in class TableMap
Parameters:
e - the event.

getValueAt

public Object getValueAt(int aRow,
                         int aColumn)
Changes the value of a table cell.

Specified by:
getValueAt in interface TableModel
Overrides:
getValueAt in class TableMap
Parameters:
aRow - the row of the TableCell to be changed.
aColumn - the column of the table cell to be changed.
Returns:
the value at (aRow, aColumn).

getRecord

public Object getRecord(int row)
Gets the record.

Overrides:
getRecord in class TableMap
Parameters:
row - the affected table row.
Returns:
the appropriate record.

setValueAt

public void setValueAt(Object aValue,
                       int aRow,
                       int aColumn)
Changes the value of a table cell.

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class TableMap
Parameters:
aValue - the value to set.
aRow - the row of the TableCell to be changed.
aColumn - the column of the table cell to be changed.

sortByColumn

public void sortByColumn(int column)
Sorts the table ascending by a column.

Parameters:
column - the column by which the table should be sorted.

sortByColumn

public void sortByColumn(int column,
                         boolean ascending)
Sorts the table by a column.

Parameters:
column - the column by which the table should be sorted.
ascending - if true sort sequence is ascending, otherwise descending.

addMouseListenerToHeaderInTable

public void addMouseListenerToHeaderInTable(JTable table,
                                            Object[] ao)
Adds a mouse listener to the table header.

Parameters:
table - the table to which header the listener is to be added

SalesPoint Framework v3.1