gov.nih.nlm.swing
Class TableSorter

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

public class TableSorter
extends TableMap

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
See Also:
Serialized Form

Field Summary
 
Fields inherited from class gov.nih.nlm.swing.TableMap
model
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TableSorter()
          Instantiates an empty TableSorter.
TableSorter(TableModel model)
          Instantiates a TableSorter with the specified model.
 
Method Summary
 void addMouseListenerToHeaderInTable(JTable table)
          Adds a mouse listener to the Table to trigger a table sort when a column heading is clicked in the JTable.
 void checkModel()
          Validates the model.
 int compare(int row1, int row2)
          Perform comparison on the sorting column.
 int compareRowsByColumn(int row1, int row2, int column)
          Compares data in sorted column by type.
 void dump()
          Debug method.
 Object getValueAt(int r, int c)
          Returns the value at the specified row and column from the underlying model.
 int mapIndex(int i)
          Returns the mapped index for the specified table row index.
 void reallocateIndexes()
          Resets the map so that the first row of the table corresponds to the first index of the underlying model.
 void resetSort()
          Resets the sorting mechanism to wait until a header is clicked before sorting the table.
 int reverseMapIndex(int i)
          Returns the table row index for the underlying model index.
 void setModel(TableModel model)
          Sets the model and resets the map.
 void setValueAt(Object value, int r, int c)
          Sets the value at the specified row and column from the underlying model.
 void shuttlesort(int[] from, int[] to, int low, int high)
          This is a home-grown implementation which we have not had time to research - it may perform poorly in some circumstances.
 void sort(Object sender)
          Sorts the table.
 void sortByColumn(int column)
          Sorts table by a column (ascending by default).
 void sortByColumn(int column, boolean ascending)
          Sorts table by a column.
 void swap(int i, int j)
          Swap two elements of the map.
 void tableChanged(TableModelEvent e)
          Handles the table change event.
 
Methods inherited from class gov.nih.nlm.swing.TableMap
getColumnClass, getColumnCount, getColumnName, getModel, getRowCount, isCellEditable
 
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()
Instantiates an empty TableSorter.


TableSorter

public TableSorter(TableModel model)
Instantiates a TableSorter with the specified model.

Parameters:
model - the TableModel
Method Detail

setModel

public void setModel(TableModel model)
Sets the model and resets the map.

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

resetSort

public void resetSort()
Resets the sorting mechanism to wait until a header is clicked before sorting the table.


compareRowsByColumn

public int compareRowsByColumn(int row1,
                               int row2,
                               int column)
Compares data in sorted column by type. If the type is an object type, it sorts by string value.

Parameters:
row1 - A table row
row2 - Another table row
column - A table column
Returns:
a value indicating the relative sort order of row1 and row2 for the specified column

compare

public int compare(int row1,
                   int row2)
Perform comparison on the sorting column.

Parameters:
row1 - A table row
row2 - Another table row
Returns:
an value indicating the relative sort order of row1 and row2

reallocateIndexes

public void reallocateIndexes()
Resets the map so that the first row of the table corresponds to the first index of the underlying model.


tableChanged

public void tableChanged(TableModelEvent e)
Handles the table change event.

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

checkModel

public void checkModel()
Validates the model. This simple check verifies that the index map has the same row count as the underlying model.


sort

public void sort(Object sender)
Sorts the table. Actually, it sorts the appropriate column and re-orders the index map to produce a sorted table column.

Parameters:
sender - Object

shuttlesort

public void shuttlesort(int[] from,
                        int[] to,
                        int low,
                        int high)
This is a home-grown implementation which we have not had time to research - it may perform poorly in some circumstances. It requires twice the space of an in-place algorithm and makes NlogN assigments shuttling the values between the two arrays. The number of compares appears to vary between N-1 and NlogN depending on the initial order but the main reason for using it here is that, unlike qsort, it is stable.

Parameters:
from - source
to - target
low - low value
high - high value

swap

public void swap(int i,
                 int j)
Swap two elements of the map.

Parameters:
i - first index to swap
j - second index to swap

getValueAt

public Object getValueAt(int r,
                         int c)
Returns the value at the specified row and column from the underlying model. This operation is performed by mapping the row index through the map.

Specified by:
getValueAt in interface TableModel
Overrides:
getValueAt in class TableMap
Parameters:
r - a table row
c - a table column
Returns:
the value at the specified row and column from the underlying model

setValueAt

public void setValueAt(Object value,
                       int r,
                       int c)
Sets the value at the specified row and column from the underlying model. This operation is performed by mapping the row index through the map.

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class TableMap
Parameters:
r - a table row
c - a table column
value - an Object value.

sortByColumn

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

Parameters:
column - the column number

sortByColumn

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

Parameters:
column - the column number
ascending - flag indicating sort order

mapIndex

public int mapIndex(int i)
Returns the mapped index for the specified table row index.

Parameters:
i - a table row
Returns:
the index of the underlying model

reverseMapIndex

public int reverseMapIndex(int i)
Returns the table row index for the underlying model index.

Parameters:
i - the underlying index of the model
Returns:
the corresponding table row index

dump

public void dump()
Debug method.


addMouseListenerToHeaderInTable

public void addMouseListenerToHeaderInTable(JTable table)
Adds a mouse listener to the Table to trigger a table sort when a column heading is clicked in the JTable.

Parameters:
table - the sortable JTable


Copyright ©2005