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, Object option, String value)
155        {
156            this(model, label, option);
157    
158            _value = value;
159        }
160    
161        /**
162         * Returns the underlying IPropertySelectionModel.
163         * 
164         * @return the underlying IPropertySelectionModel
165         */
166        public IPropertySelectionModel getModel()
167        {
168            return _model;
169        }
170    
171        /**
172         * Sets the underlying IPropertySelectionModel.
173         * 
174         * @param model
175         *            the IPropertySelectionModel to set
176         */
177        public void setModel(IPropertySelectionModel model)
178        {
179            _model = model;
180        }
181    
182        /**
183         * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount()
184         */
185        public int getOptionCount()
186        {
187            return _model.getOptionCount() + 1;
188        }
189    
190        /**
191         * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int)
192         */
193        public Object getOption(int index)
194        {
195            return (index == 0) ? _option : _model.getOption(index - 1);
196        }
197    
198        /**
199         * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int)
200         */
201        public String getLabel(int index)
202        {
203            return (index == 0) ? _label : _model.getLabel(index - 1);
204        }
205    
206        /**
207         * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int)
208         */
209        public String getValue(int index)
210        {
211            return (index == 0) ? _value : _model.getValue(index - 1);
212        }
213    
214        public boolean isDisabled(int index)
215        {
216            return index == 0 && _option == null;
217        }
218        
219        /**
220         * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String)
221         */
222        public Object translateValue(String value)
223        {
224            if (value == null)
225                return null;
226            
227            return value.equals(_value) ? _option : _model.translateValue(value);
228        }
229    
230        /**
231         * Returns the label of the initial IPropertySelectionModel option.
232         * 
233         * @return a IPropertySelectionModel option label
234         */
235        public String getLabel()
236        {
237            return _label;
238        }
239    
240        /**
241         * Sets the label of the initial IPropertySelectionModel option.
242         * 
243         * @param label
244         *            a IPropertySelectionModel option label
245         */
246        public void setLabel(String label)
247        {
248            _label = label;
249        }
250    
251        /**
252         * Returns the value of the initial IPropertySelectionModel option.
253         * 
254         * @return a IPropertySelectionModel option value
255         */
256        public String getValue()
257        {
258            return _value;
259        }
260    
261        /**
262         * Sets the value of the initial IPropertySelectionModel option.
263         * 
264         * @param value
265         *            a IPropertySelectionModel option value
266         */
267        public void setValue(String value)
268        {
269            _value = value;
270        }
271    
272        /**
273         * Returns the initial option.
274         * 
275         * @return a PropertySelectionModel option
276         */
277        public Object getOption()
278        {
279            return _option;
280        }
281    
282        /**
283         * Sets the initial IPropertySelectionModel option.
284         * 
285         * @param option
286         *            a IPropertySelectionModel option
287         */
288        public void setOption(Object option)
289        {
290            _option = option;
291        }
292    }