dsto.dfc.swing.table
Class SortedTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by dsto.dfc.swing.table.AbstractDfcTableModel
          extended by dsto.dfc.swing.table.ProxyTableModel
              extended by dsto.dfc.swing.table.SortedTableModel
All Implemented Interfaces:
DfcTableModel, Serializable, EventListener, TableModelListener, TableModel

public class SortedTableModel
extends ProxyTableModel

Presents a DfcTableModel in sorted order.

Version:
$Revision$
Author:
Matthew Phillips
See Also:
Serialized Form
To do:
Currently the C&P methods only support the case where a single row is being copied - this should be extended at some point.

Nested Class Summary
static class SortedTableModel.SortingColumn
          Stores information about column to be sorted by.
 
Field Summary
 
Fields inherited from class dsto.dfc.swing.table.ProxyTableModel
model
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
SortedTableModel(DfcTableModel model)
           
 
Method Summary
 SortedTableModel.SortingColumn addSortingColumn(int columnIndex, boolean ascending)
          Add a column to the end of the list of columns to sort by (ie last in the sort order).
 boolean canCopyCells(int startRow, int endRow, int startColumn, int endColumn)
          Test if a copyCells () operation is permitted.
 boolean canCopyRows(int startRow, int endRow)
           
 boolean canDeleteCells(int startRow, int endRow, int startColumn, int endColumn)
          Test if a deleteCells () operation is permitted.
 boolean canDeleteRows(int rowStart, int rowEnd)
          Test if a deleteRows () operation is supported.
 boolean canMoveRows(int startRow, int endRow, int newStartRow)
          NOTE: only handles case where startRow == endRow.
 boolean canPasteCells(Transferable transferable, int startRow, int endRow, int startColumn, int endColumn)
          Should return true if rows may be pasted from a given Transferable into a block of cells.
protected  void checkModel()
          Simple check that the enclosed model has not changed without informing us.
protected  void checkUpdateRowMapSize()
          Check whether rowMap needs to be resized based on current table size.
 void clearSorting()
          Remove all columns from the sort order.
protected  int compare(int row1, int row2)
          Compare two rows in the table using the current column sort order.
protected  int compareRowsByColumn(int row1, int row2, int column)
          Compare two rows by their values in a given column.
 Transferable copyCells(int startRow, int endRow, int startColumn, int endColumn)
          Copy a rectangular block of cells into an AWT Transferable.
 Transferable copyRows(int startRow, int endRow)
           
 void deleteCells(int startRow, int endRow, int startColumn, int endColumn)
          Delete a rectangular block of cells.
 void deleteRows(int rowStart, int rowEnd)
          NOTE: only handles case where rowStart == rowEnd.
 SortedTableModel.SortingColumn findSortingColumn(int columnIndex)
          Find a sorting column on sortingColumns with a matching column index.
 Object getValueAt(int row, int col)
           
protected  void initRowMap()
          Initialise row map with row indexes in unsorted (natural) order.
 void insertRows(int rowStart, int rowEnd)
          Insert empty rows.
 int mapRow(int row)
          Map a row from this table's row space to the enclosed table's row space.
 boolean moveRows(int startRow, int endRow, int newStartRow)
           
 void pasteCells(Transferable transferable, int startRow, int endRow, int startColumn, int endColumn)
           
 void pasteRows(Transferable transferable, int rowStart)
           
 void removeSortingColumn(int columnIndex)
          Remove a column from the sort order.
 void setValueAt(Object aValue, int rowIndex, int columnIndex)
           
protected  void shuttlesort(int[] from, int[] to, int low, int high)
          (mpp) NOTE: this method is shamelessly ripped from Sun's TableSorter demo class - I did make it look prettier though.
 void sort()
          (Re)Sort the model.
 void tableChanged(TableModelEvent e)
          Called when the enclosing table changes.
 
Methods inherited from class dsto.dfc.swing.table.ProxyTableModel
canDeleteColumns, canInsertColumn, canInsertRows, canPasteRows, deleteColumns, getColumnCount, getColumnName, getRowCount, getSubModel, insertColumn, isCellEditable
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, 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, removeTableModelListener
 

Constructor Detail

SortedTableModel

public SortedTableModel(DfcTableModel model)
Method Detail

addSortingColumn

public SortedTableModel.SortingColumn addSortingColumn(int columnIndex,
                                                       boolean ascending)
Add a column to the end of the list of columns to sort by (ie last in the sort order). If the column is already in the sort order, it is replaced and moved to the end of the order.

Note: this method, and all other methods that change the sort order, do not automatically sort the table - see sort().

Parameters:
columnIndex - The column to sort by.
ascending - True if sorting should be by ascending value, false for descending value.
See Also:
removeSortingColumn(int)

removeSortingColumn

public void removeSortingColumn(int columnIndex)
Remove a column from the sort order.

See Also:
addSortingColumn(int, boolean), clearSorting()

clearSorting

public void clearSorting()
Remove all columns from the sort order.

See Also:
addSortingColumn(int, boolean), removeSortingColumn(int)

findSortingColumn

public SortedTableModel.SortingColumn findSortingColumn(int columnIndex)
Find a sorting column on sortingColumns with a matching column index.


mapRow

public final int mapRow(int row)
Map a row from this table's row space to the enclosed table's row space.


initRowMap

protected void initRowMap()
Initialise row map with row indexes in unsorted (natural) order.


checkUpdateRowMapSize

protected void checkUpdateRowMapSize()
Check whether rowMap needs to be resized based on current table size. Note that this may zap the sorted rows, so be sure sort () gets called sometime after this.


checkModel

protected void checkModel()
Simple check that the enclosed model has not changed without informing us.


sort

public void sort()
(Re)Sort the model.


shuttlesort

protected void shuttlesort(int[] from,
                           int[] to,
                           int low,
                           int high)
(mpp) NOTE: this method is shamelessly ripped from Sun's TableSorter demo class - I did make it look prettier though. This is a home-grown implementation which we have not had time to research - it may perform poorly in some circumstances. It requires twice the space of an in-place algorithm and makes NlogN assigments shuttling the values between the two arrays. The number of compares appears to vary between N-1 and NlogN depending on the initial order but the main reason for using it here is that, unlike qsort, it is stable.


compare

protected int compare(int row1,
                      int row2)
Compare two rows in the table using the current column sort order.


compareRowsByColumn

protected int compareRowsByColumn(int row1,
                                  int row2,
                                  int column)
Compare two rows by their values in a given column.


tableChanged

public void tableChanged(TableModelEvent e)
Called when the enclosing table changes.

Specified by:
tableChanged in interface TableModelListener
Overrides:
tableChanged in class ProxyTableModel

getValueAt

public Object getValueAt(int row,
                         int col)
Specified by:
getValueAt in interface TableModel
Overrides:
getValueAt in class ProxyTableModel

canMoveRows

public boolean canMoveRows(int startRow,
                           int endRow,
                           int newStartRow)
NOTE: only handles case where startRow == endRow.

Specified by:
canMoveRows in interface DfcTableModel
Overrides:
canMoveRows in class ProxyTableModel

deleteRows

public void deleteRows(int rowStart,
                       int rowEnd)
                throws UnsupportedOperationException
NOTE: only handles case where rowStart == rowEnd.

Specified by:
deleteRows in interface DfcTableModel
Overrides:
deleteRows in class ProxyTableModel
Parameters:
rowStart - The row to begin inserting.
rowEnd - The last row to delete.
Throws:
UnsupportedOperationException - if this is not supported.
See Also:
DfcTableModel.canDeleteRows(int, int)

canCopyRows

public boolean canCopyRows(int startRow,
                           int endRow)
Specified by:
canCopyRows in interface DfcTableModel
Overrides:
canCopyRows in class ProxyTableModel

canDeleteCells

public boolean canDeleteCells(int startRow,
                              int endRow,
                              int startColumn,
                              int endColumn)
Description copied from interface: DfcTableModel
Test if a deleteCells () operation is permitted.

Specified by:
canDeleteCells in interface DfcTableModel
Overrides:
canDeleteCells in class ProxyTableModel
Parameters:
startRow - The first row to delete.
endRow - The last row to delete.
startColumn - The first column to delete.
endColumn - The last column to delete.
Returns:
True if the operation is permitted.

pasteCells

public void pasteCells(Transferable transferable,
                       int startRow,
                       int endRow,
                       int startColumn,
                       int endColumn)
                throws IOException,
                       CloneNotSupportedException,
                       UnsupportedFlavorException,
                       UnsupportedOperationException
Specified by:
pasteCells in interface DfcTableModel
Overrides:
pasteCells in class ProxyTableModel
Throws:
IOException
CloneNotSupportedException
UnsupportedFlavorException
UnsupportedOperationException

canDeleteRows

public boolean canDeleteRows(int rowStart,
                             int rowEnd)
Description copied from interface: DfcTableModel
Test if a deleteRows () operation is supported.

Specified by:
canDeleteRows in interface DfcTableModel
Overrides:
canDeleteRows in class ProxyTableModel
Parameters:
rowStart - The row to begin inserting.
rowEnd - The last row to delete.
Returns:
True if the delete is possible.

copyRows

public Transferable copyRows(int startRow,
                             int endRow)
                      throws CloneNotSupportedException,
                             UnsupportedOperationException
Specified by:
copyRows in interface DfcTableModel
Overrides:
copyRows in class ProxyTableModel
Throws:
CloneNotSupportedException
UnsupportedOperationException

copyCells

public Transferable copyCells(int startRow,
                              int endRow,
                              int startColumn,
                              int endColumn)
                       throws CloneNotSupportedException,
                              UnsupportedOperationException
Description copied from interface: DfcTableModel
Copy a rectangular block of cells into an AWT Transferable. The flavors supported by the transferable may include TableModelTransferable.TABLE_MODEL_FLAVOR but this is not required.

Specified by:
copyCells in interface DfcTableModel
Overrides:
copyCells in class ProxyTableModel
Parameters:
startRow - The first row to copy.
endRow - The last row to copy.
startColumn - The first column to copy.
endColumn - The last column to copy.
Throws:
CloneNotSupportedException - if a data item could not be cloned.
UnsupportedOperationException
See Also:
DfcTableModel.canCopyRows(int, int)

moveRows

public boolean moveRows(int startRow,
                        int endRow,
                        int newStartRow)
                 throws UnsupportedOperationException
Specified by:
moveRows in interface DfcTableModel
Overrides:
moveRows in class ProxyTableModel
Throws:
UnsupportedOperationException

canPasteCells

public boolean canPasteCells(Transferable transferable,
                             int startRow,
                             int endRow,
                             int startColumn,
                             int endColumn)
Description copied from interface: DfcTableModel
Should return true if rows may be pasted from a given Transferable into a block of cells. Note that the data in the transferable may not be accessible at this time (eg during a drap and drop operation): calling getTransferData () will result in an IOException in this case.

Specified by:
canPasteCells in interface DfcTableModel
Overrides:
canPasteCells in class ProxyTableModel
Parameters:
transferable - The transferable.
startRow - The row to insert the transferable at.
Returns:
True if the transferable can be pasted at startRow.

setValueAt

public void setValueAt(Object aValue,
                       int rowIndex,
                       int columnIndex)
Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class ProxyTableModel

pasteRows

public void pasteRows(Transferable transferable,
                      int rowStart)
               throws IOException,
                      CloneNotSupportedException,
                      UnsupportedFlavorException,
                      UnsupportedOperationException
Specified by:
pasteRows in interface DfcTableModel
Overrides:
pasteRows in class ProxyTableModel
Throws:
IOException
CloneNotSupportedException
UnsupportedFlavorException
UnsupportedOperationException

canCopyCells

public boolean canCopyCells(int startRow,
                            int endRow,
                            int startColumn,
                            int endColumn)
Description copied from interface: DfcTableModel
Test if a copyCells () operation is permitted.

Specified by:
canCopyCells in interface DfcTableModel
Overrides:
canCopyCells in class ProxyTableModel
Parameters:
startRow - The first row to copy.
endRow - The last row to copy.
startColumn - The first column to copy.
endColumn - The last column to copy.
Returns:
True if the operation is permitted.

insertRows

public void insertRows(int rowStart,
                       int rowEnd)
Description copied from interface: DfcTableModel
Insert empty rows.

Specified by:
insertRows in interface DfcTableModel
Overrides:
insertRows in class ProxyTableModel
Parameters:
rowStart - The row to begin inserting.
rowEnd - The number of rows to insert.
See Also:
DfcTableModel.canInsertRows(int, int)

deleteCells

public void deleteCells(int startRow,
                        int endRow,
                        int startColumn,
                        int endColumn)
                 throws UnsupportedOperationException
Description copied from interface: DfcTableModel
Delete a rectangular block of cells. The deleted cells should be reset to their default values.

Specified by:
deleteCells in interface DfcTableModel
Overrides:
deleteCells in class ProxyTableModel
Parameters:
startRow - The first row to delete.
endRow - The last row to delete.
startColumn - The first column to delete.
endColumn - The last column to delete.
Throws:
UnsupportedOperationException
See Also:
DfcTableModel.canDeleteCells(int, int, int, int)


Copyright © 2008 Commonwealth of Australia