KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > LabeledPropertySelectionModel


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form;
16
17 /**
18  * Decorates an underlying {@link IPropertySelectionModel}adding an initial
19  * property. The label, option, and value of the initial property are
20  * configurable.
21  *
22  * @author Paul Ferraro
23  * @since 4.0
24  */

25 public class LabeledPropertySelectionModel implements IPropertySelectionModel
26 {
27     private IPropertySelectionModel _model;
28     private String JavaDoc _label = "";
29     private Object JavaDoc _option = null;
30     private String JavaDoc _value = "";
31
32     /**
33      * Constructs a new LabeledPropertySelectionModel using an empty model and
34      * default label, option, and value. Default constructor is made available
35      * so that this model may be specified as a component helper bean.
36      */

37     public LabeledPropertySelectionModel()
38     {
39         this(EMPTY_MODEL);
40     }
41
42     /**
43      * Constructs a new LabeledPropertySelectionModel using the specified model
44      * and default label, option, and value.
45      * @param model the underlying model to decorate
46      */

47     public LabeledPropertySelectionModel(IPropertySelectionModel model)
48     {
49         _model = model;
50     }
51
52     /**
53      * Constructs a new LabeledPropertySelectionModel using the specified model
54      * and label, and default option and value.
55      * @param model the underlying model to decorate
56      * @param label the label of the initial property
57      */

58     public LabeledPropertySelectionModel(IPropertySelectionModel model,
59             String JavaDoc label)
60     {
61         this(model);
62
63         _label = label;
64     }
65
66     /**
67      * Constructs a new LabeledPropertySelectionModel using the specified model,
68      * label, and option; and default value.
69      * @param model the underlying model to decorate
70      * @param label the label of the initial property
71      * @param option the option value of the initial property
72      */

73     public LabeledPropertySelectionModel(IPropertySelectionModel model,
74             String JavaDoc label, Object JavaDoc option)
75     {
76         this(model, label);
77
78         _option = option;
79     }
80
81     /**
82      * Constructs a new LabeledPropertySelectionModel using the specified model,
83      * label, option, and value.
84      * @param model the underlying model to decorate
85      * @param label the label of the initial property
86      * @param option the option value of the initial property
87      * @param value the value of the initial property
88      */

89     public LabeledPropertySelectionModel(IPropertySelectionModel model,
90             String JavaDoc label, Object JavaDoc option, String JavaDoc value)
91     {
92         this(model, label, option);
93
94         _value = value;
95     }
96
97     /**
98      * Returns the underlying IPropertySelectionModel
99      * @return the underlying IPropertySelectionModel
100      */

101     public IPropertySelectionModel getModel()
102     {
103         return _model;
104     }
105     
106     /**
107      * Sets the underlying IPropertySelectionModel
108      * @param model the IPropertySelectionModel to set
109      */

110     public void setModel(IPropertySelectionModel model)
111     {
112         _model = model;
113     }
114     
115     /**
116      * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount()
117      */

118     public int getOptionCount()
119     {
120         return _model.getOptionCount() + 1;
121     }
122
123     /**
124      * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int)
125      */

126     public Object JavaDoc getOption(int index)
127     {
128         return (index == 0) ? _option : _model.getOption(index - 1);
129     }
130
131     /**
132      * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int)
133      */

134     public String JavaDoc getLabel(int index)
135     {
136         return (index == 0) ? _label : _model.getLabel(index - 1);
137     }
138
139     /**
140      * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int)
141      */

142     public String JavaDoc getValue(int index)
143     {
144         return (index == 0) ? _value : _model.getValue(index - 1);
145     }
146
147     /**
148      * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String)
149      */

150     public Object JavaDoc translateValue(String JavaDoc value)
151     {
152         return value.equals(_value) ? _option : _model.translateValue(value);
153     }
154
155     /**
156      * Returns the label of the initial IPropertySelectionModel option
157      * @return a IPropertySelectionModel option label
158      */

159     public String JavaDoc getLabel()
160     {
161         return _label;
162     }
163
164     /**
165      * Sets the label of the initial IPropertySelectionModel option
166      * @param label a IPropertySelectionModel option label
167      */

168     public void setLabel(String JavaDoc label)
169     {
170         _label = label;
171     }
172
173     /**
174      * Returns the value of the initial IPropertySelectionModel option
175      * @return a IPropertySelectionModel option value
176      */

177     public String JavaDoc getValue()
178     {
179         return _value;
180     }
181
182     /**
183      * Sets the value of the initial IPropertySelectionModel option
184      * @param value a IPropertySelectionModel option value
185      */

186     public void setValue(String JavaDoc value)
187     {
188         _value = value;
189     }
190
191     /**
192      * Returns the initial option
193      * @return a PropertySelectionModel option
194      */

195     public Object JavaDoc getOption()
196     {
197         return _option;
198     }
199
200     /**
201      * Sets the initial IPropertySelectionModel option
202      * @param option a IPropertySelectionModel option
203      */

204     public void setOption(Object JavaDoc option)
205     {
206         _option = option;
207     }
208
209     /**
210      * Empty model implementation. Avoids NullPointerExceptions when default
211      * constructor is used.
212      */

213     private static final IPropertySelectionModel EMPTY_MODEL = new IPropertySelectionModel()
214     {
215         /**
216          * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount()
217          */

218         public int getOptionCount()
219         {
220             return 0;
221         }
222
223         /**
224          * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int)
225          */

226         public Object JavaDoc getOption(int index)
227         {
228             return null;
229         }
230
231         /**
232          * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int)
233          */

234         public String JavaDoc getLabel(int index)
235         {
236             return null;
237         }
238
239         /**
240          * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int)
241          */

242         public String JavaDoc getValue(int index)
243         {
244             return null;
245         }
246
247         /**
248          * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String)
249          */

250         public Object JavaDoc translateValue(String JavaDoc value)
251         {
252             return null;
253         }
254     };
255 }
256
Popular Tags