com.sciapp.table
Class SortTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.sciapp.table.ListTableMap
          extended by com.sciapp.table.SortTableModel
All Implemented Interfaces:
ReorderListener, ListTableModel, ReorderModel, Serializable, EventListener, TableModelListener, TableModel

public class SortTableModel
extends ListTableMap

SortTableModel offers sorting capabilities to a table. This class needs a ListTableModel that holds the data of the table and a JTableHeader, which it will use to catch mouse clicks and render appropriately.

The data of the table is sorted at runtime by left clicking on a column. By holding the Ctrl key on the keyboard, a sorting column is added, whereas by holding the Alt key, a column is removed. Additionally, SortTableModel includes methods for programmatically changing the sorting behaviour of the model. Rows are sorted using comparators found in the com.sciapp.comparators package, but you can also define your own and assign them with setComparator.


Field Summary
static int ADD_SORT
          Defines the mode for adding sorting columns after clearing existing ones.
protected  HashMap comparatorsByField
          A table of objects that compare two object with each other, indexed by field as declared in compareTo in the Comparable interface.
protected  Hashtable defaultComparatorsByColumnClass
          A table of objects that compare two object with each other, indexed by class as declared in compareTo in the Comparable interface.
static int INSERT_SORT
          Defines the mode for insering sorting columns.
static int MULTI_SORT
          Defines multiple sort mode.
static int REMOVE_SORT
          Defines the mode for removing sorting columns.
static int SINGLE_SORT
          Defines single sort mode.
 
Fields inherited from class com.sciapp.table.ListTableMap
tableModel
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
SortTableModel(ListTableModel tableModel)
          Constructs a SortTableModel with tableModel as the TableModel that holds the tabular data.
SortTableModel(ListTableModel tableModel, JTableHeader tableHeader)
          Constructs a SortTableModel with tableModel as the TableModel that holds the tabular data and tableHeader as the JTable's header.
 
Method Summary
protected  void createDefaultComparators()
          Creates default comparators for objects, strings and boolean values.
protected  SortTableRenderer createDefaultSortTableRenderer()
          Creates the default SortTableRenderer, which is a SortTableButtonRenderer.
 void fireTableChanged(TableModelEvent e)
          Forwards the given notification event to all TableModelListeners that registered themselves as listeners for this table model.
 Comparator getComparator(int column)
          Returns an appropriate comparator for the objects under the column defined by column.
 Comparator getDefaultComparator(Class columnClass)
          Returns the comparator to use for objects of the specified columnClass.
 int[] getNonSortableColumns()
          Returns an array of ints representing columns that are not sortable.
 int getSortMode()
          Returns the sorting mode used by the SortTableModel.
 SortState[] getSortStates()
          Returns the current sorting state of the model, as an array of SortStates.
 SortTableRenderer getSortTableRenderer()
          Returns the SortTableRenderer used to render the table header.
 JTableHeader getTableHeader()
          Returns the table header associated with this model.
 boolean isSortable(int column)
          Determines if column is sortable.
 void rowsReordered(ReorderEvent e)
          Does nothing.
 void setComparator(int column, Comparator comparator)
          Sets a comparator for the column defined by column.
 void setDefaultComparator(Class columnClass, Comparator comparator)
          Sets the default comparator for columnClass.
 void setHeader(JTableHeader newTableHeader)
          Associates newTableHeader with this model.
 void setSortableColumn(int column, boolean sortable)
          Sets the sortability of a column.
 void setSortMode(int mode)
          Sets the sorting mode.
 void setSortStates(SortState[] states)
          Sets the sorting behaviour of the model using an array of SortStates.
 void sort(int column, int mode)
          Tells the model to change sorting on a column or columns.
 
Methods inherited from class com.sciapp.table.ListTableMap
addReorderListener, addRow, addRows, clear, fireRowsReordered, getCellValue, getColumnClass, getColumnCount, getColumnName, getModel, getRowCount, getRows, getValueAt, isCellEditable, removeReorderListener, removeRow, removeRows, setModel, setValueAt, tableChanged
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, 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
 
Methods inherited from interface com.sciapp.table.ListTableModel
fireTableCellUpdated, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, removeTableModelListener
 

Field Detail

SINGLE_SORT

public static final int SINGLE_SORT
Defines single sort mode.

See Also:
Constant Field Values

MULTI_SORT

public static final int MULTI_SORT
Defines multiple sort mode.

See Also:
Constant Field Values

ADD_SORT

public static final int ADD_SORT
Defines the mode for adding sorting columns after clearing existing ones.

See Also:
Constant Field Values

REMOVE_SORT

public static final int REMOVE_SORT
Defines the mode for removing sorting columns.

See Also:
Constant Field Values

INSERT_SORT

public static final int INSERT_SORT
Defines the mode for insering sorting columns.

See Also:
Constant Field Values

defaultComparatorsByColumnClass

protected Hashtable defaultComparatorsByColumnClass
A table of objects that compare two object with each other, indexed by class as declared in compareTo in the Comparable interface.


comparatorsByField

protected HashMap comparatorsByField
A table of objects that compare two object with each other, indexed by field as declared in compareTo in the Comparable interface.

Constructor Detail

SortTableModel

public SortTableModel(ListTableModel tableModel)
Constructs a SortTableModel with tableModel as the TableModel that holds the tabular data. The model is initialized with a dummy JTableHeader. If you use this constructor, you need also to set the table header later on, using setHeader, otherwise mouse clicks from the header will not be intercepted.

Parameters:
tableModel - the underlying ListTableModel

SortTableModel

public SortTableModel(ListTableModel tableModel,
                      JTableHeader tableHeader)
Constructs a SortTableModel with tableModel as the TableModel that holds the tabular data and tableHeader as the JTable's header.

Parameters:
tableModel - the underlying ListTableModel
tableHeader - the table's header
Method Detail

createDefaultComparators

protected void createDefaultComparators()
Creates default comparators for objects, strings and boolean values.


createDefaultSortTableRenderer

protected SortTableRenderer createDefaultSortTableRenderer()
Creates the default SortTableRenderer, which is a SortTableButtonRenderer. This will be used to render the table header.


fireTableChanged

public void fireTableChanged(TableModelEvent e)
Forwards the given notification event to all TableModelListeners that registered themselves as listeners for this table model.

Specified by:
fireTableChanged in interface ListTableModel
Overrides:
fireTableChanged in class AbstractTableModel
Parameters:
e - the event to be forwarded
See Also:
AbstractTableModel.addTableModelListener(javax.swing.event.TableModelListener), TableModelEvent, EventListenerList

getComparator

public Comparator getComparator(int column)
Returns an appropriate comparator for the objects under the column defined by column. If a comparator is defined for the specified column, it returns that. Otherwise, it returns a comparator based on the column's class. If a comparator has not been assigned to the object's class, the default comparator is returned which is a GeneralComparator. GeneralComparator assumes that the objects compared implement the Comparable interface, hence a ClassCastException can be thrown in some cases.

Parameters:
column - the column of the data
Returns:
the assigned comparator

getDefaultComparator

public Comparator getDefaultComparator(Class columnClass)
Returns the comparator to use for objects of the specified columnClass. If there is no entry for this columnClass, this method returns the entry for the most specific superclass. SortTableModel installs entries for Object, Number, and Boolean, all of which can be modified or replaced.

Parameters:
columnClass - the class for which we want to find a comparator
Returns:
the comparator for this columnClass

getNonSortableColumns

public int[] getNonSortableColumns()
Returns an array of ints representing columns that are not sortable.

Returns:
an int array of non-sortable columns

getSortMode

public int getSortMode()
Returns the sorting mode used by the SortTableModel. SINGLE_SORT is used when only one column is allowed to be sorted and MULTI_SORT when mulitple columns are allowed to be selected for sorting.

Returns:
the sorting mode

getSortStates

public SortState[] getSortStates()
Returns the current sorting state of the model, as an array of SortStates.

Returns:
an array of SortState objects.

getSortTableRenderer

public SortTableRenderer getSortTableRenderer()
Returns the SortTableRenderer used to render the table header.

Returns:
this SortTableModel's SortTableRenderer

getTableHeader

public JTableHeader getTableHeader()
Returns the table header associated with this model.

Returns:
this SortTableModel's table header

isSortable

public boolean isSortable(int column)
Determines if column is sortable.

Parameters:
column - the column which sortability we want to determine.
Returns:
true if column is sortable, false otherwise.

rowsReordered

public void rowsReordered(ReorderEvent e)
Does nothing. Called whenever the rows of a JTable have been restructured.

Specified by:
rowsReordered in interface ReorderListener
Overrides:
rowsReordered in class ListTableMap
Parameters:
e - the ReorderEvent object

setComparator

public void setComparator(int column,
                          Comparator comparator)
Sets a comparator for the column defined by column.

Parameters:
column - the column for which a comparator we want to set
comparator - the comparator to set

setDefaultComparator

public void setDefaultComparator(Class columnClass,
                                 Comparator comparator)
Sets the default comparator for columnClass.

Parameters:
columnClass - the Class for which a comparator we want to set
comparator - the comparator to set

setHeader

public void setHeader(JTableHeader newTableHeader)
Associates newTableHeader with this model.

Parameters:
newTableHeader - the table header to set

setSortableColumn

public void setSortableColumn(int column,
                              boolean sortable)
Sets the sortability of a column.

Parameters:
column - the table column
sortable - true if sorting is desired for this column, false otherwise

setSortMode

public void setSortMode(int mode)
Sets the sorting mode. Valid modes are SINGLE_SORT and MULTI_SORT.

Parameters:
mode - the sorting mode to set

setSortStates

public void setSortStates(SortState[] states)
Sets the sorting behaviour of the model using an array of SortStates.

Parameters:
states - an array of SortState objects.

sort

public void sort(int column,
                 int mode)
Tells the model to change sorting on a column or columns. mode defines whether a column should be added, removed or inserted. ADD_SORT clears sorting columns before adding a column to sort, REMOVE_SORT removes a column from sorting and INSERT_SORT adds a sorting column.

Parameters:
column - the column to sort
mode - the sort mode