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
015 package org.apache.tapestry.form;
016
017 /**
018 * Decorates an underlying {@link IPropertySelectionModel}adding an initial property. The label,
019 * option, and value of the initial property are configurable.
020 *
021 * @author Paul Ferraro
022 * @since 4.0
023 */
024 public class LabeledPropertySelectionModel implements IPropertySelectionModel
025 {
026 /**
027 * Empty model implementation. Avoids NullPointerExceptions when default constructor is used.
028 */
029 private static final IPropertySelectionModel EMPTY_MODEL = new IPropertySelectionModel()
030 {
031 /**
032 * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount()
033 */
034 public int getOptionCount()
035 {
036 return 0;
037 }
038
039 /**
040 * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int)
041 */
042 public Object getOption(int index)
043 {
044 return null;
045 }
046
047 /**
048 * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int)
049 */
050 public String getLabel(int index)
051 {
052 return null;
053 }
054
055 /**
056 * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int)
057 */
058 public String getValue(int index)
059 {
060 return null;
061 }
062
063 /**
064 * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String)
065 */
066 public Object translateValue(String value)
067 {
068 return null;
069 }
070 };
071
072 private IPropertySelectionModel _model;
073
074 private String _label = "";
075
076 private Object _option = null;
077
078 private String _value = "";
079
080 /**
081 * Constructs a new LabeledPropertySelectionModel using an empty model and default label,
082 * option, and value. Default constructor is made available so that this model may be specified
083 * as a component helper bean.
084 */
085 public LabeledPropertySelectionModel()
086 {
087 this(EMPTY_MODEL);
088 }
089
090 /**
091 * Constructs a new LabeledPropertySelectionModel using the specified model and default label,
092 * option, and value.
093 *
094 * @param model
095 * the underlying model to decorate
096 */
097 public LabeledPropertySelectionModel(IPropertySelectionModel model)
098 {
099 _model = model;
100 }
101
102 /**
103 * Constructs a new LabeledPropertySelectionModel using the specified model and label, and
104 * default option and value.
105 *
106 * @param model
107 * the underlying model to decorate
108 * @param label
109 * the label of the initial property
110 */
111 public LabeledPropertySelectionModel(IPropertySelectionModel model, String label)
112 {
113 this(model);
114
115 _label = label;
116 }
117
118 /**
119 * Constructs a new LabeledPropertySelectionModel using the specified model, label, and option;
120 * and default value.
121 *
122 * @param model
123 * the underlying model to decorate
124 * @param label
125 * the label of the initial property
126 * @param option
127 * the option value of the initial property
128 */
129 public LabeledPropertySelectionModel(IPropertySelectionModel model, String label, Object option)
130 {
131 this(model, label);
132
133 _option = option;
134 }
135
136 /**
137 * Constructs a new LabeledPropertySelectionModel using the specified model, label, option, and
138 * value.
139 *
140 * @param model
141 * the underlying model to decorate
142 * @param label
143 * the label of the initial property
144 * @param option
145 * the option value of the initial property
146 * @param value
147 * the value of the initial property
148 */
149 public LabeledPropertySelectionModel(IPropertySelectionModel model, String label,
150 Object option, String value)
151 {
152 this(model, label, option);
153
154 _value = value;
155 }
156
157 /**
158 * Returns the underlying IPropertySelectionModel.
159 *
160 * @return the underlying IPropertySelectionModel
161 */
162 public IPropertySelectionModel getModel()
163 {
164 return _model;
165 }
166
167 /**
168 * Sets the underlying IPropertySelectionModel.
169 *
170 * @param model
171 * the IPropertySelectionModel to set
172 */
173 public void setModel(IPropertySelectionModel model)
174 {
175 _model = model;
176 }
177
178 /**
179 * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount()
180 */
181 public int getOptionCount()
182 {
183 return _model.getOptionCount() + 1;
184 }
185
186 /**
187 * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int)
188 */
189 public Object getOption(int index)
190 {
191 return (index == 0) ? _option : _model.getOption(index - 1);
192 }
193
194 /**
195 * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int)
196 */
197 public String getLabel(int index)
198 {
199 return (index == 0) ? _label : _model.getLabel(index - 1);
200 }
201
202 /**
203 * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int)
204 */
205 public String getValue(int index)
206 {
207 return (index == 0) ? _value : _model.getValue(index - 1);
208 }
209
210 /**
211 * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String)
212 */
213 public Object translateValue(String value)
214 {
215 if (value == null)
216 return null;
217
218 return value.equals(_value) ? _option : _model.translateValue(value);
219 }
220
221 /**
222 * Returns the label of the initial IPropertySelectionModel option.
223 *
224 * @return a IPropertySelectionModel option label
225 */
226 public String getLabel()
227 {
228 return _label;
229 }
230
231 /**
232 * Sets the label of the initial IPropertySelectionModel option.
233 *
234 * @param label
235 * a IPropertySelectionModel option label
236 */
237 public void setLabel(String label)
238 {
239 _label = label;
240 }
241
242 /**
243 * Returns the value of the initial IPropertySelectionModel option.
244 *
245 * @return a IPropertySelectionModel option value
246 */
247 public String getValue()
248 {
249 return _value;
250 }
251
252 /**
253 * Sets the value of the initial IPropertySelectionModel option.
254 *
255 * @param value
256 * a IPropertySelectionModel option value
257 */
258 public void setValue(String value)
259 {
260 _value = value;
261 }
262
263 /**
264 * Returns the initial option.
265 *
266 * @return a PropertySelectionModel option
267 */
268 public Object getOption()
269 {
270 return _option;
271 }
272
273 /**
274 * Sets the initial IPropertySelectionModel option.
275 *
276 * @param option
277 * a IPropertySelectionModel option
278 */
279 public void setOption(Object option)
280 {
281 _option = option;
282 }
283 }