com.anthonyeden.lib.util
Class TableSorter

java.lang.Object
  extended byjavax.swing.table.AbstractTableModel
      extended bycom.anthonyeden.lib.util.TableMap
          extended bycom.anthonyeden.lib.util.TableSorter
All Implemented Interfaces:
java.util.EventListener, java.io.Serializable, javax.swing.table.TableModel, javax.swing.event.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.

Version:
1.5 12/17/97
Author:
Philip Milne, Anthony Eden
See Also:
Serialized Form

Field Summary
 
Fields inherited from class com.anthonyeden.lib.util.TableMap
model
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TableSorter()
          Construct a TableSorter with no TableModel.
TableSorter(javax.swing.table.TableModel model)
          Construct a TableSorter with the given TableModel.
 
Method Summary
 void addMouseListenerToHeaderInTable(javax.swing.JTable table)
          Add a MouseListener to the given table which will cause the table to be sorted when the header is clicked.
 void checkModel()
          Check to see if the index length matches the model row count.
 int compare(int row1, int row2)
          Compare two rows.
 int compareRowsByColumn(int row1, int row2, int column)
          Compare values row by row.
 int getColumn()
          Get the column which is being sorted.
 int getRealIndex(int row)
          Get the real index of the given row.
 java.lang.Object getValueAt(int aRow, int aColumn)
          Get the value at the given row and column.
 boolean isAscending()
          Return true if the data is sorted in ascending order.
 void n2sort()
           
 void reallocateIndexes()
          Reallocate the array which holds sorted indexes.
 void setModel(javax.swing.table.TableModel model)
          Set the TableModel.
 void setValueAt(java.lang.Object aValue, int aRow, int aColumn)
          Set the value at the given row and column.
 void shuttlesort(int[] from, int[] to, int low, int high)
           
 void sort(java.lang.Object sender)
          Sort the table data.
 void sortByColumn(int column)
          Sort by the given column in ascending order.
 void sortByColumn(int column, boolean ascending)
          Sort by the given column and order.
 void swap(int i, int j)
           
 void tableChanged(javax.swing.event.TableModelEvent e)
          Fire a TableModelEvent signaling that the table has changed.
 
Methods inherited from class com.anthonyeden.lib.util.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()
Construct a TableSorter with no TableModel.


TableSorter

public TableSorter(javax.swing.table.TableModel model)
Construct a TableSorter with the given TableModel.

Parameters:
model - The TableModel
Method Detail

setModel

public void setModel(javax.swing.table.TableModel model)
Set the TableModel.

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

compareRowsByColumn

public int compareRowsByColumn(int row1,
                               int row2,
                               int column)
Compare values row by row. This method can compare Strings, Booleans, Dates, or Numbers. It compares objects by calling each object's toString() method and then comparing the Strings. It can also compare objects which implement the java.lang.Comparable interface.

Parameters:
row1 - Row 1
row2 - Row 2
column - The column to sort
Returns:
1 If row 1 is greater than row 2, -1 if row 1 is less than row 2, or 0 if the values match

compare

public int compare(int row1,
                   int row2)
Compare two rows. All sorting columns will be sorted.

Parameters:
row1 - Row 1
row2 - Row 2
Returns:
1, 0, or -1

reallocateIndexes

public void reallocateIndexes()
Reallocate the array which holds sorted indexes.


tableChanged

public void tableChanged(javax.swing.event.TableModelEvent e)
Fire a TableModelEvent signaling that the table has changed.

Specified by:
tableChanged in interface javax.swing.event.TableModelListener
Overrides:
tableChanged in class TableMap
Parameters:
e - The TableModelEvent

checkModel

public void checkModel()
Check to see if the index length matches the model row count. If not then the sorter was never informed of a change in the model.


sort

public void sort(java.lang.Object sender)
Sort the table data.

Parameters:
sender - The object which invoked the sort

n2sort

public void n2sort()

shuttlesort

public void shuttlesort(int[] from,
                        int[] to,
                        int low,
                        int high)

swap

public void swap(int i,
                 int j)

getValueAt

public java.lang.Object getValueAt(int aRow,
                                   int aColumn)
Description copied from class: TableMap
Get the value at the given row and column.

Specified by:
getValueAt in interface javax.swing.table.TableModel
Overrides:
getValueAt in class TableMap
Parameters:
aRow - The row
aColumn - The column
Returns:
The value

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int aRow,
                       int aColumn)
Description copied from class: TableMap
Set the value at the given row and column.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class TableMap
Parameters:
aValue - The value
aRow - The row
aColumn - The column

sortByColumn

public void sortByColumn(int column)
Sort by the given column in ascending order.

Parameters:
column - The column

sortByColumn

public void sortByColumn(int column,
                         boolean ascending)
Sort by the given column and order.

Parameters:
column - The column
ascending - True to sort in ascending order

getColumn

public int getColumn()
Get the column which is being sorted.

Returns:
The sorted column

getRealIndex

public int getRealIndex(int row)
Get the real index of the given row.

Parameters:
row - The row index
Returns:
The index

isAscending

public boolean isAscending()
Return true if the data is sorted in ascending order.

Returns:
True if sorted in ascending order

addMouseListenerToHeaderInTable

public void addMouseListenerToHeaderInTable(javax.swing.JTable table)
Add a MouseListener to the given table which will cause the table to be sorted when the header is clicked. The table will be sorted in ascending order initially. If the table was already sorted in ascending order and the same column is clicked then the order will be reversed.

Parameters:
table - The JTable


Copyright (c) 2000-2003 Anthony Eden. All Rights Reserved.