edu.northwestern.at.utils.swing
Class SortedTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by edu.northwestern.at.utils.swing.SortedTableModel
All Implemented Interfaces:
java.io.Serializable, javax.swing.table.TableModel
Direct Known Subclasses:
WordHoardSortedTableModel

public class SortedTableModel
extends javax.swing.table.AbstractTableModel

Sorted table model.

This class extends AbstractTableModel to support sorted tables.

The table model is linked to a single partner JTable table view. The view is modified to use a custom header cell renderer which includes a filled triangle pointing up or down to indicate the current sort column and sort order. Single clicks on sortable column headers resort on that column in ascending order. Shift-clicks on sortable column headers resort on that column in descending order.

Note: We only support one view at a time. It would be possible to support multiple views, each with its own sort state, but we don't currently need this so we haven't done it.

See Also:
Serialized Form

Nested Class Summary
static interface SortedTableModel.Row
          Row interface for sorted tables.
 
Field Summary
protected  java.lang.String[] columnNames
          Column names.
protected  java.util.Comparator comparator
          The comparator for sorting rows.
protected  java.util.Comparator defaultComparator
          A row comparator.
protected static javax.swing.Icon downTriangle
          Descending sort order triangle.
protected  java.awt.Color headerBackgroundColor
          Background color for column headers.
protected  java.awt.event.MouseAdapter headerClick
          Handles mouse clicks in the table header.
protected  javax.swing.table.TableCellRenderer headerRenderer
          Renderer for table header cells.
protected  java.util.ArrayList rows
          List of rows in the table.
protected  boolean[] sortableColumns
          Sortable column flags.
protected  boolean sortAscending
          The current sort order: True if ascending, false if descending.
protected  int sortColumn
          The current sort column.
protected  javax.swing.table.JTableHeader tableHeader
          The view's table header.
protected static javax.swing.Icon upTriangle
          Ascending sort order triangle.
protected  javax.swing.JTable view
          The linked JTable view.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
SortedTableModel(java.lang.String[] columnNames, int initialSortColumn, boolean initialSortAscending)
          Constructs a new empty sorted table model.
 
Method Summary
 void add(SortedTableModel.Row row)
          Adds a row.
 int getColumnCount()
          Gets the column count.
 java.lang.String getColumnName(int columnIndex)
          Gets a column name.
 java.lang.String[] getColumnNames()
          Gets the column names.
 SortedTableModel.Row getRow(int rowIndex)
          Gets a row.
 int getRowCount()
          Gets the row count.
 boolean getSortAscending()
          Gets the sort order.
 int getSortColumn()
          Gets the sort column.
 java.lang.Object getValueAt(int rowIndex, int columnIndex)
          Gets the value of a table cell.
protected  void initColumnSizes(java.lang.Object[] longValues, boolean setMaxWidth)
          Initializes the column sizes.
protected  void initColumnSizes(java.lang.Object[] longValues, boolean setMaxWidth, boolean setMinWidth)
          Initializes the column sizes.
 void remove(int rowIndex)
          Removes a row.
 void remove(SortedTableModel.Row row)
          Removes a row.
 void resort()
          Resorts the table.
 void setComparator(java.util.Comparator comparator)
          Set comparator for table rows.
 void setData(java.util.Collection collection)
          Sets new table data.
 void setDefaultComparator()
          Set default comparator for table rows.
 void setHeaderBackground(java.awt.Color backgroundColor)
          Set background color for table header.
 void setView(javax.swing.JTable view, boolean[] sortableColumns, java.lang.Object[] longValues, boolean setMaxWidth)
          Sets the view.
 void setView(javax.swing.JTable view, boolean[] sortableColumns, java.lang.Object[] longValues, boolean setMaxWidth, boolean setMinWidth)
          Sets the view.
 void sort(int sortColumn, boolean sortAscending)
          Sorts the table.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

upTriangle

protected static final javax.swing.Icon upTriangle
Ascending sort order triangle.


downTriangle

protected static final javax.swing.Icon downTriangle
Descending sort order triangle.


headerBackgroundColor

protected java.awt.Color headerBackgroundColor
Background color for column headers. Default is pale blue.


rows

protected java.util.ArrayList rows
List of rows in the table.


columnNames

protected java.lang.String[] columnNames
Column names.


sortableColumns

protected boolean[] sortableColumns
Sortable column flags. Column i is sortable if and only if sortableColumns[i] is true. May be null to indicate that all columns are sortable.


sortColumn

protected int sortColumn
The current sort column.


sortAscending

protected boolean sortAscending
The current sort order: True if ascending, false if descending.


view

protected javax.swing.JTable view
The linked JTable view.


tableHeader

protected javax.swing.table.JTableHeader tableHeader
The view's table header.


comparator

protected java.util.Comparator comparator
The comparator for sorting rows.


defaultComparator

protected java.util.Comparator defaultComparator
A row comparator. Rows are compared using the current sort column and then the unique ID if the two rows compare equal on the current sort column.


headerRenderer

protected javax.swing.table.TableCellRenderer headerRenderer
Renderer for table header cells. This renderer uses a triangle pointing up or down to indicate the current sort column and sort order.


headerClick

protected java.awt.event.MouseAdapter headerClick
Handles mouse clicks in the table header. A click resorts the table in ascending order on the clicked column. Shift-click resorts the table in descending order on the clicked column.

Constructor Detail

SortedTableModel

public SortedTableModel(java.lang.String[] columnNames,
                        int initialSortColumn,
                        boolean initialSortAscending)
Constructs a new empty sorted table model.

Parameters:
columnNames - Column names.
initialSortColumn - The initial sort column.
initialSortAscending - The initial sort order.
Method Detail

setHeaderBackground

public void setHeaderBackground(java.awt.Color backgroundColor)
Set background color for table header.

Parameters:
backgroundColor - The background color.

setComparator

public void setComparator(java.util.Comparator comparator)
Set comparator for table rows.

Parameters:
comparator - The comparator.

setDefaultComparator

public void setDefaultComparator()
Set default comparator for table rows.


initColumnSizes

protected void initColumnSizes(java.lang.Object[] longValues,
                               boolean setMaxWidth)
Initializes the column sizes.

Parameters:
longValues - An array of long values for the cell contents.
setMaxWidth - Set maximum cell width to size of long values.

initColumnSizes

protected void initColumnSizes(java.lang.Object[] longValues,
                               boolean setMaxWidth,
                               boolean setMinWidth)
Initializes the column sizes.

Parameters:
longValues - An array of long values for the cell contents.
setMaxWidth - Set maximum cell width to size of long values.
setMinWidth - Set minimum cell width to size of long values.

getRowCount

public int getRowCount()
Gets the row count.

Returns:
The row count.

getColumnCount

public int getColumnCount()
Gets the column count.

Returns:
The column count.

getValueAt

public java.lang.Object getValueAt(int rowIndex,
                                   int columnIndex)
Gets the value of a table cell.

Parameters:
rowIndex - Row index.
columnIndex - Column index.
Returns:
Cell value.

getRow

public SortedTableModel.Row getRow(int rowIndex)
Gets a row.

Parameters:
rowIndex - Row index.
Returns:
The row.

getColumnName

public java.lang.String getColumnName(int columnIndex)
Gets a column name.

Specified by:
getColumnName in interface javax.swing.table.TableModel
Overrides:
getColumnName in class javax.swing.table.AbstractTableModel
Parameters:
columnIndex - Column index.
Returns:
The column name.

getColumnNames

public java.lang.String[] getColumnNames()
Gets the column names.

Returns:
Array of column names.

getSortColumn

public int getSortColumn()
Gets the sort column.

Returns:
The current sort column.

getSortAscending

public boolean getSortAscending()
Gets the sort order.

Returns:
True if ascending, false if descending.

resort

public void resort()
Resorts the table.


sort

public void sort(int sortColumn,
                 boolean sortAscending)
Sorts the table.

Parameters:
sortColumn - Sort column.
sortAscending - Sort order.

setData

public void setData(java.util.Collection collection)
Sets new table data.

The old table rows are discarded, then the new ones are sorted and set.

Parameters:
collection - Collection of rows.

add

public void add(SortedTableModel.Row row)
Adds a row.

The row is added in the proper position to maintain the current sort order.

Parameters:
row - The new row. N.B.: make sure your chosen comparator properly handles duplicate values in rows.

remove

public void remove(SortedTableModel.Row row)
Removes a row.

Parameters:
row - The row to be removed. N.B.: the first row which compares equal to the given row is removed.

remove

public void remove(int rowIndex)
Removes a row.

Parameters:
rowIndex - The index of the row to be removed.

setView

public void setView(javax.swing.JTable view,
                    boolean[] sortableColumns,
                    java.lang.Object[] longValues,
                    boolean setMaxWidth,
                    boolean setMinWidth)
Sets the view.

If the longValues parameter is not null, the column preferred widths in the linked JTable view are initialized so that each column is wide enough to accomodate the column name and a sort order triangle indicator, and is also wide enough to accomodate a long cell value.

Parameters:
view - The linked view.
sortableColumns - Sortable column flags. Column i is sortable if and only if sortableColumns[i] is true. May be null to indicate that all columns are sortable.
longValues - An array of long values for the cell contents. The i'th element is a long value for column i.
setMaxWidth - Set the maximum column widths to the sizes derived from the long values.
setMinWidth - Set the minimum column widths to the sizes derived from the long values.

setView

public void setView(javax.swing.JTable view,
                    boolean[] sortableColumns,
                    java.lang.Object[] longValues,
                    boolean setMaxWidth)
Sets the view.

If the longValues parameter is not null, the column preferred widths in the linked JTable view are initialized so that each column is wide enough to accomodate the column name and a sort order triangle indicator, and is also wide enough to accomodate a long cell value.

Parameters:
view - The linked view.
sortableColumns - Sortable column flags. Column i is sortable if and only if sortableColumns[i] is true. May be null to indicate that all columns are sortable.
longValues - An array of long values for the cell contents. The i'th element is a long value for column i.
setMaxWidth - Set the maximum column widths to the sizes derived from the long values.