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 019 * property. The label, option, and value of the initial property are 020 * configurable. 021 * 022 * @author Paul Ferraro 023 * @since 4.0 024 */ 025 public class LabeledPropertySelectionModel implements IPropertySelectionModel 026 { 027 private IPropertySelectionModel _model; 028 private String _label = ""; 029 private Object _option = null; 030 private String _value = ""; 031 032 /** 033 * Constructs a new LabeledPropertySelectionModel using an empty model and 034 * default label, option, and value. Default constructor is made available 035 * so that this model may be specified as a component helper bean. 036 */ 037 public LabeledPropertySelectionModel() 038 { 039 this(EMPTY_MODEL); 040 } 041 042 /** 043 * Constructs a new LabeledPropertySelectionModel using the specified model 044 * and default label, option, and value. 045 * @param model the underlying model to decorate 046 */ 047 public LabeledPropertySelectionModel(IPropertySelectionModel model) 048 { 049 _model = model; 050 } 051 052 /** 053 * Constructs a new LabeledPropertySelectionModel using the specified model 054 * and label, and default option and value. 055 * @param model the underlying model to decorate 056 * @param label the label of the initial property 057 */ 058 public LabeledPropertySelectionModel(IPropertySelectionModel model, 059 String label) 060 { 061 this(model); 062 063 _label = label; 064 } 065 066 /** 067 * Constructs a new LabeledPropertySelectionModel using the specified model, 068 * label, and option; and default value. 069 * @param model the underlying model to decorate 070 * @param label the label of the initial property 071 * @param option the option value of the initial property 072 */ 073 public LabeledPropertySelectionModel(IPropertySelectionModel model, 074 String label, Object option) 075 { 076 this(model, label); 077 078 _option = option; 079 } 080 081 /** 082 * Constructs a new LabeledPropertySelectionModel using the specified model, 083 * label, option, and value. 084 * @param model the underlying model to decorate 085 * @param label the label of the initial property 086 * @param option the option value of the initial property 087 * @param value the value of the initial property 088 */ 089 public LabeledPropertySelectionModel(IPropertySelectionModel model, 090 String label, Object option, String value) 091 { 092 this(model, label, option); 093 094 _value = value; 095 } 096 097 /** 098 * Returns the underlying IPropertySelectionModel 099 * @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 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 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 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 translateValue(String 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 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 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 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 value) 187 { 188 _value = value; 189 } 190 191 /** 192 * Returns the initial option 193 * @return a PropertySelectionModel option 194 */ 195 public Object 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 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 getOption(int index) 227 { 228 return null; 229 } 230 231 /** 232 * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int) 233 */ 234 public String getLabel(int index) 235 { 236 return null; 237 } 238 239 /** 240 * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int) 241 */ 242 public String 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 translateValue(String value) 251 { 252 return null; 253 } 254 }; 255 }