JIDE 3.5.15

com.jidesoft.grid
Class SortableTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.jidesoft.grid.TableModelWrapperImpl
          extended by com.jidesoft.grid.DefaultTableModelWrapper
              extended by com.jidesoft.grid.SortableTableModel
All Implemented Interfaces:
ColumnIdentifierTableModel, ContextSensitiveTableModel, EditorStyleTableModel, IndexChangeEventGenerator, IndexedRowTableModelWrapper, ISortableTableModel, MultiTableModel, NavigableModel, RowTableModelWrapper, SpanModel, StyleModel, TableModelWrapper, Serializable, EventListener, TableModelListener, TableModel
Direct Known Subclasses:
SortableAggregateTableModel, SortableTreeTableModel

public class SortableTableModel
extends DefaultTableModelWrapper
implements ISortableTableModel

TableModel used by SortableTable.

See Also:
Serialized Form

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 SortListeners 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.DefaultTableModelWrapper
fireIndexChanged, fireIndexChanging, getActualRowAt, getCellClassAt, getCellSpanAt, getConverterContextAt, getEditorContextAt, getEditorStyleAt, getIndexes, getRowCount, getValueAt, getVisualRowAt, isCacheEnabled, isCellEditable, isRowCountChanged, reallocateIndexes, setCacheEnabled, setValueAt, tableCellsUpdated, tableDataChanged, tableDataChanged, tableRowsDeleted, tableRowsInserted, tableRowsUpdated, tableStructureChanged
 
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

_sortItemSupport

protected SortItemSupport _sortItemSupport
Constructor Detail

SortableTableModel

public SortableTableModel(TableModel model)
Creates a SortableTableModel from any table model.

Parameters:
model - the table model.
Method Detail

getSortedRowAt

public int getSortedRowAt(int actualRow)
Gets the visual row.

Specified by:
getSortedRowAt in interface ISortableTableModel
Parameters:
actualRow - the actual row in actual model.
Returns:
the row on UI. -1 if cannot find the row.

tableChanged

public void tableChanged(TableModelEvent e)
Description copied from class: TableModelWrapperImpl
Implementation of the 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:

shouldOptimize

protected boolean shouldOptimize(int firstRow,
                                 int lastRow)
Should we optimize more to avoid unnecessary resorting on table model change events. By default, this method simply returns true.

This method will only take effect if isOptimized() returns true and isAutoResort() returns true.

Parameters:
firstRow - the first row which is updated.
lastRow - the last row which is updated
Returns:
true or false. If it returns false, we will simply call resort to sort all the rows. If true, we will look at each row instead. Obviously it is a trade-off. If there are many rows that are to be updated, it could be faster to resort the entire table instead of adding/removing the remain row indices on fly.

isInSortRanges

protected boolean isInSortRanges(int rowIndex)

sort

protected void sort()
Sorts the table model. Subclass can override to provide an optimized way to sort the table. The sorting column information can be get using getSortingColumns().


getSortRanges

protected int[] getSortRanges()
Gets the row index range that will be sorted. It will be a one dimension int array with the first value to be the low row index of the range and the second value to be the high row index of the range. The low row index is included in the range and the high row index is not included in the range. By default, we will return new int[]{0, _indexes.length} which is from the first row to the last row, the whole range. If your last row is a summary row so you don't want it to be sorted, you can return new int[]{0, _indexes.length - 1}. Or if you want the row at index 3 to always stay there, you can return new int[]{0, 3, 4, _indexes.length}.

Returns:
the sort ranges.

insert

protected int insert(int row)
Inserts a row to current _indexes.

Before insert the actual row, the _indexes as already adjusted for all inserted rows to compare accurately.

Parameters:
row - the actual row to insert
Returns:
the visual row index to insert the actual row

append

protected int[] append(int firstRow,
                       int lastRow)

search

protected int search(int[] indexes,
                     int row)
Searches the specified array of rows for the specified value using binary search. The array must be sorted prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.
Subclass can override this method to implement their own algorithm to do the search. When doing comparison in the algorithm, use the compare(int, int) method defined in this class. The original indexes are sorted in a way that compare(indexes[n], indexes[n + 1]) will always return 1 or 0. So if you compare(indexes[n], row) and it returns -1, you knows the row index should be greater than n.

Parameters:
indexes - the array of row to be searched.
row - the index of the row to be searched for.
Returns:
index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

sort

protected void sort(int[] from,
                    int[] to,
                    int low,
                    int high)
Sorts the rows.

Subclass can override this method to implement their own algorithm to sort. When doing comparison in the algorithm, use the compare(int, int) method defined in this class.

Parameters:
from - an int array of row indices to be sorted
to - an int array of row indices to store the result after sorting
low - the start index of the row in the array to be sorted
high - the end index of the row in the array to be sorted

cacheComparators

protected void cacheComparators()
For performance, all comparators for each column should be cached first.


getMasterSortColumns

public int[] getMasterSortColumns()
Gets the master sort columns.

Specified by:
getMasterSortColumns in interface ISortableTableModel
Returns:
the master sort columns.

setMasterSortColumns

public void setMasterSortColumns(int[] masterSortColumns)
Sets the master sort column. If the master sort columns are set, the row order will only be changed when the master sort columns have the same value. This is used when there are cell spans in certain column and you don't want the sorting to mess up the cell span as those rows will have to stay together.

Specified by:
setMasterSortColumns in interface ISortableTableModel
Parameters:
masterSortColumns - the new master sort columns.

getMasterSortColumnIndex

public int getMasterSortColumnIndex(int column)

compare

protected int compare(int row1,
                      int row2)
Compares two rows to decide the order. It will consider the ascending or descending and return different value so the return has different meaning comparing to that of Comparator.compare(o1, o2).
If it returns -1, it means keep the current order. If it returns 1, it means the order should be swapped. If it returns 0, it means the order doesn't matter. It could be either the table model is not sorted so the order doesn't matter or the two rows have the same order.

Please be noted that row1 and row2 are the index in the table model this table model wraps. A common mistake is that row1 and row2 were used as the row index in this table model itself, which will causes unexpected issues.

Parameters:
row1 - index of the first row to be compared
row2 - index of the second row to be compared
Returns:
If it returns -1, it means keep the current order. If it returns 1, it means the order should be swapped. If it returns 0, it means the order doesn't matter. It could be either the table model is not sorted so the order doesn't matter or the two rows have the same order.

compare

public int compare(int row1,
                   int row2,
                   int column)
Compares the two cell values at the two rows.

Parameters:
row1 - index of the first row to be compared
row2 - index of the second row to be compared
column - the column index of both rows.
Returns:
If it returns 1, it means the first value is greater than the second value. If it returns -1, it means the second value is greater than the first value. If it returns 0, it means the two values are the same. If it returns Integer.MAX_VALUE, the first value is always appear after the second value, regarding of the order is ascending or descending. If it returns Integer.MIN_VALUE, the first value is always appear before the second value, regarding of the order is ascending or descending.

compare

protected int compare(Object o1,
                      Object o2,
                      int column)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. By default, we will compareTo method if both objects are Comparable (if String, we will use compareToIgnore method on String). Otherwise, we will use Comparator to do the comparison. If you want to always use Comparator to compare even when the objects are Comparable, you can use setAlwaysUseComparators(boolean) and set it to true. Subclass can override it to provide your own way to compare.

Parameters:
o1 - the first object to be compared
o2 - the second object to be compared
column - the column index where the objects are
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. 0 if there is an exception.

getComparator

protected Comparator getComparator(int columnIndex)
Gets the column comparator. Subclass can override it to return a comparator for a particular column. By default, SortableTableModel will look up for a comparator from ObjectComparatorManager based on getColumnClass return value and getColumnComparatorContext return value. Please note, for the performance reason, if both objects are Comparable of the same type, we will use Comparable#compareTo method to compare the two objects. Then we will use the Comparator. If you want to always use Comparator to compare, you can call setAlwaysUseComparators(boolean) and set it to true.

Parameters:
columnIndex - the column index.
Returns:
the comparator for the specified column.

getColumnComparatorContext

public ComparatorContext getColumnComparatorContext(int column)
Gets the column comparator context. Subclass can override it to return a comparator context. By default it will return null. It should only be used when the column class for two columns are the same but you want to compare them differently. First you need to register two different comparators using 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.

Parameters:
column - the column index
Returns:
the comparator context for the column.

isResetOnTableStructureChangeEvent

public boolean isResetOnTableStructureChangeEvent()
Get the flag indicating if the sorting columns should be reset while firing table structure change events.

By default, the flag is true. If you don't want this behavior, please turn it to false.

Returns:
true if the sorting columns should be reset while firing table structure change events.

setResetOnTableStructureChangeEvent

public void setResetOnTableStructureChangeEvent(boolean resetOnTableStructureChangeEvent)
Set the flag indicating if the sorting columns should be reset while firing table structure change events.

Parameters:
resetOnTableStructureChangeEvent - the flag
See Also:
isResetOnTableStructureChangeEvent()

fireTableStructureChanged

public void fireTableStructureChanged()
Overrides:
fireTableStructureChanged in class AbstractTableModel

fireTableChanged

public void fireTableChanged(TableModelEvent e)
Overrides:
fireTableChanged in class TableModelWrapperImpl

getColumnComparatorContextProvider

public SortableTableModel.ColumnComparatorContextProvider getColumnComparatorContextProvider()
Gets the ColumnComparatorContextProvider.

Returns:
the ColumnComparatorContextProvider.

setColumnComparatorContextProvider

public void setColumnComparatorContextProvider(SortableTableModel.ColumnComparatorContextProvider columnComparatorContextProvider)
Sets the 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.

Parameters:
columnComparatorContextProvider - the columnComparatorContextProvider

setIndexes

public void setIndexes(int[] indexes)
Overrides the method in DefaultTableModelWrapper to clear up any soring columns.

Specified by:
setIndexes in interface IndexedRowTableModelWrapper
Overrides:
setIndexes in class DefaultTableModelWrapper
Parameters:
indexes - the new index array.

getPopupMenuItems

public JMenuItem[] getPopupMenuItems(int column)
Gets a list of menu items for the specified column. It has "Sort Ascending", "Sort Descending", and "Unsort".

Parameters:
column - the column
Returns:
the list of menu items

getSortableModel

public static ISortableTableModel getSortableModel(TableModel model)
Gets the sortable table model. If model is a TableModelWrapper, it will get the actual model until it finds the first sortable table model.

Parameters:
model - the table model
Returns:
sortable table model.

isOptimized

public boolean isOptimized()
Checks if the sortable table model is in optimized mode.

Returns:
the optimized flag.

setOptimized

public 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. If the flag is false, SortableTableModel will resort completely whenever data changed.
Default is true.

Parameters:
optimized - true or false.

isAutoResort

public boolean isAutoResort()
Checks if the table is automatically resorted when data changes.

Returns:
true if autoResort. Otherwise false.

setAutoResort

public void setAutoResort(boolean autoResort)
AutoResort is a feature that automatically resort the table when table data changes. This is the default behavior and useful in most cases. However there are cases when the data changes frequently, resort will make it very hard for user to read the value. So in these cases, you can set autoResort to false. Then you will need to provide a button to call resort() to resort the table.

Note: this flag is only used when optimized flag is true. If optimized is false, autoResort is always true.

Parameters:
autoResort - true or false.

sortColumn

public void sortColumn(int column)
Sort the column, equals to sortColumn(column, false).

Specified by:
sortColumn in interface ISortableTableModel
Parameters:
column - column to be sorted

sortColumn

public void sortColumn(int column,
                       boolean reset)
If reset is true, it will remove all existing sort-by columns and only sorts by column. If reset is false, it will keep existing sort-by columns and add the column as a new sort-by column.

Specified by:
sortColumn in interface ISortableTableModel
Parameters:
column - the column index.
reset - true to reset all existing sorting columns before sorting the new column.

unsortColumn

public void unsortColumn(int column)
Unsorts the column.

Specified by:
unsortColumn in interface ISortableTableModel
Parameters:
column - column to be removed from sort-by columns

reverseColumnSortOrder

public void reverseColumnSortOrder(int column)
Reverses the sort order of the column. The column must be one of sorted column, or else the method will do nothing

Specified by:
reverseColumnSortOrder in interface ISortableTableModel
Parameters:
column - the column index.

sortColumn

public void sortColumn(int column,
                       boolean reset,
                       boolean ascending)
Sorts a column.

Specified by:
sortColumn in interface ISortableTableModel
Parameters:
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.

isColumnSorted

public boolean isColumnSorted(int column)
Checks if the column is sorted.

Specified by:
isColumnSorted in interface ISortableTableModel
Parameters:
column - the column index.
Returns:
true if the column is sorted.

isColumnAscending

public boolean isColumnAscending(int column)
Checks if the column is sorted ascendingly.

Specified by:
isColumnAscending in interface ISortableTableModel
Parameters:
column - the column index.
Returns:
true if column is ascendingly sorted. If it's not sorted or sorted but descending, it will return false.

reset

public void reset()
Resets. No columns will be sorted. Nothing will be done if the model was not sorted.

Specified by:
reset in interface ISortableTableModel

getSortingColumns

public List<ISortableTableModel.SortItem> getSortingColumns()
Gets the sorting columns. It's a ArrayList. The element in the list is SortItem which has the column index and sorting direction.

Specified by:
getSortingColumns in interface ISortableTableModel
Returns:
an ArrayList of sorting columns.

setSortingColumns

public void setSortingColumns(List<ISortableTableModel.SortItem> list)
Sets the soring columns. It will do a sort action automatically.

Specified by:
setSortingColumns in interface ISortableTableModel
Parameters:
list - a list of SortItems.

getColumnSortRank

public 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.

Specified by:
getColumnSortRank in interface ISortableTableModel
Parameters:
column - the column index.
Returns:
the rank of this column within all sorted columns. -1 is the column is not sorted. 0 means the first rank and so on.

isMultiColumnSortable

public boolean isMultiColumnSortable()
Does this table allow sort by multiple columns.

Specified by:
isMultiColumnSortable in interface ISortableTableModel
Returns:
true if this table allows sort by multiple columns

setMultiColumnSortable

public void setMultiColumnSortable(boolean multiColumnSortable)
Set the value if this table allows sort by multiple columns.

Specified by:
setMultiColumnSortable in interface ISortableTableModel
Parameters:
multiColumnSortable - pass in true if this you want this table allows sort by multiple columns

isColumnSortable

public boolean isColumnSortable(int column)
Check if a certain column is sortable.

Specified by:
isColumnSortable in interface ISortableTableModel
Parameters:
column - the column index.
Returns:
true if this table column is sortable.
See Also:
setColumnSortable(int, boolean)

setColumnSortable

public void setColumnSortable(int column,
                              boolean sortable)
Sets a column sortable or not sortable. Please note, you will have to set it again if the table model structure is changed. By default, all columns are sortable.

Specified by:
setColumnSortable in interface ISortableTableModel
Parameters:
column - the column index.
sortable - true to make the column sortable.

isSortable

public boolean isSortable()
Checks if the sortable table model is sortable.

Specified by:
isSortable in interface ISortableTableModel
Returns:
true or false.

setSortable

public void setSortable(boolean sortable)
Sets the table model sortable. If the model is not sortable, toggleSortOrder(int, boolean) will have no effect.

Specified by:
setSortable in interface ISortableTableModel
Parameters:
sortable - true or false.

resort

public void resort()
Resort the table. When autoResort is false, the table will be out of order after data changes. The method will resort the table while keeping the sorting columns.

Specified by:
resort in interface ISortableTableModel

addSortListener

public void addSortListener(SortListener l)
Adds the specified listener to receive SortEvents pane events from this SortableTableModel.

Specified by:
addSortListener in interface ISortableTableModel
Parameters:
l - the SortListener

removeSortListener

public void removeSortListener(SortListener l)
Removes the specified SortListener so that it no longer receives SortEvents from this SortableTableModel .

Specified by:
removeSortListener in interface ISortableTableModel
Parameters:
l - the SortableTableModel listener

getSortListeners

public SortListener[] getSortListeners()
Returns an array of all the SortListeners added to this SortableTableModel with addSortListener.

Specified by:
getSortListeners in interface ISortableTableModel
Returns:
all of the SortListeners added or an empty array if no listeners have been added
See Also:
addSortListener(com.jidesoft.grid.SortListener)

fireSortEvent

public void fireSortEvent()
Fires sort event.


fireSortingEvent

public void fireSortingEvent()
Fires sort event.


getSortPriority

public int getSortPriority()
Gets the sort priority.

Specified by:
getSortPriority in interface ISortableTableModel
Returns:
the sort priority. It could be either ISortableTableModel.SORT_PRIORITY_FILO (the default) or ISortableTableModel.SORT_PRIORITY_FIFO.

setSortPriority

public void setSortPriority(int sortPriority)
Sets the sort priority. This property only has effect when multiple columns are sorted. When sort priority is FILO (first-in-last-out), the first sorted column has the smallest sort rank (with a number "1" on its column header). If there are more columns being sorted, their sort tank are getting higher based on the the order when they are sorted. Oppositely, when sort priority is FIFO (first-in-first-out), the first sorted column has the sort rank 1. But the moment a new column is sorted, it will get sort rank 1 and push the previous sorted column's sort rank to 2. And so on. If you view it as a queue, you will see it's either a FILO queue or FIFO queue. That's why we call it FIFO or FILO.

Specified by:
setSortPriority in interface ISortableTableModel
Parameters:
sortPriority - must be one the following value: SORT_PRIORITY_FILO (the default), or SORT_PRIORITY_FIFO

getMaximumSortColumns

public int getMaximumSortColumns()
Gets the maximum columns can be sorted at once. If user tries to sort another columns when maximum count is met, depending on the value of 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.

Specified by:
getMaximumSortColumns in interface ISortableTableModel
Returns:
the maximum sorted column number.

setMaximumSortColumns

public void setMaximumSortColumns(int maximumSortColumns)
Set the maximum number of columns that can be sorted at once.

Specified by:
setMaximumSortColumns in interface ISortableTableModel
Parameters:
maximumSortColumns - the maximum number of columns that can be sorted at once.

getSortOrderHandler

public SortableTableModel.SortOrderHandler getSortOrderHandler()
Gets the SortOrderHandler.

Returns:
the SortOrderHandler.

setSortOrderHandler

public void setSortOrderHandler(SortableTableModel.SortOrderHandler sortOrderHandler)
Sets the SortOrderHandler.

Parameters:
sortOrderHandler - a new SortOrderHandler.
See Also:
toggleSortOrder(int, boolean)

toggleSortOrder

public void toggleSortOrder(int column,
                            boolean extend)
Toggles the sort order on the specified column. By default, it will sort the column if not sorted. If sorted ascending, it will change to descending. If descending, it will unsort.

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.

Specified by:
toggleSortOrder in interface ISortableTableModel
Parameters:
column - the column index.
extend - if true, extend the current sort to add more sorted columns.

shouldReverseIndices

protected boolean shouldReverseIndices(int column,
                                       boolean onlyColumnSorted,
                                       boolean ascending)
Returns if the SortableTableModel could improve performance by just reversing the current indices.

It returns false by default to keep generic so that your customization sorting would not be broken. However, if you are just using the default sorting behavior and want to speed up the sorting while switching between ascending and descending, you could override this method with the following sample code to improve the performance.

    return _sortItemSupport.getSortOrderHandler() == null && onlyColumnSorted && getSortingColumns().size() == 1
 && getSortingColumns().get(0).getColumn() == column && getSortingColumns().get(0).isAscending() != ascending &&
 _indexes != null;
 

Parameters:
column - current sorting column
onlyColumnSorted - the flag if the column is the only column sorted before
ascending - the flag if the column is sorted in ascending or descending order
Returns:
false by default.

isAlwaysUseComparators

public boolean isAlwaysUseComparators()
Checks if the alwaysUseComparators flag value.

Returns:
true if alwaysUseComparators is true. Otherwise false.

setAlwaysUseComparators

public void setAlwaysUseComparators(boolean alwaysUseComparators)
Sets the alwaysUseComparators flag. By default, we will check if the two values are Comparable. If they both are, we will use 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.

Parameters:
alwaysUseComparators - true or false.

setSortingPaused

public void setSortingPaused(boolean pause)
If sorting is paused, keep rows in same relative positions Inserts/Deletes shift rows without re-ordering.

Parameters:
pause - TRUE = Sorting is Paused; FALSE = Sorting is Active

isSortingPaused

public boolean isSortingPaused()

JIDE 3.5.15