edu.ucla.stat.SOCR.util.tablemodels
Class SortedTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by edu.ucla.stat.SOCR.util.tablemodels.SortedTableModel
All Implemented Interfaces:
SortOrderConstants, java.io.Serializable, java.util.EventListener, javax.swing.event.TableModelListener, javax.swing.table.TableModel
Direct Known Subclasses:
MotionTableModel

public class SortedTableModel
extends javax.swing.table.AbstractTableModel
implements SortOrderConstants

An AbstractTableModel implementation with sorting and multisorting support. It uses supplied RowComparator to sort table rows. The original TableModel is used only as the data source and is not modified.

Sample usage:

    // Old code:
    //    JTable jTable = new JTable(tableModel);
    // New code:
    SortedTableModel sortedTableModel = new SortedTableModel(tableModel);
    JTable jTable = new JTable(sortedTableModel);
    // Adding mouse listener that invokes sorting when table column's header is clicked
    jTable.getTableHeader().addMouseListener(new SortedTableHeaderMouseListener(jTable, sortedTableModel));
    // Adding renderer to display sorting icon
    jTable.getTableHeader().setDefaultRenderer(new SortedTableHeaderRenderer(jTable, sortedTableModel));
After that your table has ability to sort its columns. If the user clicks on column's header it will be sorted. The first left click will set ascending sort order, the second descending, and the third reset sort order back to unsorted. The right click will show popup menu that allows set sort order exactly you want. To enable multisorting support just hold CTRL key and click on several table headers. The column header will show an icon than represents column sort order (ascending/descending) and column sort rank.

See Also:
RowComparator, Serialized Form

Field Summary
protected  javax.swing.table.TableModel model
          Original TableModel used as data source.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Fields inherited from interface edu.ucla.stat.SOCR.util.tablemodels.SortOrderConstants
ASCENDING, DESCENDING, NOT_SORTED
 
Constructor Summary
SortedTableModel(javax.swing.table.TableModel model)
          Creates SortedTableModel object with specified TableModel as the data source.
 
Method Summary
protected  boolean checkModel()
           
 java.lang.Class getColumnClass(int columnIndex)
           
 int getColumnCount()
           
 java.lang.String getColumnName(int columnIndex)
           
 int getColumnSortOrder(int columnIndex)
          Returns column's sort order.
 int getColumnSortRank(int columnIndex)
          Returns column's sort rank within all sorted columns.
 javax.swing.table.TableModel getModel()
          Returns original TableModel used as data source.
 int getRealRowIndex(int rowIndex)
          Returns row index in original model by index in current model (view index).
 RowComparator getRowComparator()
          Returns RowComparator used to sort table rows.
 int getRowCount()
           
 int[] getSortingColumnIndexes()
          Returns array that contains sorted columns indexes.
 java.util.List getSortingColumns()
          Returns list with sorted columns info.
 int getSortingColumnsCount()
          Returns the number of sorting columns.
 java.lang.Object getValueAt(int rowIndex, int columnIndex)
           
 boolean isCellEditable(int rowIndex, int columnIndex)
           
 boolean isColumnAscending(int columnIndex)
          Returns true if columnIndex is sorted and sort mode is SortOrderConstants.ASCENDING, false otherwise.
 boolean isColumnDescending(int columnIndex)
          Returns true if columnIndex is sorted and sort mode is SortOrderConstants.DESCENDING, false otherwise.
 boolean isColumnSorted(int columnIndex)
          Returns true if column is sorted, false otherwise.
 void setModel(javax.swing.table.TableModel model)
          Sets original TableModel used as data source.
 void setRowComparator(RowComparator rowComparator)
          Sets RowComparator used to sort table rows.
 void setSortingColumns(java.util.List sortingColumns)
          Specifies sorting columns infos and calls sort() method.
 void setValueAt(java.lang.Object aValue, int rowIndex, int columnIndex)
           
 void sort()
          Forces sorting of data contained in model.
 void sortColumn(int columnIndex, boolean resetSort)
          Sorts specified column ascending allowing to reset or keep all others columns' sort order.
 void sortColumn(int columnIndex, int sortOrder, boolean resetSort)
          Sorts specified column using sortOrder and allowing to reset or keep all others columns' sort order.
 void tableChanged(javax.swing.event.TableModelEvent e)
           
 
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

model

protected javax.swing.table.TableModel model
Original TableModel used as data source.

Constructor Detail

SortedTableModel

public SortedTableModel(javax.swing.table.TableModel model)
Creates SortedTableModel object with specified TableModel as the data source.

Parameters:
model - TableModel used as the data source
Method Detail

getRowComparator

public RowComparator getRowComparator()
Returns RowComparator used to sort table rows. The default is DefaultRowComparator instance.

Returns:
RowComparator used to sort table rows.

setRowComparator

public void setRowComparator(RowComparator rowComparator)
Sets RowComparator used to sort table rows. Invokes sort() before return. The default is DefaultRowComparator instance.

Parameters:
rowComparator - RowComparator used to sort table rows

sort

public void sort()
Forces sorting of data contained in model. The original model is not modified.


sortColumn

public void sortColumn(int columnIndex,
                       boolean resetSort)
Sorts specified column ascending allowing to reset or keep all others columns' sort order.
Note: to specify several sorting columns at one time (multisort support) use setSortingColumns(java.util.List)

Parameters:
columnIndex - model column index
resetSort - if true reset all previously sorted columns, if false specified column is added to the sorted column set with incremental sort rank
See Also:
sortColumn(int, int, boolean)

sortColumn

public void sortColumn(int columnIndex,
                       int sortOrder,
                       boolean resetSort)
Sorts specified column using sortOrder and allowing to reset or keep all others columns' sort order.
Note: to specify several sorting columns at one time (multisort support) use setSortingColumns(java.util.List)

Parameters:
columnIndex - model column index
sortOrder - column sort order, must be one of the following values: SortOrderConstants.ASCENDING, SortOrderConstants.DESCENDING or SortOrderConstants.NOT_SORTED
resetSort - if true reset all previously sorted columns, if false specified column is added to the sorted column set with incremental sort rank
See Also:
sortColumn(int, boolean)

getRealRowIndex

public int getRealRowIndex(int rowIndex)
Returns row index in original model by index in current model (view index).

Parameters:
rowIndex - index in current model (view index)
Returns:
row index in original model.

isColumnSorted

public boolean isColumnSorted(int columnIndex)
Returns true if column is sorted, false otherwise.

Parameters:
columnIndex - model column index
Returns:
true if column is sorted, false otherwise.

isColumnAscending

public boolean isColumnAscending(int columnIndex)
Returns true if columnIndex is sorted and sort mode is SortOrderConstants.ASCENDING, false otherwise.

Parameters:
columnIndex - model column index
Returns:
true if columnIndex is sorted ascending.
See Also:
isColumnDescending(int), isColumnSorted(int)

isColumnDescending

public boolean isColumnDescending(int columnIndex)
Returns true if columnIndex is sorted and sort mode is SortOrderConstants.DESCENDING, false otherwise.
Note: to check whether column's sort order is ascending use {}

Parameters:
columnIndex - model column index
Returns:
true if columnIndex is sorted desending.
See Also:
isColumnAscending(int), isColumnSorted(int)

getColumnSortOrder

public int getColumnSortOrder(int columnIndex)
Returns column's sort order. The return values are: SortOrderConstants.ASCENDING, SortOrderConstants.DESCENDING or SortOrderConstants.NOT_SORTED

Parameters:
columnIndex - model column index
Returns:
column's sort order
See Also:
SortOrderConstants

getColumnSortRank

public int getColumnSortRank(int columnIndex)
Returns column's sort rank within all sorted columns.

Parameters:
columnIndex - model column index
Returns:
column's sort rank.

getSortingColumnIndexes

public int[] getSortingColumnIndexes()
Returns array that contains sorted columns indexes.

Returns:
array that contains sorted columns indexes.
See Also:
getSortingColumns()

getSortingColumns

public java.util.List getSortingColumns()
Returns list with sorted columns info.

Returns:
list with sorted columns info.
See Also:
getSortingColumnIndexes(), SortedColumnInfo

setSortingColumns

public void setSortingColumns(java.util.List sortingColumns)
Specifies sorting columns infos and calls sort() method.

Parameters:
sortingColumns - list of SortedColumnInfo

getSortingColumnsCount

public int getSortingColumnsCount()
Returns the number of sorting columns.

Returns:
the number of sorting columns.

setModel

public void setModel(javax.swing.table.TableModel model)
Sets original TableModel used as data source.

Parameters:
model - original TableModel used as data source

getValueAt

public java.lang.Object getValueAt(int rowIndex,
                                   int columnIndex)
Specified by:
getValueAt in interface javax.swing.table.TableModel

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int rowIndex,
                       int columnIndex)
Specified by:
setValueAt in interface javax.swing.table.TableModel

tableChanged

public void tableChanged(javax.swing.event.TableModelEvent e)
Specified by:
tableChanged in interface javax.swing.event.TableModelListener

checkModel

protected boolean checkModel()

getModel

public javax.swing.table.TableModel getModel()
Returns original TableModel used as data source.

Returns:
original TableModel used as data source.

getRowCount

public int getRowCount()
Specified by:
getRowCount in interface javax.swing.table.TableModel

getColumnCount

public int getColumnCount()
Specified by:
getColumnCount in interface javax.swing.table.TableModel

getColumnName

public java.lang.String getColumnName(int columnIndex)
Specified by:
getColumnName in interface javax.swing.table.TableModel
Overrides:
getColumnName in class javax.swing.table.AbstractTableModel

getColumnClass

public java.lang.Class getColumnClass(int columnIndex)
Specified by:
getColumnClass in interface javax.swing.table.TableModel
Overrides:
getColumnClass in class javax.swing.table.AbstractTableModel

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
Specified by:
isCellEditable in interface javax.swing.table.TableModel
Overrides:
isCellEditable in class javax.swing.table.AbstractTableModel