|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.swing.table.AbstractTableModel edu.northwestern.at.utils.swing.SortedTableModel
public class SortedTableModel
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.
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 |
---|
protected static final javax.swing.Icon upTriangle
protected static final javax.swing.Icon downTriangle
protected java.awt.Color headerBackgroundColor
protected java.util.ArrayList rows
protected java.lang.String[] columnNames
protected boolean[] sortableColumns
protected int sortColumn
protected boolean sortAscending
protected javax.swing.JTable view
protected javax.swing.table.JTableHeader tableHeader
protected java.util.Comparator comparator
protected java.util.Comparator defaultComparator
protected javax.swing.table.TableCellRenderer headerRenderer
protected java.awt.event.MouseAdapter headerClick
Constructor Detail |
---|
public SortedTableModel(java.lang.String[] columnNames, int initialSortColumn, boolean initialSortAscending)
columnNames
- Column names.initialSortColumn
- The initial sort column.initialSortAscending
- The initial sort order. Method Detail |
---|
public void setHeaderBackground(java.awt.Color backgroundColor)
backgroundColor
- The background color. public void setComparator(java.util.Comparator comparator)
comparator
- The comparator. public void setDefaultComparator()
protected void initColumnSizes(java.lang.Object[] longValues, boolean setMaxWidth)
longValues
- An array of long values for the cell contents.setMaxWidth
- Set maximum cell width to size of long values. protected void initColumnSizes(java.lang.Object[] longValues, boolean setMaxWidth, boolean setMinWidth)
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. public int getRowCount()
public int getColumnCount()
public java.lang.Object getValueAt(int rowIndex, int columnIndex)
rowIndex
- Row index.columnIndex
- Column index.
public SortedTableModel.Row getRow(int rowIndex)
rowIndex
- Row index.
public java.lang.String getColumnName(int columnIndex)
getColumnName
in interface javax.swing.table.TableModel
getColumnName
in class javax.swing.table.AbstractTableModel
columnIndex
- Column index.
public java.lang.String[] getColumnNames()
public int getSortColumn()
public boolean getSortAscending()
public void resort()
public void sort(int sortColumn, boolean sortAscending)
sortColumn
- Sort column.sortAscending
- Sort order. public void setData(java.util.Collection collection)
The old table rows are discarded, then the new ones are sorted and set.
collection
- Collection of rows. public void add(SortedTableModel.Row row)
The row is added in the proper position to maintain the current sort order.
row
- The new row.
N.B.: make sure your chosen comparator properly
handles duplicate values in rows. public void remove(SortedTableModel.Row row)
row
- The row to be removed.
N.B.: the first row which compares equal
to the given row is removed. public void remove(int rowIndex)
rowIndex
- The index of the row to be removed. public void setView(javax.swing.JTable view, boolean[] sortableColumns, java.lang.Object[] longValues, boolean setMaxWidth, boolean setMinWidth)
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.
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. public void setView(javax.swing.JTable view, boolean[] sortableColumns, java.lang.Object[] longValues, boolean setMaxWidth)
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.
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.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |