biggests.tables
Class TableSorter

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by biggests.tables.TableSorter
All Implemented Interfaces:
java.io.Serializable, javax.swing.table.TableModel
Direct Known Subclasses:
TableSorterFunctionGroup, TableSorterFunctionIndividual, TableSorterValues

public class TableSorter
extends javax.swing.table.AbstractTableModel

Title: Table Sorter - a model for sorting tables

Description: TableSorter is a decorator for TableModels adding sorting functionality to a supplied TableModel. TableSorter does not store or copy the data in its TableModel; instead it maintains a map from the row indexes of the view to the row indexes of the model. As requests are made of the sorter (like getValueAt(row, col)) they are passed to the underlying model after the row numbers have been translated via the internal mapping array. This way, the TableSorter appears to hold another copy of the table with the rows in a different order.
TableSorter registers itself as a listener to the underlying model, just as the JTable itself would. Events received from the model are examined, sometimes manipulated (typically widened), and then passed on to the TableSorter's listeners (typically the JTable). If a change to the model has invalidated the order of TableSorter's rows, a note of this is made and the sorter will resort the rows the next time a value is requested.
When the tableHeader property is set, either by using the setTableHeader() method or the two argument constructor, the table header may be used as a complete UI for TableSorter. The default renderer of the tableHeader is decorated with a renderer that indicates the sorting status of each column. In addition, a mouse listener is installed with the following behavior:

This is a long overdue rewrite of a class of the same name that first appeared in the swing table demos in 1997.

Copyright: Copyright (C) 2007 Joana Gonçalves This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Version of 02/27/04

See Also:
Serialized Form

Field Summary
static int ASCENDING
          The status that indicates that a specific column is sorted in ascending order.
protected  java.util.HashMap<java.lang.Class,java.util.Comparator> columnComparators
          Hash map for the columns' comparators.
static java.util.Comparator COMPARABLE_COMPARATOR
          The comparator for specific comparable objects.
static int DESCENDING
          The status that indicates that a specific column is sorted in descending order.
static java.util.Comparator LEXICAL_COMPARATOR
          The lexical comparator.
protected  java.awt.event.MouseListener mouseListener
          The mouse listener of the table.
static int NOT_SORTED
          The status that indicates that a specific column is not sorted.
protected  javax.swing.table.JTableHeader tableHeader
          The header of the table.
protected  javax.swing.table.TableModel tableModel
          The table model.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TableSorter()
          Creates a new TableSorter with default mouse and table model listeners.
TableSorter(javax.swing.table.TableModel tableModel)
          Creates a new TableSorter with the given table model.
TableSorter(javax.swing.table.TableModel tableModel, javax.swing.table.JTableHeader tableHeader)
          Creates a new TableSorter with the given table model and header.
 
Method Summary
 java.lang.Class<?> getColumnClass(int column)
          Returns the class of the column with index column.
 int getColumnCount()
          Returns the number of columns in the table model.
 java.lang.String getColumnName(int column)
          Returns the name of the given column.
protected  java.util.Comparator getComparator(int column)
          Returns the appropriate comparator for the objects in the given column.
protected  javax.swing.Icon getHeaderRendererIcon(int column, int size)
          Returns the Icon corresponding to the sorting status of the given column.
 int getRowCount()
          Returns the number of rows in the table model.
 int getSortingStatus(int column)
          Returns the sorting status of the given column:
DESCENDING NOT_SORTED ASCENDING
 javax.swing.table.JTableHeader getTableHeader()
          Returns the table header.
 javax.swing.table.TableModel getTableModel()
          Returns the table model.
 java.lang.Object getValueAt(int row, int column)
          Returns the element in the cell at row and column indexes.
 boolean isCellEditable(int row, int column)
          Checks if the cell with the given row and column indexes is editable.
 boolean isSorting()
          Checks if the table is sorting.
 int modelIndex(int viewIndex)
          Returns the model index for a given viewIndex.
 void setColumnComparator(java.lang.Class type, java.util.Comparator comparator)
          Sets the given comparator for the given objects' type.
 void setSortingStatus(int column, int status)
          Sets the sorting status for a given column.
 void setTableHeader(javax.swing.table.JTableHeader tableHeader)
          Sets the table header.
 void setTableModel(javax.swing.table.TableModel tableModel)
          Sets the table model.
 void setValueAt(java.lang.Object aValue, int row, int column)
          Sets the element of the cell with row and column) indexes.
 
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

tableModel

protected javax.swing.table.TableModel tableModel
The table model.


DESCENDING

public static final int DESCENDING
The status that indicates that a specific column is sorted in descending order.

See Also:
Constant Field Values

NOT_SORTED

public static final int NOT_SORTED
The status that indicates that a specific column is not sorted.

See Also:
Constant Field Values

ASCENDING

public static final int ASCENDING
The status that indicates that a specific column is sorted in ascending order.

See Also:
Constant Field Values

COMPARABLE_COMPARATOR

public static final java.util.Comparator COMPARABLE_COMPARATOR
The comparator for specific comparable objects.


LEXICAL_COMPARATOR

public static final java.util.Comparator LEXICAL_COMPARATOR
The lexical comparator.


tableHeader

protected javax.swing.table.JTableHeader tableHeader
The header of the table. Set to protected access to be used by subclasses (by Joana Gon�alves on 24-11-2006)


mouseListener

protected java.awt.event.MouseListener mouseListener
The mouse listener of the table. Set to protected access to be used by subclasses (by Joana Gon�alves on 24-11-2006).


columnComparators

protected java.util.HashMap<java.lang.Class,java.util.Comparator> columnComparators
Hash map for the columns' comparators. Set to protected access to be used by subclasses (by Joana Gon�alves on 24-11-2006).

Constructor Detail

TableSorter

public TableSorter()
Creates a new TableSorter with default mouse and table model listeners.


TableSorter

public TableSorter(javax.swing.table.TableModel tableModel)
Creates a new TableSorter with the given table model.

Parameters:
tableModel - TableModel the model for this table

TableSorter

public TableSorter(javax.swing.table.TableModel tableModel,
                   javax.swing.table.JTableHeader tableHeader)
Creates a new TableSorter with the given table model and header.

Parameters:
tableModel - the TableModel
tableHeader - the JTableHeader
Method Detail

getTableModel

public javax.swing.table.TableModel getTableModel()
Returns the table model.

Returns:
TableModel

setTableModel

public void setTableModel(javax.swing.table.TableModel tableModel)
Sets the table model.

Parameters:
tableModel - the TableModel

getTableHeader

public javax.swing.table.JTableHeader getTableHeader()
Returns the table header.

Returns:
JTableHeader the header of the table

setTableHeader

public void setTableHeader(javax.swing.table.JTableHeader tableHeader)
Sets the table header.

Parameters:
tableHeader - the new JTableHeader

isSorting

public boolean isSorting()
Checks if the table is sorting. Returns true if table is sorting, false otherwise.

Returns:
boolean true if table is sorting, false otherwise

getSortingStatus

public int getSortingStatus(int column)
Returns the sorting status of the given column:

Parameters:
column - int the index of the column
Returns:
int the sorting status of the given column

setSortingStatus

public void setSortingStatus(int column,
                             int status)
Sets the sorting status for a given column.

Parameters:
column - int the column to set sorting status
status - int the new sorting status for the given column

getHeaderRendererIcon

protected javax.swing.Icon getHeaderRendererIcon(int column,
                                                 int size)
Returns the Icon corresponding to the sorting status of the given column. The icon will have the specified size.

Parameters:
column - int the column to return the sorting status icon for
size - int the size of the sorting status icon
Returns:
Icon

setColumnComparator

public void setColumnComparator(java.lang.Class type,
                                java.util.Comparator comparator)
Sets the given comparator for the given objects' type.

Parameters:
type - the Class of objects to use the given comparator with
comparator - the appropriate Comparator for the objects of the specified type

getComparator

protected java.util.Comparator getComparator(int column)
Returns the appropriate comparator for the objects in the given column.

Parameters:
column - int the column for which the appropriate comparator should be returned
Returns:
Comparator the appropriate comparator for the given column

modelIndex

public int modelIndex(int viewIndex)
Returns the model index for a given viewIndex.

Parameters:
viewIndex - int the index that is shown in the table
Returns:
int the model index: the index of the row in the table model, which may be different from the view index, if some column is being sorted

getRowCount

public int getRowCount()
Returns the number of rows in the table model.

Returns:
int the number of rows in the table model

getColumnCount

public int getColumnCount()
Returns the number of columns in the table model.

Returns:
int the number of columns in the table model

getColumnName

public java.lang.String getColumnName(int column)
Returns the name of the given column.

Specified by:
getColumnName in interface javax.swing.table.TableModel
Overrides:
getColumnName in class javax.swing.table.AbstractTableModel
Parameters:
column - int the index of the intended column
Returns:
String the name of the given column

getColumnClass

public java.lang.Class<?> getColumnClass(int column)
Returns the class of the column with index column.
Changed return class from Class to Class so this method overrides AbstractTableModel's getColumnClass method (by Joana Gon�alves).

Specified by:
getColumnClass in interface javax.swing.table.TableModel
Overrides:
getColumnClass in class javax.swing.table.AbstractTableModel
Parameters:
column - int value: the column being queried
Returns:
the Class of the column at index column

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Checks if the cell with the given row and column indexes is editable.

Specified by:
isCellEditable in interface javax.swing.table.TableModel
Overrides:
isCellEditable in class javax.swing.table.AbstractTableModel
Parameters:
row - int the row index of the cell
column - int the column index of the cell
Returns:
boolean true if the cell at (row, column) is editable, false otherwise

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Returns the element in the cell at row and column indexes.

Parameters:
row - int the row index of the cell
column - int the column index of the cell
Returns:
Object the element in the cell at (row, column)

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int row,
                       int column)
Sets the element of the cell with row and column) indexes.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class javax.swing.table.AbstractTableModel
Parameters:
aValue - Object the new element for the cell
row - int the row index of the cell
column - int the column index of the cell