public class IlvTableModelMapper extends Object implements Cloneable
TableModel
to
properties of another model and to conversely set back values inside the
TableModel
from the foreign model. The default behavior is just
to look for values for a particular property in the column that have the name
of the property. More complex cases can be configured using
addPropertyDescriptor(String, IlvTableModelPropertyDescriptor, Class)
method. Here is a typical usage example:
TableModel tableModel = new ...; IlvTableModelMapper mapper = new IlvTableModelMapper(tableModel); mapper.addPropertyDescriptor("ID", new IlvBasicTableModelPropertyDescriptor(0), String.class); mapper.addPropertyDescriptor("From", new IlvBasicTableModelPropertyDescriptor(1), String.class); mapper.addPropertyDescriptor("To", new IlvBasicTableModelPropertyDescriptor(2), String.class); IlvTableModelPropertyDesctiptor specialDescriptor = new IlvTableModelPropertyDescriptor() { public Object getProperty(TableModel model, int rowIndex) { return model.getValueAt(3, rowIndex)+"-"+model.getValueAt(4, rowIndex); } public void setProperty(TableModel model, int rowIndex, Object propertyValue) throws IlvConvertException { // ... whatever is needed to set back the values at the right place } public int[] getColumns(TableModel model) { return new int[] {3, 4}; } }; mapper.addPropertyDescriptor("SpecialProperty", specialDescriptor, String.class); mapper.addPropertyDescriptor("Number", new IlvBasicTableModelPropertyDescriptor(5), Integer.class); IlvConvert.addConverter(... // add an IlvConverter from String to Integer for example // ...); OtherModel model = new OtherModel(tableModel, mapper);Inside the other model code to provide values from the table model:
return (String) mapper.getProperty("ID", objectIndex);Inside the other model code to set values back to the table mode:
mapper.setProperty("ID", objectIndex, "anID");Finally in order to be able to notify the OtherModel that a particular property have changed when one cell of the underlying TableModel has changed something like the following can be done:
public void tableChanged(TableModelEvent e) { if (event.getType() == TableModelEvent.UPDATE) { // list of properties impacted... List pName = mapper.getPropertyName(event.getColumn()); for (int rowIndex = event.getFirstRow(); rowIndex <= event.getLastRow(); rowIndex++) { // for each property impacted (usually one) firePropertyHasChanged(pName, rowIndex); } } }
Constructor and Description |
---|
IlvTableModelMapper(TableModel model)
Creates a
IlvTableModelMapper to map values from the given
TableModel to properties of another model and conversely. |
Modifier and Type | Method and Description |
---|---|
void |
addPropertyDescriptor(String propertyName,
IlvTableModelPropertyDescriptor descriptor,
Class requiredType)
Registers an
IlvTableModelPropertyDescriptor and/or a required
type for a given property. |
boolean |
check()
Checks wether properties mapped to single columns can be translated from
the column type to the required type or not.
|
void |
clear()
Clears all the
IlvTableModelPropertyDescriptor instances
and/or required types registered on this class. |
Object |
clone()
Clone the
IlvTableModelMapper . |
static IlvTableModelMapper |
deserialize(Element element)
Reads the description of a table model mapper from an XML element.
|
protected int |
findColumn(String propertyName)
Find the column corresponding to the given property name.
|
Object |
getProperty(String propertyName,
int rowIndex)
Lookup the
TableModel at row index rowIndex and
returns a value for a given property. |
IlvTableModelPropertyDescriptor |
getPropertyDescriptor(String propertyName)
Returns the
IlvTableModelPropertyDescriptor for the given
property. |
List<String> |
getPropertyName(int columnIndex)
Returns the name of the properties that must be updated when the column at
index
columnIndex is changed. |
Collection<String> |
getPropertyNames()
Returns a copy of the list of property names for which descriptors and/or
required types have been registered on this mapper.
|
Class |
getRequiredType(String propertyName)
Returns the required type for the given property if any.
|
Collection<String> |
getSupportedPropertyNames()
Returns a copy of the list of supported property names that have been
registered on this mapper.
|
TableModel |
getTableModel()
Returns the
TableModel this IlvTableModelMapper
is working on. |
boolean |
isPropertyEditable(String propertyName,
int rowIndex)
Returns whether or not the given property is editable (i.e.
|
boolean |
isPropertySupported(String propertyName)
Returns whetever a given property can be mapped with this mapper.
|
void |
removePropertyDescriptor(String propertyName)
Unregister the
IlvTableModelPropertyDescriptor for the given
property. |
void |
removePropertyDescriptor(String propertyName,
boolean value)
Unregister the
IlvTableModelPropertyDescriptor for the given
property. |
void |
serialize(Element element)
Writes the description of a table model mapper to an XML element.
|
void |
setProperty(String propertyName,
int rowIndex,
Object propertyValue)
Sets back the given property value inside the
TableModel at
the row index rowIndex . |
void |
setTableModel(TableModel model)
Changes the
TableModel this IlvTableModelMapper
is working on. |
public IlvTableModelMapper(TableModel model)
IlvTableModelMapper
to map values from the given
TableModel
to properties of another model and conversely. The
default behavior is to look for values into the TableModel
at
the column which names is the same as the queried property.public final TableModel getTableModel()
TableModel
this IlvTableModelMapper
is working on.public final void setTableModel(TableModel model)
TableModel
this IlvTableModelMapper
is working on.public final void addPropertyDescriptor(String propertyName, IlvTableModelPropertyDescriptor descriptor, Class requiredType)
IlvTableModelPropertyDescriptor
and/or a required
type for a given property. One of the two parameters (
descriptor
or requiredType
) can be
null
. When a requiredType
is set, after the look
up in the TableModel
the IlvTableModelMapper
will
try to convert the value to this type using the IlvConvert
class.propertyName
- The name of the property.descriptor
- The table model property descriptor.requiredType
- The required type.public final void removePropertyDescriptor(String propertyName)
IlvTableModelPropertyDescriptor
for the given
property.propertyName
- The name of the property.public final void removePropertyDescriptor(String propertyName, boolean value)
IlvTableModelPropertyDescriptor
for the given
property.propertyName
- The name of the property.value
- If true
remove also the requiredType corresponding to
the property.public final IlvTableModelPropertyDescriptor getPropertyDescriptor(String propertyName)
IlvTableModelPropertyDescriptor
for the given
property.propertyName
- The name of the property.addPropertyDescriptor(String, IlvTableModelPropertyDescriptor, Class)
public final Class getRequiredType(String propertyName)
propertyName
- The name of the property.addPropertyDescriptor(String, IlvTableModelPropertyDescriptor, Class)
public final void clear()
IlvTableModelPropertyDescriptor
instances
and/or required types registered on this class.public final Object getProperty(String propertyName, int rowIndex) throws IlvConvertException, IlvTableModelMappingException
TableModel
at row index rowIndex
and
returns a value for a given property. This is first trying to find a
IlvTableModelPropertyDescriptor
for this property and use it for
getting the value. If not available, the method will look into the table
model for a column named as the property to get the value in it. If a
requiredType
has been set, the method is using
IlvConvert
to perform that additional conversion. You can register
IlvConverter
instances on the
IlvConvert
class. When no conversion can be performed an exception
is thrown. When the property cannot be found in the table an exception is
also thrown.propertyName
- The name of the property.rowIndex
- The row index.IlvConvertException
- if there is a problem during the conversionIlvTableModelMappingException
- if the property cannot be found in the TableModel
.public final void setProperty(String propertyName, int rowIndex, Object propertyValue) throws IlvConvertException, IlvTableModelMappingException
TableModel
at
the row index rowIndex
. This is first trying to find a
IlvTableModelPropertyDescriptor
for this property and use it for
the job. If not available, the method will look into the table model for a
column named as the property to set the value in it while trying to convert
it to the right type using IlvConvert
. When no conversion can be
performed an exception is thrown. When the property cannot be found in the
table an exception is also thrown.propertyName
- The property to consider.rowIndex
- The row index.propertyValue
- The value to set.IlvConvertException
- if there is a problem during the conversionIlvTableModelMappingException
- if the property cannot be found in the TableModel
.public final boolean isPropertyEditable(String propertyName, int rowIndex)
setProperty(String, int, Object)
will actually set back the value
in the table) or not.propertyName
- The name of the property.rowIndex
- The row index.public final List<String> getPropertyName(int columnIndex)
columnIndex
is changed.columnIndex
- The index of the column.public final Collection<String> getPropertyNames()
public final Collection<String> getSupportedPropertyNames()
getPropertyNames()
,
isPropertySupported(String)
public final boolean isPropertySupported(String propertyName)
findColumn(String)
method.getPropertyDescriptor(String)
public final boolean check()
true
if mapping is ok, false
otherwise.protected int findColumn(String propertyName)
propertyName
- The name of the property.public void serialize(Element element)
element
- The XML element to which the table model mapper description must
be written.public static IlvTableModelMapper deserialize(Element element) throws Exception
element
- The XML element from which the table model mapper description must
be read.Exception
- If an error occurred while reading the description.© Copyright Rogue Wave Software, Inc. 1997, 2018. All Rights Reserved.