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