uic.model
Class TableRowSorter

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by uic.model.TableRowSorter
All Implemented Interfaces:
Serializable, EventListener, TableModelListener, TableModel, TableViewInterface

public class TableRowSorter
extends AbstractTableModel
implements TableModelListener, TableViewInterface

A sorter for TableModels. The sorter has a model (conforming to TableModel) and itself implements TableModel. TableRowSorter does not store or copy the data in the TableModel, instead it maintains an array of integers which it keeps the same size as the number of rows in its model. When the model changes it notifies the sorter that something has changed eg. "rowsAdded" so that its internal array of integers can be reallocated. As requests are made of the sorter (like getValueAt(row, col) it redirects them to its model via the mapping array. That way the TableRowSorter appears to hold another copy of the table with the rows in a different order. The sorting algorthm used is stable which means that it does not move around rows when its comparison function returns 0 to denote that they are equivalent. Various bugfixes and make-faster patches by Roy van der Kuil and Thomas Zander

Version:
1.5 12/17/97
Author:
Philip Milne, Roy van der Kuil, Thomas Zander
See Also:
Serialized Form

Field Summary
protected  TableModel model
           
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TableRowSorter()
          Empty constructor, be sure to do a 'setModel' afterwards.
TableRowSorter(TableModel model)
          Main constructor; creates a new table sorter wrapping your data model to pass to that table.
 
Method Summary
 void addKeyListenerToTable(JTable table)
          Add a key listener to the table for making it possible to browse with key events.
 void addMouseListenerToHeaderInTable(JTable table)
          Adds functionality so that clicking on the table header of the given JTable Changes the sorting of the corresponding column
 void addSortedColumn(int column, boolean ascending)
          Add a sorting column in a given direction
 Class getColumnClass(int column)
          Return the column class of the specified column index
 int getColumnCount()
          Return number of columns that this model has
 String getColumnName(int column)
          Return the column name of the specified column index
 TableModel getModel()
          Returns the (data) model
 int getOriginalIndex(int localIndex)
          Return the original index of the view index
 int getRowCount()
          Return number of rows that this model has
 int[] getSortedColumns()
          Returns all sorted columns or an empty array if no columns are being sorted
 Object getValueAt(int row, int column)
           
 boolean isAscending()
          Is current sort ascending?
 boolean isCellEditable(int row, int column)
          Returns true if cell is editable Per default ONLY booleans are editable
 void setModel(TableModel model)
          set the (data)model this sorter wraps.
 void setValueAt(Object value, int row, int column)
          Update cell value
 void sort()
          This is the general, please sort me, method.
 void sortByColumn(int column)
          Sort column
 void sortByColumn(int column, boolean ascending)
          Sort column in a given direction
 void tableChanged(TableModelEvent e)
          React on table change events.
 void updateModel()
           
 
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
 

Field Detail

model

protected TableModel model
Constructor Detail

TableRowSorter

public TableRowSorter()
Empty constructor, be sure to do a 'setModel' afterwards.


TableRowSorter

public TableRowSorter(TableModel model)
Main constructor; creates a new table sorter wrapping your data model to pass to that table. Example usage:
UICDefaultTableModel tableTableModel = new UICDefaultTableModel();
       tableTableModel.addColumn(translate().i18n("Name"));
       tableTableModel.addColumn(translate().i18n("Address"));
       tableTableModel.addColumn(translate().i18n("City"));
       TableRowSorter sorter = new TableRowSorter(tableTableModel);
       table = new UICTable(sorter);

Method Detail

tableChanged

public void tableChanged(TableModelEvent e)
React on table change events. If table row count has changed.. update the sort

Specified by:
tableChanged in interface TableModelListener

updateModel

public void updateModel()

sort

public void sort()
This is the general, please sort me, method. During implementation we can swift between a simple sorter and the shuttlesorter. If this performs poorly; we will have to check if the implementation of this method should not forward the sorting request to someone else.


getValueAt

public Object getValueAt(int row,
                         int column)
Specified by:
getValueAt in interface TableModel

setValueAt

public void setValueAt(Object value,
                       int row,
                       int column)
Update cell value

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class AbstractTableModel

sortByColumn

public void sortByColumn(int column)
Sort column


sortByColumn

public void sortByColumn(int column,
                         boolean ascending)
Sort column in a given direction


addSortedColumn

public void addSortedColumn(int column,
                            boolean ascending)
Add a sorting column in a given direction


getSortedColumns

public int[] getSortedColumns()
Returns all sorted columns or an empty array if no columns are being sorted


isAscending

public boolean isAscending()
Is current sort ascending?


addMouseListenerToHeaderInTable

public void addMouseListenerToHeaderInTable(JTable table)
Adds functionality so that clicking on the table header of the given JTable Changes the sorting of the corresponding column

Parameters:
table - the table which header should react on mouse events

addKeyListenerToTable

public void addKeyListenerToTable(JTable table)
Add a key listener to the table for making it possible to browse with key events.

Parameters:
table - the table to add the keylistener to

getRowCount

public int getRowCount()
Return number of rows that this model has

Specified by:
getRowCount in interface TableModel
Returns:
number of rows in the model

getColumnCount

public int getColumnCount()
Return number of columns that this model has

Specified by:
getColumnCount in interface TableModel
Returns:
number of columns in the model

getColumnName

public String getColumnName(int column)
Return the column name of the specified column index

Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel
Returns:
column name at specified index

getColumnClass

public Class getColumnClass(int column)
Return the column class of the specified column index

Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel
Returns:
column class at specified index

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Returns true if cell is editable Per default ONLY booleans are editable

Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in class AbstractTableModel
Returns:
true if a cell is editable

getModel

public TableModel getModel()
Returns the (data) model

Specified by:
getModel in interface TableViewInterface
Returns:
the child model, or null if none.

getOriginalIndex

public int getOriginalIndex(int localIndex)
Return the original index of the view index

Specified by:
getOriginalIndex in interface TableViewInterface
Parameters:
localIndex - the visible index
Returns:
original index of the visible index localIndex

setModel

public void setModel(TableModel model)
set the (data)model this sorter wraps.

Specified by:
setModel in interface TableViewInterface
Parameters:
model - the new child model this model will start using.


Copyright © 2002-2004 Thomas Zander Available under the Free Apache licence