-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello,
I need to bind a radiogroup to a property that is of type enum (java 5). I think it is possible with TypeConverters but I couldn't find a documentation to understand how it works, anyone has any tips or examples?
SomeEnumType {A, B, C} class WithEnum { SomeEnumType propertyName // getters and setters } <radiogroup // how to bind it? ...
Regards,
Felipe Cypriano
I also got that InstantiationException when I was trying fmcypriano's EnumRadiogroupConverter.
I think this exception is due to a failure related to default constructor of OrigemEstoqueRadiogroupConverter. So I modified original fmcypriano's solution ensuring default constructor exists and works, then data binding worked too!!!.
BTW, I had to get rid of generics in: EnumRadiogroupConverter<T extends Enum> ...
Thanks fmcypriano for that solution it help me a lot. ;) :)
Regards.
Hi I am running my app in Google App engine and this type converter is not working for me. I am getting the following exception:
Failed to load /WEB-INF/views/npi.zul
Cause: java.lang.InstantiationException: com.bodyfs.ui.GenderRadioConverter.<init>()
org.zkoss.zk.ui.UiException: java.lang.InstantiationException: com.bodyfs.ui.GenderRadioConverter.<init>()
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120)
at org.zkoss.lang.Classes.newInstance(Classes.java:76)
at org.zkoss.lang.Exceptions.wrap(Exceptions.java:162)
at org.zkoss.zk.ui.UiException$Aide.wrap(UiException.java:44)
at org.zkoss.zkplus.databind.Binding.setConverter(Binding.java:312)
Any ideas how to make this to work?
@fmcypriano : thanks, exactly what i was looking for!
>> I think it is working
Yes, but like my example only for the selectedItem. You could not load/create the radiogroup with a model like i.e. a combobox.
/Robert
I think it is working:
package vc.zk.databinding; import org.zkoss.zk.ui.Component; import org.zkoss.zkplus.databind.RadiogroupSelectedItemConverter; /** * Created by Felipe Cypriano * Date: 02/06/2009 * Time: 09:24:34 * * <p>The radio's value attribute must be the same as the enum constant</p> * <strong>Thread Safe</strong> */ public class EnumRadiogroupConverter<T extends Enum> extends RadiogroupSelectedItemConverter { private final Class<T> className; public EnumRadiogroupConverter(Class<T> className) { this.className = className; } @Override public Object coerceToUi(Object val, Component component) { if (val instanceof Enum) { Enum value = (Enum) val; String constanteName = value.name(); return super.coerceToUi(constanteName, component); } else if (val == null){ return null; } else { throw new IllegalArgumentException("val Object must be an Enum"); } } @Override public Object coerceToBean(Object val, Component component) { String enumName = (String) super.coerceToBean(val, component); return Enum.valueOf(className, enumName); } }
// public class OrigemEstoqueRadiogroupConverter extends EnumRadiogroupConverter<OrigemEstoque>{ ... <radiogroup selectedItem="@{pedidoVenda.origemEstoque, converter='vc.zk.databinding.OrigemEstoqueRadiogroupConverter'}"> ...
I'm doing the tests right now.
There is no model for the radiogroup - so you could not work with @databinding to setup up the group.
You could try to mix EL's with @databinding.
<radiogroup selectedItem="@{selectedItem}"> <radio foreach="${tryEnum}"...>
/Robert
Check my homepage for some simple examples.
Asked: 2009-06-02 12:56:58 +0800
Seen: 2,908 times
Last updated: Jun 10 '11