This is more or less a follow up on
http://forum.springframework.org/showthread.php?t=17183
The problem is that the default value of my enum property isn't showing up in my comboboxes when opening the form. The strange this is that they do save.
context:
Code:<bean id="conversionService" class="org.springframework.richclient.application.DefaultConversionService"/> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <ref local="conversionService"/> </property> <property name="targetMethod"> <value>addConverters</value> </property> <property name="arguments"> <list> <list> <bean class="be.kahosl.mammoet.swingclient.util.TigerEnumToText"/> <bean class="be.kahosl.mammoet.swingclient.util.TextToTigerEnum"> <!-- TODO fix me to make targetClass for all Enums --> <property name="targetClassList"> <list> <value type="java.lang.Class">be.kahosl.mammoet.model.order.OrderState</value> <value type="java.lang.Class">be.kahosl.mammoet.model.order.OrderParticipantState</value> <value type="java.lang.Class">be.kahosl.mammoet.model.order.FreightType</value> </list> </property> </bean> </list> </list> </property> </bean>Code:public class TextToTigerEnum extends AbstractConverter { private List<Class> targetClassList; private final boolean allowEmpty; public void setTargetClassList(List<Class> targetClassList) { this.targetClassList = targetClassList; } public TextToTigerEnum() { this(true); } protected TextToTigerEnum(boolean allowEmpty) { this.allowEmpty = allowEmpty; } public Class[] getSourceClasses() { return new Class[]{String.class}; } public Class[] getTargetClasses() { return targetClassList.toArray(new Class[]{}); } protected Object doConvert(Object source, Class targetClass) throws Exception { return (!allowEmpty || StringUtils.hasText((String) source)) ? Enum.valueOf(targetClass, (String) source) : null; } }Code:public class TigerEnumToText extends AbstractConverter { private boolean allowEmpty; public TigerEnumToText() { this(true); } protected TigerEnumToText(boolean allowEmpty) { this.allowEmpty = allowEmpty; } public Class[] getSourceClasses() { return new Class[]{Enum.class}; } public Class[] getTargetClasses() { return new Class[]{String.class}; } protected Object doConvert(Object source, Class targetClass) throws Exception { return (!allowEmpty || source != null) ? source.toString() : null; } }Code:public class TigerEnumComboBoxBinder extends ComboBoxBinder { protected TigerEnumComboBoxBinder() { super(); } // protected Binding doBind(JComponent control, FormModel formModel, String formPropertyPath, Map context) { // Assert.isTrue(control instanceof JComboBox, formPropertyPath); // ComboBoxBinding binding = new ComboBoxBinding((JComboBox)control, formModel, formPropertyPath); // binding.setSelectableItemsHolder(new ValueHolder(theEnumValues)); // return binding; // } protected Binding doBind(JComponent control, FormModel formModel, String formPropertyPath, Map context) { // TODO refactor and cache: see AbstractCachingLabeledEnumResolver // TODO i18n Assert.isTrue(control instanceof JComboBox, formPropertyPath); ComboBoxBinding binding = new ComboBoxBinding((JComboBox) control, formModel, formPropertyPath); applyContext(binding, context); binding.setSelectableItemsHolder(createEnumSelectableItemsHolder(formModel, formPropertyPath)); return binding; } private ValueModel createEnumSelectableItemsHolder(FormModel formModel, String formPropertyPath) { Class propertyType = getPropertyType(formModel, formPropertyPath); Collection valueCollection = new ArrayList(); Class<Enum> enumPropertyType = propertyType; Enum[] enumConstants = enumPropertyType.getEnumConstants(); for (Enum enumConstant : enumConstants) { valueCollection.add(enumConstant.name()); } Assert.notNull(valueCollection, "Unable to resolve enums for class '" + getPropertyType(formModel, formPropertyPath).getName() + "'."); return new ValueHolder(valueCollection); } }
For example my value object:
Code:public class Freight extends PersistableObject { private FreightType freightType = FreightType.BULK; // combobox has all enum values but is empty to start (bad) private double weightInTon = 5.0; // starts on 5 (good) public Freight() { } //... }


Reply With Quote