1 18 package org.apache.beehive.netui.tags.html; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import org.apache.beehive.netui.pageflow.ProcessPopulate; 23 import org.apache.beehive.netui.script.common.DataAccessProviderStack; 24 import org.apache.beehive.netui.tags.naming.FormDataNameInterceptor; 25 import org.apache.beehive.netui.tags.naming.IndexedNameInterceptor; 26 import org.apache.beehive.netui.tags.naming.PrefixNameInterceptor; 27 import org.apache.beehive.netui.tags.rendering.StringBuilderRenderAppender; 28 import org.apache.beehive.netui.tags.rendering.TagRenderingBase; 29 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender; 30 import org.apache.beehive.netui.util.Bundle; 31 import org.apache.beehive.netui.util.logging.Logger; 32 import org.apache.beehive.netui.util.tags.GroupOption; 33 34 import javax.servlet.ServletRequest ; 35 import javax.servlet.jsp.JspException ; 36 import java.util.*; 37 38 163 public class RadioButtonGroup 164 extends HtmlGroupBaseTag 165 { 166 private static final Logger logger = Logger.getInstance(RadioButtonGroup.class); 168 169 public static final String RADIOBUTTONGROUP_KEY = "radio_button_group_key"; 170 171 private String _match; private String _defaultRadio; private Object _dynamicAttrs; private InternalStringBuilder _saveBody; private WriteRenderAppender _writer; 176 177 178 private static final List _internalNamingChain; 179 180 static 181 { 182 List l = new ArrayList(3); 183 l.add(new FormDataNameInterceptor()); 184 l.add(new IndexedNameInterceptor()); 185 l.add(new PrefixNameInterceptor(RADIOBUTTONGROUP_KEY)); 186 _internalNamingChain = Collections.unmodifiableList(l); 187 } 188 189 static 190 { 191 org.apache.beehive.netui.pageflow.ProcessPopulate.registerPrefixHandler(RADIOBUTTONGROUP_KEY, new RadioButtonGroupPrefixHandler()); 192 } 193 194 197 public static class RadioButtonGroupPrefixHandler 198 implements org.apache.beehive.netui.pageflow.RequestParameterHandler 199 { 200 public void process(javax.servlet.http.HttpServletRequest request, String key, 201 String expr, ProcessPopulate.ExpressionUpdateNode node) 202 { 203 if (logger.isDebugEnabled()) { 204 logger.debug("*********************************************\n" + 205 "process with key \"" + key + "\" and expression \"" + node.expression + "\"" + 206 "*********************************************\n"); 207 } 208 } 209 } 210 211 public RadioButtonGroup() 212 { 213 super(); 214 } 215 216 219 public String getTagName() 220 { 221 return "RadioButtonGroup"; 222 } 223 224 230 protected List getNamingChain() 231 { 232 return _internalNamingChain; 233 } 234 235 240 private String evaluateDefaultValue() 241 { 242 Object val = _defaultValue; 243 244 if (val != null) 245 return val.toString(); 246 return ""; 247 } 248 249 253 public boolean isMatched(String value, Boolean defaultValue) 254 { 255 if (value == null) 257 return false; 258 if (_match != null) 259 return value.equals(_match); 260 if (_defaultRadio != null) 261 return value.equals(_defaultRadio); 262 263 return false; 264 } 265 266 270 public int doStartTag() 271 throws JspException  272 { 273 Object val = evaluateDataSource(); 275 if (val != null) 276 _match = val.toString(); 277 278 pageContext.setAttribute(RADIOBUTTONGROUP_KEY, this); 280 _defaultRadio = evaluateDefaultValue(); 281 282 if (hasErrors()) 284 return SKIP_BODY; 285 286 ServletRequest req = pageContext.getRequest(); 287 if (_cr == null) 288 _cr = TagRenderingBase.Factory.getConstantRendering(req); 289 290 _writer = new WriteRenderAppender(pageContext); 291 if (isVertical()) { 292 _cr.TABLE(_writer); 293 } 294 295 _dynamicAttrs = evaluateOptionsDataSource(); 297 assert (_dynamicAttrs != null); 298 assert (_dynamicAttrs instanceof Map || 299 _dynamicAttrs instanceof Iterator); 300 301 if (_repeater) { 302 if (_dynamicAttrs instanceof Map) { 303 _dynamicAttrs = ((Map) _dynamicAttrs).entrySet().iterator(); 304 305 } 306 if (!(_dynamicAttrs instanceof Iterator)) { 307 String s = Bundle.getString("Tags_OptionsDSIteratorError"); 308 registerTagError(s, null); 309 return SKIP_BODY; 310 } 311 while (((Iterator) _dynamicAttrs).hasNext()) { 312 _repCurItem = ((Iterator) _dynamicAttrs).next(); 313 if (_repCurItem != null) 314 break; 315 } 316 if (isVertical()) 317 _cr.TR_TD(_writer); 318 319 DataAccessProviderStack.addDataAccessProvider(this, pageContext); 320 } 321 _saveBody = new InternalStringBuilder(640); 324 return EVAL_BODY_INCLUDE; 325 } 326 327 332 public int doAfterBody() throws JspException  333 { 334 StringBuilderRenderAppender writer = new StringBuilderRenderAppender(_saveBody); 335 if (bodyContent != null) { 336 String value = bodyContent.getString(); 337 bodyContent.clearBody(); 338 if (value == null) 339 value = ""; 340 _saveBody.append(value); 341 } 342 343 if (_repeater) { 344 ServletRequest req = pageContext.getRequest(); 345 if (_cr == null) 346 _cr = TagRenderingBase.Factory.getConstantRendering(req); 347 if (isVertical()) 348 _cr.end_TD_TR(writer); 349 350 while (((Iterator) _dynamicAttrs).hasNext()) { 351 _repCurItem = ((Iterator) _dynamicAttrs).next(); 352 if (_repCurItem != null) { 353 _repIdx++; 354 if (isVertical()) 355 _cr.TR_TD(writer); 356 return EVAL_BODY_AGAIN; 357 } 358 } 359 } 360 361 return SKIP_BODY; 362 } 363 364 368 public int doEndTag() throws JspException  369 { 370 if (hasErrors()) 371 return reportAndExit(EVAL_PAGE); 372 373 String idScript = null; 374 String altText = null; 375 char accessKey = 0x00; 376 377 pageContext.removeAttribute(RADIOBUTTONGROUP_KEY); 379 ServletRequest req = pageContext.getRequest(); 380 if (_cr == null) 381 _cr = TagRenderingBase.Factory.getConstantRendering(req); 382 383 if (_saveBody != null) 385 write(_saveBody.toString()); 386 387 if (_repeater) { 389 if (isVertical()) 391 _cr.end_TABLE(_writer); 392 393 if (idScript != null) 394 write(idScript); 395 396 localRelease(); 398 return EVAL_PAGE; 399 } 400 401 if (_dynamicAttrs instanceof Map) { 403 Map dynamicRadiosMap = (Map) _dynamicAttrs; 404 Iterator keyIterator = dynamicRadiosMap.keySet().iterator(); 405 int idx = 0; 406 while (keyIterator.hasNext()) { 407 Object optionValue = keyIterator.next(); 408 String optionDisplay = null; 409 if (dynamicRadiosMap.get(optionValue) != null) { 410 optionDisplay = dynamicRadiosMap.get(optionValue).toString(); 411 } 412 else { 413 optionDisplay = ""; 414 } 415 416 addOption(_writer, INPUT_RADIO, optionValue.toString(), optionDisplay, idx++, altText, accessKey, _disabled); 417 if (hasErrors()) { 418 reportErrors(); 419 localRelease(); 420 return EVAL_PAGE; 421 } 422 write("\n"); 423 424 } 425 } 426 else { 427 assert(_dynamicAttrs instanceof Iterator); 428 429 Iterator it = (Iterator) _dynamicAttrs; 430 int idx = 0; 431 while (it.hasNext()) { 432 Object o = it.next(); 433 if (o == null) 434 continue; 435 436 if (o instanceof GroupOption) { 437 GroupOption go = (GroupOption) o; 438 addOption(_writer, INPUT_RADIO, go.getValue(), go.getName(), idx++, go.getAlt(), go.getAccessKey(), _disabled); 439 } 440 else { 441 String radioValue = o.toString(); 442 addOption(_writer, INPUT_RADIO, radioValue, radioValue, idx++, altText, accessKey, _disabled); 443 } 444 if (hasErrors()) { 445 reportErrors(); 446 localRelease(); 447 return EVAL_PAGE; 448 } 449 write("\n"); 450 } 451 } 452 453 if (isVertical()) { 454 _cr.end_TABLE(_writer); 455 } 456 457 if (idScript != null) 458 write(idScript); 459 460 localRelease(); 462 return EVAL_PAGE; 463 } 464 465 468 protected void localRelease() 469 { 470 if (_repeater) 472 DataAccessProviderStack.removeDataAccessProvider(pageContext); 473 474 super.localRelease(); 475 476 _match = null; 477 _defaultRadio = null; 478 } 479 } 480 | Popular Tags |