org.aitools.programd.interfaces.graphical
Class TableSorter

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by org.aitools.programd.interfaces.graphical.TableMap
          extended by org.aitools.programd.interfaces.graphical.TableSorter
All Implemented Interfaces:
java.io.Serializable, java.util.EventListener, javax.swing.event.TableModelListener, javax.swing.table.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 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, Noel Bush
See Also:
Serialized Form

Field Summary
(package private)  boolean ascending
           
(package private)  int compares
           
(package private)  int[] indexes
           
(package private)  java.util.Vector<java.lang.Integer> sortingColumns
           
 
Fields inherited from class org.aitools.programd.interfaces.graphical.TableMap
model
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TableSorter()
          Creates a new TableSorter.
TableSorter(javax.swing.table.TableModel modelToSet)
           
 
Method Summary
 void addMouseListenerToHeaderInTable(javax.swing.JTable table)
          Adds a mouse listener to the header of the given table to trigger a table sort when a column heading is clicked in the JTable.
 int compare(int row1, int row2)
          Compares two rows.
 int compareRowsByColumn(int row1, int row2, int column)
          Compares the given rows in the given column
 java.lang.Object getValueAt(int aRow, int aColumn)
           
 void checkModel()
          Checks that the model is valid (has not been changed without informing the sorter).
 void n2sort()
          Sorts the table according to an n2 algorithm.
 void reallocateIndexes()
          Sets up a new array of indices with the correct number of elements for the new data model, and initializes with the identity mapping.
 void setModel(javax.swing.table.TableModel modelToSet)
          Sets the TableMap's model to the given model.
 void setValueAt(java.lang.Object aValue, int aRow, int aColumn)
           
 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()
           
 void sortByColumn(int column)
          Sorts the table by the specified column.
 void sortByColumn(int column, boolean ascendingSetting)
          Sorts by the specified column, either ascending or descending.
 void swap(int i, int j)
          Swaps the cells indicated by i and j.
 void tableChanged(javax.swing.event.TableModelEvent e)
           
 
Methods inherited from class org.aitools.programd.interfaces.graphical.TableMap
getColumnClass, getColumnCount, getColumnName, getModel, getRowCount, isCellEditable
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableDataChanged, fireTableChanged, 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
 

Field Detail

indexes

int[] indexes

sortingColumns

java.util.Vector<java.lang.Integer> sortingColumns

ascending

boolean ascending

compares

int compares
Constructor Detail

TableSorter

public TableSorter()
Creates a new TableSorter.


TableSorter

public TableSorter(javax.swing.table.TableModel modelToSet)
Parameters:
modelToSet -
Method Detail

setModel

public void setModel(javax.swing.table.TableModel modelToSet)
Description copied from class: TableMap
Sets the TableMap's model to the given model.

Overrides:
setModel in class TableMap
Parameters:
modelToSet - the model to set
See Also:
TableMap.setModel(javax.swing.table.TableModel)

compareRowsByColumn

public int compareRowsByColumn(int row1,
                               int row2,
                               int column)
Compares the given rows in the given column

Parameters:
row1 - one row
row2 - the other row
column - the column in which to compare
Returns:
an indicator of the comparison of the two columns (?)

compare

public int compare(int row1,
                   int row2)
Compares two rows.

Parameters:
row1 - one row
row2 - another row
Returns:
the result of comparing the two rows

reallocateIndexes

public void reallocateIndexes()
Sets up a new array of indices with the correct number of elements for the new data model, and initializes with the identity mapping.


tableChanged

public void tableChanged(javax.swing.event.TableModelEvent e)
Specified by:
tableChanged in interface javax.swing.event.TableModelListener
Overrides:
tableChanged in class TableMap
See Also:
TableMap.tableChanged(javax.swing.event.TableModelEvent)

checkModel

public void checkModel()
Checks that the model is valid (has not been changed without informing the sorter).


sort

public void sort()

n2sort

public void n2sort()
Sorts the table according to an n2 algorithm.


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 - ?
to - ?
low - ?
high - ?

swap

public void swap(int i,
                 int j)
Swaps the cells indicated by i and j.

Parameters:
i - a cell
j - a cell

getValueAt

public java.lang.Object getValueAt(int aRow,
                                   int aColumn)
Specified by:
getValueAt in interface javax.swing.table.TableModel
Overrides:
getValueAt in class TableMap
See Also:
TableMap.getValueAt(int, int)

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int aRow,
                       int aColumn)
Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class TableMap
See Also:
TableMap.setValueAt(java.lang.Object, int, int)

sortByColumn

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

Parameters:
column - the column by which to sort

sortByColumn

public void sortByColumn(int column,
                         boolean ascendingSetting)
Sorts by the specified column, either ascending or descending.

Parameters:
column - the column by which to sort
ascendingSetting - whether to sort ascending

addMouseListenerToHeaderInTable

public void addMouseListenerToHeaderInTable(javax.swing.JTable table)
Adds a mouse listener to the header of the given table to trigger a table sort when a column heading is clicked in the JTable.

Parameters:
table - the table to whose header to add a mouse listener.