Top Description Inners Fields Constructors Methods
org.hsqldb.util

public Class TableSorter

extends AbstractTableModel
Class Inheritance
Imports
java.util.ArrayList, .Arrays, .Comparator, .HashMap, .Iterator, .List, .Map, java.awt.Color, .Component, .Graphics, java.awt.event.MouseAdapter, .MouseEvent, .MouseListener, javax.swing.Icon, .JLabel, .JTable, javax.swing.event.TableModelEvent, .TableModelListener, javax.swing.table.AbstractTableModel, .JTableHeader, .TableCellRenderer, .TableColumnModel, .TableModel

TableSorter is a decorator for TableModels; adding sorting functionality to a supplied TableModel. TableSorter does not store or copy the data in its TableModel; instead it maintains a map from the row indexes of the view to the row indexes of the model. As requests are made of the sorter (like getValueAt(row, col)) they are passed to the underlying model after the row numbers have been translated via the internal mapping array. This way, the TableSorter appears to hold another copy of the table with the rows in a different order.

TableSorter registers itself as a listener to the underlying model, just as the JTable itself would. Events recieved from the model are examined, sometimes manipulated (typically widened), and then passed on to the TableSorter's listeners (typically the JTable). If a change to the model has invalidated the order of TableSorter's rows, a note of this is made and the sorter will resort the rows the next time a value is requested.

When the tableHeader property is set, either by using the setTableHeader() method or the two argument constructor, the table header may be used as a complete UI for TableSorter. The default renderer of the tableHeader is decorated with a renderer that indicates the sorting status of each column. In addition, a mouse listener is installed with the following behavior:

This is a long overdue rewrite of a class of the same name that first appeared in the swing table demos in 1997.

Authors
Philip Milne, Brendon McLean, Dan van Enckevort, Parwinder Sekhon
Version
2.0 02/27/04

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private class
private class
private class
private class

Field Summary

Modifier and TypeField and Description
public static final int
private Map<K, V>
public static final Comparator<T>
public static final int
private static TableSorter.Directive
public static final Comparator<T>
private int[]
private MouseListener
public static final int
private List<E>
private JTableHeader
protected TableModel
private TableModelListener
private TableSorter.Row[]
Inherited from javax.swing.table.AbstractTableModel:
listenerList

Constructor Summary

AccessConstructor and Description
public
public
TableSorter(TableModel tableModel)

public
TableSorter(TableModel tableModel, JTableHeader tableHeader)

Method Summary

Modifier and TypeMethod and Description
private void
private void
public Class<T>
getColumnClass(int column)

Overrides javax.swing.table.AbstractTableModel.getColumnClass.

Implements javax.swing.table.TableModel.getColumnClass.

Returns Object.class regardless of columnIndex.

public int
getColumnCount()

Implements javax.swing.table.TableModel.getColumnCount.

Returns the number of columns in the model.

public String
getColumnName(int column)

Overrides javax.swing.table.AbstractTableModel.getColumnName.

Implements javax.swing.table.TableModel.getColumnName.

Returns a default name for the column using spreadsheet conventions: A, B, C, ...

protected Comparator<T>
getComparator(int column)

private TableSorter.Directive
getDirective(int column)

protected Icon
getHeaderRendererIcon(int column, int size)

private int[]
public int
getRowCount()

Implements javax.swing.table.TableModel.getRowCount.

Returns the number of rows in the model.

public int
getSortingStatus(int column)

public JTableHeader
public TableModel
public Object
getValueAt(int row, int column)

Implements javax.swing.table.TableModel.getValueAt.

Returns the value for the cell at columnIndex and rowIndex.

private TableSorter.Row[]
public boolean
public boolean
public int
modelIndex(int viewIndex)

public void
setColumnComparator(Class<T> type, Comparator<T> comparator)

public void
setSortingStatus(int column, int status)

public void
public void
public void
setValueAt(Object aValue, int row, int column)

Overrides javax.swing.table.AbstractTableModel.setValueAt.

Implements javax.swing.table.TableModel.setValueAt.

This empty implementation is provided so users don't have to implement this method if their data model is not editable.

private void
Inherited from javax.swing.table.AbstractTableModel:
addTableModelListenerfindColumnfireTableCellUpdatedfireTableChangedfireTableDataChangedfireTableRowsDeletedfireTableRowsInsertedfireTableRowsUpdatedfireTableStructureChangedgetListenersgetTableModelListenersremoveTableModelListener

Field Detail

ASCENDINGback to summary
public static final int ASCENDING
columnComparatorsback to summary
private Map<K, V> columnComparators
COMPARABLE_COMPARATORback to summary
public static final Comparator<T> COMPARABLE_COMPARATOR
DESCENDINGback to summary
public static final int DESCENDING
EMPTY_DIRECTIVEback to summary
private static TableSorter.Directive EMPTY_DIRECTIVE
LEXICAL_COMPARATORback to summary
public static final Comparator<T> LEXICAL_COMPARATOR
modelToViewback to summary
private int[] modelToView
mouseListenerback to summary
private MouseListener mouseListener
NOT_SORTEDback to summary
public static final int NOT_SORTED
sortingColumnsback to summary
private List<E> sortingColumns
tableHeaderback to summary
private JTableHeader tableHeader
tableModelback to summary
protected TableModel tableModel
tableModelListenerback to summary
private TableModelListener tableModelListener
viewToModelback to summary
private TableSorter.Row[] viewToModel

Constructor Detail

TableSorterback to summary
public TableSorter()
TableSorterback to summary
public TableSorter(TableModel tableModel)
TableSorterback to summary
public TableSorter(TableModel tableModel, JTableHeader tableHeader)

Method Detail

cancelSortingback to summary
private void cancelSorting()
clearSortingStateback to summary
private void clearSortingState()
getColumnClassback to summary
public Class<T> getColumnClass(int column)

Overrides javax.swing.table.AbstractTableModel.getColumnClass.

Implements javax.swing.table.TableModel.getColumnClass.

Doc from javax.swing.table.AbstractTableModel.getColumnClass.

Returns Object.class regardless of columnIndex.

getColumnCountback to summary
public int getColumnCount()

Implements javax.swing.table.TableModel.getColumnCount.

Doc from javax.swing.table.TableModel.getColumnCount.

Returns the number of columns in the model. A JTable uses this method to determine how many columns it should create and display by default.

getColumnNameback to summary
public String getColumnName(int column)

Overrides javax.swing.table.AbstractTableModel.getColumnName.

Implements javax.swing.table.TableModel.getColumnName.

Doc from javax.swing.table.AbstractTableModel.getColumnName.

Returns a default name for the column using spreadsheet conventions: A, B, C, ... Z, AA, AB, etc. If column cannot be found, returns an empty string.

getComparatorback to summary
protected Comparator<T> getComparator(int column)
getDirectiveback to summary
private TableSorter.Directive getDirective(int column)
getHeaderRendererIconback to summary
protected Icon getHeaderRendererIcon(int column, int size)
getModelToViewback to summary
private int[] getModelToView()
getRowCountback to summary
public int getRowCount()

Implements javax.swing.table.TableModel.getRowCount.

Doc from javax.swing.table.TableModel.getRowCount.

Returns the number of rows in the model. A JTable uses this method to determine how many rows it should display. This method should be quick, as it is called frequently during rendering.

getSortingStatusback to summary
public int getSortingStatus(int column)
getTableHeaderback to summary
public JTableHeader getTableHeader()
getTableModelback to summary
public TableModel getTableModel()
getValueAtback to summary
public Object getValueAt(int row, int column)

Implements javax.swing.table.TableModel.getValueAt.

Doc from javax.swing.table.TableModel.getValueAt.

Returns the value for the cell at columnIndex and rowIndex.

getViewToModelback to summary
private TableSorter.Row[] getViewToModel()
isCellEditableback to summary
public boolean isCellEditable(int row, int column)

Overrides javax.swing.table.AbstractTableModel.isCellEditable.

Implements javax.swing.table.TableModel.isCellEditable.

Doc from javax.swing.table.AbstractTableModel.isCellEditable.

Returns false. This is the default implementation for all cells.

isSortingback to summary
public boolean isSorting()
modelIndexback to summary
public int modelIndex(int viewIndex)
setColumnComparatorback to summary
public void setColumnComparator(Class<T> type, Comparator<T> comparator)
setSortingStatusback to summary
public void setSortingStatus(int column, int status)
setTableHeaderback to summary
public void setTableHeader(JTableHeader tableHeader)
setTableModelback to summary
public void setTableModel(TableModel tableModel)
setValueAtback to summary
public void setValueAt(Object aValue, int row, int column)

Overrides javax.swing.table.AbstractTableModel.setValueAt.

Implements javax.swing.table.TableModel.setValueAt.

Doc from javax.swing.table.AbstractTableModel.setValueAt.

This empty implementation is provided so users don't have to implement this method if their data model is not editable.

sortingStatusChangedback to summary
private void sortingStatusChanged()
org.hsqldb.util back to summary

private Class TableSorter.Arrow

extends Object
implements Icon
Class Inheritance
All Implemented Interfaces
javax.swing.Icon

Field Summary

Modifier and TypeField and Description
private boolean
private int
private int

Constructor Summary

AccessConstructor and Description
public
Arrow(boolean descending, int size, int priority)

Method Summary

Modifier and TypeMethod and Description
public int
getIconHeight()

Implements javax.swing.Icon.getIconHeight.

Returns the icon's height.

public int
getIconWidth()

Implements javax.swing.Icon.getIconWidth.

Returns the icon's width.

public void
paintIcon(Component c, Graphics g, int x, int y)

Implements javax.swing.Icon.paintIcon.

Draw the icon at the specified location.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

descendingback to summary
private boolean descending
priorityback to summary
private int priority
sizeback to summary
private int size

Constructor Detail

Arrowback to summary
public Arrow(boolean descending, int size, int priority)

Method Detail

getIconHeightback to summary
public int getIconHeight()

Implements javax.swing.Icon.getIconHeight.

Doc from javax.swing.Icon.getIconHeight.

Returns the icon's height.

getIconWidthback to summary
public int getIconWidth()

Implements javax.swing.Icon.getIconWidth.

Doc from javax.swing.Icon.getIconWidth.

Returns the icon's width.

paintIconback to summary
public void paintIcon(Component c, Graphics g, int x, int y)

Implements javax.swing.Icon.paintIcon.

Doc from javax.swing.Icon.paintIcon.

Draw the icon at the specified location. Icon implementations may use the Component argument to get properties useful for painting, e.g. the foreground or background color.

org.hsqldb.util back to summary

private Class TableSorter.Directive

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private int
private int

Constructor Summary

AccessConstructor and Description
public
Directive(int column, int direction)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

columnback to summary
private int column
directionback to summary
private int direction

Constructor Detail

Directiveback to summary
public Directive(int column, int direction)
org.hsqldb.util back to summary

private Class TableSorter.MouseHandler

extends MouseAdapter
Class Inheritance

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
mouseClicked(MouseEvent e)

Overrides java.awt.event.MouseAdapter.mouseClicked.

Implements java.awt.event.MouseListener.mouseClicked.

Invoked when the mouse button has been clicked (pressed and released) on a component.

Inherited from java.awt.event.MouseAdapter:
mouseDraggedmouseEnteredmouseExitedmouseMovedmousePressedmouseReleasedmouseWheelMoved

Constructor Detail

MouseHandlerback to summary
private MouseHandler()

Method Detail

mouseClickedback to summary
public void mouseClicked(MouseEvent e)

Overrides java.awt.event.MouseAdapter.mouseClicked.

Implements java.awt.event.MouseListener.mouseClicked.

Doc from java.awt.event.MouseAdapter.mouseClicked.

Invoked when the mouse button has been clicked (pressed and released) on a component.

org.hsqldb.util back to summary

private Class TableSorter.Row

extends Object
implements Comparable
Class Inheritance
All Implemented Interfaces
java.lang.Comparable

Field Summary

Modifier and TypeField and Description
private int

Constructor Summary

AccessConstructor and Description
public
Row(int index)

Method Summary

Modifier and TypeMethod and Description
public int
compareTo(Object o)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

modelIndexback to summary
private int modelIndex

Constructor Detail

Rowback to summary
public Row(int index)

Method Detail

compareToback to summary
public int compareTo(Object o)

Implements java.lang.Comparable.compareTo.

Doc from java.lang.Comparable.compareTo.

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor must ensure signum(x.compareTo(y)) == -signum(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0.

Finally, the implementor must ensure that x.compareTo(y)==0 implies that signum(x.compareTo(z)) == signum(y.compareTo(z)), for all z.

org.hsqldb.util back to summary

private Class TableSorter.SortableHeaderRenderer

extends Object
implements TableCellRenderer
Class Inheritance
All Implemented Interfaces
javax.swing.table.TableCellRenderer

Field Summary

Modifier and TypeField and Description
private TableCellRenderer

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Component
getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

Implements javax.swing.table.TableCellRenderer.getTableCellRendererComponent.

Returns the component used for drawing the cell.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

tableCellRendererback to summary
private TableCellRenderer tableCellRenderer

Constructor Detail

SortableHeaderRendererback to summary
public SortableHeaderRenderer(TableCellRenderer tableCellRenderer)

Method Detail

getTableCellRendererComponentback to summary
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

Implements javax.swing.table.TableCellRenderer.getTableCellRendererComponent.

Doc from javax.swing.table.TableCellRenderer.getTableCellRendererComponent.

Returns the component used for drawing the cell. This method is used to configure the renderer appropriately before drawing.

The TableCellRenderer is also responsible for rendering the the cell representing the table's current DnD drop location if it has one. If this renderer cares about rendering the DnD drop location, it should query the table directly to see if the given row and column represent the drop location:

    JTable.DropLocation dropLocation = table.getDropLocation();
    if (dropLocation != null
            && !dropLocation.isInsertRow()
            && !dropLocation.isInsertColumn()
            && dropLocation.getRow() == row
            && dropLocation.getColumn() == column) {

        // this cell represents the current drop location
        // so render it specially, perhaps with a different color
    }

During a printing operation, this method will be called with isSelected and hasFocus values of false to prevent selection and focus from appearing in the printed output. To do other customization based on whether or not the table is being printed, check the return value from javax.swing.JComponent#isPaintingForPrint().

org.hsqldb.util back to summary

private Class TableSorter.TableModelHandler

extends Object
implements TableModelListener
Class Inheritance
All Implemented Interfaces
javax.swing.event.TableModelListener, java.util.EventListener

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
tableChanged(TableModelEvent e)

Implements javax.swing.event.TableModelListener.tableChanged.

This fine grain notification tells listeners the exact range of cells, rows, or columns that changed.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

TableModelHandlerback to summary
private TableModelHandler()

Method Detail

tableChangedback to summary
public void tableChanged(TableModelEvent e)

Implements javax.swing.event.TableModelListener.tableChanged.

Doc from javax.swing.event.TableModelListener.tableChanged.

This fine grain notification tells listeners the exact range of cells, rows, or columns that changed.