ilog.views.util.data
Class IlvTableModelMapper

java.lang.Object
  extended by ilog.views.util.data.IlvTableModelMapper
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
IlvBasicTableModelMapper

public class IlvTableModelMapper
extends Object
implements Cloneable

This class helps in converting values from a given 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);
    }
   }
 }
 

Since:
JViews 6.0

Constructor Summary
IlvTableModelMapper(TableModel model)
          Creates a IlvTableModelMapper to map values from the given TableModel to properties of another model and conversely.
 
Method Summary
 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 getPropertyName(int columnIndex)
          Returns the name of the properties that must be updated when the column at index columnIndex is changed.
 Collection 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 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.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IlvTableModelMapper

public IlvTableModelMapper(TableModel model)
Creates a 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.

Method Detail

clone

public Object clone()
Clone the IlvTableModelMapper.

Overrides:
clone in class Object

getTableModel

public final TableModel getTableModel()
Returns the TableModel this IlvTableModelMapper is working on.


setTableModel

public final void setTableModel(TableModel model)
Changes the TableModel this IlvTableModelMapper is working on.


addPropertyDescriptor

public final void addPropertyDescriptor(String propertyName,
                                        IlvTableModelPropertyDescriptor descriptor,
                                        Class requiredType)
Registers an 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.

Parameters:
propertyName -
descriptor -
requiredType -

removePropertyDescriptor

public final void removePropertyDescriptor(String propertyName)
Unregister the IlvTableModelPropertyDescriptor for the given property.

Parameters:
propertyName -

removePropertyDescriptor

public final void removePropertyDescriptor(String propertyName,
                                           boolean value)
Unregister the IlvTableModelPropertyDescriptor for the given property.

Parameters:
propertyName - the property name
value - if true remove also the requiredType corresponding to the property.

getPropertyDescriptor

public final IlvTableModelPropertyDescriptor getPropertyDescriptor(String propertyName)
Returns the IlvTableModelPropertyDescriptor for the given property.

Parameters:
propertyName -
See Also:
addPropertyDescriptor(String, IlvTableModelPropertyDescriptor, Class)

getRequiredType

public final Class getRequiredType(String propertyName)
Returns the required type for the given property if any.

Parameters:
propertyName -
See Also:
addPropertyDescriptor(String, IlvTableModelPropertyDescriptor, Class)

clear

public final void clear()
Clears all the IlvTableModelPropertyDescriptor instances and/or required types registered on this class.


getProperty

public final Object getProperty(String propertyName,
                                int rowIndex)
                         throws IlvConvertException,
                                IlvTableModelMappingException
Lookup the 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.

Parameters:
propertyName - the property to consider
rowIndex - the row index
Returns:
the property value
Throws:
IlvConvertException - if there is a problem during the conversion
IlvTableModelMappingException - if the property cannot be found in the TableModel.

setProperty

public final void setProperty(String propertyName,
                              int rowIndex,
                              Object propertyValue)
                       throws IlvConvertException,
                              IlvTableModelMappingException
Sets back the given property value inside the 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.

Parameters:
propertyName - the property to consider
rowIndex - the row index
propertyValue - the value to set
Throws:
IlvConvertException - if there is a problem during the conversion
IlvTableModelMappingException - if the property cannot be found in the TableModel.

isPropertyEditable

public final boolean isPropertyEditable(String propertyName,
                                        int rowIndex)
Returns whether or not the given property is editable (i.e. setProperty(String, int, Object) will actually set back the value in the table) or not.

Parameters:
propertyName - the property to consider
rowIndex - the row index

getPropertyName

public final List getPropertyName(int columnIndex)
Returns the name of the properties that must be updated when the column at index columnIndex is changed.

Parameters:
columnIndex - the index of the column

getPropertyNames

public final Collection getPropertyNames()
Returns a copy of the list of property names for which descriptors and/or required types have been registered on this mapper.


getSupportedPropertyNames

public final Collection getSupportedPropertyNames()
Returns a copy of the list of supported property names that have been registered on this mapper.

See Also:
getPropertyNames(), isPropertySupported(String)

isPropertySupported

public final boolean isPropertySupported(String propertyName)
Returns whetever a given property can be mapped with this mapper. Which means it either have property descriptor attached to it or can be recognized using the findColumn(String) method.

See Also:
getPropertyDescriptor(String)

check

public final boolean check()
Checks wether properties mapped to single columns can be translated from the column type to the required type or not.

Returns:
true if mapping is ok, false otherwise.

findColumn

protected int findColumn(String propertyName)
Find the column corresponding to the given property name.

Parameters:
propertyName -

serialize

public void serialize(Element element)
Writes the description of a table model mapper to an XML element.

Parameters:
element - The XML element to which the table model mapper description must be written.

deserialize

public static IlvTableModelMapper deserialize(Element element)
                                       throws Exception
Reads the description of a table model mapper from an XML element.

Parameters:
element - The XML element from which the table model mapper description must be read.
Returns:
A new IlvTableModelMapper.
Throws:
Exception - If an error occurred while reading the description.


Copyright © 1996-2007 ILOG S.A. All rights reserved.   Documentation homepage.