JIDE 3.5.15

com.jidesoft.grid
Class SortableTreeTableModel<T extends Row>

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
                  extended by com.jidesoft.grid.SortableTreeTableModel<T>
All Implemented Interfaces:
ColumnIdentifierTableModel, ContextSensitiveTableModel, EditorStyleTableModel, IndexChangeEventGenerator, IndexedRowTableModelWrapper, ISortableTableModel, ITreeTableModel<T>, MultiTableModel, NavigableModel, RowTableModelWrapper, SpanModel, StyleModel, TableModelWrapper, Serializable, EventListener, TableModelListener, TableModel

public class SortableTreeTableModel<T extends Row>
extends SortableTableModel
implements ITreeTableModel<T>

A sortable TreeTableModel used by TreeTable. If the TreeTable is sortable, it will use this model by default.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class com.jidesoft.grid.SortableTableModel
SortableTableModel.ColumnComparatorContextProvider, SortableTableModel.SortOrderHandler
 
Nested classes/interfaces inherited from interface com.jidesoft.grid.ISortableTableModel
ISortableTableModel.SortItem
 
Field Summary
 TableModel _actualTableModel
           
static int SORTABLE_ALL_LEVELS
           
static int SORTABLE_LEAF_LEVEL
           
static int SORTABLE_NON_LEAF_LEVEL
           
static int SORTABLE_NON_ROOT_LEVEL
           
static int SORTABLE_NONE
           
static int SORTABLE_ROOT_LEVEL
           
 
Fields inherited from class com.jidesoft.grid.SortableTableModel
_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
SortableTreeTableModel(TableModel model)
           
 
Method Summary
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 rowIndex1, int rowIndex2, int column)
          Compares the two cell values at the two rows.
 int getDefaultSortableOption()
          Gets the default sortable option.
 Object getRoot()
          Gets the root expandable row which has the original rows as children.
 Row getRowAt(int rowIndex)
          Returns the row at row specified by row.
 int getRowIndex(T row)
          Gets the index of the row.
 int getSortableOption(int column)
          Gets the sortable option for the specified column.
 List<ISortableTableModel.SortItem> getSortingColumns()
          Gets the sorting columns.
protected  Object getValueAt(Row row, int column)
          Gets the value at the column in the Row
 void setDefaultSortableOption(int defaultSortableOption)
          Sets the sortable option.
 void setSortableOption(int column, int sortableOption)
          Sets the sortable option for the specified column.
protected  boolean shouldCompare(int sortableOption, Row row1, Row row2, boolean root, boolean leaf1, boolean leaf2, int column)
          In a tree, there are cases if two nodes should be compared.
protected  void sort()
          Sorts the table model.
 
Methods inherited from class com.jidesoft.grid.SortableTableModel
addSortListener, compare, fireSortEvent, fireSortingEvent, fireTableChanged, fireTableStructureChanged, getColumnComparatorContext, getColumnComparatorContextProvider, getColumnSortRank, getComparator, getMasterSortColumnIndex, getMasterSortColumns, getMaximumSortColumns, getPopupMenuItems, getSortableModel, getSortedRowAt, getSortListeners, getSortOrderHandler, getSortPriority, getSortRanges, insert, isAlwaysUseComparators, isAutoResort, isColumnAscending, isColumnSortable, isColumnSorted, isInSortRanges, isMultiColumnSortable, isOptimized, isResetOnTableStructureChangeEvent, isSortable, isSortingPaused, removeSortListener, reset, resort, reverseColumnSortOrder, search, setAlwaysUseComparators, setAutoResort, setColumnComparatorContextProvider, setColumnSortable, setIndexes, setMasterSortColumns, setMaximumSortColumns, setMultiColumnSortable, setOptimized, setResetOnTableStructureChangeEvent, setSortable, setSortingColumns, setSortingPaused, setSortOrderHandler, setSortPriority, shouldOptimize, shouldReverseIndices, sort, sortColumn, sortColumn, sortColumn, tableChanged, toggleSortOrder, unsortColumn
 
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

SORTABLE_NONE

public static final int SORTABLE_NONE
See Also:
Constant Field Values

SORTABLE_ROOT_LEVEL

public static final int SORTABLE_ROOT_LEVEL
See Also:
Constant Field Values

SORTABLE_LEAF_LEVEL

public static final int SORTABLE_LEAF_LEVEL
See Also:
Constant Field Values

SORTABLE_NON_ROOT_LEVEL

public static final int SORTABLE_NON_ROOT_LEVEL
See Also:
Constant Field Values

SORTABLE_NON_LEAF_LEVEL

public static final int SORTABLE_NON_LEAF_LEVEL
See Also:
Constant Field Values

SORTABLE_ALL_LEVELS

public static final int SORTABLE_ALL_LEVELS
See Also:
Constant Field Values

_actualTableModel

public TableModel _actualTableModel
Constructor Detail

SortableTreeTableModel

public SortableTreeTableModel(TableModel model)
Method Detail

cacheComparators

protected void cacheComparators()
Description copied from class: SortableTableModel
For performance, all comparators for each column should be cached first.

Overrides:
cacheComparators in class SortableTableModel

getSortingColumns

public List<ISortableTableModel.SortItem> getSortingColumns()
Description copied from class: SortableTableModel
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
Overrides:
getSortingColumns in class SortableTableModel
Returns:
an ArrayList of sorting columns.

sort

protected void sort()
Description copied from class: SortableTableModel
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().

Overrides:
sort in class SortableTableModel

compare

protected int compare(int row1,
                      int row2)
Description copied from class: SortableTableModel
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.

Overrides:
compare in class SortableTableModel
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 rowIndex1,
                   int rowIndex2,
                   int column)
Description copied from class: SortableTableModel
Compares the two cell values at the two rows.

Overrides:
compare in class SortableTableModel
Parameters:
rowIndex1 - index of the first row to be compared
rowIndex2 - 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.

shouldCompare

protected boolean shouldCompare(int sortableOption,
                                Row row1,
                                Row row2,
                                boolean root,
                                boolean leaf1,
                                boolean leaf2,
                                int column)
In a tree, there are cases if two nodes should be compared. First of all, no nodes should be compared if they do not belong to the same parent. Even if they belong to the same parent, you may not want to compare them if one is a leaf node and the other is not. Thus we introduced setDefaultSortableOption(int) and setSortableOption(int,int) method so that you can set a default sortable option or set different sortable option for each column.

Here is the default implementation.

 return (root && sortableOption == SORTABLE_ROOT_LEVEL)
  || (!root && sortableOption == SORTABLE_NON_ROOT_LEVEL)
  || (leaf1 && leaf2 && sortableOption == SORTABLE_LEAF_LEVEL)
  || (!leaf1 && !leaf2 && sortableOption == SORTABLE_NON_LEAF_LEVEL)
  || sortableOption == SORTABLE_ALL_LEVELS;
 
If returning false, we will not compare the rows when sorting. Understanding that this default implementation may not be good enough, we made this method protected so that you can subclass and provide your own way to decide if you want to compare the two rows.

Parameters:
sortableOption - the sortable option. If you didn't call setSortableOption(int,int) on any column, this value will be the defaultSortableOption.
row1 - the first row index
row2 - the second row index
root - whether the two nodes' parent is the root node.
leaf1 - whether the first node is a leaf node.
leaf2 - whether the second node is a leaf node.
column - the column that will be compared.
Returns:
true if the two rows should be compared. Otherwise false.

getValueAt

protected Object getValueAt(Row row,
                            int column)
Gets the value at the column in the Row

Parameters:
row - the row instance
column - the column index
Returns:
the value.

getDefaultSortableOption

public int getDefaultSortableOption()
Gets the default sortable option. The valid sortable options are SORTABLE_ALL_LEVELS, SORTABLE_LEAF_LEVEL,SORTABLE_ROOT_LEVEL or SORTABLE_NONE.

Returns:
the sortable option.

setDefaultSortableOption

public void setDefaultSortableOption(int defaultSortableOption)
Sets the sortable option. There are two different ways to set sortable option. This method set the default sortable option for all columns if setSortableOption(int,int) method is used called.

Parameters:
defaultSortableOption - Valid sortable options are SORTABLE_ALL_LEVELS, SORTABLE_LEAF_LEVEL,SORTABLE_ROOT_LEVEL or SORTABLE_NONE.

setSortableOption

public void setSortableOption(int column,
                              int sortableOption)
Sets the sortable option for the specified column.

Please note the option set using this method will be erased when table model's column count changes. So if you know the table model column count changes, you should have code to set the sortable option again.

Parameters:
column - the column index in table model. It's not the visual index but the model index.
sortableOption - Valid sortable options are SORTABLE_ALL_LEVELS, SORTABLE_LEAF_LEVEL,SORTABLE_ROOT_LEVEL, SORTABLE_NON_LEAF_LEVEL, SORTABLE_NON_ROOT_LEVEL or SORTABLE_NONE.

getSortableOption

public int getSortableOption(int column)
Gets the sortable option for the specified column. The valid sortable options are SORTABLE_ALL_LEVELS, SORTABLE_LEAF_LEVEL,SORTABLE_ROOT_LEVEL or SORTABLE_NONE. The sortable option set using this method will overwrite the value set by setDefaultSortableOption(int).

Parameters:
column - the column index in table model. It's not the visual index but the model index.
Returns:
the sortable option for the specified column.

append

protected int[] append(int firstRow,
                       int lastRow)
Overrides:
append in class SortableTableModel

getRowAt

public Row getRowAt(int rowIndex)
Returns the row at row specified by row.

Specified by:
getRowAt in interface ITreeTableModel<T extends Row>
Parameters:
rowIndex - the row whose row is to be queried
Returns:
the row at the specified row index

getRowIndex

public int getRowIndex(T row)
Gets the index of the row.

Specified by:
getRowIndex in interface ITreeTableModel<T extends Row>
Parameters:
row - row
Returns:
the index of the row. If the row is displayed in the table, it will return the index. Otherwise, it will return -1. So -1 could mean two things - the row is not displayed or the row is not in the tree hierarchy at all.

getRoot

public Object getRoot()
Description copied from interface: ITreeTableModel
Gets the root expandable row which has the original rows as children.

Specified by:
getRoot in interface ITreeTableModel<T extends Row>
Returns:
the root expandable row.

JIDE 3.5.15