de.kupzog.ktable
Class KTableSortedModel

java.lang.Object
  extended by de.kupzog.ktable.KTableDefaultModel
      extended by de.kupzog.ktable.KTableSortedModel
All Implemented Interfaces:
KTableModel
Direct Known Subclasses:
SortableModelExample

public abstract class KTableSortedModel
extends KTableDefaultModel

Provides a transparently sorted tablemodel: For model calls, the row indices are mapped according to the sorting.

The approach taken here has one drawback: the KTable does not know anything about the sorting. Thus, when setting selections, listening to selections or dealing with the KTable directly, you have to manually map the row indices using mapRowIndexToTable(int).

Author:
Lorenz Maierhofer

Constructor Summary
KTableSortedModel()
           
 
Method Summary
 org.eclipse.swt.graphics.Point belongsToCell(int col, int row)
          Sorting disables all cell spanning.
 KTableCellEditor getCellEditor(int col, int row)
          Maps between the KTable row index and the model row index!
 KTableCellRenderer getCellRenderer(int col, int row)
          Maps between the KTable row index and the model row index!
 java.lang.Object getContentAt(int col, int row)
          Maps between the KTable row index and the model row index!
 int getSortColumn()
           
 int getSortState()
           
 java.lang.String getTooltipAt(int col, int row)
          Returns the tooltip for the given cell.
 void initialize()
          This method initializes the provided baseimplementation of the model properly.
 int mapRowIndexToModel(int row)
          Maps the given row index as it is requested by the KTable to the real model index as it is seen from within the model.
 int mapRowIndexToTable(int row)
          Maps the given row index as seen from a model implementor to the row index needed by the KTable.
 void setContentAt(int col, int row, java.lang.Object value)
          Maps between the KTable row index and the model row index!
 void sort(KTableSortComparator comparator)
          Sorts the model elements so that the retrieval methods by index (e.g. of type method(int col, int row)) return the content ordered in the given direction.
 
Methods inherited from class de.kupzog.ktable.KTableDefaultModel
doBelongsToCell, doGetCellEditor, doGetCellRenderer, doGetColumnCount, doGetContentAt, doGetRowCount, doGetTooltipAt, doSetContentAt, getColumnCount, getColumnWidth, getFirstRowHeight, getFixedColumnCount, getFixedRowCount, getInitialColumnWidth, getInitialFirstRowHeight, getInitialRowHeight, getRowCount, getRowHeight, isFixedCell, isHeaderCell, setColumnWidth, setFirstRowHeight, setRowHeight
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface de.kupzog.ktable.KTableModel
getFixedHeaderColumnCount, getFixedHeaderRowCount, getFixedSelectableColumnCount, getFixedSelectableRowCount, getRowHeightMinimum, isColumnResizable, isRowResizable
 

Constructor Detail

KTableSortedModel

public KTableSortedModel()
Method Detail

initialize

public void initialize()
Description copied from class: KTableDefaultModel
This method initializes the provided baseimplementation of the model properly.

This is not done in the constructor because there might be cases where some base data is set in the constructor and must be present for properly working model getter methods.

MUST BE CALLED BY ANY SUBCLASS!

Overrides:
initialize in class KTableDefaultModel

getSortState

public final int getSortState()
Returns:
Returns the current sort state of the sorted model. Can be:

  • SORT_NONE: Unsorted (default)
  • SORT_UP: Sorted with largest value up
  • SORT_DOWN: Sorted with largest value down.

sort

public void sort(KTableSortComparator comparator)
Sorts the model elements so that the retrieval methods by index (e.g. of type method(int col, int row)) return the content ordered in the given direction.

Note: To make the table reflect this sorting, it must be refreshed/redrawn!

Note: Often it is desired that there is some visual sign of how the sorting is.

Parameters:
comparator - The KTableSortComparator that knows how to sort the rows!

getSortColumn

public final int getSortColumn()
Returns:
Returns the column that is used for sorting, or -1 if no sorting is present or sorting is SORT_NONE.

mapRowIndexToModel

public int mapRowIndexToModel(int row)
Maps the given row index as it is requested by the KTable to the real model index as it is seen from within the model.

Overrides:
mapRowIndexToModel in class KTableDefaultModel
Parameters:
row - The row index used by the KTable.
Returns:
Returns the row index as processed by the model.

mapRowIndexToTable

public int mapRowIndexToTable(int row)
Maps the given row index as seen from a model implementor to the row index needed by the KTable.

Overrides:
mapRowIndexToTable in class KTableDefaultModel
Parameters:
row - The row index as used in the model.
Returns:
Returns the row index as needed by the KTable.

getContentAt

public java.lang.Object getContentAt(int col,
                                     int row)
Maps between the KTable row index and the model row index!

Accesses the sorted model accordingly. Delegates the real content retrieval to the method doGetContentAt(int, int).

Specified by:
getContentAt in interface KTableModel
Overrides:
getContentAt in class KTableDefaultModel
See Also:
KTableModel.getContentAt(int, int)

getTooltipAt

public java.lang.String getTooltipAt(int col,
                                     int row)
Returns the tooltip for the given cell.

Calls the method doGetTooltipAt() to retrieve content.

Specified by:
getTooltipAt in interface KTableModel
Overrides:
getTooltipAt in class KTableDefaultModel
Parameters:
col - The column index
row - The row index
Returns:
Returns the text that should be displayed when the tooltip for the cell is shown to the user.
See Also:
KTableModel.getTooltipAt(int, int)

getCellEditor

public KTableCellEditor getCellEditor(int col,
                                      int row)
Maps between the KTable row index and the model row index!

Delegates the real editor retrieval to the method doGetCellEditor(int, int).

Specified by:
getCellEditor in interface KTableModel
Overrides:
getCellEditor in class KTableDefaultModel
Parameters:
col - The column index
row - The row index
Returns:
Returns an instance of KTableCellEditor that will be responsible of showing an editor control and writing back the changed value by calling setContentAt().
See Also:
KTableModel.getCellEditor(int, int)

setContentAt

public void setContentAt(int col,
                         int row,
                         java.lang.Object value)
Maps between the KTable row index and the model row index!

Calls the method doSetContentAt(int, int, Object) to actually set the content of a table cell to the model.

Specified by:
setContentAt in interface KTableModel
Overrides:
setContentAt in class KTableDefaultModel
Parameters:
col - The column index.
row - The row index.
See Also:
KTableModel.setContentAt(int, int, java.lang.Object)

getCellRenderer

public KTableCellRenderer getCellRenderer(int col,
                                          int row)
Maps between the KTable row index and the model row index!

Calls the method doGetCellRenderer(int, int) to retrieve the cell renderer for a cell.

Specified by:
getCellRenderer in interface KTableModel
Overrides:
getCellRenderer in class KTableDefaultModel
Parameters:
col - The column index
row - The row index
Returns:
Returns the cell renderer that is responsible for drawing the cell.
See Also:
KTableModel.getCellRenderer(int, int)

belongsToCell

public org.eclipse.swt.graphics.Point belongsToCell(int col,
                                                    int row)
Sorting disables all cell spanning. The behavior is: spanned cells are spittet, but all cells originally spanned get the content of the spanned cell.

Specified by:
belongsToCell in interface KTableModel
Overrides:
belongsToCell in class KTableDefaultModel
Parameters:
col - the column index
row - the row index
Returns:
Return the given cell, or a cell the given cell should be merged with. Point.x corresponds to column index, Point.y corresponds to row index.
See Also:
KTableModel.belongsToCell(int, int)