EchoPoint
2.1.0rc4

echopointng.table
Class DefaultSortableTableModel

java.lang.Object
  extended bynextapp.echo2.app.table.AbstractTableModel
      extended byechopointng.table.DefaultSortableTableModel
All Implemented Interfaces:
java.io.Serializable, SortableTableModel, nextapp.echo2.app.table.TableModel
Direct Known Subclasses:
DefaultPageableSortableTableModel

public class DefaultSortableTableModel
extends nextapp.echo2.app.table.AbstractTableModel
implements SortableTableModel

DefaultSortableTableModel is a decorator for TableModel's; adding sorting functionality.

DefaultSortableTableModel 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(col, row)) they are passed to the underlying model after the row numbers have been translated via the internal mapping array.

This way, the DefaultSortableTableModel appears to hold another copy of the table with the rows in a different order.

This also makes this decorator TableModel very light weight and if no sorting is specified then no extra sorting information is kept.

This code has been adapted from

 http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting
 http://java.sun.com/docs/books/tutorial/uiswing/components/examples/TableSorter.java
 

Author:
David Nedde
See Also:
Serialized Form

Field Summary
static java.util.Comparator COMPARABLE_COMPARATOR
          You can use this comparator if the column objects impment Comparable
static java.util.Comparator LEXICAL_COMPARATOR
          This comparator converts the objects into Strings first and then compares them based on lexical order.
protected  nextapp.echo2.app.table.TableModel underlyingTableModel
          This is the underlying TableModel being decorated by this class
 
Fields inherited from interface echopointng.table.SortableTableModel
ASCENDING, DESCENDING, NOT_SORTED
 
Constructor Summary
DefaultSortableTableModel(nextapp.echo2.app.table.TableColumnModel columnModel)
           
DefaultSortableTableModel(nextapp.echo2.app.table.TableModel underlyingTableModel)
          Contructs a DefaultSortableTableModel based on the underlying TableModel
DefaultSortableTableModel(nextapp.echo2.app.table.TableModel underlyingTableModel, nextapp.echo2.app.table.TableColumnModel columnModel)
          Contructs a DefaultSortableTableModel based on the underlying TableModel and the provided TableColumnModel
 
Method Summary
 java.lang.Class getColumnClass(int column)
           
 int getColumnCount()
           
 java.lang.String getColumnName(int column)
           
protected  java.util.Comparator getComparator(int column)
          Called to get a Comparator for a given column.
 int getCurrentSortColumn()
          Returns the primarily sorted column number.
 int getRowCount()
           
 int getSortDirective(int column)
          Returns the sort directive for the specified column
 nextapp.echo2.app.table.TableModel getUnderlyingTableModel()
           
 java.lang.Object getValueAt(int column, int row)
           
 void setColumnComparator(java.lang.Class type, java.util.Comparator comparator)
          Colums can be sorted by Class by setting this method.
 void setSortDirective(int column, int sortDirective)
          Sets th sort directive for the given TableModel column.
 void setUnderlyingTableModel(nextapp.echo2.app.table.TableModel newTableModel)
          Sets the underlying TableModel to use for this DefaultSortableTableModel.
 void sortByColumn(int column, int sortDirective)
          Sorts the data backing this model based on the given column and sort directive
 int toSortedViewRowIndex(int modelRowIndex)
          This converts the unsorted model row index into the equivalent sorted view row index.
 int toUnsortedModelRowIndex(int viewRowIndex)
          This converts the sorted view row index into the equivalent unsorted model row index.
 
Methods inherited from class nextapp.echo2.app.table.AbstractTableModel
addTableModelListener, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getEventListenerList, removeTableModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface nextapp.echo2.app.table.TableModel
addTableModelListener, removeTableModelListener
 

Field Detail

underlyingTableModel

protected nextapp.echo2.app.table.TableModel underlyingTableModel
This is the underlying TableModel being decorated by this class


COMPARABLE_COMPARATOR

public static final java.util.Comparator COMPARABLE_COMPARATOR
You can use this comparator if the column objects impment Comparable


LEXICAL_COMPARATOR

public static final java.util.Comparator LEXICAL_COMPARATOR
This comparator converts the objects into Strings first and then compares them based on lexical order. It is used by default if no specific sorting Comprator can be found for a table column.

Constructor Detail

DefaultSortableTableModel

public DefaultSortableTableModel(nextapp.echo2.app.table.TableModel underlyingTableModel)
Contructs a DefaultSortableTableModel based on the underlying TableModel

Parameters:
underlyingTableModel - - the underlying and unsorted TableModel

DefaultSortableTableModel

public DefaultSortableTableModel(nextapp.echo2.app.table.TableModel underlyingTableModel,
                                 nextapp.echo2.app.table.TableColumnModel columnModel)
Contructs a DefaultSortableTableModel based on the underlying TableModel and the provided TableColumnModel

Parameters:
underlyingTableModel - - the underlying and unsorted TableModel
columnModel - - the TableColumnModel to use

DefaultSortableTableModel

public DefaultSortableTableModel(nextapp.echo2.app.table.TableColumnModel columnModel)
Method Detail

getUnderlyingTableModel

public nextapp.echo2.app.table.TableModel getUnderlyingTableModel()
Returns:
the underlying TableModel

setUnderlyingTableModel

public void setUnderlyingTableModel(nextapp.echo2.app.table.TableModel newTableModel)
Sets the underlying TableModel to use for this DefaultSortableTableModel.

Parameters:
newTableModel - - the new underlying TableModel

getSortDirective

public int getSortDirective(int column)
Description copied from interface: SortableTableModel
Returns the sort directive for the specified column

Specified by:
getSortDirective in interface SortableTableModel
Returns:
retutrns on of these values
  • SortableTableModel.DESCENDING
  • SortableTableModel.NOT_SORTED
  • SortableTableModel.ASCEDING
See Also:
SortableTableModel.getSortDirective(int)

setSortDirective

public void setSortDirective(int column,
                             int sortDirective)
Description copied from interface: SortableTableModel
Sets th sort directive for the given TableModel column.

Specified by:
setSortDirective in interface SortableTableModel
Parameters:
column - the column in question
sortDirective - must be one of :
  • SortableTableModel.DESCENDING
  • SortableTableModel.NOT_SORTED
  • SortableTableModel.ASCEDING
See Also:
SortableTableModel.setSortDirective(int, int)

setColumnComparator

public void setColumnComparator(java.lang.Class type,
                                java.util.Comparator comparator)
Colums can be sorted by Class by setting this method. If the column is a SortableTableColumn, then that classes getComparator() will take precedence. If neither are set, the values are compared using Comparable if possible, or toString() values as a last resort.


getComparator

protected java.util.Comparator getComparator(int column)
Called to get a Comparator for a given column. A default algorithmn is used that looks for a SortableTableColumn to specify the Comprator and failing that if the TableModel column class is assignable from Comparable, that is used and faling that the LEXICAL_COMPRATOR is used.

Parameters:
column - - the column in question
Returns:
a Comparator to use to sort the column

toUnsortedModelRowIndex

public int toUnsortedModelRowIndex(int viewRowIndex)
Description copied from interface: SortableTableModel
This converts the sorted view row index into the equivalent unsorted model row index. When the underlying TableModel is being sorted, you can use this method to map from the sorted View set of row indexes into the underlying unsorted TableModel row indexes.

Specified by:
toUnsortedModelRowIndex in interface SortableTableModel
Parameters:
viewRowIndex - - a row index in terms of the sorted view that you want to convert to a unsorted model row index
Returns:
a unsorted model row index
See Also:
SortableTableModel.toUnsortedModelRowIndex(int)

toSortedViewRowIndex

public int toSortedViewRowIndex(int modelRowIndex)
Description copied from interface: SortableTableModel
This converts the unsorted model row index into the equivalent sorted view row index. When the underlying TableModel is being sorted, you can use this method to map from the underlying TableModel set of row indexes into sorted View row indexes.

Specified by:
toSortedViewRowIndex in interface SortableTableModel
Parameters:
modelRowIndex - - a row index in terms of the unsorted model that you want to convert to a sorted view row index
Returns:
a sorted view row index
See Also:
SortableTableModel.toSortedViewRowIndex(int)

getRowCount

public int getRowCount()
Specified by:
getRowCount in interface nextapp.echo2.app.table.TableModel

getColumnCount

public int getColumnCount()
Specified by:
getColumnCount in interface nextapp.echo2.app.table.TableModel

getColumnName

public java.lang.String getColumnName(int column)
Specified by:
getColumnName in interface nextapp.echo2.app.table.TableModel

getColumnClass

public java.lang.Class getColumnClass(int column)
Specified by:
getColumnClass in interface nextapp.echo2.app.table.TableModel

getValueAt

public java.lang.Object getValueAt(int column,
                                   int row)
Specified by:
getValueAt in interface nextapp.echo2.app.table.TableModel

sortByColumn

public void sortByColumn(int column,
                         int sortDirective)
Description copied from interface: SortableTableModel
Sorts the data backing this model based on the given column and sort directive

Specified by:
sortByColumn in interface SortableTableModel
Parameters:
column - the column to sort by
sortDirective - must be one of :
  • SortableTableModel.DESCENDING
  • SortableTableModel.NOT_SORTED
  • SortableTableModel.ASCEDING
See Also:
SortableTableModel.sortByColumn(int, int)

getCurrentSortColumn

public int getCurrentSortColumn()
Description copied from interface: SortableTableModel
Returns the primarily sorted column number.

Specified by:
getCurrentSortColumn in interface SortableTableModel
Returns:
the column number.
See Also:
SortableTableModel.getCurrentSortColumn()

EchoPoint
2.1.0rc4