use of java.beans.PropertyDescriptor in project nebula.widgets.nattable by eclipse.
the class ReflectiveColumnPropertyAccessor method setDataValue.
@Override
public void setDataValue(R rowObj, int columnIndex, Object newValue) {
try {
PropertyDescriptor propertyDesc = getPropertyDescriptor(rowObj, columnIndex);
Method writeMethod = propertyDesc.getWriteMethod();
if (writeMethod == null) {
throw new RuntimeException(// $NON-NLS-1$
"Setter method not found in backing bean for value at column index: " + columnIndex);
}
writeMethod.invoke(rowObj, newValue);
} catch (IllegalArgumentException ex) {
// $NON-NLS-1$
LOG.error("Data type being set does not match the data type of the setter method in the backing bean", ex);
} catch (Exception e) {
LOG.error(e);
// $NON-NLS-1$
throw new RuntimeException("Error while setting data value");
}
}
use of java.beans.PropertyDescriptor in project nebula.widgets.nattable by eclipse.
the class ReflectiveColumnPropertyAccessor method getPropertyDescriptor.
/**
* @param rowObj
* The Java Bean for which the {@link PropertyDescriptor} is
* requested.
* @param propertyName
* The name of the property for which the {@link PropertyDescriptor}
* is requested.
* @return The {@link PropertyDescriptor} that describes the property with the
* given name in the given Java Bean object that exports it via a pair
* of accessor methods.
* @throws IntrospectionException
* if an exception occurs during introspection
* @since 1.6
*/
protected PropertyDescriptor getPropertyDescriptor(Object rowObj, String propertyName) throws IntrospectionException {
synchronized (rowObj) {
Map<String, PropertyDescriptor> descriptorMap = this.propertyDescriptorMap.get(rowObj.getClass());
if (descriptorMap == null) {
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(rowObj.getClass()).getPropertyDescriptors();
Map<String, PropertyDescriptor> propertiesByAttribute = new HashMap<String, PropertyDescriptor>();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
propertiesByAttribute.put(propertyDescriptor.getName(), propertyDescriptor);
}
descriptorMap = propertiesByAttribute;
this.propertyDescriptorMap.put(rowObj.getClass(), propertiesByAttribute);
}
return descriptorMap.get(propertyName);
}
}
use of java.beans.PropertyDescriptor in project nebula.widgets.nattable by eclipse.
the class HierarchicalHelper method getPropertyDescriptor.
/**
* Retrieves the {@link PropertyDescriptor} for the given object and
* property name.
*
* @param rowObj
* The object for which the {@link PropertyDescriptor} is
* requested.
* @param propertyName
* The name of the property for which the
* {@link PropertyDescriptor} is requested.
* @param propertyDescriptorMap
* The map of already identified {@link PropertyDescriptor}s, to
* avoid multiple lookups via reflection.
* @return The {@link PropertyDescriptor} for the property with the given
* name in the given object.
* @throws IntrospectionException
* if an exception occurs during introspection
*/
private static PropertyDescriptor getPropertyDescriptor(Object rowObj, String propertyName, Map<Class<?>, Map<String, PropertyDescriptor>> propertyDescriptorMap) throws IntrospectionException {
synchronized (rowObj) {
Map<String, PropertyDescriptor> descriptorMap = propertyDescriptorMap.get(rowObj.getClass());
if (descriptorMap == null) {
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(rowObj.getClass()).getPropertyDescriptors();
Map<String, PropertyDescriptor> propertiesByAttribute = new HashMap<String, PropertyDescriptor>();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
propertiesByAttribute.put(propertyDescriptor.getName(), propertyDescriptor);
}
descriptorMap = propertiesByAttribute;
propertyDescriptorMap.put(rowObj.getClass(), propertiesByAttribute);
}
return descriptorMap.get(propertyName);
}
}
use of java.beans.PropertyDescriptor in project nebula.widgets.nattable by eclipse.
the class HierarchicalReflectiveColumnPropertyAccessor method setDataValue.
@Override
public void setDataValue(HierarchicalWrapper rowObj, int columnIndex, Object newValue) {
String propertyName = getColumnProperty(columnIndex);
String[] split = propertyName.split(HierarchicalHelper.PROPERTY_SEPARATOR_REGEX);
Object levelObject = rowObj.getObject(split.length - 1);
if (levelObject != null) {
try {
PropertyDescriptor propertyDesc = getPropertyDescriptor(levelObject, split[split.length - 1]);
Method writeMethod = propertyDesc.getWriteMethod();
if (writeMethod == null) {
throw new RuntimeException(// $NON-NLS-1$
"Setter method not found in backing bean for value at column index: " + columnIndex);
}
writeMethod.invoke(levelObject, newValue);
} catch (IllegalArgumentException ex) {
// $NON-NLS-1$
LOG.error("Data type being set does not match the data type of the setter method in the backing bean", ex);
} catch (Exception e) {
LOG.error(e);
// $NON-NLS-1$
throw new RuntimeException("Error while setting data value");
}
}
}
use of java.beans.PropertyDescriptor in project nebula.widgets.nattable by eclipse.
the class HierarchicalReflectiveColumnPropertyAccessor method getDataValue.
@Override
public Object getDataValue(HierarchicalWrapper rowObj, int columnIndex) {
String propertyName = getColumnProperty(columnIndex);
String[] split = propertyName.split(HierarchicalHelper.PROPERTY_SEPARATOR_REGEX);
Object levelObject = rowObj.getObject(split.length - 1);
if (levelObject != null) {
try {
PropertyDescriptor propertyDesc = getPropertyDescriptor(levelObject, split[split.length - 1]);
Method readMethod = propertyDesc.getReadMethod();
return readMethod.invoke(levelObject);
} catch (Exception e) {
LOG.warn(e);
throw new RuntimeException(e);
}
}
return null;
}
Aggregations