|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| LabeledPropertySelectionModel.java | 100% | 67.9% | 60.9% | 70.5% |
|
||||||||||||||
| 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 property. The label, | |
| 19 | * option, and value of the initial property are configurable. | |
| 20 | * | |
| 21 | * @author Paul Ferraro | |
| 22 | * @since 4.0 | |
| 23 | */ | |
| 24 | public class LabeledPropertySelectionModel implements IPropertySelectionModel | |
| 25 | { | |
| 26 | private IPropertySelectionModel _model; | |
| 27 | ||
| 28 | private String _label = ""; | |
| 29 | ||
| 30 | private Object _option = null; | |
| 31 | ||
| 32 | private String _value = ""; | |
| 33 | ||
| 34 | /** | |
| 35 | * Constructs a new LabeledPropertySelectionModel using an empty model and default label, | |
| 36 | * option, and value. Default constructor is made available so that this model may be specified | |
| 37 | * as a component helper bean. | |
| 38 | */ | |
| 39 | 6 | public LabeledPropertySelectionModel() |
| 40 | { | |
| 41 | 6 | this(EMPTY_MODEL); |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * Constructs a new LabeledPropertySelectionModel using the specified model and default label, | |
| 46 | * option, and value. | |
| 47 | * | |
| 48 | * @param model | |
| 49 | * the underlying model to decorate | |
| 50 | */ | |
| 51 | 12 | public LabeledPropertySelectionModel(IPropertySelectionModel model) |
| 52 | { | |
| 53 | 12 | _model = model; |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * Constructs a new LabeledPropertySelectionModel using the specified model and label, and | |
| 58 | * default option and value. | |
| 59 | * | |
| 60 | * @param model | |
| 61 | * the underlying model to decorate | |
| 62 | * @param label | |
| 63 | * the label of the initial property | |
| 64 | */ | |
| 65 | 3 | public LabeledPropertySelectionModel(IPropertySelectionModel model, String label) |
| 66 | { | |
| 67 | 3 | this(model); |
| 68 | ||
| 69 | 3 | _label = label; |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Constructs a new LabeledPropertySelectionModel using the specified model, label, and option; | |
| 74 | * and default value. | |
| 75 | * | |
| 76 | * @param model | |
| 77 | * the underlying model to decorate | |
| 78 | * @param label | |
| 79 | * the label of the initial property | |
| 80 | * @param option | |
| 81 | * the option value of the initial property | |
| 82 | */ | |
| 83 | 3 | public LabeledPropertySelectionModel(IPropertySelectionModel model, String label, Object option) |
| 84 | { | |
| 85 | 3 | this(model, label); |
| 86 | ||
| 87 | 3 | _option = option; |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Constructs a new LabeledPropertySelectionModel using the specified model, label, option, and | |
| 92 | * value. | |
| 93 | * | |
| 94 | * @param model | |
| 95 | * the underlying model to decorate | |
| 96 | * @param label | |
| 97 | * the label of the initial property | |
| 98 | * @param option | |
| 99 | * the option value of the initial property | |
| 100 | * @param value | |
| 101 | * the value of the initial property | |
| 102 | */ | |
| 103 | 3 | public LabeledPropertySelectionModel(IPropertySelectionModel model, String label, |
| 104 | Object option, String value) | |
| 105 | { | |
| 106 | 3 | this(model, label, option); |
| 107 | ||
| 108 | 3 | _value = value; |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * Returns the underlying IPropertySelectionModel | |
| 113 | * | |
| 114 | * @return the underlying IPropertySelectionModel | |
| 115 | */ | |
| 116 | 0 | public IPropertySelectionModel getModel() |
| 117 | { | |
| 118 | 0 | return _model; |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Sets the underlying IPropertySelectionModel | |
| 123 | * | |
| 124 | * @param model | |
| 125 | * the IPropertySelectionModel to set | |
| 126 | */ | |
| 127 | 0 | public void setModel(IPropertySelectionModel model) |
| 128 | { | |
| 129 | 0 | _model = model; |
| 130 | } | |
| 131 | ||
| 132 | /** | |
| 133 | * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount() | |
| 134 | */ | |
| 135 | 18 | public int getOptionCount() |
| 136 | { | |
| 137 | 18 | return _model.getOptionCount() + 1; |
| 138 | } | |
| 139 | ||
| 140 | /** | |
| 141 | * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int) | |
| 142 | */ | |
| 143 | 21 | public Object getOption(int index) |
| 144 | { | |
| 145 | 21 | return (index == 0) ? _option : _model.getOption(index - 1); |
| 146 | } | |
| 147 | ||
| 148 | /** | |
| 149 | * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int) | |
| 150 | */ | |
| 151 | 21 | public String getLabel(int index) |
| 152 | { | |
| 153 | 21 | return (index == 0) ? _label : _model.getLabel(index - 1); |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int) | |
| 158 | */ | |
| 159 | 21 | public String getValue(int index) |
| 160 | { | |
| 161 | 21 | return (index == 0) ? _value : _model.getValue(index - 1); |
| 162 | } | |
| 163 | ||
| 164 | /** | |
| 165 | * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String) | |
| 166 | */ | |
| 167 | 24 | public Object translateValue(String value) |
| 168 | { | |
| 169 | 24 | if (value == null) |
| 170 | 3 | return null; |
| 171 | ||
| 172 | 21 | 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 | 3 | public String getLabel() |
| 181 | { | |
| 182 | 3 | 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 | 0 | public void setLabel(String label) |
| 192 | { | |
| 193 | 0 | _label = label; |
| 194 | } | |
| 195 | ||
| 196 | /** | |
| 197 | * Returns the value of the initial IPropertySelectionModel option | |
| 198 | * | |
| 199 | * @return a IPropertySelectionModel option value | |
| 200 | */ | |
| 201 | 3 | public String getValue() |
| 202 | { | |
| 203 | 3 | 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 | 0 | public void setValue(String value) |
| 213 | { | |
| 214 | 0 | _value = value; |
| 215 | } | |
| 216 | ||
| 217 | /** | |
| 218 | * Returns the initial option | |
| 219 | * | |
| 220 | * @return a PropertySelectionModel option | |
| 221 | */ | |
| 222 | 3 | public Object getOption() |
| 223 | { | |
| 224 | 3 | return _option; |
| 225 | } | |
| 226 | ||
| 227 | /** | |
| 228 | * Sets the initial IPropertySelectionModel option | |
| 229 | * | |
| 230 | * @param option | |
| 231 | * a IPropertySelectionModel option | |
| 232 | */ | |
| 233 | 0 | public void setOption(Object option) |
| 234 | { | |
| 235 | 0 | _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 | 6 | public int getOptionCount() |
| 247 | { | |
| 248 | 6 | return 0; |
| 249 | } | |
| 250 | ||
| 251 | /** | |
| 252 | * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int) | |
| 253 | */ | |
| 254 | 0 | public Object getOption(int index) |
| 255 | { | |
| 256 | 0 | return null; |
| 257 | } | |
| 258 | ||
| 259 | /** | |
| 260 | * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int) | |
| 261 | */ | |
| 262 | 0 | public String getLabel(int index) |
| 263 | { | |
| 264 | 0 | return null; |
| 265 | } | |
| 266 | ||
| 267 | /** | |
| 268 | * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int) | |
| 269 | */ | |
| 270 | 0 | public String getValue(int index) |
| 271 | { | |
| 272 | 0 | return null; |
| 273 | } | |
| 274 | ||
| 275 | /** | |
| 276 | * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String) | |
| 277 | */ | |
| 278 | 0 | public Object translateValue(String value) |
| 279 | { | |
| 280 | 0 | return null; |
| 281 | } | |
| 282 | }; | |
| 283 | } |
|
||||||||||