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