Class 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 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:
  • Constructor Details

    • TableSorter

      public TableSorter()
      Creates an empty sorter with no delegate model.
    • TableSorter

      public TableSorter(TableModel model)
      Creates a sorter for the supplied table model.
      Parameters:
      model - the model to wrap
  • Method Details

    • setModel

      public void setModel(TableModel model)
      Replaces the wrapped model and rebuilds the cached row index mapping.
      Overrides:
      setModel in class TableMap
      Parameters:
      model - the model to sort
    • 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 index
      row2 - the second source row index
      column - 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 index
      row2 - 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

      public void tableChanged(TableModelEvent e)
      Rebuilds the cached row mapping after any delegate table-model change.
      Specified by:
      tableChanged in interface TableModelListener
      Overrides:
      tableChanged in class TableMap
      Parameters:
      e - the table-model event
    • checkModel

      public void checkModel()
      Verifies that the cached index mapping still matches the delegate model.
    • sort

      public void sort(Object sender)
      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 array
      to - the target index array
      low - the inclusive lower bound
      high - 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 index
      j - the second mapped row index
    • getValueAt

      public Object getValueAt(int aRow, int aColumn)
      Returns the value from the source row mapped to the supplied view row.
      Specified by:
      getValueAt in interface TableModel
      Overrides:
      getValueAt in class TableMap
      Parameters:
      aRow - the sorted view row
      aColumn - the model column
      Returns:
      the mapped cell value
    • setValueAt

      public void setValueAt(Object aValue, int aRow, int aColumn)
      Writes a value to the source row mapped from the supplied view row.
      Specified by:
      setValueAt in interface TableModel
      Overrides:
      setValueAt in class TableMap
      Parameters:
      aValue - the new cell value
      aRow - the sorted view row
      aColumn - 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 by
      ascending - true for ascending order, false for descending order
    • addMouseListenerToHeaderInTable

      public void addMouseListenerToHeaderInTable(JTable table)
      Installs a header listener that resorts the table when a column header is clicked.
      Parameters:
      table - the table whose header should trigger sorting