|
JIDE 3.5.15 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.swing.table.AbstractTableModel com.jidesoft.grid.TableModelWrapperImpl com.jidesoft.grid.DefaultTableModelWrapper com.jidesoft.grid.SortableTableModel
public class SortableTableModel
TableModel used by SortableTable.
Nested Class Summary | |
---|---|
static interface |
SortableTableModel.ColumnComparatorContextProvider
An interface used by setColumnComparatorContextProvider(com.jidesoft.grid.SortableTableModel.ColumnComparatorContextProvider) . |
static interface |
SortableTableModel.SortOrderHandler
A handler to handle the toggling of sort order. |
Nested classes/interfaces inherited from interface com.jidesoft.grid.ISortableTableModel |
---|
ISortableTableModel.SortItem |
Field Summary | |
---|---|
protected SortItemSupport |
_sortItemSupport
|
Fields inherited from class com.jidesoft.grid.DefaultTableModelWrapper |
---|
_indexes |
Fields inherited from class com.jidesoft.grid.TableModelWrapperImpl |
---|
_model |
Fields inherited from class javax.swing.table.AbstractTableModel |
---|
listenerList |
Fields inherited from interface com.jidesoft.grid.ISortableTableModel |
---|
SORT_PRIORITY_FIFO, SORT_PRIORITY_FILO |
Fields inherited from interface com.jidesoft.grid.MultiTableModel |
---|
FOOTER_COLUMN, HEADER_COLUMN, REGULAR_COLUMN |
Fields inherited from interface com.jidesoft.grid.EditorStyleTableModel |
---|
EDITOR_STYLE_EDITABLE, EDITOR_STYLE_NORMAL, EDITOR_STYLE_READ_ONLY, EDITOR_STYLE_SELECT_ONLY |
Constructor Summary | |
---|---|
SortableTableModel(TableModel model)
Creates a SortableTableModel from any table model. |
Method Summary | |
---|---|
void |
addSortListener(SortListener l)
Adds the specified listener to receive SortEvents pane events from this SortableTableModel. |
protected int[] |
append(int firstRow,
int lastRow)
|
protected void |
cacheComparators()
For performance, all comparators for each column should be cached first. |
protected int |
compare(int row1,
int row2)
Compares two rows to decide the order. |
int |
compare(int row1,
int row2,
int column)
Compares the two cell values at the two rows. |
protected int |
compare(Object o1,
Object o2,
int column)
Compares its two arguments for order. |
void |
fireSortEvent()
Fires sort event. |
void |
fireSortingEvent()
Fires sort event. |
void |
fireTableChanged(TableModelEvent e)
|
void |
fireTableStructureChanged()
|
ComparatorContext |
getColumnComparatorContext(int column)
Gets the column comparator context. |
SortableTableModel.ColumnComparatorContextProvider |
getColumnComparatorContextProvider()
Gets the ColumnComparatorContextProvider. |
int |
getColumnSortRank(int column)
In the case of sort by multiple columns, this method will return the rank of this column within all sorted columns. |
protected Comparator |
getComparator(int columnIndex)
Gets the column comparator. |
int |
getMasterSortColumnIndex(int column)
|
int[] |
getMasterSortColumns()
Gets the master sort columns. |
int |
getMaximumSortColumns()
Gets the maximum columns can be sorted at once. |
JMenuItem[] |
getPopupMenuItems(int column)
Gets a list of menu items for the specified column. |
static ISortableTableModel |
getSortableModel(TableModel model)
Gets the sortable table model. |
int |
getSortedRowAt(int actualRow)
Gets the visual row. |
List<ISortableTableModel.SortItem> |
getSortingColumns()
Gets the sorting columns. |
SortListener[] |
getSortListeners()
Returns an array of all the SortListener s added to this SortableTableModel with
addSortListener . |
SortableTableModel.SortOrderHandler |
getSortOrderHandler()
Gets the SortOrderHandler. |
int |
getSortPriority()
Gets the sort priority. |
protected int[] |
getSortRanges()
Gets the row index range that will be sorted. |
protected int |
insert(int row)
Inserts a row to current _indexes. |
boolean |
isAlwaysUseComparators()
Checks if the alwaysUseComparators flag value. |
boolean |
isAutoResort()
Checks if the table is automatically resorted when data changes. |
boolean |
isColumnAscending(int column)
Checks if the column is sorted ascendingly. |
boolean |
isColumnSortable(int column)
Check if a certain column is sortable. |
boolean |
isColumnSorted(int column)
Checks if the column is sorted. |
protected boolean |
isInSortRanges(int rowIndex)
|
boolean |
isMultiColumnSortable()
Does this table allow sort by multiple columns. |
boolean |
isOptimized()
Checks if the sortable table model is in optimized mode. |
boolean |
isResetOnTableStructureChangeEvent()
Get the flag indicating if the sorting columns should be reset while firing table structure change events. |
boolean |
isSortable()
Checks if the sortable table model is sortable. |
boolean |
isSortingPaused()
|
void |
removeSortListener(SortListener l)
Removes the specified SortListener so that it no longer receives SortEvents from this SortableTableModel . |
void |
reset()
Resets. |
void |
resort()
Resort the table. |
void |
reverseColumnSortOrder(int column)
Reverses the sort order of the column. |
protected int |
search(int[] indexes,
int row)
Searches the specified array of rows for the specified value using binary search. |
void |
setAlwaysUseComparators(boolean alwaysUseComparators)
Sets the alwaysUseComparators flag. |
void |
setAutoResort(boolean autoResort)
AutoResort is a feature that automatically resort the table when table data changes. |
void |
setColumnComparatorContextProvider(SortableTableModel.ColumnComparatorContextProvider columnComparatorContextProvider)
Sets the ColumnComparatorContextProvider . |
void |
setColumnSortable(int column,
boolean sortable)
Sets a column sortable or not sortable. |
void |
setIndexes(int[] indexes)
Overrides the method in DefaultTableModelWrapper to clear up any soring columns. |
void |
setMasterSortColumns(int[] masterSortColumns)
Sets the master sort column. |
void |
setMaximumSortColumns(int maximumSortColumns)
Set the maximum number of columns that can be sorted at once. |
void |
setMultiColumnSortable(boolean multiColumnSortable)
Set the value if this table allows sort by multiple columns. |
void |
setOptimized(boolean optimized)
If optimized flag is true and the table is already sorted, SortableTableModel will do incremental sorting when data is only partially changed. |
void |
setResetOnTableStructureChangeEvent(boolean resetOnTableStructureChangeEvent)
Set the flag indicating if the sorting columns should be reset while firing table structure change events. |
void |
setSortable(boolean sortable)
Sets the table model sortable. |
void |
setSortingColumns(List<ISortableTableModel.SortItem> list)
Sets the soring columns. |
void |
setSortingPaused(boolean pause)
If sorting is paused, keep rows in same relative positions Inserts/Deletes shift rows without re-ordering. |
void |
setSortOrderHandler(SortableTableModel.SortOrderHandler sortOrderHandler)
Sets the SortOrderHandler. |
void |
setSortPriority(int sortPriority)
Sets the sort priority. |
protected boolean |
shouldOptimize(int firstRow,
int lastRow)
Should we optimize more to avoid unnecessary resorting on table model change events. |
protected boolean |
shouldReverseIndices(int column,
boolean onlyColumnSorted,
boolean ascending)
Returns if the SortableTableModel could improve performance by just reversing the current indices. |
protected void |
sort()
Sorts the table model. |
protected void |
sort(int[] from,
int[] to,
int low,
int high)
Sorts the rows. |
void |
sortColumn(int column)
Sort the column, equals to sortColumn(column, false). |
void |
sortColumn(int column,
boolean reset)
If reset is true, it will remove all existing sort-by columns and only sorts by column . |
void |
sortColumn(int column,
boolean reset,
boolean ascending)
Sorts a column. |
void |
tableChanged(TableModelEvent e)
Implementation of the TableChangeListener interface. |
void |
toggleSortOrder(int column,
boolean extend)
Toggles the sort order on the specified column. |
void |
unsortColumn(int column)
Unsorts the column. |
Methods inherited from class com.jidesoft.grid.TableModelWrapperImpl |
---|
addIndexChangeListener, createCompoundTableModelEvent, fireTableCellsUpdated, getActualModel, getCellStyleAt, getColumnClass, getColumnCount, getColumnIdentifier, getColumnName, getColumnType, getIndexChangeListeners, getTableIndex, isCellSpanOn, isCellStyleOn, isNavigableAt, isNavigationOn, removeIndexChangeListener |
Methods inherited from class javax.swing.table.AbstractTableModel |
---|
addTableModelListener, findColumn, fireTableCellUpdated, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, 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.jidesoft.grid.ISortableTableModel |
---|
getActualRowAt |
Methods inherited from interface javax.swing.table.TableModel |
---|
addTableModelListener, getColumnClass, getColumnCount, getColumnName, getRowCount, getValueAt, isCellEditable, removeTableModelListener, setValueAt |
Methods inherited from interface com.jidesoft.grid.TableModelWrapper |
---|
getActualModel |
Field Detail |
---|
protected SortItemSupport _sortItemSupport
Constructor Detail |
---|
public SortableTableModel(TableModel model)
model
- the table model.Method Detail |
---|
public int getSortedRowAt(int actualRow)
getSortedRowAt
in interface ISortableTableModel
actualRow
- the actual row in actual model.
public void tableChanged(TableModelEvent e)
TableModelWrapperImpl
TableChangeListener
interface. Depending on the type of the
TableModelEvent
, this method delegates to one of the following methods based on the contract of the
TableModelEvent
object: TableModelWrapperImpl.tableRowsInserted(int, int)
TableModelWrapperImpl.tableRowsDeleted(int, int)
TableModelWrapperImpl.tableStructureChanged()
TableModelWrapperImpl.tableDataChanged()
TableModelWrapperImpl.tableRowsUpdated(int, int)
TableModelWrapperImpl.tableCellsUpdated(int, int, int)
TableModelEvent
to the listeners on this model. Most Subclasses
will override one or more of the above protected methods to provide custom behavior for particular events.
However, some implementations may wish to override this method instead. Obviously, if this method is overridden,
the above protected methods will not be called unless the overridden implementation does so explicitly.
tableChanged
in interface TableModelListener
tableChanged
in class TableModelWrapperImpl
e
- the TableModelEvent
representing the changeprotected boolean shouldOptimize(int firstRow, int lastRow)
isOptimized()
returns true and isAutoResort()
returns
true.
firstRow
- the first row which is updated.lastRow
- the last row which is updated
protected boolean isInSortRanges(int rowIndex)
protected void sort()
protected int[] getSortRanges()
protected int insert(int row)
row
- the actual row to insert
protected int[] append(int firstRow, int lastRow)
protected int search(int[] indexes, int row)
indexes
- the array of row to be searched.row
- the index of the row to be searched for.
protected void sort(int[] from, int[] to, int low, int high)
from
- an int array of row indices to be sortedto
- an int array of row indices to store the result after sortinglow
- the start index of the row in the array to be sortedhigh
- the end index of the row in the array to be sortedprotected void cacheComparators()
public int[] getMasterSortColumns()
getMasterSortColumns
in interface ISortableTableModel
public void setMasterSortColumns(int[] masterSortColumns)
setMasterSortColumns
in interface ISortableTableModel
masterSortColumns
- the new master sort columns.public int getMasterSortColumnIndex(int column)
protected int compare(int row1, int row2)
row1
- index of the first row to be comparedrow2
- index of the second row to be compared
public int compare(int row1, int row2, int column)
row1
- index of the first row to be comparedrow2
- index of the second row to be comparedcolumn
- the column index of both rows.
protected int compare(Object o1, Object o2, int column)
setAlwaysUseComparators(boolean)
and set it to true. Subclass can override it to provide your own way to
compare.
o1
- the first object to be comparedo2
- the second object to be comparedcolumn
- the column index where the objects are
protected Comparator getComparator(int columnIndex)
setAlwaysUseComparators(boolean)
and set it to true.
columnIndex
- the column index.
public ComparatorContext getColumnComparatorContext(int column)
ObjectComparatorManager
using different comparator context in ObjectComparatorManager.registerComparator(Class,
java.util.Comparator, com.jidesoft.comparator.ComparatorContext)
method. Then return the corresponding context
when overriding this method.
In 1.9.2.04 release, we also add a setter for this. You can call setColumnComparatorContextProvider(com.jidesoft.grid.SortableTableModel.ColumnComparatorContextProvider)
to provide a ColumnComparatorContextProvider. This provider will return a ComparatorContext for each column. This
will be helpful when it's hard to override SortableTableModel.
column
- the column index
public boolean isResetOnTableStructureChangeEvent()
public void setResetOnTableStructureChangeEvent(boolean resetOnTableStructureChangeEvent)
resetOnTableStructureChangeEvent
- the flagisResetOnTableStructureChangeEvent()
public void fireTableStructureChanged()
fireTableStructureChanged
in class AbstractTableModel
public void fireTableChanged(TableModelEvent e)
fireTableChanged
in class TableModelWrapperImpl
public SortableTableModel.ColumnComparatorContextProvider getColumnComparatorContextProvider()
public void setColumnComparatorContextProvider(SortableTableModel.ColumnComparatorContextProvider columnComparatorContextProvider)
ColumnComparatorContextProvider
. This provider will provide a
ComparatorContext
for each column. If this table model has several columns that have the same types
in getColumnClass method but you want to use different comparators to compare and sort them, you would need to
register several comparators on ObjectComparatorManager
using different
ComparatorContext
. Then you can this provider to return different contexts so that
SortableTableModel
can find the correct comparator registered on
ObjectComparatorManager
. If you call this method with a non-null provider, we will automatically
call setAlwaysUseComparators(boolean)
and set it to true.
columnComparatorContextProvider
- the columnComparatorContextProviderpublic void setIndexes(int[] indexes)
setIndexes
in interface IndexedRowTableModelWrapper
setIndexes
in class DefaultTableModelWrapper
indexes
- the new index array.public JMenuItem[] getPopupMenuItems(int column)
column
- the column
public static ISortableTableModel getSortableModel(TableModel model)
model
- the table model
public boolean isOptimized()
public void setOptimized(boolean optimized)
optimized
- true or false.public boolean isAutoResort()
public void setAutoResort(boolean autoResort)
resort()
to resort the table.
Note: this flag is only used when optimized flag is true. If optimized is false, autoResort is always true.
autoResort
- true or false.public void sortColumn(int column)
sortColumn
in interface ISortableTableModel
column
- column to be sortedpublic void sortColumn(int column, boolean reset)
column
. If reset is
false, it will keep existing sort-by columns and add the column
as a new sort-by column.
sortColumn
in interface ISortableTableModel
column
- the column index.reset
- true to reset all existing sorting columns before sorting the new column.public void unsortColumn(int column)
unsortColumn
in interface ISortableTableModel
column
- column to be removed from sort-by columnspublic void reverseColumnSortOrder(int column)
reverseColumnSortOrder
in interface ISortableTableModel
column
- the column index.public void sortColumn(int column, boolean reset, boolean ascending)
sortColumn
in interface ISortableTableModel
column
- the column index.reset
- true to reset all existing sorting columns before sorting the new column.ascending
- true to sort ascending. False to sort descending.public boolean isColumnSorted(int column)
isColumnSorted
in interface ISortableTableModel
column
- the column index.
public boolean isColumnAscending(int column)
isColumnAscending
in interface ISortableTableModel
column
- the column index.
public void reset()
reset
in interface ISortableTableModel
public List<ISortableTableModel.SortItem> getSortingColumns()
getSortingColumns
in interface ISortableTableModel
public void setSortingColumns(List<ISortableTableModel.SortItem> list)
setSortingColumns
in interface ISortableTableModel
list
- a list of SortItems.public int getColumnSortRank(int column)
getColumnSortRank
in interface ISortableTableModel
column
- the column index.
public boolean isMultiColumnSortable()
isMultiColumnSortable
in interface ISortableTableModel
public void setMultiColumnSortable(boolean multiColumnSortable)
setMultiColumnSortable
in interface ISortableTableModel
multiColumnSortable
- pass in true if this you want this table allows sort by multiple columnspublic boolean isColumnSortable(int column)
isColumnSortable
in interface ISortableTableModel
column
- the column index.
setColumnSortable(int, boolean)
public void setColumnSortable(int column, boolean sortable)
setColumnSortable
in interface ISortableTableModel
column
- the column index.sortable
- true to make the column sortable.public boolean isSortable()
isSortable
in interface ISortableTableModel
public void setSortable(boolean sortable)
toggleSortOrder(int, boolean)
will have no
effect.
setSortable
in interface ISortableTableModel
sortable
- true or false.public void resort()
resort
in interface ISortableTableModel
public void addSortListener(SortListener l)
addSortListener
in interface ISortableTableModel
l
- the SortListenerpublic void removeSortListener(SortListener l)
removeSortListener
in interface ISortableTableModel
l
- the SortableTableModel listenerpublic SortListener[] getSortListeners()
SortListener
s added to this SortableTableModel
with
addSortListener
.
getSortListeners
in interface ISortableTableModel
SortListener
s added or an empty array if no listeners have been addedaddSortListener(com.jidesoft.grid.SortListener)
public void fireSortEvent()
public void fireSortingEvent()
public int getSortPriority()
getSortPriority
in interface ISortableTableModel
ISortableTableModel.SORT_PRIORITY_FILO
(the default) or
ISortableTableModel.SORT_PRIORITY_FIFO
.public void setSortPriority(int sortPriority)
setSortPriority
in interface ISortableTableModel
sortPriority
- must be one the following value: SORT_PRIORITY_FILO (the default), or SORT_PRIORITY_FIFOpublic int getMaximumSortColumns()
getSortPriority()
, the behavior is different. If sort priority is FILO,
nothing will happen when user tries to sort one column. If FIFO, it will push the column with the largest sort
rank out.
getMaximumSortColumns
in interface ISortableTableModel
public void setMaximumSortColumns(int maximumSortColumns)
setMaximumSortColumns
in interface ISortableTableModel
maximumSortColumns
- the maximum number of columns that can be sorted at once.public SortableTableModel.SortOrderHandler getSortOrderHandler()
public void setSortOrderHandler(SortableTableModel.SortOrderHandler sortOrderHandler)
sortOrderHandler
- a new SortOrderHandler.toggleSortOrder(int, boolean)
public void toggleSortOrder(int column, boolean extend)
See default implementation below.
if (isMultiColumnSortable() && extend) {
if (!isColumnSorted(column))
sortColumn(column, false, true);
else if (isColumnAscending(column))
reverseColumnSortOrder(column);
else
unsortColumn(column);
}
else {
if (!isColumnSorted(column))
sortColumn(column, true, true);
else if (isColumnAscending(column))
reverseColumnSortOrder(column);
else
reset();
}
If you want a different behavior, you can use setSortOrderHandler(com.jidesoft.grid.SortableTableModel.SortOrderHandler)
and provide your own way to toggle the sort order.
toggleSortOrder
in interface ISortableTableModel
column
- the column index.extend
- if true, extend the current sort to add more sorted columns.protected boolean shouldReverseIndices(int column, boolean onlyColumnSorted, boolean ascending)
return _sortItemSupport.getSortOrderHandler() == null && onlyColumnSorted && getSortingColumns().size() == 1
&& getSortingColumns().get(0).getColumn() == column && getSortingColumns().get(0).isAscending() != ascending &&
_indexes != null;
column
- current sorting columnonlyColumnSorted
- the flag if the column is the only column sorted beforeascending
- the flag if the column is sorted in ascending or descending order
public boolean isAlwaysUseComparators()
public void setAlwaysUseComparators(boolean alwaysUseComparators)
Comparable.compareTo(Object)
to compare. This is the default preferred way because this gives
developer a finer control of the comparison result. People also sometimes forgot to implement getColumnClass and
getColumnComparatorContext. If so, a wrong comparator could be used. If this flag is set to true, we will always
use Comparators to compare which you means you must implement getColumnClass and optionally implement
getColumnComparatorContext or use setColumnComparatorContextProvider(com.jidesoft.grid.SortableTableModel.ColumnComparatorContextProvider)
so that the correct comparator will be used for each column.
alwaysUseComparators
- true or false.public void setSortingPaused(boolean pause)
pause
- TRUE = Sorting is Paused; FALSE = Sorting is Activepublic boolean isSortingPaused()
|
JIDE 3.5.15 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |