Package adams.gui.core
Class SortableAndSearchableWrapperTableModel
- java.lang.Object
-
- javax.swing.table.AbstractTableModel
-
- adams.gui.core.SortableAndSearchableWrapperTableModel
-
- All Implemented Interfaces:
CustomSearchTableModel
,SearchableTableModel
,SortableTableModel
,TableModelWithColumnFilters
,Serializable
,EventListener
,TableModelListener
,TableModel
public class SortableAndSearchableWrapperTableModel extends AbstractTableModel implements TableModelListener, SortableTableModel, SearchableTableModel, CustomSearchTableModel, TableModelWithColumnFilters
Wraps around any table model and makes them automatically sortable and searchable.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SortableAndSearchableWrapperTableModel.SortContainer
Helper class for sorting the columns.
-
Field Summary
Fields Modifier and Type Field Description protected gnu.trove.map.hash.TIntObjectHashMap<String>
m_ColumnFilters
the filter strings per column.protected gnu.trove.map.hash.TIntObjectHashMap<Double>
m_ColumnFiltersNumeric
the numeric filter strings per column.protected gnu.trove.map.hash.TIntObjectHashMap<BaseRegExp>
m_ColumnFiltersRegExp
the filter regexps per column.protected boolean[]
m_ColumnIsNumeric
whether a column is numeric.protected gnu.trove.list.array.TIntArrayList
m_DisplayIndices
the indices of the rows to display that match a search string.protected boolean
m_IsComparableTableModel
the model is a comparable one.protected TableModel
m_Model
the actual table model.protected MouseListener
m_MouseListener
the mouse listener in use.protected boolean
m_RegExpSearch
whether the search was a regular expression based one.protected String
m_SearchString
the string that was searched for.protected boolean
m_SortAscending
whether sorting is ascending or descending.protected boolean
m_SortCaseSensitive
whether sorting is case-sensitive (default).protected int
m_SortColumn
the sort column.protected int[]
m_SortedIndices
the mapping between displayed and actual index.-
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
-
-
Constructor Summary
Constructors Constructor Description SortableAndSearchableWrapperTableModel()
initializes with no model.SortableAndSearchableWrapperTableModel(TableModel model)
initializes with the given model.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addMouseListenerToHeader(JTable table)
Adds a mouselistener to the header: left-click on the header sorts in ascending manner, using shift-left-click in descending manner.protected void
doSearchAndFilter()
Performs a search with the current search settings and applies column filters.void
fireTableStructureChanged()
Table structure changed.protected Comparable
fixCase(Comparable comp)
Fixes the case of strings passing through if not case-sensitive.int
getActualRow(int visibleRow)
Returns the actual underlying row the given visible one represents.int
getActualRowCount()
Returns the actual row count in the model.Class
getColumnClass(int columnIndex)
Returns the most specific superclass for all the cell values in the column.protected Class
getColumnClassForComparison(int columnIndex)
Returns the class of the column.int
getColumnCount()
Returns the number of columns in the model.String
getColumnFilter(int column)
Returns the filter for the column.String
getColumnName(int columnIndex)
Returns the name of the column at columnIndex.int
getDisplayRow(int internalRow)
Returns the "visible" row derived from row in the actual table model.MouseListener
getHeaderMouseListener()
Returns the mouse listener that got assigned to the header, if any.int
getRowCount()
Returns the number of rows in the model.String
getSeachString()
Returns the current search string.int
getSortColumn()
Returns the sort column.TableModel
getUnsortedModel()
returns the underlying model, can be null.Object
getValueAt(int rowIndex, int columnIndex)
Returns the value for the cell at columnIndex and rowIndex.protected Object
getValueForComparison(int row, int column)
Returns the value used in the comparison.protected void
initialize()
Initializes indices etc.protected void
initializeColumnTypes()
Determines which column is a numeric one (skipped in search).protected void
initializeSortIndices()
(re-)initializes the indices.boolean
isAnyColumnFiltered()
Returns whether there is at least one filter active.boolean
isAscending()
Returns whether sorting is ascending or not.boolean
isCaseSensitive()
Returns whether the sorting is case-sensitive.boolean
isCellEditable(int rowIndex, int columnIndex)
Returns true if the cell at rowIndex and columnIndex is editable.boolean
isColumnFiltered(int column)
Returns whether there is a filter active for the column.boolean
isColumnFilterRegExp(int column)
Returns whether the filter for the column is a regular expression.protected boolean
isColumnNumeric(int colIndex)
Returns whether the specified column is numeric.protected boolean
isInitialized()
whether the model is initialized.boolean
isRegExpSearch()
Returns whether the last search was a regular expression based one.boolean
isSearchMatch(SearchParameters params, int row)
Tests whether the search matches the specified row.boolean
isSorted()
returns whether the table was sorted.void
removeAllColumnFilters()
Removes all column filtersvoid
removeColumnFilter(int column)
Removes any filter for the column.void
search(String searchString, boolean regexp)
Performs a search for the given string.void
setCaseSensitive(boolean value)
Sets whether the sorting is case-sensitive.void
setColumnFilter(int column, String filter, boolean isRegExp)
Sets the filter for the column.void
setUnsortedModel(TableModel value)
Sets the model to use.void
setUnsortedModel(TableModel value, boolean restoreSorting)
Sets the model to use.void
setValueAt(Object aValue, int rowIndex, int columnIndex)
Sets the value in the cell at columnIndex and rowIndex to aValue.void
sort(int columnIndex)
sorts the table over the given column (ascending).void
sort(int columnIndex, boolean ascending)
sorts the table over the given column, either ascending or descending.void
tableChanged(TableModelEvent e)
This fine grain notification tells listeners the exact range of cells, rows, or columns that changed.-
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, 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 javax.swing.table.TableModel
addTableModelListener, removeTableModelListener
-
-
-
-
Field Detail
-
m_Model
protected TableModel m_Model
the actual table model.
-
m_SortedIndices
protected int[] m_SortedIndices
the mapping between displayed and actual index.
-
m_SortColumn
protected int m_SortColumn
the sort column.
-
m_SortAscending
protected boolean m_SortAscending
whether sorting is ascending or descending.
-
m_SortCaseSensitive
protected boolean m_SortCaseSensitive
whether sorting is case-sensitive (default).
-
m_MouseListener
protected MouseListener m_MouseListener
the mouse listener in use.
-
m_IsComparableTableModel
protected boolean m_IsComparableTableModel
the model is a comparable one.
-
m_SearchString
protected String m_SearchString
the string that was searched for.
-
m_RegExpSearch
protected boolean m_RegExpSearch
whether the search was a regular expression based one.
-
m_DisplayIndices
protected gnu.trove.list.array.TIntArrayList m_DisplayIndices
the indices of the rows to display that match a search string.
-
m_ColumnIsNumeric
protected boolean[] m_ColumnIsNumeric
whether a column is numeric.
-
m_ColumnFilters
protected gnu.trove.map.hash.TIntObjectHashMap<String> m_ColumnFilters
the filter strings per column.
-
m_ColumnFiltersNumeric
protected gnu.trove.map.hash.TIntObjectHashMap<Double> m_ColumnFiltersNumeric
the numeric filter strings per column.
-
m_ColumnFiltersRegExp
protected gnu.trove.map.hash.TIntObjectHashMap<BaseRegExp> m_ColumnFiltersRegExp
the filter regexps per column.
-
-
Constructor Detail
-
SortableAndSearchableWrapperTableModel
public SortableAndSearchableWrapperTableModel()
initializes with no model.
-
SortableAndSearchableWrapperTableModel
public SortableAndSearchableWrapperTableModel(TableModel model)
initializes with the given model.- Parameters:
model
- the model to initialize the sorted model with
-
-
Method Detail
-
setUnsortedModel
public void setUnsortedModel(TableModel value)
Sets the model to use. Discards any sorting.- Specified by:
setUnsortedModel
in interfaceSortableTableModel
- Parameters:
value
- the model to use
-
setUnsortedModel
public void setUnsortedModel(TableModel value, boolean restoreSorting)
Sets the model to use.- Parameters:
value
- the model to userestoreSorting
- whether to restore the sorting (if any)
-
getUnsortedModel
public TableModel getUnsortedModel()
returns the underlying model, can be null.- Specified by:
getUnsortedModel
in interfaceSortableTableModel
- Returns:
- the current model
-
initialize
protected void initialize()
Initializes indices etc.
-
initializeColumnTypes
protected void initializeColumnTypes()
Determines which column is a numeric one (skipped in search).
-
isColumnNumeric
protected boolean isColumnNumeric(int colIndex)
Returns whether the specified column is numeric.- Parameters:
colIndex
- the index of the column- Returns:
- true if numeric
- See Also:
m_ColumnIsNumeric
,initializeColumnTypes()
-
initializeSortIndices
protected void initializeSortIndices()
(re-)initializes the indices.
-
setCaseSensitive
public void setCaseSensitive(boolean value)
Sets whether the sorting is case-sensitive.- Specified by:
setCaseSensitive
in interfaceSortableTableModel
- Parameters:
value
- true if case-sensitive
-
isCaseSensitive
public boolean isCaseSensitive()
Returns whether the sorting is case-sensitive.- Specified by:
isCaseSensitive
in interfaceSortableTableModel
- Returns:
- true if case-sensitive
-
isSorted
public boolean isSorted()
returns whether the table was sorted.- Specified by:
isSorted
in interfaceSortableTableModel
- Returns:
- true if the table was sorted
-
getSortColumn
public int getSortColumn()
Returns the sort column.- Specified by:
getSortColumn
in interfaceSortableTableModel
- Returns:
- the sort column
-
isAscending
public boolean isAscending()
Returns whether sorting is ascending or not.- Specified by:
isAscending
in interfaceSortableTableModel
- Returns:
- true if ascending
- See Also:
isSorted()
,getSortColumn()
-
isInitialized
protected boolean isInitialized()
whether the model is initialized.- Returns:
- true if the model is not null and the sort indices match the number of rows
-
getActualRow
public int getActualRow(int visibleRow)
Returns the actual underlying row the given visible one represents. Useful for retrieving "non-visual" data that is also stored in a TableModel.- Specified by:
getActualRow
in interfaceSearchableTableModel
- Specified by:
getActualRow
in interfaceSortableTableModel
- Parameters:
visibleRow
- the displayed row to retrieve the original row for- Returns:
- the original row
-
getDisplayRow
public int getDisplayRow(int internalRow)
Returns the "visible" row derived from row in the actual table model.- Specified by:
getDisplayRow
in interfaceSortableTableModel
- Parameters:
internalRow
- the row in the actual model- Returns:
- the row in the sorted model, -1 in case of an error
-
getColumnClass
public Class getColumnClass(int columnIndex)
Returns the most specific superclass for all the cell values in the column.- Specified by:
getColumnClass
in interfaceTableModel
- Overrides:
getColumnClass
in classAbstractTableModel
- Parameters:
columnIndex
- the index of the column- Returns:
- the class of the specified column
-
getColumnCount
public int getColumnCount()
Returns the number of columns in the model.- Specified by:
getColumnCount
in interfaceTableModel
- Returns:
- the number of columns in the model
-
getColumnName
public String getColumnName(int columnIndex)
Returns the name of the column at columnIndex.- Specified by:
getColumnName
in interfaceTableModel
- Overrides:
getColumnName
in classAbstractTableModel
- Parameters:
columnIndex
- the column to retrieve the name for- Returns:
- the name of the specified column
-
getRowCount
public int getRowCount()
Returns the number of rows in the model.- Specified by:
getRowCount
in interfaceTableModel
- Returns:
- the number of rows in the model
-
getValueAt
public Object getValueAt(int rowIndex, int columnIndex)
Returns the value for the cell at columnIndex and rowIndex.- Specified by:
getValueAt
in interfaceTableModel
- Parameters:
rowIndex
- the rowcolumnIndex
- the column- Returns:
- the value of the sepcified cell
-
isCellEditable
public boolean isCellEditable(int rowIndex, int columnIndex)
Returns true if the cell at rowIndex and columnIndex is editable.- Specified by:
isCellEditable
in interfaceTableModel
- Overrides:
isCellEditable
in classAbstractTableModel
- Parameters:
rowIndex
- the rowcolumnIndex
- the column- Returns:
- true if the cell is editable
-
setValueAt
public void setValueAt(Object aValue, int rowIndex, int columnIndex)
Sets the value in the cell at columnIndex and rowIndex to aValue.- Specified by:
setValueAt
in interfaceTableModel
- Overrides:
setValueAt
in classAbstractTableModel
- Parameters:
aValue
- the new value of the cellrowIndex
- the rowcolumnIndex
- the column
-
getColumnClassForComparison
protected Class getColumnClassForComparison(int columnIndex)
Returns the class of the column.- Parameters:
columnIndex
- the index of the column- Returns:
- the class of the column
-
sort
public void sort(int columnIndex)
sorts the table over the given column (ascending).- Specified by:
sort
in interfaceSortableTableModel
- Parameters:
columnIndex
- the column to sort over
-
fixCase
protected Comparable fixCase(Comparable comp)
Fixes the case of strings passing through if not case-sensitive.- Parameters:
comp
- the comparable to check- Returns:
- the (potentially) updated comparable
-
sort
public void sort(int columnIndex, boolean ascending)
sorts the table over the given column, either ascending or descending.- Specified by:
sort
in interfaceSortableTableModel
- Parameters:
columnIndex
- the column to sort overascending
- ascending if true, otherwise descending
-
getValueForComparison
protected Object getValueForComparison(int row, int column)
Returns the value used in the comparison.- Parameters:
row
- the row of the cellcolumn
- the column of the cell- Returns:
- the cell value
-
tableChanged
public void tableChanged(TableModelEvent e)
This fine grain notification tells listeners the exact range of cells, rows, or columns that changed.- Specified by:
tableChanged
in interfaceTableModelListener
- Parameters:
e
- the event
-
addMouseListenerToHeader
public void addMouseListenerToHeader(JTable table)
Adds a mouselistener to the header: left-click on the header sorts in ascending manner, using shift-left-click in descending manner.- Parameters:
table
- the table to add the listener to
-
getHeaderMouseListener
public MouseListener getHeaderMouseListener()
Returns the mouse listener that got assigned to the header, if any.- Returns:
- the listener, can be null
-
getActualRowCount
public int getActualRowCount()
Returns the actual row count in the model.- Specified by:
getActualRowCount
in interfaceSearchableTableModel
- Returns:
- the row count in the underlying data
-
isSearchMatch
public boolean isSearchMatch(SearchParameters params, int row)
Tests whether the search matches the specified row.
Default implementation just checks against the strings that getValueAt(...) returns (using the toString() method of the returned objects). Derived classes should override this method in order to implement a proper/faster search functionality. Skips numeric columns.- Specified by:
isSearchMatch
in interfaceCustomSearchTableModel
- Parameters:
params
- the search parametersrow
- the row of the underlying, unsorted model- Returns:
- true if the search matches this row
-
search
public void search(String searchString, boolean regexp)
Performs a search for the given string. Limits the display of rows to ones containing the search string.- Specified by:
search
in interfaceSearchableTableModel
- Parameters:
searchString
- the string to search forregexp
- whether to perform regular expression matching or just plain string comparison
-
doSearchAndFilter
protected void doSearchAndFilter()
Performs a search with the current search settings and applies column filters.
-
getSeachString
public String getSeachString()
Returns the current search string.- Specified by:
getSeachString
in interfaceSearchableTableModel
- Returns:
- the search string, null if not filtered
-
isRegExpSearch
public boolean isRegExpSearch()
Returns whether the last search was a regular expression based one.- Specified by:
isRegExpSearch
in interfaceSearchableTableModel
- Returns:
- true if last search was a reg exp one
-
setColumnFilter
public void setColumnFilter(int column, String filter, boolean isRegExp)
Sets the filter for the column.- Specified by:
setColumnFilter
in interfaceTableModelWithColumnFilters
- Parameters:
column
- the column to filterfilter
- the filter stringisRegExp
- whether the filter is a regular expression
-
getColumnFilter
public String getColumnFilter(int column)
Returns the filter for the column.- Specified by:
getColumnFilter
in interfaceTableModelWithColumnFilters
- Parameters:
column
- the column to query- Returns:
- the filter, null if none present
-
isColumnFilterRegExp
public boolean isColumnFilterRegExp(int column)
Returns whether the filter for the column is a regular expression.- Specified by:
isColumnFilterRegExp
in interfaceTableModelWithColumnFilters
- Parameters:
column
- the column to query- Returns:
- true if filter set and regular expression
-
isColumnFiltered
public boolean isColumnFiltered(int column)
Returns whether there is a filter active for the column.- Specified by:
isColumnFiltered
in interfaceTableModelWithColumnFilters
- Parameters:
column
- the column to query- Returns:
- true if a filter is active
-
isAnyColumnFiltered
public boolean isAnyColumnFiltered()
Returns whether there is at least one filter active.- Specified by:
isAnyColumnFiltered
in interfaceTableModelWithColumnFilters
- Returns:
- true if at least one filter is active
-
removeColumnFilter
public void removeColumnFilter(int column)
Removes any filter for the column.- Specified by:
removeColumnFilter
in interfaceTableModelWithColumnFilters
- Parameters:
column
- the column to update
-
removeAllColumnFilters
public void removeAllColumnFilters()
Removes all column filters- Specified by:
removeAllColumnFilters
in interfaceTableModelWithColumnFilters
-
fireTableStructureChanged
public void fireTableStructureChanged()
Table structure changed.- Overrides:
fireTableStructureChanged
in classAbstractTableModel
-
-