org.jcreme.swing.table
Class AbstractSortableTableModel

java.lang.Object
  extended byjavax.swing.table.AbstractTableModel
      extended byorg.jcreme.swing.table.AbstractSortableTableModel
All Implemented Interfaces:
java.io.Serializable, SortableTableModel, javax.swing.table.TableModel
Direct Known Subclasses:
ReportTableModel

public abstract class AbstractSortableTableModel
extends javax.swing.table.AbstractTableModel
implements SortableTableModel

This class is an abstract implementation of the SortableTableModel interface. Some of the features are inspired from the Java Tutorial TableSorter.java sample. The concrete sub classes of this class can implement either of two schemes: the data manipulator scheme described in the tutorial, the usual scheme to create a direct subclass of this one. This class does not specify how the resort is handled. It could be through the TableChangeEvents or through any other scheme.

Version:
$Revision: 1.1 $
Author:
$Author: dbregeon $
See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
AbstractSortableTableModel()
           
 
Method Summary
protected  int compare(int row1, int row2)
          Compares two rows using the sortingColumns to determine which columns (and in which order) are to be used in the sort.
 int compareRowsByColumn(int row1, int row2, int column)
          This method enables to compare the values of a column for two different rows.
 int getOriginalRow(int row)
          Enables to find a row number in the original model.
protected abstract  java.lang.Object getOriginalValueAt(int row, int column)
          This method returns the value at the original (non sorted) row.
protected  SortableTableColumn[] getSortingColumns()
          Provides access to the columns used for sorting.
 java.lang.Object getValueAt(int row, int column)
           
protected  void initConverterArray()
          This method enables to ensure that the conversion array is big enough.
 boolean isCellEditable(int rowIndex, int columnIndex)
           
protected abstract  boolean isOriginalCellEditable(int row, int column)
          This method enables to determine if the value at the original (non sorted) row can be modified.
protected  int[][] originalIntervalToSortedIntervals(int originalStart, int originalEnd)
          This method enables to transform an interval of rows in the original data into a set of intervals in the sorted data.
protected abstract  void setOriginalValueAt(java.lang.Object value, int row, int column)
          This method enables to change the value at the original (non sorted) row.
 void setValueAt(java.lang.Object value, int row, int column)
           
protected  void shuttleSort(int[] from, int[] to, int low, int high)
           
 void sort(SortableTableColumn[] columns)
          Sorts the TableModel using the columns given in the columns parameter in the order given in columns.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getColumnName, getListeners, getTableModelListeners, removeTableModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, getColumnClass, getColumnCount, getColumnName, getRowCount, removeTableModelListener
 

Constructor Detail

AbstractSortableTableModel

public AbstractSortableTableModel()
Method Detail

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Specified by:
getValueAt in interface javax.swing.table.TableModel
See Also:
TableModel.getValueAt(int, int)

setValueAt

public void setValueAt(java.lang.Object value,
                       int row,
                       int column)
Specified by:
setValueAt in interface javax.swing.table.TableModel
See Also:
TableModel.setValueAt(java.lang.Object, int, int)

getOriginalRow

public int getOriginalRow(int row)
Enables to find a row number in the original model.

Parameters:
row - the row as presented after sorting.
Returns:
the row in the original (unsorted) model that corresponds to the parameter row.

getOriginalValueAt

protected abstract java.lang.Object getOriginalValueAt(int row,
                                                       int column)
This method returns the value at the original (non sorted) row.

Parameters:
row - the row number in the original model.
column - the column in the model.
Returns:
the value at these coordinates in the original model.

setOriginalValueAt

protected abstract void setOriginalValueAt(java.lang.Object value,
                                           int row,
                                           int column)
This method enables to change the value at the original (non sorted) row.

Parameters:
value - the value to set these coordinates in the original model.
row - the row number in the original model.
column - the column in the model.

sort

public void sort(SortableTableColumn[] columns)
Sorts the TableModel using the columns given in the columns parameter in the order given in columns. It also fires an event to signal the change to eventual listeners.

Specified by:
sort in interface SortableTableModel
Parameters:
columns - the columns used to sort the table in the order they will be used.

shuttleSort

protected void shuttleSort(int[] from,
                           int[] to,
                           int low,
                           int high)

compare

protected int compare(int row1,
                      int row2)
Compares two rows using the sortingColumns to determine which columns (and in which order) are to be used in the sort. Inspired From the Java Tutorial.

Parameters:
row1 - the index of the first row.
row2 - the index of the second row.
Returns:
the comparison result.

compareRowsByColumn

public int compareRowsByColumn(int row1,
                               int row2,
                               int column)
This method enables to compare the values of a column for two different rows.

Parameters:
row1 - the reference row.
row2 - the compared row.
column - the column of comparison.
Returns:
0 if the values of the column for each row are equal. A negative number if the value in row2 is greater than the one in row1. A positive number if the value in row1 is greater than the value in row2.

initConverterArray

protected void initConverterArray()
This method enables to ensure that the conversion array is big enough.


originalIntervalToSortedIntervals

protected int[][] originalIntervalToSortedIntervals(int originalStart,
                                                    int originalEnd)
This method enables to transform an interval of rows in the original data into a set of intervals in the sorted data.

Parameters:
originalStart - the row number of the start of the interval.
originalEnd - the row number of the end of the interval.
Returns:
an array of intervals (size nx2).

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
Specified by:
isCellEditable in interface javax.swing.table.TableModel
See Also:
TableModel.isCellEditable(int, int)

isOriginalCellEditable

protected abstract boolean isOriginalCellEditable(int row,
                                                  int column)
This method enables to determine if the value at the original (non sorted) row can be modified.

Parameters:
row - the row number in the original model.
column - the column in the model.
Returns:
true if the cell at the original coordinates is editable.

getSortingColumns

protected SortableTableColumn[] getSortingColumns()
Provides access to the columns used for sorting.

Returns:
the sortingColumns.


Copyright 2006 null. All Rights Reserved.