Class SortableAndSearchableWrapperTableModel

    • 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,
                                     boolean restoreSorting)
        Sets the model to use.
        Parameters:
        value - the model to use
        restoreSorting - whether to restore the sorting (if any)
      • 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 interface SortableTableModel
        Parameters:
        value - true if case-sensitive
      • isCaseSensitive

        public boolean isCaseSensitive()
        Returns whether the sorting is case-sensitive.
        Specified by:
        isCaseSensitive in interface SortableTableModel
        Returns:
        true if case-sensitive
      • isSorted

        public boolean isSorted()
        returns whether the table was sorted.
        Specified by:
        isSorted in interface SortableTableModel
        Returns:
        true if the table was sorted
      • getSortColumn

        public int getSortColumn()
        Returns the sort column.
        Specified by:
        getSortColumn in interface SortableTableModel
        Returns:
        the sort column
      • 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 interface SearchableTableModel
        Specified by:
        getActualRow in interface SortableTableModel
        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 interface SortableTableModel
        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 interface TableModel
        Overrides:
        getColumnClass in class AbstractTableModel
        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 interface TableModel
        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 interface TableModel
        Overrides:
        getColumnName in class AbstractTableModel
        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 interface TableModel
        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 interface TableModel
        Parameters:
        rowIndex - the row
        columnIndex - 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 interface TableModel
        Overrides:
        isCellEditable in class AbstractTableModel
        Parameters:
        rowIndex - the row
        columnIndex - 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 interface TableModel
        Overrides:
        setValueAt in class AbstractTableModel
        Parameters:
        aValue - the new value of the cell
        rowIndex - the row
        columnIndex - 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 interface SortableTableModel
        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 interface SortableTableModel
        Parameters:
        columnIndex - the column to sort over
        ascending - 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 cell
        column - 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 interface TableModelListener
        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 interface SearchableTableModel
        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 interface CustomSearchTableModel
        Parameters:
        params - the search parameters
        row - 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 interface SearchableTableModel
        Parameters:
        searchString - the string to search for
        regexp - 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.
      • isRegExpSearch

        public boolean isRegExpSearch()
        Returns whether the last search was a regular expression based one.
        Specified by:
        isRegExpSearch in interface SearchableTableModel
        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 interface TableModelWithColumnFilters
        Parameters:
        column - the column to filter
        filter - the filter string
        isRegExp - whether the filter is a regular expression
      • getColumnFilter

        public String getColumnFilter​(int column)
        Returns the filter for the column.
        Specified by:
        getColumnFilter in interface TableModelWithColumnFilters
        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 interface TableModelWithColumnFilters
        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 interface TableModelWithColumnFilters
        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 interface TableModelWithColumnFilters
        Returns:
        true if at least one filter is active