Copyright © 2005 Scientific Applications. All Rights Reserved.

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, SortListModel, Serializable, EventListener, TableModelListener, TableModel

public class SortTableModel
extends ListTableMap
implements SortListModel

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.

See Also:
Serialized Form

Nested Class Summary
protected  class SortTableModel.SorterMouseHandler
          The default mouse listener for processing mouse events on the header.
 class SortTableModel.SortTableButtonRendererFactory
          A cell renderer factory that creates SortTableButtonRenderer objects.
 
Field Summary
static int ADD_SORT
          Defines the mode for adding sorting columns after clearing existing ones.
protected  boolean allowSort
          a boolean flag that determines whether the model will perform the sorting if the underlying model changes (via updates, deletes or additions).
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.
protected  Cursor defaultHeaderCursor
          the header's default cursor
static int INSERT_SORT
          Defines the mode for inserting 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
 int compareRows(int row1, int row2)
          Compares two rows with each other and returns the result of the comparison.
 int compareRows(Object o1, Object o2)
          Compares two rows with each other and returns the result of the comparison.
 int convertRowIndexToModel(int viewRowIndex)
          Determines the index of the row in the underlying model in terms of the row index in this map.
 int convertRowIndexToView(int modelRowIndex)
          Determines the index of the row in this map in terms of the row index in the underlying model.
protected  void createDefaultComparators()
          Creates default comparators for objects, strings and boolean values.
protected  SortTableRenderer createDefaultSortTableRenderer()
          Creates the default SortTableRenderer, which is a SortTableButtonRenderer.
protected  MouseInputListener createMouseHandler()
          Creates and returns the mouse listener that will process mouse events received on the sortable header.
 boolean getAllowSort()
          Determines whether the model will perform the sorting if the underlying model changes (via updates, deletes or additions).
 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.
 Cursor getDefaultHeaderCursor()
          Retrieves the default header's cursor.
 int[] getNonSortableColumns()
          Returns an array of ints representing columns that are not sortable.
 List getRows()
          Returns tableModel.getRows
 int[] getSortedIndexes()
          Returns an int array showing the relation between the original and the sorted data.
 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.
 String getSortStatesAsString()
          Returns the current sorting state of the model, as a String.
 SortTableRenderer getSortTableRenderer()
          Returns the SortTableRenderer used to render the table header.
 JTableHeader getTableHeader()
          Returns the table header associated with this model.
 Object getValueAt(int row, int column)
          Returns tableModel.getValueAt
 boolean isCellEditable(int rowIndex, int columnIndex)
          Returns tableModel.isCellEditable
 boolean isSortable(int column)
          Determines if column is sortable.
 void reIndex()
          Reindexes the sorting indexes.
 void removeRow(int row)
          Removes a row from the data model
 void removeRows(int[] deletedRows)
          Removes a few rows from the data model
 void rowsReordered(ReorderEvent e)
          Called whenever the rows of a JTable have been restructured.
 void setAllowSort(boolean allowSort)
          Determines whether the model will perform the sorting if the underlying model changes (via updates, deletes or additions).
 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 setDefaultHeaderCursor(Cursor cursor)
          Assigns the default headers' cursor.
 void setHeader(JTableHeader newTableHeader)
          Associates newTableHeader with this model.
 void setModel(ListTableModel newModel)
          Sets the underlying ListTableModel to newModel.
 void setSortableColumn(int column, boolean sortable)
          Sets the sortability of a column.
 void setSortedIndexes(int[] indexes)
          Assigns the sorting indexes.
 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 setSortStatesAsString(String state)
          Sets the sorting behaviour of the model using a string as input.
 void setSortTableRenderer(SortTableRenderer renderer)
          Assigns the SortTableRenderer used to render the table header.
 void setValueAt(Object aValue, int rowIndex, int columnIndex)
          Returns tableModel.setValueAt
 void sort(int column, int mode)
          Tells the model to change sorting on a column or columns.
protected  int[] sortData()
          Sorts the data.
protected  void sorterMoved(MouseEvent e)
          This method is called when the mouse is being moved on the sortable header.
protected  void sorterPressed(MouseEvent e)
          This method is called when a mouse button is pressed on the sortable header.
protected  void sorterReleased(MouseEvent e)
          This method is called when a mouse button is released on the sortable header.
 void tableChanged(TableModelEvent e)
          This fine grain notification tells listeners the exact range of cells, rows, or columns that changed.
 
Methods inherited from class com.sciapp.table.ListTableMap
addReorderListener, addRow, addRows, clear, fireRowsMapped, fireRowsReordered, getCellValue, getColumnClass, getColumnCount, getColumnName, getInnerModel, getModel, getRowCount, removeReorderListener
 
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
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, getColumnClass, getColumnCount, getColumnName, getRowCount, removeTableModelListener
 
Methods inherited from interface com.sciapp.table.ListTableModel
fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged
 

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 inserting sorting columns.

See Also:
Constant Field Values

allowSort

protected boolean allowSort
a boolean flag that determines whether the model will perform the sorting if the underlying model changes (via updates, deletes or additions).


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.


defaultHeaderCursor

protected Cursor defaultHeaderCursor
the header's default cursor

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

compareRows

public int compareRows(int row1,
                       int row2)
Compares two rows with each other and returns the result of the comparison.

Specified by:
compareRows in interface SortListModel
Parameters:
row1 - the first row
row2 - the second row
Returns:
-1 if row1 evaluates less than row2, 0 if equal or 1 if row1 evaluates more than row2

compareRows

public int compareRows(Object o1,
                       Object o2)
Compares two rows with each other and returns the result of the comparison. This method is preferred if the row data are contained in a list.

Specified by:
compareRows in interface SortListModel
Parameters:
o1 - the first row object
o2 - the second row object
Returns:
-1 if o1 evaluates less than o2, 0 if equal or 1 if o1 evaluates more than o2

convertRowIndexToModel

public int convertRowIndexToModel(int viewRowIndex)
Determines the index of the row in the underlying model in terms of the row index in this map.

Overrides:
convertRowIndexToModel in class ListTableMap
Parameters:
viewRowIndex - the index of the row in this map
Returns:
the index of the corresponding row in the underlying model

convertRowIndexToView

public int convertRowIndexToView(int modelRowIndex)
Determines the index of the row in this map in terms of the row index in the underlying model. If the row is not viewable in the map (such as filtered out), -1 is returned.

Overrides:
convertRowIndexToView in class ListTableMap
Parameters:
modelRowIndex - the index of the row in the underlying model
Returns:
the index of the corresponding row in this map

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.

Returns:
the renderer to use when drawing the sortable header cells

createMouseHandler

protected MouseInputListener createMouseHandler()
Creates and returns the mouse listener that will process mouse events received on the sortable header. By default, this is a SorterMouseHandler.

Returns:
the mouse listener

getAllowSort

public boolean getAllowSort()
Determines whether the model will perform the sorting if the underlying model changes (via updates, deletes or additions). The default is true.

Note that this setting has no effect when the sorting parameters change in some way (via clicks on the header or programatically). The model will always perform sorting in this case.

Returns:
true if the model will perform the sorting when the underlying data changes, false otherwise.

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

getDefaultHeaderCursor

public Cursor getDefaultHeaderCursor()
Retrieves the default header's cursor.

Returns:
the default header's cursor

getNonSortableColumns

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

Returns:
an int array of non-sortable columns

getRows

public List getRows()
Returns tableModel.getRows

Specified by:
getRows in interface ListTableModel
Overrides:
getRows in class ListTableMap
Returns:
the rows of the datamodel as a java.util.List

getSortedIndexes

public int[] getSortedIndexes()
Returns an int array showing the relation between the original and the sorted data. More specifically, the integers in the array correspond to the position of objects in the original data. e.g. if this method returns {1, 0, 3, 2}:

row (0) -> row (1)
row (1) -> row (0)
row (2) -> row (3)
row (3) -> row (2)

Returns:
an int array showing the relation between the original and the sorted data.

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.

getSortStatesAsString

public String getSortStatesAsString()
Returns the current sorting state of the model, as a String. The string is formatted as follows: i1:asc1,i2:asc2,... where i is the column's model index and asc a number which is 1 for ascending order and 0 for descending.

Returns:
a string representation of the model's sorting state.

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

getValueAt

public Object getValueAt(int row,
                         int column)
Returns tableModel.getValueAt

Specified by:
getValueAt in interface TableModel
Overrides:
getValueAt in class ListTableMap
Parameters:
row - the row whose value is to be queried
column - the column whose value is to be queried
Returns:
the value Object at the specified cell

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
Returns tableModel.isCellEditable

Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in class ListTableMap
Parameters:
rowIndex - the row being queried
columnIndex - the column being queried
Returns:
false
See Also:
TableModel.setValueAt(java.lang.Object, int, int)

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.

reIndex

public void reIndex()
Reindexes the sorting indexes.


removeRow

public void removeRow(int row)
Removes a row from the data model

Specified by:
removeRow in interface ListTableModel
Overrides:
removeRow in class ListTableMap
Parameters:
row - the index of the row being removed

removeRows

public void removeRows(int[] deletedRows)
Removes a few rows from the data model

Specified by:
removeRows in interface ListTableModel
Overrides:
removeRows in class ListTableMap
Parameters:
deletedRows - an integer array that contains indexes of the rows being deleted

rowsReordered

public void rowsReordered(ReorderEvent e)
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

setAllowSort

public void setAllowSort(boolean allowSort)
Determines whether the model will perform the sorting if the underlying model changes (via updates, deletes or additions). The default is true.

Note that this setting has no effect when the sorting parameters change in some way (via clicks on the header or programatically). The model will always perform sorting in this case.

Parameters:
allowSort - true if the model will perform the sorting when the underlying data changes, false otherwise.

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

setDefaultHeaderCursor

public void setDefaultHeaderCursor(Cursor cursor)
Assigns the default headers' cursor.

Parameters:
cursor - the default header cursor to assign

setHeader

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

Parameters:
newTableHeader - the table header to set

setModel

public void setModel(ListTableModel newModel)
Sets the underlying ListTableModel to newModel.

Overrides:
setModel in class ListTableMap
Parameters:
newModel - the new ListTableModel 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

setSortedIndexes

public void setSortedIndexes(int[] indexes)
Assigns the sorting indexes.

Parameters:
indexes - the indexes to assign.

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.

setSortStatesAsString

public void setSortStatesAsString(String state)
Sets the sorting behaviour of the model using a string as input. The string is formatted as follows: i1:asc1,i2:asc2,... where i is the column's model index and asc a number which is 1 for ascending order and 0 for descending.

Parameters:
state - the string representation of the sorting state.

setSortTableRenderer

public void setSortTableRenderer(SortTableRenderer renderer)
Assigns the SortTableRenderer used to render the table header.

Parameters:
renderer - the SortTableModel's SortTableRenderer to assign

setValueAt

public void setValueAt(Object aValue,
                       int rowIndex,
                       int columnIndex)
Returns tableModel.setValueAt

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class ListTableMap
Parameters:
aValue - value to assign to cell
rowIndex - row of cell
columnIndex - column of cell
See Also:
TableModel.getValueAt(int, int), TableModel.isCellEditable(int, int)

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

sortData

protected int[] sortData()
Sorts the data. Returns an int array that shows how the row indexes have changed. See ReorderEvent.

Returns:
a representation of how the row indexes of the table have changed.

sorterMoved

protected void sorterMoved(MouseEvent e)
This method is called when the mouse is being moved on the sortable header.

Parameters:
e - the corresponding mouse event

sorterPressed

protected void sorterPressed(MouseEvent e)
This method is called when a mouse button is pressed on the sortable header.

Parameters:
e - the corresponding mouse event

sorterReleased

protected void sorterReleased(MouseEvent e)
This method is called when a mouse button is released on the sortable header.

Parameters:
e - the corresponding mouse event

tableChanged

public void tableChanged(TableModelEvent e)
This fine grain notification tells listeners the exact range of cells, rows, or columns that changed.

Specified by:
tableChanged in interface TableModelListener
Overrides:
tableChanged in class ListTableMap

Copyright © 2005 Scientific Applications. All Rights Reserved.