001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014 package org.apache.tapestry.form;
015
016 import java.io.Serializable;
017 import java.util.ArrayList;
018 import java.util.Arrays;
019 import java.util.Collection;
020 import java.util.List;
021
022 import org.apache.commons.beanutils.BeanUtils;
023
024
025 /**
026 * This class is a property selection model for an object list.
027 * This is used in PropertySelection, MultiplePropertySelection or Palette tapestry components.
028 *
029 * For example, to use for a Hospital class, and have the labels be the hospital names.
030 * <code>
031 * List<Hospital> list = ...;
032 * return new BeanPropertySelectionModel(hospitals, "name");
033 * </code>
034 * This will use getName() on the Hospital object, as its display.
035 *
036 * @author Gabriel Handford
037 */
038 public class BeanPropertySelectionModel implements IPropertySelectionModel, Serializable
039 {
040
041 /** Comment for <code>serialVersionUID</code>. */
042 private static final long serialVersionUID = 3763091973006766644L;
043 private List _list;
044 private String _labelField;
045
046 /**
047 * Build an empty property selection model.
048 */
049 public BeanPropertySelectionModel() {
050 this(Arrays.asList(new Object[0]), null);
051 }
052
053 /**
054 * Build a bean property selection model.
055 * @param list The list
056 * @param labelField The label field
057 */
058 public BeanPropertySelectionModel(List list, String labelField) {
059 _list = list;
060 _labelField = labelField;
061 }
062
063 /**
064 * Build a bean property selection model.
065 * @param c Collection
066 * @param labelField The label field
067 */
068 public BeanPropertySelectionModel(Collection c, String labelField) {
069 _list = new ArrayList(c);
070 _labelField = labelField;
071 }
072
073 /**
074 * Get the number of options.
075 * @return option count
076 */
077 public int getOptionCount() { return _list.size(); }
078
079 /**
080 * Get the option at index.
081 * @param index Index
082 * @return object Object at index
083 */
084 public Object getOption(int index) {
085 return _list.get(index);
086 }
087
088 /**
089 * Get the label at index.
090 * @param index Index
091 * @return label Label at index
092 */
093 public String getLabel(int index) {
094 Object obj = _list.get(index);
095 try {
096 return BeanUtils.getProperty(obj, _labelField);
097 } catch (Exception e) {
098 throw new RuntimeException("Error getting property", e);
099 }
100 }
101
102 /**
103 * Get the value at index.
104 * @param index Index
105 * @return value Value at index
106 */
107 public String getValue(int index) {
108 return String.valueOf(index);
109 }
110
111 /**
112 * Translate value to object.
113 * @param value Value
114 * @return object Object from value
115 */
116 public Object translateValue(String value) {
117 return getOption(Integer.parseInt(value));
118 }
119 }