You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Dan <l0...@gmail.com> on 2005/06/30 20:40:43 UTC
[snippet] Java 1.5 Enums IPropertySelectionModel facade
Hi,
The snippet below is an implementation IPropertySelectionModel
which is in fact facade over JDK1.5 enums.
Usage:
public enum MySampleEnum {
ONE, TWO, THREE;
}
IPropertySelectionModel model = new EnumPropertySelectionModel(
MySampleEnum.ONE );
Maybe it will be useful for someone...
---- start -----
package foo.bar;
import java.lang.reflect.Method;
import org.apache.tapestry.form.IPropertySelectionModel;
public class EnumPropertySelectionModel implements IPropertySelectionModel {
public EnumPropertySelectionModel(Enum en){
clazz = en.getClass();
try {
Method m = en.getClass().getMethod("values", new Class[0]);
Object ret = m.invoke(null, new Object[0]);
values = (Enum[]) ret;
}catch(RuntimeException e){
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int getOptionCount() {
return values.length;
}
public Object getOption(int index) {
return values[index];
}
public String getLabel(int index) {
return values[index].name();
}
public String getValue(int index) {
return Integer.toString(index);
}
public Object translateValue(String value) {
return Enum.valueOf(clazz, value);
}
private Enum[] values;
private Class<? extends Enum> clazz;
}
---- stop -----
Regards
Dan
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org