jwo.utils.gui
Class TableSorter

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by jwo.utils.gui.TableMap
          extended by jwo.utils.gui.TableSorter
All Implemented Interfaces:
Serializable, EventListener, TableModelListener, TableModel

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 e.g. "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.

Version:
2.3, 17th December, 1997, last modified 4th October, 2007.
Author:
Philip Milne, modifed by Jo Wood.
See Also:
Serialized Form

Field Summary
 
Fields inherited from class jwo.utils.gui.TableMap
model
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TableSorter()
          Creates the sorter with no underlying table model.
TableSorter(TableModel model)
          Creates the sorter using the given table model.
 
Method Summary
 void addMouseListenerToHeaderInTable(JTable table)
           
 void checkModel()
          Checks to see if model is ready for possible changes.
 int compare(int row1, int row2)
          Compares two rows and indicates their order.
 int compareRowsByColumn(int row1, int row2, int column)
          Performs a comparison between two rows based on the values in the given column.
 Object getValueAt(int aRow, int aColumn)
          Reports the table value at the given coordinates.
 void n2sort()
           
 void reallocateIndexes()
          Reallocates row indices in table.
 void setModel(TableModel model)
          Sets table model that can be sorted.
 void setValueAt(Object aValue, int aRow, int aColumn)
          Sets the table value at the given coordinates.
 void shuttlesort(int[] from, int[] to, int low, int high)
           
 void sort()
           
 void sortByColumn(int column)
           
 void sortByColumn(int column, boolean ascending)
           
 void swap(int i, int j)
           
 void tableChanged(TableModelEvent e)
          Used to signal that there has been a change to something in the table.
 
Methods inherited from class jwo.utils.gui.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()
Creates the sorter with no underlying table model. Use setModel to associate the sorter with a particular table model.


TableSorter

public TableSorter(TableModel model)
Creates the sorter using the given table model.

Parameters:
model - Table model to be sortable by this class.
Method Detail

setModel

public void setModel(TableModel model)
Sets table model that can be sorted.

Overrides:
setModel in class TableMap
Parameters:
model - Table model to be sortable by this class.

compareRowsByColumn

public int compareRowsByColumn(int row1,
                               int row2,
                               int column)
Performs a comparison between two rows based on the values in the given column.

Parameters:
row1 - First row to compare.
row2 - Second row to compare.
column - Column value to identify the two cells to compare.
Returns:
0 if cells in both rows are equal, -1 if first cell less than second one and 1 if first greater than second.

compare

public int compare(int row1,
                   int row2)
Compares two rows and indicates their order.

Parameters:
row1 - First row to compare.
row2 - Second row to compare.
Returns:
Order of the two rows. 0 if rows identical, positive if first row first otherwise negative.

reallocateIndexes

public void reallocateIndexes()
Reallocates row indices in table.


tableChanged

public void tableChanged(TableModelEvent e)
Description copied from class: TableMap
Used to signal that there has been a change to something in the table.

Specified by:
tableChanged in interface TableModelListener
Overrides:
tableChanged in class TableMap
Parameters:
e - Event encapsulating the table change.

checkModel

public void checkModel()
Checks to see if model is ready for possible changes.


sort

public void 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 Object getValueAt(int aRow,
                         int aColumn)
Description copied from class: TableMap
Reports the table value at the given coordinates.

Specified by:
getValueAt in interface TableModel
Overrides:
getValueAt in class TableMap
Parameters:
aRow - Row of table cell to report.
aColumn - Column of table cell to report.
Returns:
Object at given table coordinates.

setValueAt

public void setValueAt(Object aValue,
                       int aRow,
                       int aColumn)
Description copied from class: TableMap
Sets the table value at the given coordinates.

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class TableMap
Parameters:
aValue - Object to place at given table coordinates.
aRow - Row of table cell to set.
aColumn - Column of table cell to set.

sortByColumn

public void sortByColumn(int column)

sortByColumn

public void sortByColumn(int column,
                         boolean ascending)

addMouseListenerToHeaderInTable

public void addMouseListenerToHeaderInTable(JTable table)


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009