1 18 package org.apache.beehive.netui.tags.html; 19 20 import org.apache.beehive.netui.script.common.IDataAccessProvider; 21 import org.apache.beehive.netui.tags.AbstractClassicTag; 22 import org.apache.beehive.netui.tags.ExpressionHandling; 23 import org.apache.beehive.netui.tags.IAttributeConsumer; 24 import org.apache.beehive.netui.tags.rendering.*; 25 import org.apache.beehive.netui.util.Bundle; 26 import org.apache.beehive.netui.util.iterator.IteratorFactory; 27 import org.apache.beehive.netui.util.logging.Logger; 28 29 import javax.servlet.ServletRequest ; 30 import javax.servlet.jsp.JspException ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 40 abstract public class HtmlGroupBaseTag 41 extends AbstractClassicTag implements IDataAccessProvider, HtmlConstants, IAttributeConsumer 42 { 43 private static final Logger logger = Logger.getInstance(HtmlGroupBaseTag.class); 44 45 48 public final String HORIZONTAL_VALUE = "horizontal"; 49 50 53 public final String VERTICAL_VALUE = "vertical"; 54 55 private InputBooleanTag.State _inputState = new InputBooleanTag.State(); 56 private SpanTag.State _spanState = new SpanTag.State(); 57 protected ConstantRendering _cr; 58 59 private HashMap _attrs; 60 61 protected String _dataSource; protected Object _defaultValue; protected Object _optionsDataSource; protected boolean _disabled; 65 66 private String _orientation; private Boolean _orientVal; 69 private String _realName; 71 private String _style; private String _class; 74 protected String _labelStyle = null; 75 protected String _labelStyleClass = null; 76 77 protected boolean _repeater; 79 protected int _repIdx = 0; protected Object _repCurItem; 83 public HtmlGroupBaseTag() 84 { 85 super(); 86 } 87 88 92 public abstract boolean isMatched(String value, Boolean defaultValue); 93 94 105 public void setAttribute(String name, String value, String facet) 106 throws JspException 107 { 108 if (name == null || name.length() <= 0) { 110 String s = Bundle.getString("Tags_AttributeNameNotSet"); 111 registerTagError(s, null); 112 return; 113 } 114 115 if (name.equals(ID) || name.equals(NAME)) { 117 String s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object []{name}); 118 registerTagError(s, null); 119 return; 120 } 121 122 if (facet != null) { 125 if (facet.equals("span")) { 126 _spanState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, name, value); 127 return; 128 } 129 else if (facet.equals("input")) { 130 } 132 else { 133 String s = Bundle.getString("Tags_AttributeFacetNotSupported", new Object []{facet}); 134 registerTagError(s, null); 135 return; 136 } 137 } 138 139 if (name.equals(VALUE)) { 141 String s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object []{name}); 142 registerTagError(s, null); 143 return; 144 } 145 146 if (_attrs == null) { 148 _attrs = new HashMap (); 149 } 150 _attrs.put(name, value); 151 } 152 153 159 public final String getQualifiedDataSourceName() 160 throws JspException 161 { 162 if (_realName == null) 163 _realName = doNaming(); 164 return _realName; 165 } 166 167 172 public boolean isDisabled() 173 { 174 return _disabled; 175 } 176 177 189 public void setDisabled(boolean disabled) 190 { 191 _disabled = disabled; 192 } 193 194 204 public void setOrientation(String orientation) 205 { 206 _orientation = setNonEmptyValueAttribute(orientation); 207 } 208 209 213 public boolean isVertical() 214 { 215 if (_orientVal == null) { 216 boolean val = VERTICAL_VALUE.equalsIgnoreCase(_orientation); 217 _orientVal = new Boolean (val); 218 } 219 return _orientVal.booleanValue(); 220 } 221 222 231 public void setRepeater(boolean repeater) 232 { 233 _repeater = repeater; 234 } 235 236 240 public boolean isRepeater() 241 { 242 return _repeater; 243 } 244 245 254 public void setStyle(String style) 255 { 256 _style = setNonEmptyValueAttribute(style); 257 } 258 259 268 public void setStyleClass(String styleClass) 269 { 270 _class = setNonEmptyValueAttribute(styleClass); 271 } 272 273 277 public String getLabelStyle() 278 { 279 return _labelStyle; 280 } 281 282 291 public void setLabelStyle(String labelStyle) 292 { 293 _labelStyle = setNonEmptyValueAttribute(labelStyle); 294 } 295 296 300 public String getLabelStyleClass() 301 { 302 return _labelStyleClass; 303 } 304 305 314 public void setLabelStyleClass(String labelStyleClass) 315 { 316 _labelStyleClass = setNonEmptyValueAttribute(labelStyleClass); 317 } 318 319 320 330 public void setDataSource(String dataSource) 331 throws JspException 332 { 333 _dataSource = dataSource; 334 } 335 336 340 public String getDataSource() 341 { 342 return _dataSource; 343 } 344 345 348 353 public int getCurrentIndex() 354 { 355 return _repIdx; 356 } 357 358 362 public Object getCurrentItem() 363 { 364 return _repCurItem; 365 } 366 367 376 public Object getCurrentMetadata() 377 { 378 return null; 379 } 380 381 391 public IDataAccessProvider getProviderParent() 392 { 393 return (IDataAccessProvider) findAncestorWithClass(this, IDataAccessProvider.class); 394 } 395 396 402 protected List getNamingChain() 403 { 404 return AbstractClassicTag.DefaultNamingChain; 405 } 406 407 412 protected Object evaluateDataSource() 413 throws JspException 414 { 415 ExpressionHandling expr = new ExpressionHandling(this); 416 String dataSource = "{" + _dataSource + "}"; 417 String ds = expr.ensureValidExpression(dataSource, "dataSource", "DataSourceError"); 418 if (ds == null) 419 return null; 420 421 return expr.evaluateExpression(dataSource, "dataSource", pageContext); 423 } 424 425 protected String doNaming() 426 throws JspException 427 { 428 String dataSource = "{" + _dataSource + "}"; 429 return applyNamingChain(dataSource); 430 } 431 432 441 public void setDefaultValue(Object defaultValue) 442 throws JspException 443 { 444 if (defaultValue == null) { 445 String s = Bundle.getString("Tags_AttrValueRequired", new Object []{"defaultValue"}); 446 registerTagError(s, null); 447 return; 448 } 449 _defaultValue = defaultValue; 450 } 451 452 456 public Object getOptionsDataSource() 457 { 458 return _optionsDataSource; 459 } 460 461 470 public void setOptionsDataSource(Object optionsDataSource) 471 throws JspException 472 { 473 if (optionsDataSource == null) { 474 String s = Bundle.getString("Tags_AttrValueRequired", new Object []{"optionsDataSource"}); 475 registerTagError(s, null); 476 return; 477 } 478 _optionsDataSource = optionsDataSource; 479 } 480 481 487 protected Object evaluateOptionsDataSource() 488 throws JspException 489 { 490 if (_optionsDataSource == null) { 491 logger.warn(Bundle.getString("Tags_IteratorError", 493 new Object []{getTagName(), "optionsDataSource", _optionsDataSource})); 494 return IteratorFactory.EMPTY_ITERATOR; 495 } 496 497 if (_optionsDataSource instanceof Map ) 498 return _optionsDataSource; 499 500 Iterator it; 501 it = IteratorFactory.createIterator(_optionsDataSource); 502 if (it == null) { 503 it = IteratorFactory.EMPTY_ITERATOR; 504 } 505 506 assert (it != null && it instanceof Iterator ); 507 return it; 508 } 509 510 513 protected void addOption(AbstractRenderAppender buffer, String type, String optionValue, 514 String optionDisplay, int idx, String altText, char accessKey, boolean disabled) 515 throws JspException 516 { 517 ServletRequest req = pageContext.getRequest(); 518 if (_cr == null) 519 _cr = TagRenderingBase.Factory.getConstantRendering(req); 520 521 assert(buffer != null); 522 assert(optionValue != null); 523 assert(optionDisplay != null); 524 assert(type != null); 525 526 if (_orientation != null && isVertical()) { 527 _cr.TR_TD(buffer); 528 } 529 530 _inputState.clear(); 531 _inputState.type = type; 532 _inputState.name = getQualifiedDataSourceName(); 533 _inputState.value = optionValue; 534 _inputState.style = _style; 535 _inputState.styleClass = _class; 536 537 if (isMatched(optionValue, null)) { 538 _inputState.checked = true; 539 } 540 _inputState.disabled = isDisabled(); 541 _inputState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALT, altText); 542 if (accessKey != 0x00) 543 _inputState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ACCESSKEY, Character.toString(accessKey)); 544 545 if (_attrs != null && _attrs.size() > 0) { 547 Iterator iterator = _attrs.keySet().iterator(); 548 for (; iterator.hasNext();) { 549 String key = (String ) iterator.next(); 550 if (key == null) 551 continue; 552 553 String value = (String ) _attrs.get(key); 554 _inputState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, key, value); 555 } 556 } 557 558 TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.INPUT_BOOLEAN_TAG, req); 559 br.doStartTag(buffer, _inputState); 560 br.doEndTag(buffer); 561 562 String ls = _labelStyle; 563 String lsc = _labelStyleClass; 564 565 _spanState.style = ls; 566 _spanState.styleClass = lsc; 567 568 br = TagRenderingBase.Factory.getRendering(TagRenderingBase.SPAN_TAG, req); 569 br.doStartTag(buffer, _spanState); 570 buffer.append(optionDisplay); 571 br.doEndTag(buffer); 572 573 if (_orientation == null) { 575 _cr.BR(buffer); 576 } 577 else { 578 if (isVertical()) { 579 _cr.TR_TD(buffer); 580 } 581 else { 582 _cr.NBSP(buffer); 583 } 584 } 585 } 586 587 590 protected void localRelease() 591 { 592 super.localRelease(); 593 if (_attrs != null) 594 _attrs.clear(); 595 _cr = null; 596 _dataSource = null; 597 _defaultValue = null; 598 _optionsDataSource = null; 599 _disabled = false; 600 _orientation = null; 601 _orientVal = null; 602 _realName = null; 603 _style = null; 604 _class = null; 605 _labelStyle = null; 606 _labelStyleClass = null; 607 608 _repIdx = 0; 609 _repCurItem = null; 610 } 611 612 } 613 | Popular Tags |