just a implementation of the interface with a constructor that accepts the 
value and the label

public class SimpleOptionModel<T> implements OptionModel
{
        private final T value;
        private final String label;
 
        public SimpleOptionModel(T value, String label)
        {
                this.label = label;
                this.value = value;
        }
..........

        



Telefon +43 (0)662 4670-6676
Fax +43 (0)662 4670-16676
kristian.marinko...@porsche.co.at

Porsche Informatik Gesellschaft m.b.H. | A – 5101 Bergheim | 
Handelszentrum 7 
Sitz: Salzburg | FN 72830 d / Landesgericht Salzburg | DVR 88439 | UID ATU 
36773309
http://www.porsche-informatik.at




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


An
Tapestry users <users@tapestry.apache.org>
Kopie

Thema
Re: Easy Way to create a select model?









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