1 18 19 package org.apache.struts.taglib.html; 20 21 import java.util.Locale ; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.tagext.BodyTagSupport ; 25 26 import org.apache.struts.Globals; 27 import org.apache.struts.taglib.TagUtils; 28 import org.apache.struts.util.MessageResources; 29 30 37 public class OptionTag extends BodyTagSupport { 38 39 41 45 protected static final Locale defaultLocale = Locale.getDefault(); 46 47 50 protected static MessageResources messages = 51 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 52 53 56 protected String text = null; 57 58 60 64 protected String bundle = Globals.MESSAGES_KEY; 65 66 public String getBundle() { 67 return (this.bundle); 68 } 69 70 public void setBundle(String bundle) { 71 this.bundle = bundle; 72 } 73 74 77 protected boolean disabled = false; 78 79 public boolean getDisabled() { 80 return (this.disabled); 81 } 82 83 public void setDisabled(boolean disabled) { 84 this.disabled = disabled; 85 } 86 87 91 protected String key = null; 92 93 public String getKey() { 94 return (this.key); 95 } 96 97 public void setKey(String key) { 98 this.key = key; 99 } 100 101 105 protected String locale = Globals.LOCALE_KEY; 106 107 public String getLocale() { 108 return (this.locale); 109 } 110 111 public void setLocale(String locale) { 112 this.locale = locale; 113 } 114 115 118 private String style = null; 119 120 public String getStyle() { 121 return style; 122 } 123 124 public void setStyle(String style) { 125 this.style = style; 126 } 127 128 131 private String styleClass = null; 132 133 public String getStyleClass() { 134 return styleClass; 135 } 136 137 public void setStyleClass(String styleClass) { 138 this.styleClass = styleClass; 139 } 140 141 144 protected String styleId = null; 145 146 149 public String getStyleId() { 150 151 return (this.styleId); 152 153 } 154 155 160 public void setStyleId(String styleId) { 161 162 this.styleId = styleId; 163 164 } 165 166 171 protected String value = null; 172 173 public String getValue() { 174 return (this.value); 175 } 176 177 public void setValue(String value) { 178 this.value = value; 179 } 180 181 183 188 public int doStartTag() throws JspException { 189 190 this.text = null; 192 193 return (EVAL_BODY_TAG); 195 196 } 197 198 203 public int doAfterBody() throws JspException { 204 205 if (bodyContent != null) { 206 String text = bodyContent.getString(); 207 if (text != null) { 208 text = text.trim(); 209 if (text.length() > 0) { 210 this.text = text; 211 } 212 } 213 } 214 return (SKIP_BODY); 215 216 } 217 218 223 public int doEndTag() throws JspException { 224 225 TagUtils.getInstance().write(pageContext, this.renderOptionElement()); 226 227 return (EVAL_PAGE); 228 } 229 230 235 protected String renderOptionElement() throws JspException { 236 StringBuffer results = new StringBuffer ("<option value=\""); 237 238 results.append(this.value); 239 results.append("\""); 240 if (disabled) { 241 results.append(" disabled=\"disabled\""); 242 } 243 if (this.selectTag().isMatched(this.value)) { 244 results.append(" selected=\"selected\""); 245 } 246 if (style != null) { 247 results.append(" style=\""); 248 results.append(style); 249 results.append("\""); 250 } 251 if (styleId != null) { 252 results.append(" id=\""); 253 results.append(styleId); 254 results.append("\""); 255 } 256 if (styleClass != null) { 257 results.append(" class=\""); 258 results.append(styleClass); 259 results.append("\""); 260 } 261 results.append(">"); 262 263 results.append(text()); 264 265 results.append("</option>"); 266 return results.toString(); 267 } 268 269 273 private SelectTag selectTag() throws JspException { 274 SelectTag selectTag = 275 (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY); 276 277 if (selectTag == null) { 278 JspException e = 279 new JspException (messages.getMessage("optionTag.select")); 280 281 TagUtils.getInstance().saveException(pageContext, e); 282 throw e; 283 } 284 285 return selectTag; 286 } 287 288 291 public void release() { 292 super.release(); 293 bundle = Globals.MESSAGES_KEY; 294 disabled = false; 295 key = null; 296 locale = Globals.LOCALE_KEY; 297 style = null; 298 styleClass = null; 299 text = null; 300 value = null; 301 } 302 303 305 310 protected String text() throws JspException { 311 String optionText = this.text; 312 313 if ((optionText == null) && (this.key != null)) { 314 optionText = 315 TagUtils.getInstance().message(pageContext, bundle, locale, key); 316 } 317 318 if (optionText == null) { 320 optionText = this.value; 321 } 322 323 return optionText; 324 } 325 326 } 327 | Popular Tags |