|
Copyright © 2011 Citra Technologies. All Rights Reserved. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.swing.table.AbstractTableModel
com.citra.table.ListTableMap
com.citra.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.citra.comparators package, but you
can also define your own and assign them with setComparator
.
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. |
protected boolean |
nullsStart
variable that determines the position of null values during sorting |
static int |
REMOVE_SORT
Defines the mode for removing sorting columns. |
static int |
SINGLE_SORT
Defines single sort mode. |
Fields inherited from class com.citra.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. |
boolean |
getNullsPositionToStart()
Determines how null values will be sorted. |
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 |
setNullsPositionToStart(boolean nullsStart)
Determines how null values will be sorted. |
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.citra.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.citra.table.ListTableModel |
---|
fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged |
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 boolean allowSort
protected Hashtable defaultComparatorsByColumnClass
protected HashMap comparatorsByField
protected Cursor defaultHeaderCursor
protected boolean nullsStart
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 |
---|
public int compareRows(int row1, int row2)
compareRows
in interface SortListModel
row1
- the first rowrow2
- the second row
public int compareRows(Object o1, Object o2)
compareRows
in interface SortListModel
o1
- the first row objecto2
- the second row object
public int convertRowIndexToModel(int viewRowIndex)
convertRowIndexToModel
in class ListTableMap
viewRowIndex
- the index of the row in this map
public int convertRowIndexToView(int modelRowIndex)
convertRowIndexToView
in class ListTableMap
modelRowIndex
- the index of the row in the underlying model
protected void createDefaultComparators()
protected SortTableRenderer createDefaultSortTableRenderer()
protected MouseInputListener createMouseHandler()
public boolean getAllowSort()
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.
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 Cursor getDefaultHeaderCursor()
public int[] getNonSortableColumns()
public List getRows()
getRows
in interface ListTableModel
getRows
in class ListTableMap
public int[] getSortedIndexes()
public int getSortMode()
public SortState[] getSortStates()
public String getSortStatesAsString()
public SortTableRenderer getSortTableRenderer()
public JTableHeader getTableHeader()
public Object getValueAt(int row, int column)
getValueAt
in interface TableModel
getValueAt
in class ListTableMap
row
- the row whose value is to be queriedcolumn
- the column whose value is to be queried
public boolean isCellEditable(int rowIndex, int columnIndex)
isCellEditable
in interface TableModel
isCellEditable
in class ListTableMap
rowIndex
- the row being queriedcolumnIndex
- the column being queried
TableModel.setValueAt(java.lang.Object, int, int)
public boolean isSortable(int column)
column
is sortable.
column
- the column which sortability we want to determine.
column
is sortable, false otherwise.public void reIndex()
public void removeRow(int row)
removeRow
in interface ListTableModel
removeRow
in class ListTableMap
row
- the index of the row being removedpublic void removeRows(int[] deletedRows)
removeRows
in interface ListTableModel
removeRows
in class ListTableMap
deletedRows
- an integer array that contains indexes of the rows being deletedpublic void rowsReordered(ReorderEvent e)
rowsReordered
in interface ReorderListener
rowsReordered
in class ListTableMap
e
- the ReorderEvent objectpublic void setAllowSort(boolean allowSort)
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.
allowSort
- true if the model will perform the sorting when the underlying data changes,
false otherwise.public 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 setDefaultHeaderCursor(Cursor cursor)
cursor
- the default header cursor to assignpublic void setHeader(JTableHeader newTableHeader)
newTableHeader
- the table header to setpublic void setModel(ListTableModel newModel)
setModel
in class ListTableMap
newModel
- the new ListTableModel to setpublic void setSortableColumn(int column, boolean sortable)
column
- the table columnsortable
- true if sorting is desired for this column, false otherwisepublic void setSortedIndexes(int[] indexes)
indexes
- the indexes to assign.public void setSortMode(int mode)
mode
- the sorting mode to setpublic void setSortStates(SortState[] states)
states
- an array of SortState objects.public void setSortStatesAsString(String state)
state
- the string representation of the sorting state.public void setSortTableRenderer(SortTableRenderer renderer)
renderer
- the SortTableModel's SortTableRenderer to assignpublic void setValueAt(Object aValue, int rowIndex, int columnIndex)
setValueAt
in interface TableModel
setValueAt
in class ListTableMap
aValue
- value to assign to cellrowIndex
- row of cellcolumnIndex
- column of cellTableModel.getValueAt(int, int)
,
TableModel.isCellEditable(int, int)
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
.
protected void sorterMoved(MouseEvent e)
e
- the corresponding mouse eventprotected void sorterPressed(MouseEvent e)
e
- the corresponding mouse eventprotected void sorterReleased(MouseEvent e)
e
- the corresponding mouse eventpublic void tableChanged(TableModelEvent e)
tableChanged
in interface TableModelListener
tableChanged
in class ListTableMap
public void setNullsPositionToStart(boolean nullsStart)
nullsStart
is true
, null values, when sorting in ascending order, will be positioned at the beginning.
nullsStart
- true, if nulls are sorted towards the start, false otherwisepublic boolean getNullsPositionToStart()
|
Copyright © 2011 Citra Technologies. All Rights Reserved. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |