|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.swing.table.AbstractTableModel
com.sciapp.table.ListTableMap
com.sciapp.table.SortTableModel
public class SortTableModel
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 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 | |
---|---|
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)
Called whenever the rows of a JTable have been restructured. |
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. |
protected int[] |
sortData()
Sorts the data. |
Methods inherited from class com.sciapp.table.ListTableMap |
---|
addReorderListener, addRow, addRows, clear, fireRowsReordered, getCellValue, getColumnClass, getColumnCount, getColumnName, getInnerModel, 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 |
---|
public static final int SINGLE_SORT
public static final int MULTI_SORT
public static final int ADD_SORT
public static final int REMOVE_SORT
public static final int INSERT_SORT
protected transient Hashtable defaultComparatorsByColumnClass
protected transient HashMap comparatorsByField
Constructor Detail |
---|
public SortTableModel(ListTableModel tableModel)
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.
tableModel
- the underlying ListTableModelpublic SortTableModel(ListTableModel tableModel, JTableHeader tableHeader)
tableModel
as the TableModel
that holds the tabular data and tableHeader
as the JTable's header.
tableModel
- the underlying ListTableModeltableHeader
- the table's headerMethod Detail |
---|
protected void createDefaultComparators()
protected SortTableRenderer createDefaultSortTableRenderer()
public void fireTableChanged(TableModelEvent e)
TableModelListeners
that registered
themselves as listeners for this table model.
fireTableChanged
in interface ListTableModel
fireTableChanged
in class AbstractTableModel
e
- the event to be forwardedAbstractTableModel.addTableModelListener(javax.swing.event.TableModelListener)
,
TableModelEvent
,
EventListenerList
public Comparator getComparator(int column)
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.
column
- the column of the data
public Comparator getDefaultComparator(Class columnClass)
columnClass
- the class for which we want to find a comparator
public int[] getNonSortableColumns()
public int getSortMode()
public SortState[] getSortStates()
public SortTableRenderer getSortTableRenderer()
public JTableHeader getTableHeader()
public boolean isSortable(int column)
column
is sortable.
column
- the column which sortability we want to determine.
column
is sortable, false otherwise.public void rowsReordered(ReorderEvent e)
rowsReordered
in interface ReorderListener
rowsReordered
in class ListTableMap
e
- the ReorderEvent objectpublic void setComparator(int column, Comparator comparator)
column
.
column
- the column for which a comparator we want to setcomparator
- the comparator to setpublic void setDefaultComparator(Class columnClass, Comparator comparator)
columnClass
- the Class for which a comparator we want to setcomparator
- the comparator to setpublic void setHeader(JTableHeader newTableHeader)
newTableHeader
- the table header to setpublic void setSortableColumn(int column, boolean sortable)
column
- the table columnsortable
- true if sorting is desired for this column, false otherwisepublic void setSortMode(int mode)
mode
- the sorting mode to setpublic void setSortStates(SortState[] states)
states
- an array of SortState objects.public void sort(int column, int mode)
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.
column
- the column to sortmode
- the sort modeprotected int[] sortData()
ReorderEvent
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |