javatools.swing.table
Class IndexedTableSorter

java.lang.Object
  |
  +--javax.swing.table.AbstractTableModel
        |
        +--javatools.swing.table.IndexedTableMap
              |
              +--javatools.swing.table.IndexedTableSorter
All Implemented Interfaces:
java.util.EventListener, java.io.Serializable, javax.swing.table.TableModel, javax.swing.event.TableModelListener

public class IndexedTableSorter
extends IndexedTableMap

It seems a table, it is a table sorter for indexed tables. Based upon the sorter in the Java tutorial.

See Also:
Serialized Form

Field Summary
protected  boolean ascending
          true: ascending order; false: descending order.
protected  int compares
          Contains the number of made comparisons.
protected  int[] indexes
          The indexes to use for correct order.
protected  int lastSortedColumn
          Contains the last sorted column to alternate ascending and descending order.
protected  boolean lastSortingDirection
          Contains the last sorting direction, to alternate ascending and descending order.
protected  java.util.Vector sortingColumns
          Contains the columns to use to sort.
 
Fields inherited from class javatools.swing.table.IndexedTableMap
model
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
IndexedTableSorter()
          Creates new IndexedTableSorter
IndexedTableSorter(IndexedTableModel model)
          Creates a new IndexedTableSorter.
 
Method Summary
 void addMouseListenerToHeaderInTable(javax.swing.JTable table)
          Method used to add a mouse listener to catch click on table headers.
 void checkModel()
          Checks if the model is OK.
 int compare(int row1, int row2)
          Compares two columns.
 int compareRowsByColumn(int row1, int row2, int column)
          Compares two rows by a column.
 java.lang.Object getIndex(int row)
          Returns the index.
 java.lang.Object getValueAt(int aRow, int aColumn)
          Returns the value in a certain position.
 void n2sort()
          Sorts 2 items.
 void reallocateIndexes()
          Initializes indexes.
 void removeRow(int row)
          Removes a row.
 void setIndex(int row, java.lang.Object index)
          Sets an index.
 void setModel(IndexedTableModel model)
          Sets the table model to sort.
 void setValueAt(java.lang.Object aValue, int aRow, int aColumn)
          Sets a value in the table.
 void shuttlesort(int[] from, int[] to, int low, int high)
          Sorts using shuttlesort.
 void sort(java.lang.Object sender)
          Sorts the table.
 void sortByColumn(int column)
          Sorts the table by a column.
 void sortByColumn(int column, boolean ascending)
          Sorts the table by column.
 void swap(int i, int j)
          Swaps (logically) two rows.
 void tableChanged(javax.swing.event.TableModelEvent e)
          Method that is called whenever the table changes.
 
Methods inherited from class javatools.swing.table.IndexedTableMap
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
 

Field Detail

indexes

protected int[] indexes
The indexes to use for correct order.


sortingColumns

protected java.util.Vector sortingColumns
Contains the columns to use to sort.


ascending

protected boolean ascending
true: ascending order; false: descending order.


compares

protected int compares
Contains the number of made comparisons.


lastSortedColumn

protected int lastSortedColumn
Contains the last sorted column to alternate ascending and descending order.


lastSortingDirection

protected boolean lastSortingDirection
Contains the last sorting direction, to alternate ascending and descending order.

Constructor Detail

IndexedTableSorter

public IndexedTableSorter()
Creates new IndexedTableSorter


IndexedTableSorter

public IndexedTableSorter(IndexedTableModel model)
Creates a new IndexedTableSorter.

Parameters:
model - The base table model to use.
Method Detail

setModel

public void setModel(IndexedTableModel model)
Sets the table model to sort.

Overrides:
setModel in class IndexedTableMap
Parameters:
model - The model.

compareRowsByColumn

public int compareRowsByColumn(int row1,
                               int row2,
                               int column)
Compares two rows by a column.

Parameters:
row1 - The first row.
row2 - The second row.
column - The column to use as a criterion.
Returns:
The compare result.

compare

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

Parameters:
row1 - The first row.
row2 - The second row.
Returns:
The compare result.

reallocateIndexes

public void reallocateIndexes()
Initializes indexes.


tableChanged

public void tableChanged(javax.swing.event.TableModelEvent e)
Method that is called whenever the table changes.

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

checkModel

public void checkModel()
Checks if the model is OK.


sort

public void sort(java.lang.Object sender)
Sorts the table.

Parameters:
sender - Ignored (what????).

n2sort

public void n2sort()
Sorts 2 items.


shuttlesort

public void shuttlesort(int[] from,
                        int[] to,
                        int low,
                        int high)
Sorts using shuttlesort.

Parameters:
from - The origin indexes.
to - The destination indexes.
low - The lower limit.
high - The higher limit.

swap

public void swap(int i,
                 int j)
Swaps (logically) two rows.

Parameters:
i - The first index.
j - The second index.

getValueAt

public java.lang.Object getValueAt(int aRow,
                                   int aColumn)
Returns the value in a certain position.

Specified by:
getValueAt in interface javax.swing.table.TableModel
Overrides:
getValueAt in class IndexedTableMap
Parameters:
aRow - The row index.
aColumn - The column index.
Returns:
The value.

getIndex

public java.lang.Object getIndex(int row)
                          throws java.lang.ArrayIndexOutOfBoundsException
Returns the index.

Overrides:
getIndex in class IndexedTableMap
Parameters:
row - The row index.
Returns:
The index.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If row is not correct.

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int aRow,
                       int aColumn)
Sets a value in the table.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class IndexedTableMap
Parameters:
aValue - The value.
aRow - The row index.
aColumn - The column index.

setIndex

public void setIndex(int row,
                     java.lang.Object index)
              throws java.lang.ArrayIndexOutOfBoundsException
Sets an index.

Overrides:
setIndex in class IndexedTableMap
Parameters:
row - The row index.
index - The index.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If row is not correct.

removeRow

public void removeRow(int row)
Removes a row.

Parameters:
row - The row index.

sortByColumn

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

Parameters:
column - The column to sort by.

sortByColumn

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

Parameters:
column - The column to use.
ascending - true: ascending order; false: descending order.

addMouseListenerToHeaderInTable

public void addMouseListenerToHeaderInTable(javax.swing.JTable table)
Method used to add a mouse listener to catch click on table headers.

Parameters:
table - The table.