form2.setFormFieldFactory(new FormFieldFactory() {
@Override
public Field createField(Item item, Object propertyId,
Component uiContext) {
AbstractField f = (AbstractField) DefaultFieldFactory.get()
.createField(item, propertyId, uiContext);
if (propertyId.equals("age")) {
f.setPropertyDataSource(new PropertyFormatter() {
@Override
public Object parse(String formattedValue)
throws Exception {
String str = formattedValue.replaceAll("[^0-9.]",
"");
if (formattedValue.toLowerCase().contains("months")) {
return Double.parseDouble(str) / 12;
}
return Double.parseDouble(str);
}
@Override
public String format(Object value) {
Double dValue = (Double) value;
if (dValue < 1) {
return ((int) (dValue * 12)) + " months";
}
return dValue + " years";
}
});
f.setImmediate(true);
}
return f;
}
});
form2.setItemDataSource(createItem());