Class TableSorter
- All Implemented Interfaces:
Serializable,EventListener,TableModelListener,TableModel
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 algorithm used is stable which means that it does not move around rows when its comparison function returns 0 to denote that they are equivalent.
- See Also:
-
Field Summary
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty sorter with no delegate model.TableSorter(TableModel model) Creates a sorter for the supplied table model. -
Method Summary
Modifier and TypeMethodDescriptionvoidInstalls a header listener that resorts the table when a column header is clicked.voidVerifies that the cached index mapping still matches the delegate model.intcompare(int row1, int row2) Compares two source rows using the active sort columns.intcompareRowsByColumn(int row1, int row2, int column) Compares two source rows using the values from the supplied column.getValueAt(int aRow, int aColumn) Returns the value from the source row mapped to the supplied view row.voidn2sort()Sorts the row mapping using a simple quadratic algorithm.voidRebuilds the row index mapping to match the current delegate row count.voidsetModel(TableModel model) Replaces the wrapped model and rebuilds the cached row index mapping.voidsetValueAt(Object aValue, int aRow, int aColumn) Writes a value to the source row mapped from the supplied view row.voidshuttlesort(int[] from, int[] to, int low, int high) Performs a stable merge sort over the cached row mapping.voidSorts the cached row mapping using the currently configured sort columns.voidsortByColumn(int column) Sorts the table by the supplied model column in ascending order.voidsortByColumn(int column, boolean ascending) Sorts the table by the supplied model column and direction.voidswap(int i, int j) Swaps two entries in the cached row mapping.voidRebuilds the cached row mapping after any delegate table-model change.Methods inherited from class groovy.swing.table.TableMap
getColumnClass, getColumnCount, getColumnName, getModel, getRowCount, isCellEditableMethods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
-
Constructor Details
-
TableSorter
public TableSorter()Creates an empty sorter with no delegate model. -
TableSorter
Creates a sorter for the supplied table model.- Parameters:
model- the model to wrap
-
-
Method Details
-
setModel
Replaces the wrapped model and rebuilds the cached row index mapping. -
compareRowsByColumn
public int compareRowsByColumn(int row1, int row2, int column) Compares two source rows using the values from the supplied column.- Parameters:
row1- the first source row indexrow2- the second source row indexcolumn- the model column index to compare- Returns:
- a negative value, zero, or a positive value depending on sort order
-
compare
public int compare(int row1, int row2) Compares two source rows using the active sort columns.- Parameters:
row1- the first source row indexrow2- the second source row index- Returns:
- the comparison result in the currently configured direction
-
reallocateIndexes
public void reallocateIndexes()Rebuilds the row index mapping to match the current delegate row count. -
tableChanged
Rebuilds the cached row mapping after any delegate table-model change.- Specified by:
tableChangedin interfaceTableModelListener- Overrides:
tableChangedin classTableMap- Parameters:
e- the table-model event
-
checkModel
public void checkModel()Verifies that the cached index mapping still matches the delegate model. -
sort
Sorts the cached row mapping using the currently configured sort columns.- Parameters:
sender- the caller requesting the sort
-
n2sort
public void n2sort()Sorts the row mapping using a simple quadratic algorithm. -
shuttlesort
public void shuttlesort(int[] from, int[] to, int low, int high) Performs a stable merge sort over the cached row mapping.- Parameters:
from- the source index arrayto- the target index arraylow- the inclusive lower boundhigh- the exclusive upper bound
-
swap
public void swap(int i, int j) Swaps two entries in the cached row mapping.- Parameters:
i- the first mapped row indexj- the second mapped row index
-
getValueAt
Returns the value from the source row mapped to the supplied view row.- Specified by:
getValueAtin interfaceTableModel- Overrides:
getValueAtin classTableMap- Parameters:
aRow- the sorted view rowaColumn- the model column- Returns:
- the mapped cell value
-
setValueAt
Writes a value to the source row mapped from the supplied view row.- Specified by:
setValueAtin interfaceTableModel- Overrides:
setValueAtin classTableMap- Parameters:
aValue- the new cell valueaRow- the sorted view rowaColumn- the model column
-
sortByColumn
public void sortByColumn(int column) Sorts the table by the supplied model column in ascending order.- Parameters:
column- the model column index to sort by
-
sortByColumn
public void sortByColumn(int column, boolean ascending) Sorts the table by the supplied model column and direction.- Parameters:
column- the model column index to sort byascending-truefor ascending order,falsefor descending order
-
addMouseListenerToHeaderInTable
Installs a header listener that resorts the table when a column header is clicked.- Parameters:
table- the table whose header should trigger sorting
-