org.tbee.swing.table
Class TableModelValueWrapper

java.lang.Object
  extended by org.tbee.swing.table.TableModelValueWrapper
All Implemented Interfaces:
javax.swing.table.TableModel, TableModelForEdit

public class TableModelValueWrapper
extends java.lang.Object
implements TableModelForEdit

This class adds ValueWrapper support to a TableModel. The reason behind this is that there are situations where you want to work with describing objects. For example a JComboBox could want the following behaviour: - the actual value stored could be an integer - but the displayed value should be a text If Integer were not a final class, one could extend that class and add a description plus change the toString method. But alas, Integer is final. So the actual value needs to be wrapped in a class that adds the description. However the model's getValueAt and setValueAt want Integers and not the wrapping classes, so somewhere there has to be a conversion from and to the wrapper. In order to achief this, setValueAt and getValueAt in this class have been extended: - if setValueAt encounters (an instance of) the ValueWrapper, it will continue with the contained value. - if getValueAt gets the value for a column that has a ValueWrapperFactory set, it will wrap the value. (The factory is registered for a column via the setValueWrapper method.) In the JComboBox example, the combobox will work completely with ValueWrapper objects and thus showing the description. This class will unwrap the ValueWrapper to the actual values. You may of course extend ValueWrapper and alter its behaviour (it could have been an interface).

Version:
$Revision: 1.8 $
Author:
$Author: toeukpap $

Nested Class Summary
static class TableModelValueWrapper.ValueWrapper
          This a special class to enable indirect renderers and editors like ComboBoxes.
static interface TableModelValueWrapper.ValueWrapperFactory
          The interface a ValueWrapper factory should implement
 
Field Summary
static java.lang.String SOURCECODE_VERSION
          Standard variable for determining version of a class file.
 
Constructor Summary
TableModelValueWrapper(TableModelForEdit model)
           
 
Method Summary
 int addRowAt(int rowIndex)
           
 void addTableModelListener(javax.swing.event.TableModelListener l)
           
 void deleteRowAt(int rowIndex)
           
 java.lang.Class getColumnClass(int columnIndex)
          If we return wrappers, we return wrappers (this is important for the TableSorter)
 int getColumnCount()
           
 java.lang.String getColumnName(int columnIndex)
           
 java.lang.String getErrorMessageAt(int row, int col)
           
 int getRowCount()
           
 java.lang.Object getValueAt(int row, int column)
          Returns the cell value at row and column.
 TableModelValueWrapper.ValueWrapperFactory getValueWrapper(int columnIdx)
          Registration of the ValueWrapper factories
 java.lang.String getWarnMessageAt(int row, int col)
           
 boolean isCellEditable(int rowIndex, int columnIndex)
           
 void removeTableModelListener(javax.swing.event.TableModelListener l)
           
 void setValueAt(java.lang.Object aValue, int row, int column)
          Sets the value for the cell in the table model at row and column.
 void setValueWrapper(int columnIdx, TableModelValueWrapper.ValueWrapperFactory factory)
          Registration of the ValueWrapper factories
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOURCECODE_VERSION

public static final java.lang.String SOURCECODE_VERSION
Standard variable for determining version of a class file.

See Also:
Constant Field Values
Constructor Detail

TableModelValueWrapper

public TableModelValueWrapper(TableModelForEdit model)
Method Detail

getColumnClass

public java.lang.Class getColumnClass(int columnIndex)
If we return wrappers, we return wrappers (this is important for the TableSorter)

Specified by:
getColumnClass in interface javax.swing.table.TableModel

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Returns the cell value at row and column. If for this column a ValueWrapperFactory is set, the value is wrapped and then returned.

Specified by:
getValueAt in interface javax.swing.table.TableModel
Parameters:
row - the row whose value is to be queried
column - the column whose value is to be queried
Returns:
the Object at the specified cell

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int row,
                       int column)
Sets the value for the cell in the table model at row and column. If the value is a ValueWrapper, then the wrapper value is written to the model.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Parameters:
aValue - the new value
row - the row of the cell to be changed
column - the column of the cell to be changed
See Also:
getValueAt(int, int)

setValueWrapper

public void setValueWrapper(int columnIdx,
                            TableModelValueWrapper.ValueWrapperFactory factory)
Registration of the ValueWrapper factories

Parameters:
columnIdx -
factory -

getValueWrapper

public TableModelValueWrapper.ValueWrapperFactory getValueWrapper(int columnIdx)
Registration of the ValueWrapper factories

Parameters:
columnIdx -
Returns:

addRowAt

public int addRowAt(int rowIndex)
Specified by:
addRowAt in interface TableModelForEdit

addTableModelListener

public void addTableModelListener(javax.swing.event.TableModelListener l)
Specified by:
addTableModelListener in interface javax.swing.table.TableModel

deleteRowAt

public void deleteRowAt(int rowIndex)
Specified by:
deleteRowAt in interface TableModelForEdit

getColumnCount

public int getColumnCount()
Specified by:
getColumnCount in interface javax.swing.table.TableModel

getColumnName

public java.lang.String getColumnName(int columnIndex)
Specified by:
getColumnName in interface javax.swing.table.TableModel

getErrorMessageAt

public java.lang.String getErrorMessageAt(int row,
                                          int col)
Specified by:
getErrorMessageAt in interface TableModelForEdit

getWarnMessageAt

public java.lang.String getWarnMessageAt(int row,
                                         int col)
Specified by:
getWarnMessageAt in interface TableModelForEdit

getRowCount

public int getRowCount()
Specified by:
getRowCount in interface javax.swing.table.TableModel

isCellEditable

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

removeTableModelListener

public void removeTableModelListener(javax.swing.event.TableModelListener l)
Specified by:
removeTableModelListener in interface javax.swing.table.TableModel


Copyright © 2010 KnowledgePlaza. All Rights Reserved.