Module org.jpo
Package org.jpo.gui

Class TableSorter

All Implemented Interfaces:
Serializable, EventListener, TableModelListener, TableModel

public final 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.
See Also:
  • Constructor Details

    • TableSorter

      public TableSorter()
    • TableSorter

      public TableSorter(TableModel model)
  • Method Details

    • setModel

      public void setModel(TableModel model)
      Overrides:
      setModel in class TableMap
    • compareRowsByColumn

      public int compareRowsByColumn(int row1, int row2, int column)
    • compare

      public int compare(int row1, int row2)
    • reallocateIndexes

      public void reallocateIndexes()
    • tableChanged

      public void tableChanged(TableModelEvent e)
      Description copied from class: TableMap
      Implementation of the TableModelListener interface, By default forward all events to all the listeners.
      Specified by:
      tableChanged in interface TableModelListener
      Overrides:
      tableChanged in class TableMap
      Parameters:
      e - event
    • checkModel

      public void checkModel()
    • sort

      public void sort()
    • n2sort

      public void n2sort()
    • 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 assignments 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 - From
      to - To
      low - Low
      high - Hight
    • swap

      public void swap(int i, int j)
    • getValueAt

      public Object getValueAt(int aRow, int aColumn)
      The mapping only affects the contents of the data rows. Pass all requests to these rows through the mapping array: "indexes".
      Specified by:
      getValueAt in interface TableModel
      Overrides:
      getValueAt in class TableMap
      Parameters:
      aRow - a row
      aColumn - a column
      Returns:
      the value at the coordinates
    • setValueAt

      public void setValueAt(Object aValue, int aRow, int aColumn)
      Description copied from class: TableMap
      Set cell value
      Specified by:
      setValueAt in interface TableModel
      Overrides:
      setValueAt in class TableMap
      Parameters:
      aValue - a value
      aRow - a row
      aColumn - a column
    • sortByColumn

      public void sortByColumn(int column)
    • sortByColumn

      public void sortByColumn(int column, boolean ascending)
    • addMouseListenerToHeaderInTable

      public void addMouseListenerToHeaderInTable(JTable table)
      There is no-where else to put this. Add a mouse listener to the Table to trigger a table sort when a column heading is clicked in the JTable.
      Parameters:
      table - Table