org.salespointframework.desktop
Class TableSorter

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by org.salespointframework.desktop.models.AbstractTableModel
          extended by org.salespointframework.desktop.TableMap
              extended by org.salespointframework.desktop.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.

Version:
1.6 2003-03-25
Author:
Philip Milne, Thomas Medack, Andreas Bartho
See Also:
Serialized Form

Field Summary
private  int actualColumn
          The column to be sorted.
private  boolean ascending
          Sort ascending or descending.
private  int compares
          Internal helper variable.
private  int[] indexes
          Field of indexes.
private  int[] reverseIndexes
          Field of indexes.
private  java.util.Vector<java.lang.Integer> sortingColumns
          Sorting vector.
 
Fields inherited from class org.salespointframework.desktop.TableMap
model
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TableSorter()
          Constructor.
TableSorter(AbstractTableModel model)
          Constructor.
 
Method Summary
 void addMouseListenerToHeaderInTable(javax.swing.JTable table, java.lang.Object[] ao)
          Adds a mouse listener to the table header.
private  boolean checkModel()
          Helper method.
 int compare(int row1, int row2)
          Comparison of two rows.
 int compareRowsByColumn(int row1, int row2, int column)
          Compares columns of two specific rows.
private  void createReverseIndexes()
          Creates a reverse index table.
 java.lang.Object getRecord(int row)
          Gets the record.
 java.lang.Object getValueAt(int aRow, int aColumn)
          Get the value of a table cell.
private  void reallocateIndexes()
          Helper method.
 void setModel(AbstractTableModel model)
          Sets the TableModel.
 void setValueAt(java.lang.Object aValue, int aRow, int aColumn)
          Changes the value of a table cell.
private  void shuttlesort(int[] from, int[] to, int low, int high)
          Sorting method.
private  void sort()
          Sorts the model.
 void sortByColumn(int column)
          Sorts the table ascending by a column.
 void sortByColumn(int column, boolean ascending)
          Sorts the table by a column.
 void tableChanged(javax.swing.event.TableModelEvent e)
          Reacts on TableChangeEvents, either converts them as needed or passes them to the model.
 
Methods inherited from class org.salespointframework.desktop.TableMap
getColumnClass, getColumnCount, getColumnName, getModel, getRowCount, isCellEditable, setData
 
Methods inherited from class org.salespointframework.desktop.models.AbstractTableModel
getEntryDescriptor, orderByColumn, reOrderBy
 
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
 

Field Detail

indexes

private int[] indexes
Field of indexes.


reverseIndexes

private int[] reverseIndexes
Field of indexes.


sortingColumns

private java.util.Vector<java.lang.Integer> sortingColumns
Sorting vector.


ascending

private boolean ascending
Sort ascending or descending.


actualColumn

private int actualColumn
The column to be sorted.


compares

private int compares
Internal helper variable.

Constructor Detail

TableSorter

public TableSorter()
Constructor.


TableSorter

public TableSorter(AbstractTableModel model)
Constructor.

Parameters:
model - the wrapped TableModel
Method Detail

setModel

public void setModel(AbstractTableModel model)
Sets the TableModel.

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

compareRowsByColumn

public int compareRowsByColumn(int row1,
                               int row2,
                               int column)
Compares columns of two specific rows.

Attention:
This code is not 100% Java 1.5 compatible. Had to avoid the warning with SuppressWarnings because there is no clean solution for this in 1.5 yet.

Parameters:
row1 - first row.
row2 - second row.
column - the column index.
Returns:
a comparative value.

compare

public int compare(int row1,
                   int row2)
Comparison of two rows.

Parameters:
row1 - first row.
row2 - second row.
Returns:
a comparative value.

reallocateIndexes

private void reallocateIndexes()
Helper method.


tableChanged

public void tableChanged(javax.swing.event.TableModelEvent e)
Reacts on TableChangeEvents, either converts them as needed or passes them to the model.

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

createReverseIndexes

private void createReverseIndexes()
Creates a reverse index table. This method maps the line number for each table to have the view updated Example: indexes = [2 3 0 1 4]
reverseIndex stores in i, where i appeares in indexes:
0 is on position 2, 1 is on position 3, 2 is on position 0 ... => reverseIndex = [2 3 0 1 4]
Mapping this array with the row number sent by a TableChangeEvent event, all tables have the correct lines updated (has only effect on CountingStocks)


checkModel

private boolean checkModel()
Helper method.


sort

private void sort()
Sorts the model. It uses shuttlesort.


shuttlesort

private void shuttlesort(int[] from,
                         int[] to,
                         int low,
                         int high)
Sorting method.

Parameters:
from - the unsorted index array.
to - the sorted index array (i.e. the result).
low - lowest field to be taken into consideration on sorting.
high - highest field to be taken into consideration on sorting.

getValueAt

public java.lang.Object getValueAt(int aRow,
                                   int aColumn)
Get the value of a table cell.

Specified by:
getValueAt in interface javax.swing.table.TableModel
Overrides:
getValueAt in class TableMap
Parameters:
aRow - the row of the TableCell to get.
aColumn - the column of the table cell to get.
Returns:
the value at (aRow, aColumn).

getRecord

public java.lang.Object getRecord(int row)
Gets the record.

Overrides:
getRecord in class TableMap
Parameters:
row - the affected table row.
Returns:
the appropriate record.

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int aRow,
                       int aColumn)
Changes the value of a table cell.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class TableMap
Parameters:
aValue - the value to set.
aRow - the row of the TableCell to be changed.
aColumn - the column of the table cell to be changed.

sortByColumn

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

Parameters:
column - the column by which the table should be sorted.

sortByColumn

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

Parameters:
column - the column by which the table should be sorted.
ascending - if true sort sequence is ascending, otherwise descending.

addMouseListenerToHeaderInTable

public void addMouseListenerToHeaderInTable(javax.swing.JTable table,
                                            java.lang.Object[] ao)
Adds a mouse listener to the table header.

Parameters:
table - the table to which header the listener is to be added