de.tkuhn.swing.table
Class RowSortingTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by de.tkuhn.swing.table.RowSortingTableModel
All Implemented Interfaces:
java.io.Serializable, javax.swing.table.TableModel

public class RowSortingTableModel
extends javax.swing.table.AbstractTableModel
implements javax.swing.table.TableModel

This Class is a decorator wrapper for TableColumnModels that enhances them wiht the ability of sorting the rows according to the values of a column in ascending or descending order. In addition it provides a means for storing and resetting the state of the sort order. If you use JUserFriendlyTable you should not need to use this class.

Version:
0.9.3
Author:
Tilmann Kuhn
See Also:
JUserFriendlyTable, TableColumnModel, Serialized Form

Field Summary
static boolean ASCENDING
          Value for sort() in which order data should be sorted
static boolean DESCENDING
          Value for sort() in which order data should be sorted
static int NO_COLUMN
          Value for sort() if data should not be sorted
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
RowSortingTableModel()
          Creates a new instance of RowSortingTableModel
RowSortingTableModel(javax.swing.table.TableModel model)
          Creates a new instance of RowSortingTableModel that wrapps the given model
 
Method Summary
static javax.swing.JTable enableRowSorting(javax.swing.JTable table)
          Convenience method for enabling row sorting for a given table.
 java.lang.Class<?> getColumnClass(int columnIndex)
          Implements getColumnClass(col) in the interface TableModel.
 int getColumnCount()
          Implements getColumnCount() in the interface TableModel.
 java.lang.String getColumnName(int columnIndex)
          Implements getColumnName(col) in the interface TableModel.
 javax.swing.table.TableModel getModel()
          Get the TableModel decorated by this wrapper.
 int getModelRow(int row)
          Return the row number of the original model for the sorted row number.
 int getRowCount()
          Implements getRowCount() in the interface TableModel.
 java.lang.Object getState()
          Creates a Memento of the state of this wrapper.
 java.lang.Object getValueAt(int rowIndex, int columnIndex)
          Implements getValueAt(row,col) in the interface TableModel.
 void installHeaderExtension(javax.swing.JTable aTable)
          Install the header extension for event listening at the given table.
 boolean isCellEditable(int rowIndex, int columnIndex)
          Implements isCellEditable(row,col) in the interface TableModel.
 void setModel(javax.swing.table.TableModel aModel)
          Set a new TableModel in this decorator and notify all TableModelListeners.
static void setNewTableModel(javax.swing.JTable table, javax.swing.table.TableModel model)
          Set a new TableModel at a table you have enabled row sorting before using enableRowSorting(table).
 boolean setState(java.lang.Object aState)
          Resets this wrapper to the given state if the column structure is similar to the one present while getState() was called.
 void setupTableForRowSorting(javax.swing.JTable aTable)
          Registers this RowSortingTableModel at the given table by setting itself as its new model and wrapping the table's old model.
 void setValueAt(java.lang.Object aValue, int rowIndex, int columnIndex)
          Implements getColumnCount() in the interface TableModel.
 void sort(int columnIndex, boolean order)
          Have the decorator sort the rows by values of given column in given order.
 void tearDownTableFromRowSorting(javax.swing.JTable aTable)
          Remove row sorting from a table at which it was previously installed using setupTableForRowSorting(table) by setting the table's model to its old model again and removing the header extension for event listening
 void uninstallHeaderExtension(javax.swing.JTable aTable)
          Remove the header extension for event listening from the given table.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, 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

NO_COLUMN

public static final int NO_COLUMN
Value for sort() if data should not be sorted

See Also:
Constant Field Values

ASCENDING

public static final boolean ASCENDING
Value for sort() in which order data should be sorted

See Also:
Constant Field Values

DESCENDING

public static final boolean DESCENDING
Value for sort() in which order data should be sorted

See Also:
Constant Field Values
Constructor Detail

RowSortingTableModel

public RowSortingTableModel()
Creates a new instance of RowSortingTableModel


RowSortingTableModel

public RowSortingTableModel(javax.swing.table.TableModel model)
Creates a new instance of RowSortingTableModel that wrapps the given model

Parameters:
model - the model that will be decorated
Method Detail

enableRowSorting

public static javax.swing.JTable enableRowSorting(javax.swing.JTable table)
Convenience method for enabling row sorting for a given table. After you have done this you should not set a new TableModel at the table directly if you don't want to lose the sorting capability. Use setNewTableModel(..) instead. Also getModel() on the table will not return the original model of the table but an instance of RowSortingTableModel.

Parameters:
table - the table to enable row sorting for
Returns:
the same table
See Also:
setNewTableModel(javax.swing.JTable, javax.swing.table.TableModel)

setNewTableModel

public static void setNewTableModel(javax.swing.JTable table,
                                    javax.swing.table.TableModel model)
Set a new TableModel at a table you have enabled row sorting before using enableRowSorting(table). This will keep the sorting state if the new model is similar to the old one.

Parameters:
table - the table to set the new model for
model - the new model to be set
See Also:
enableRowSorting(javax.swing.JTable)

setupTableForRowSorting

public void setupTableForRowSorting(javax.swing.JTable aTable)
Registers this RowSortingTableModel at the given table by setting itself as its new model and wrapping the table's old model. Also installs the header extension for event listening at the table header.

Parameters:
aTable - the table to enable row sorting for

tearDownTableFromRowSorting

public void tearDownTableFromRowSorting(javax.swing.JTable aTable)
Remove row sorting from a table at which it was previously installed using setupTableForRowSorting(table) by setting the table's model to its old model again and removing the header extension for event listening

Parameters:
aTable - the table to remove row sorting from
See Also:
setupTableForRowSorting(javax.swing.JTable)

installHeaderExtension

public void installHeaderExtension(javax.swing.JTable aTable)
Install the header extension for event listening at the given table. You should not need to call this manually!. Use setupTableForRowSorting(table) to install row sorting.

Parameters:
aTable - the table to install the extension
See Also:
setupTableForRowSorting(javax.swing.JTable)

uninstallHeaderExtension

public void uninstallHeaderExtension(javax.swing.JTable aTable)
Remove the header extension for event listening from the given table. You should not need to call this manually!. Use tearDownTableFromRowSorting(table) to uninstall row sorting.

Parameters:
aTable - the table to uninstall the extension from
See Also:
tearDownTableFromRowSorting(javax.swing.JTable)

setModel

public void setModel(javax.swing.table.TableModel aModel)
Set a new TableModel in this decorator and notify all TableModelListeners. This will keep the sorting state if the new model is similar to the old one.

Parameters:
aModel - the new model to be installed

getModel

public javax.swing.table.TableModel getModel()
Get the TableModel decorated by this wrapper.

Returns:
the decorated TableModel

sort

public void sort(int columnIndex,
                 boolean order)
Have the decorator sort the rows by values of given column in given order. Also notifies listeners of changes.

Parameters:
columnIndex - index of column to sort by. Could be an int >= 0 or NO_COLUMN
order - can be ASCENDING or DESCENDING
See Also:
NO_COLUMN, ASCENDING, DESCENDING

getState

public java.lang.Object getState()
Creates a Memento of the state of this wrapper. This can be used for a restauration of the State.

Returns:
a JavaBean representing the state of the RowSorting Interface.

setState

public boolean setState(java.lang.Object aState)
Resets this wrapper to the given state if the column structure is similar to the one present while getState() was called. This method must be called with a state object obtained by getState().

Parameters:
aState - the state to set.
Returns:
true if the column structure was similar and the state has been reset.
See Also:
getState()

getColumnCount

public int getColumnCount()
Implements getColumnCount() in the interface TableModel.

Specified by:
getColumnCount in interface javax.swing.table.TableModel
See Also:
TableModel

getRowCount

public int getRowCount()
Implements getRowCount() in the interface TableModel.

Specified by:
getRowCount in interface javax.swing.table.TableModel
See Also:
TableModel

getValueAt

public java.lang.Object getValueAt(int rowIndex,
                                   int columnIndex)
Implements getValueAt(row,col) in the interface TableModel. Recalculate row by sort order.

Specified by:
getValueAt in interface javax.swing.table.TableModel
See Also:
TableModel

getColumnClass

public java.lang.Class<?> getColumnClass(int columnIndex)
Implements getColumnClass(col) in the interface TableModel.

Specified by:
getColumnClass in interface javax.swing.table.TableModel
Overrides:
getColumnClass in class javax.swing.table.AbstractTableModel
See Also:
TableModel

getColumnName

public java.lang.String getColumnName(int columnIndex)
Implements getColumnName(col) in the interface TableModel.

Specified by:
getColumnName in interface javax.swing.table.TableModel
Overrides:
getColumnName in class javax.swing.table.AbstractTableModel
See Also:
TableModel

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
Implements isCellEditable(row,col) in the interface TableModel. Recalculate row by sort order.

Specified by:
isCellEditable in interface javax.swing.table.TableModel
Overrides:
isCellEditable in class javax.swing.table.AbstractTableModel
See Also:
TableModel

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int rowIndex,
                       int columnIndex)
Implements getColumnCount() in the interface TableModel. Recalculate row by sort order.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class javax.swing.table.AbstractTableModel
See Also:
TableModel

getModelRow

public int getModelRow(int row)
Return the row number of the original model for the sorted row number.

Parameters:
row - the sorted row number
Returns:
the row number in the original model


Copyright © Tilmann Kuhn