I’m having a hard time with ComboBox’s and Converters.
I’ve searched the forums and what I found and tried doesn’t seem to be working for me.
I’m going from Vaadin 6 to 7 and I’m not liking the Converters at all!
Vaadin 6 I had the Select class and never had to deal with Converters, it just worked.
storeCatRuleId is a BigDecimal. I added my own converter but I keep getting this error:
java.lang.ClassCastException
ERROR MESSAGE: java.math.BigDecimal cannot be cast to java.lang.String
BigDecimalToStringConvertor.convertToModel(BigDecimalToStringConvertor.java:1)
Not sure what I’m doing wrong. Any help would be appreciated.
//ComboBox
public StoreCategoryRuleSelect(String caption){
setCaption(caption);
setItemCaptionMode(ItemCaptionMode.PROPERTY);
setItemCaptionPropertyId("storeCategoryRule");
setFilteringMode(FilteringMode.CONTAINS);
BeanContainer<BigDecimal, XxomMooRulesLov> dataSource = new BeanContainer<BigDecimal, XxomMooRulesLov>(XxomMooRulesLov.class);
dataSource.setBeanIdProperty("storeCatRuleId");
dataSource.addAll((new ArrayList<XxomMooRulesLov>()));
this.setContainerDataSource(dataSource);
}
//ComboBox creation
StoreCategoryRuleSelect storeCatRuleField = new StoreCategoryRuleSelect("Store Category");
storeCatRuleField.setConverter((Converter)new BigDecimalToStringConvertor());
binder.bind(storeCatRuleField, "storeCatRuleId");
form.addComponent(storeCatRuleField);
//Converter
public class BigDecimalToStringConvertor implements Converter<String,BigDecimal> {
private static final long serialVersionUID = 1921329645985949528L;
private DecimalFormat df;
public BigDecimalToStringConvertor() {
df = new DecimalFormat("#0");
}
public BigDecimalToStringConvertor(String decimalFormatPattern) {
df = new DecimalFormat(decimalFormatPattern);
}
@Override
public BigDecimal convertToModel(String value, Class<? extends BigDecimal> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
if (value != null) {
return new BigDecimal(value);
}
return null;
}
@Override
public String convertToPresentation(BigDecimal value, Class<? extends String> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
if (value != null) {
return df.format(value.doubleValue());
}
return null;
}
@Override
public Class<BigDecimal> getModelType() {
return BigDecimal.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
}