com.smardec.asc.table
Class SortedTableModel
java.lang.Object
|
+--javax.swing.table.AbstractTableModel
|
+--com.smardec.asc.table.TableModelMapping
|
+--com.smardec.asc.table.SortedTableModel
- All Implemented Interfaces:
- java.util.EventListener, java.io.Serializable, SortOrderConstants, javax.swing.table.TableModel, javax.swing.event.TableModelListener
- public class SortedTableModel
- extends com.smardec.asc.table.TableModelMapping
- 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
Field Summary |
protected javax.swing.table.TableModel |
model
Original TableModel used as data source. |
Fields inherited from class javax.swing.table.AbstractTableModel |
listenerList |
Constructor Summary |
SortedTableModel(javax.swing.table.TableModel model)
Creates SortedTableModel object with specified TableModel
as the data source. |
Method Summary |
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 TableModelMapping.model by index in current model (view index). |
RowComparator |
getRowComparator()
Returns RowComparator used to sort table rows. |
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. |
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 |
sort()
Forces sorting of data contained in TableModelMapping.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. |
Methods inherited from class javax.swing.table.AbstractTableModel |
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getColumnName, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface javax.swing.event.TableModelListener |
tableChanged |
Methods inherited from interface javax.swing.table.TableModel |
getColumnCount, getRowCount, getValueAt |
model
protected javax.swing.table.TableModel model
- Original
TableModel used as data source.
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
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 TableModelMapping.model. The original TableModelMapping.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 TableModelMapping.model by index in current model (view index).
- Parameters:
- rowIndex - index in current model (view index)
- Returns:
- row index in original TableModelMapping.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.
getModel
public javax.swing.table.TableModel getModel()
- Returns original
TableModel used as data source.
- Returns:
- original
TableModel used as data source.
setModel
public void setModel(javax.swing.table.TableModel model)
- Sets original
TableModel used as data source.
- Parameters:
- model - original
TableModel used as data source
|