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);
    } 
}



Andy Pahne <andy.pa...@gmail.com> 
28.01.2010 15:14
Bitte antworten an
"Tapestry users" <users@tapestry.apache.org>


An
users@tapestry.apache.org
Kopie

Thema
Easy Way to create a select model?








I feel a little dumb, because for years I have been using Tapestry.

Is there an _easy_ way og creating a SelectModel like this one (not so 
uncommon one):

    value    label
---------------------
      1         Jan
      2         Feb
      ...


I had a look at the documentation. But I cannot believe I have to deal 
with SelectModel, OptionModel, OptionGroupModel and whatelse for this 
simple usecase.

I also had a look at the Wiki. I found four pages about selects. I 
cannot believe that I have to use such bloat for my usecase.

I surely must be missing something, or not?

Andy




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


Reply via email to