qflib 0.98.1

de.qfs.lib.gui
Class FilteredAndSortedTableModel

java.lang.Object
  |
  +--de.qfs.lib.gui.TableModelAdapter
        |
        +--de.qfs.lib.gui.FilteredAndSortedTableModel
All Implemented Interfaces:
java.util.EventListener, javax.swing.table.TableModel, TableModelFilterListener, javax.swing.event.TableModelListener, TableModelSorterListener

public class FilteredAndSortedTableModel
extends TableModelAdapter
implements TableModelSorterListener, TableModelFilterListener

A TableModelAdapter that enables filtering and sorting.

This is the class that does all the hard work needed to implement tables with filter and sort mechanisms. You will rarely need to use this class directly as the SortedTableHelper provides everything that is necessary to set up filtered and sorted table.

The methods of interest to end users are getMappedRow, getAddedRows and clearAddedRows.

Version:
$Revision: 1.18 $
Author:
Gregor Schmid

Field Summary
protected  TableModelFilter filter
          The filter for the data.
 java.util.List indexMap
          A list of indexes into the original TableModel.
protected  boolean mustSort
          Whether indexMap must be sorted before data can be accessed.
 int reverseExtra
          The extra space the reverseMap was built with.
 int[] reverseMap
          An array mapping original TableModel indices to mapped inices.
protected  TableModelSorter sorter
          The sorter for the data.
protected  de.qfs.lib.gui.FilteredAndSortedTableModel.SorterCompare sorterCompare
          The sorter as Comparator.
 
Fields inherited from class de.qfs.lib.gui.TableModelAdapter
listeners, model
 
Constructor Summary
FilteredAndSortedTableModel(javax.swing.table.TableModel model)
          Construct a new FilteredAndSortedTableModel without filter and sorter.
FilteredAndSortedTableModel(javax.swing.table.TableModel model, TableModelFilter filter, TableModelSorter sorter)
          Construct a new FilteredAndSortedTableModel.
 
Method Summary
 void clearAddedRows()
          Reset the buffer for added rows.
 void filterChanged(TableModelFilterEvent event)
          Notify the FilteredAndSortedTableModel of a change in the TableModelFilter.
 int[] getAddedRows()
          Due to filtering and sorting, the result of adding rows to a TableModel that is wrapped by a FilteredAndSortedTableModel is hardly predictable.
 TableModelFilter getFilter()
          Get the filter for the data.
 int getMappedRow(int row)
          Get the mapped row index for the underlying TableModel.
 int getRowCount()
          Get the number of rows in the table.
 TableModelSorter getSorter()
          Get the TableModelSorter for the data.
 java.lang.Object getValueAt(int row, int column)
          Return an attribute value for a cell.
 boolean isCellEditable(int row, int column)
          Get the number of columns in the table.
 void setFilter(TableModelFilter filter)
          Set the filter for the data.
 void setSavedSelectionModel(javax.swing.ListSelectionModel model)
          The FilteredAndSortedTableModel can keep track of the selection state in one ListSelectionModel and keep it up to date during changes to the underlying TableModel.
 void setSorter(TableModelSorter sorter)
          Set the TableModelSorter for the data.
 void setValueAt(java.lang.Object aValue, int row, int column)
          Set the attribute value for a cell.
 void sortOrderChanged(TableModelSorterEvent e)
          Notify the FilteredAndSortedTableModel of a change in the sort order.
 void tableChanged(javax.swing.event.TableModelEvent e)
          Called, when the wrapped model has changed in some way.
 
Methods inherited from class de.qfs.lib.gui.TableModelAdapter
addTableModelListener, getColumnClass, getColumnCount, getColumnName, getModel, removeTableModelListener, setModel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

filter

protected TableModelFilter filter
The filter for the data.

sorter

protected TableModelSorter sorter
The sorter for the data.

sorterCompare

protected de.qfs.lib.gui.FilteredAndSortedTableModel.SorterCompare sorterCompare
The sorter as Comparator.

indexMap

public java.util.List indexMap
A list of indexes into the original TableModel.

mustSort

protected boolean mustSort
Whether indexMap must be sorted before data can be accessed.

reverseMap

public int[] reverseMap
An array mapping original TableModel indices to mapped inices. Only used temporarily, not updated regularly.

reverseExtra

public int reverseExtra
The extra space the reverseMap was built with.
Constructor Detail

FilteredAndSortedTableModel

public FilteredAndSortedTableModel(javax.swing.table.TableModel model)
Construct a new FilteredAndSortedTableModel without filter and sorter.
Parameters:
model - The TableModel to wrap.

FilteredAndSortedTableModel

public FilteredAndSortedTableModel(javax.swing.table.TableModel model,
                                   TableModelFilter filter,
                                   TableModelSorter sorter)
Construct a new FilteredAndSortedTableModel.
Parameters:
model - The TableModel to wrap.
filter - The TableModelFilter for the data.
sorter - The TableModelSorter that imposes the order on the data.
Method Detail

getFilter

public final TableModelFilter getFilter()
Get the filter for the data.
Returns:
The filter.

setFilter

public void setFilter(TableModelFilter filter)
Set the filter for the data. This invalidates the mapping of table rows and causes a complete rebuild.
Parameters:
filter - The filter to set.

sortOrderChanged

public void sortOrderChanged(TableModelSorterEvent e)
Notify the FilteredAndSortedTableModel of a change in the sort order.
Specified by:
sortOrderChanged in interface TableModelSorterListener
Parameters:
e - The event holding the details.

getSorter

public final TableModelSorter getSorter()
Get the TableModelSorter for the data.
Returns:
The TableModelSorter.

setSorter

public void setSorter(TableModelSorter sorter)
Set the TableModelSorter for the data. This invalidates the row mapping and causes a full sort.
Parameters:
sorter - The TableModelSorter to set.

filterChanged

public void filterChanged(TableModelFilterEvent event)
Notify the FilteredAndSortedTableModel of a change in the TableModelFilter.
Specified by:
filterChanged in interface TableModelFilterListener
Parameters:
event - Details about the change.

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Return an attribute value for a cell.
Overrides:
getValueAt in class TableModelAdapter
Parameters:
row - The row whose value is to be looked up.
column - The column whose value is to be looked up.
Returns:
The value Object at the specified cell.

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int row,
                       int column)
Set the attribute value for a cell.
Overrides:
setValueAt in class TableModelAdapter
Parameters:
value - The value to set.
row - The row whose value is to be set.
column - The column whose value is to be set.

getRowCount

public int getRowCount()
Get the number of rows in the table.
Overrides:
getRowCount in class TableModelAdapter
Returns:
The number of rows.

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Get the number of columns in the table.
Overrides:
isCellEditable in class TableModelAdapter
Returns:
The number of columns.

tableChanged

public void tableChanged(javax.swing.event.TableModelEvent e)
Called, when the wrapped model has changed in some way. Forward the event to all registered TableModelListeners.
Overrides:
tableChanged in class TableModelAdapter
Parameters:
event - The event that happened.

getMappedRow

public int getMappedRow(int row)
Get the mapped row index for the underlying TableModel.
Parameters:
row - The index in the FilteredAndSortedTableModel.
Returns:
The corresponding index in the wrapped TableModel or -1, if row is not a valid index.

getAddedRows

public int[] getAddedRows()
Due to filtering and sorting, the result of adding rows to a TableModel that is wrapped by a FilteredAndSortedTableModel is hardly predictable. This method queries the FilteredAndSortedTableModel for the rows that have been added since its creation or the last call to one of clearAddedRows, setSorter, sortOrderChanged, setFilter or filterChanged. This method does NOT clear that state.
Returns:
An integer array containing the indices of the added rows.

clearAddedRows

public void clearAddedRows()
Reset the buffer for added rows.

setSavedSelectionModel

public void setSavedSelectionModel(javax.swing.ListSelectionModel model)
The FilteredAndSortedTableModel can keep track of the selection state in one ListSelectionModel and keep it up to date during changes to the underlying TableModel.
Parameters:
model - The ListSelectionModel whose selection to save.

qflib 0.98.1