Hi Kristian.

Thanks for the code. It does not compile right now. Do you have the source for SimpleOptionModel<T> also available?

Thnak you,
Andy




Am 28.01.2010 15:17, schrieb Kristian Marinkovic:
something like this:

usage:
SelectModel m = SelectModelUtils.toBeanSelectModel(someList, "name", "id"
);

explanation:
use the value of the property "name" as label, the value of the "id"
property as value;
assuming that the bean within the list contains this properties
(RuntimeException otherwise)

code:
public class SelectModelUtils
{
         private static final BeanUtilsBean beanUtil = BeanUtilsBean.
getInstance();

     /**
      * Converts a list of beans to a {...@link SelectModel} using the values
of labelField and valueField.
      */
         public static<E>  SelectModel toBeanSelectModel(List<E>  beanList,
String labelField, String valueField)
         {
                 return new SimpleSelectModel(toBeanOptionModels(beanList,
labelField, valueField));
         }

         public static<E>  SelectModel toBeanSelectModel(List<E>  input,
String labelField)
         {
                 return toBeanSelectModel(input, labelField, null);
         }

     /**
      * Converts a list of beans to a list of {...@link OptionModel}s using
getters for label and value.
      */
         private static<E>  List<OptionModel>  toBeanOptionModels(List<E>
beanList, String labelField, String valueField)
     {
         Defense.notNull(beanList, "beanList");

         List<OptionModel>  result = newList();

         for (E bean : beanList)
             result.add(toBeanOptionModel(bean, labelField, valueField));

         return result;
     }

     /**
      * Converts an bean to an {...@link OptionModel} using getters for label
and value.
      */
     private static<E>  OptionModel toBeanOptionModel(E bean, String
labelField, String valueField)
     {
                 if(bean != null) try
                 {
                         String label = beanUtil.getProperty(bean,
labelField);
                         Object value = valueField == null ? bean :
beanUtil.getPropertyUtils().getProperty(bean, valueField);
                         return new SimpleOptionModel<Object>(value,
label);
                 }
                 catch (Exception e)
                 {
                         throw new RuntimeException(e);
                 }
                 return new SimpleOptionModel<E>(bean);
     }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to