1 16 package org.apache.taglibs.input; 17 18 import java.util.Collection ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.JspTagException ; 27 import javax.servlet.jsp.JspWriter ; 28 import javax.servlet.jsp.tagext.TagSupport ; 29 30 39 40 public class Select extends TagSupport { 41 42 private String name; 44 private String dVal; 46 private String [] dValArray; 48 private Map attributes; 50 private Map options; 52 private String attributesText; 54 private String beanId; 56 private boolean multiple; 58 private String size; 60 private List optionLabels; 62 private List optionValues; 64 private HashMap chosen; 66 public void release() { 67 super.release(); 68 name = null; 69 dVal = null; 70 dValArray = null; 71 attributes = null; 72 options = null; 73 attributesText = null; 74 beanId = null; 75 multiple = false; 76 size = null; 77 optionLabels = null; 78 optionValues = null; 79 chosen = null; 80 } 81 82 public int doStartTag() throws JspException { 83 try { 84 if (name == null || name.equals("")) 86 throw new JspTagException ("invalid null or empty 'name'"); 87 88 String beanId = this.beanId; 90 91 if (beanId == null) { 93 beanId = Util.defaultFormBeanId(this); 94 } else if (beanId.length() == 0) { 95 beanId = null; 97 } 98 99 ServletRequest req = pageContext.getRequest(); 101 JspWriter out = pageContext.getOut(); 102 103 out.print("<select name=\"" + Util.quote(name) + "\" "); 105 106 Util.printAttributes(out, attributes); 108 if (attributesText != null) { 109 out.print(attributesText + " "); 110 } 111 112 if (multiple) { 113 out.print("multiple=\"multiple\" "); 114 } 115 if (size != null) { 116 out.print("size=\"" + Util.quote(size) + "\" "); 117 } 118 119 out.println(">"); 121 122 130 131 String [] selected; 132 133 String [] beanValues = (beanId != null ? Util.beanPropertyValues( 135 pageContext.findAttribute(beanId), name) : null); 136 if (beanValues != null) { 137 selected = beanValues; 138 } else { 139 selected = req.getParameterValues(name); 141 if (selected == null) { 142 if (dValArray != null && dVal != null) { 144 selected = new String [dValArray.length + 1]; 145 selected[0] = dVal; 146 System.arraycopy(dValArray, 0, selected, 1, 147 dValArray.length); 148 } else if (dValArray != null) { 149 selected = dValArray; 150 } else if (dVal != null) { 151 selected = new String [] { dVal }; 152 } 153 } 154 } 155 156 if (selected != null 157 && selected.length > 1 158 && ((attributes == null || !attributes 159 .containsKey("multiple")) && !multiple)) 160 selected = null; 161 162 chosen = new HashMap (); 169 if (selected != null) { 170 for (int i = 0; i < selected.length; i++) { 171 if (selected[i] != null) { 172 chosen.put(selected[i], null); 173 } 174 } 175 } 176 177 if (optionLabels != null) { 179 int n = optionLabels.size(); 181 for (int i = 0; i < n; i++) { 182 Object oLabel = optionLabels.get(i); 183 Object oVal = (options != null ? options.get(oLabel) 184 : (optionValues != null ? optionValues.get(i) 185 : oLabel)); 186 187 outputOption(out, oLabel, oVal); 188 } 189 } else if (options != null) { 190 Iterator i = options.keySet().iterator(); 191 while (i.hasNext()) { 192 Object oLabel = i.next(); 193 Object oVal = options.get(oLabel); 194 195 outputOption(out, oLabel, oVal); 196 } 197 } 198 199 } catch (Exception ex) { 200 throw new JspTagException (ex.getMessage()); 201 } 202 return EVAL_BODY_INCLUDE; 203 } 204 205 private void outputOption(JspWriter out, Object oLabel, Object oVal) 206 throws java.io.IOException { 207 String label = oLabel.toString(); 208 211 String value = (oVal != null ? oVal.toString() : null); 212 213 214 out.print("<option"); 215 216 217 if ( value != null ) { 218 out.print(" value=\"" + Util.quote(value) + "\""); 219 } 220 221 224 if (value == null) 225 value = label; 227 232 if (chosen.containsKey(value)) { 233 if (!multiple) { 234 chosen.remove(value); 235 } 236 out.print(" selected=\"selected\""); 237 } 238 out.print(">"); 239 out.print(Util.quote(label)); 240 out.println("</option>"); 241 } 242 243 public int doEndTag() throws JspException { 244 try { 245 JspWriter out = pageContext.getOut(); 246 out.print("</select>"); 247 } catch (Exception ex) { 248 throw new JspTagException (ex.getMessage()); 249 } 250 return EVAL_PAGE; 251 } 252 253 public void setName(String x) { 254 name = x; 255 } 256 257 public void setAttributes(Map x) { 258 attributes = x; 259 } 260 261 public void setAttributesText(String x) { 262 attributesText = x; 263 } 264 265 public void setBean(String x) { 266 beanId = x; 267 } 268 269 public void setMultiple(boolean x) { 270 multiple = x; 271 } 272 273 public void setSize(String x) { 274 size = x; 275 } 276 277 public void setDefault(String x) { 278 dVal = x; 279 } 280 281 public void setDefaults(String [] x) { 282 dValArray = x; 283 } 284 285 public void setDefaults(Map x) { 286 dValArray = new String [x.size()]; 287 Iterator it = x.keySet().iterator(); 288 int i = 0; 289 while (it.hasNext()) { 290 dValArray[i++] = it.next().toString(); 291 } 292 } 293 294 public void setDefaults(Collection c) { 295 dValArray = new String [c.size()]; 296 Iterator it = c.iterator(); 297 int i = 0; 298 while (it.hasNext()) { 299 dValArray[i++] = it.next().toString(); 300 } 301 } 302 303 public void setOptions(Map x) { 304 options = x; 305 } 306 307 public void setOptionLabels(List x) { 308 optionLabels = x; 309 } 310 311 public void setOptionValues(List x) { 312 optionValues = x; 313 } 314 315 public HashMap getChosen() { 316 return chosen; 317 } 318 319 324 public String getName() { 325 return name; 326 } 327 328 333 public String getDefault() { 334 return dVal; 335 } 336 337 342 public String getBean() { 343 return beanId; 344 } 345 346 351 public String getAttributesText() { 352 return attributesText; 353 } 354 355 360 public Map getAttributes() { 361 return attributes; 362 } 363 364 369 public String [] getDefaults() { 370 return dValArray; 371 } 372 373 378 public boolean isMultiple() { 379 return multiple; 380 } 381 382 387 public List getOptionLabels() { 388 return optionLabels; 389 } 390 391 396 public List getOptionValues() { 397 return optionValues; 398 } 399 400 405 public Map getOptions() { 406 return options; 407 } 408 409 414 public String getSize() { 415 return size; 416 } 417 418 } | Popular Tags |