Package docking.widgets.table
Class AbstractSortedTableModel<T>
- java.lang.Object
-
- javax.swing.table.AbstractTableModel
-
- docking.widgets.table.AbstractGTableModel<T>
-
- docking.widgets.table.AbstractSortedTableModel<T>
-
- Type Parameters:
T- The row type upon which the table is based
- All Implemented Interfaces:
RowObjectTableModel<T>,SelectionStorage<T>,SortedTableModel,java.io.Serializable,javax.swing.table.TableModel
- Direct Known Subclasses:
DynamicTableModel,GDynamicColumnTableModel,LanguageSortedTableModel
public abstract class AbstractSortedTableModel<T> extends AbstractGTableModel<T> implements SortedTableModel
Table models should extends this model when they want sorting, potentially across multiple columns, but do not want Threading or do not work on Program-related data (Address, ProgramLocations, etc...).In order to define custom comparators for a column, simply override
createSortComparator(int). Otherwise, a default comparator will be created for you.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface docking.widgets.table.SortedTableModel
SortedTableModel.DefaultComparator
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanhasEverSorted-
Fields inherited from class docking.widgets.table.AbstractGTableModel
WIDTH_UNDEFINED
-
Fields inherited from interface docking.widgets.table.SortedTableModel
ASCENDING_ORDER, DEFAULT_COMPARATOR, DESCENDING_ORDER
-
-
Constructor Summary
Constructors Constructor Description AbstractSortedTableModel()AbstractSortedTableModel(int defaultSortColumn)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSortListener(SortListener l)Adds a listener to be notified when the sort state of this model changes.protected java.util.Comparator<T>createSortComparator(int columnIndex)An extension point for subclasses to insert their own comparator objects for their data.protected TableSortingContext<T>createSortingContext(TableSortState newSortState)voidfireTableChanged(javax.swing.event.TableModelEvent e)protected intgetIndexForRowObject(T rowObject)A convenience method for subclasses to quickly/efficiently search for the index of a given row object that is visible in the GUI.protected intgetIndexForRowObject(T rowObject, java.util.List<T> data)TableSortStategetPendingSortState()intgetPrimarySortColumnIndex()intgetRowIndex(T rowObject)Returns the index of the given row object in this model; -1 if the model does not contain the given object.TgetRowObject(int viewRow)Returns the corresponding object for the given row.TableSortStategetTableSortState()java.lang.ObjectgetValueAt(int rowIndex, int columnIndex)The default implementation ofTableModel.getValueAt(int, int)that calls the abstractRowObjectTableModel.getColumnValueForRow(Object, int).protected voidinitializeSorting()This method is an attempt to help models that forget to call fireTableDataChanged().booleanisSortPending()protected voidnotifyModelSorted(boolean dataChanged)Fires an event to let the listeners (like JTable) know that things have been changed.protected voidreSort()protected voidsetDefaultTableSortState(TableSortState defaultSortState)voidsetTableSortState(TableSortState newSortState)protected voidsort(java.util.List<T> data, TableSortingContext<T> sortingContext)A default sort method that uses theCollections.sort(List, Comparator)method for sorting.protected voidsortCompleted(TableSortingContext<T> sortingContext)-
Methods inherited from class docking.widgets.table.AbstractGTableModel
dispose, getLastSelectedObjects, getPreferredColumnWidth, getRowCount, refresh, setLastSelectedObjects
-
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getColumnName, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface docking.widgets.table.RowObjectTableModel
fireTableDataChanged, getColumnValueForRow, getModelData, getName
-
Methods inherited from interface docking.widgets.table.SortedTableModel
isSortable
-
-
-
-
Method Detail
-
setDefaultTableSortState
protected void setDefaultTableSortState(TableSortState defaultSortState)
-
addSortListener
public void addSortListener(SortListener l)
Description copied from interface:SortedTableModelAdds a listener to be notified when the sort state of this model changes.
Note: the listener may be stored in a weak collection, which means you have to maintain a handle to the listener so that it does not get garbage collected.- Specified by:
addSortListenerin interfaceSortedTableModel- Parameters:
l- the listener
-
getRowObject
public T getRowObject(int viewRow)
Returns the corresponding object for the given row.- Specified by:
getRowObjectin interfaceRowObjectTableModel<T>- Overrides:
getRowObjectin classAbstractGTableModel<T>- Parameters:
viewRow- The row for which to get the row object.- Returns:
- the row object.
-
getRowIndex
public int getRowIndex(T rowObject)
Returns the index of the given row object in this model; -1 if the model does not contain the given object.- Specified by:
getRowIndexin interfaceRowObjectTableModel<T>- Overrides:
getRowIndexin classAbstractGTableModel<T>- Parameters:
rowObject- the object- Returns:
- the row number
-
fireTableChanged
public void fireTableChanged(javax.swing.event.TableModelEvent e)
- Overrides:
fireTableChangedin classjavax.swing.table.AbstractTableModel
-
reSort
protected void reSort()
-
getTableSortState
public TableSortState getTableSortState()
- Specified by:
getTableSortStatein interfaceSortedTableModel
-
getPrimarySortColumnIndex
public int getPrimarySortColumnIndex()
- Specified by:
getPrimarySortColumnIndexin interfaceSortedTableModel
-
setTableSortState
public void setTableSortState(TableSortState newSortState)
- Specified by:
setTableSortStatein interfaceSortedTableModel
-
getPendingSortState
public TableSortState getPendingSortState()
-
isSortPending
public boolean isSortPending()
-
createSortingContext
protected TableSortingContext<T> createSortingContext(TableSortState newSortState)
-
getValueAt
public java.lang.Object getValueAt(int rowIndex, int columnIndex)The default implementation ofTableModel.getValueAt(int, int)that calls the abstractRowObjectTableModel.getColumnValueForRow(Object, int).- Specified by:
getValueAtin interfacejavax.swing.table.TableModel- Overrides:
getValueAtin classAbstractGTableModel<T>
-
initializeSorting
protected void initializeSorting()
This method is an attempt to help models that forget to call fireTableDataChanged(). It is expected that tables will fire the notification when they are ready to display data, even if they have that data at construction time. We put this call here so that the forgetful subclasses will have their data sorted for them the first time that this table tries to render itself.
-
getIndexForRowObject
protected int getIndexForRowObject(T rowObject)
A convenience method for subclasses to quickly/efficiently search for the index of a given row object that is visible in the GUI. The visible limitation is due to the fact that the data searched is retrieved fromRowObjectTableModel.getModelData(), which may be filtered.If a need for access to all of the data is required in the future, then an overloaded version of this method should be created that takes the data to be searched.
- Overrides:
getIndexForRowObjectin classAbstractGTableModel<T>- Parameters:
rowObject- The object for which to search.- Returns:
- the index of the item in the data returned by
-
getIndexForRowObject
protected int getIndexForRowObject(T rowObject, java.util.List<T> data)
- Overrides:
getIndexForRowObjectin classAbstractGTableModel<T>
-
sort
protected void sort(java.util.List<T> data, TableSortingContext<T> sortingContext)
A default sort method that uses theCollections.sort(List, Comparator)method for sorting. Implementors with reasonably sized data sets can rely on this method. For data sets that can become large, the ThreadedTableModel is the recommended base class, as it handles loading/sorting/filtering in a threaded way.- Parameters:
data- The data to be sortedsortingContext- The context required to sort (it contains the sorting columns, a comparator for sorting, etc...).
-
sortCompleted
protected void sortCompleted(TableSortingContext<T> sortingContext)
-
notifyModelSorted
protected void notifyModelSorted(boolean dataChanged)
Fires an event to let the listeners (like JTable) know that things have been changed. This method exists so that subclasses have a way to call the various tableChanged() methods without triggering this class's overridden version.- Parameters:
dataChanged- True signals that the actual data has changed; false signals that the data is the same, with exception that attributes of that data may be different.
-
createSortComparator
protected java.util.Comparator<T> createSortComparator(int columnIndex)
An extension point for subclasses to insert their own comparator objects for their data. Subclasses can create comparators for a single or multiple columns, as desired. TheAbstractSortedTableModel.DefaultColumnComparatoris used as a, well, default comparator.- Parameters:
columnIndex- the column index for which a comparator is desired.- Returns:
- a comparator for the given index.
-
-