qflib 0.99

de.qfs.lib.gui
Class FilteredAndSortedTableModel

java.lang.Object
  extended byde.qfs.lib.gui.TableModelAdapter
      extended byde.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 setAlwaysSort, getMappedRow, getAddedRows and clearAddedRows.

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 indices.
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 isAlwaysSort()
          Get the alwaysSort state of the FilteredAndSortedTableModel.
 boolean isCellEditable(int row, int column)
          Get the number of columns in the table.
 void setAlwaysSort(boolean alwaysSort)
          Set whether the table should sort rows automatically after each change.
 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 indices. 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.

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.

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.

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.

isAlwaysSort

public final boolean isAlwaysSort()
Get the alwaysSort state of the FilteredAndSortedTableModel.

Returns:
The alwaysSort state of the FilteredAndSortedTableModel.

setAlwaysSort

public final void setAlwaysSort(boolean alwaysSort)
Set whether the table should sort rows automatically after each change. This can be confusing if cells are editable as the edited row can be moved away right after finishing editing.

If this is set to false, changing the table by editing a cell or adding rows will automatically set the sort order to -1 and leave the table in the current state until the sort order or the filter setting is changed again.

The default is to sort automatically.

Parameters:
alwaysSort - The alwaysSort state to set.

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Return an attribute value for a cell.

Specified by:
getValueAt in interface javax.swing.table.TableModel
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.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class TableModelAdapter
Parameters:
aValue - 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.

Specified by:
getRowCount in interface javax.swing.table.TableModel
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.

Specified by:
isCellEditable in interface javax.swing.table.TableModel
Overrides:
isCellEditable 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 number of columns.
See Also:
TableModelAdapter.setValueAt(java.lang.Object, int, int)

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.

Specified by:
tableChanged in interface javax.swing.event.TableModelListener
Overrides:
tableChanged in class TableModelAdapter
Parameters:
e - 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.99