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.core.urls.URLRewriterService; 23 import org.apache.beehive.netui.pageflow.PageFlowUtils; 24 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 25 import org.apache.beehive.netui.tags.HtmlUtils; 26 import org.apache.beehive.netui.tags.IHtmlAccessable; 27 import org.apache.beehive.netui.tags.internal.PageFlowTagUtils; 28 import org.apache.beehive.netui.tags.javascript.CoreScriptFeature; 29 import org.apache.beehive.netui.tags.javascript.IScriptReporter; 30 import org.apache.beehive.netui.tags.javascript.ScriptRequestState; 31 import org.apache.beehive.netui.tags.rendering.*; 32 import org.apache.beehive.netui.util.Bundle; 33 import org.apache.beehive.netui.util.ParamHelper; 34 import org.apache.beehive.netui.util.logging.Logger; 35 import org.apache.struts.taglib.html.Constants; 36 37 import javax.servlet.ServletContext ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import javax.servlet.jsp.JspException ; 41 import java.net.URISyntaxException ; 42 import java.util.HashMap ; 43 import java.util.Map ; 44 45 70 public class Button 71 extends HtmlFocusBaseTag 72 implements IUrlParams, IHtmlAccessable, IHasPopupSupport 73 { 74 private static final Logger logger = Logger.getInstance(Button.class); 75 76 public static final String ACTION_OVERRIDE = "actionOverride:"; 77 78 private InputSubmitTag.State _state = new InputSubmitTag.State(); 79 80 private String _action; private String _value; private Map _params; private String _targetScope; private PopupSupport _popupSupport = null; private boolean _disableSecondClick = false; 87 90 public String getTagName() 91 { 92 return "Button"; 93 } 94 95 100 protected AbstractHtmlState getState() 101 { 102 return _state; 103 } 104 105 113 public void setAttribute(String name, String value, String facet) 114 throws JspException 115 { 116 if (name != null && (name.equals(TYPE) || name.equals(VALUE))) { 117 String s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object []{name}); 118 registerTagError(s, null); 119 } 120 super.setAttribute(name, value, facet); 121 } 122 123 135 public void setAction(String action) 136 throws JspException 137 { 138 _action = setRequiredValueAttribute(action, "action"); 139 } 140 141 151 public void setTargetScope(String targetScope) 152 { 153 _targetScope = targetScope; 154 } 155 156 167 public void setType(String type) 168 throws JspException 169 { 170 if (INPUT_SUBMIT.equals(type) || INPUT_BUTTON.equals(type) || INPUT_RESET.equals(type)) { 171 _state.type = type; 172 return; 173 } 174 String s = Bundle.getString("Tags_ButtonTypeError", new Object []{type}); 175 registerTagError(s, null); 176 } 177 178 187 public void setValue(String value) 188 throws JspException 189 { 190 _value = setNonEmptyValueAttribute(value); 191 } 192 193 203 public void setPopup(boolean popup) 204 { 205 _popupSupport = (popup ? new PopupSupport() : null); 206 } 207 208 218 public void setDisableSecondClick(boolean disableSecondClick) 219 { 220 _disableSecondClick = disableSecondClick; 221 } 222 223 229 public void addParameter(String name, Object value, String facet) 230 throws JspException 231 { 232 assert(name != null) : "Parameter 'name' must not be null"; 233 234 if (_params == null) { 235 _params = new HashMap (); 236 } 237 ParamHelper.addParam(_params, name, value); 238 } 239 240 244 public int doStartTag() 245 throws JspException 246 { 247 return EVAL_BODY_BUFFERED; 248 } 249 250 254 public int doAfterBody() 255 throws JspException 256 { 257 if (bodyContent != null) { 258 if (_value == null) { 259 String text = bodyContent.getString().trim(); 260 if (text.length() <= 0) 261 text = null; 262 _value = text; 263 } 264 bodyContent.clearBody(); 265 } 266 return SKIP_BODY; 267 } 268 269 273 public int doEndTag() 274 throws JspException 275 { 276 String idScript = null; 277 278 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 279 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 280 281 if (_value == null) 283 _value = Bundle.getString("Tags_ButtonText", null); 284 _state.value = _value; 285 286 _state.disabled = isDisabled(); 288 289 if (_popupSupport != null) { 291 _popupSupport.addParams(this, request); 292 } 293 294 if (_targetScope != null && _action == null) { 297 Form parentForm = getNearestForm(); 298 if (parentForm != null) { 299 _action = parentForm.getAction(); 300 } 301 } 302 303 if (_action == null && _params != null && _popupSupport == null) { 304 Form parentForm = getNearestForm(); 305 if (parentForm != null) { 306 _action = parentForm.getAction(); 307 } 308 } 309 310 if (_action != null) { 311 boolean isAction = PageFlowTagUtils.isAction(request, _action); 312 if (isAction) { 313 314 String token = PageFlowTagUtils.getToken(request, _action); 317 if (token != null) { 318 if (_params == null) { 319 _params = new HashMap (); 320 } 321 _params.put(Constants.TOKEN_KEY, token); 322 } 323 324 if (_targetScope != null) { 326 if (_params == null) { 327 _params = new HashMap (); 328 } 329 _params.put(ScopedServletUtils.SCOPE_ID_PARAM, _targetScope); 330 } 331 332 String overrideAction = ACTION_OVERRIDE + _action; 333 overrideAction = HtmlUtils.addParams(overrideAction, _params, response.getCharacterEncoding()); 334 String buttonOutput = URLRewriterService.getNamePrefix(pageContext.getServletContext(), 335 pageContext.getRequest(), overrideAction) + overrideAction; 336 if (buttonOutput.indexOf(";") > -1) { 337 buttonOutput = buttonOutput.substring(0, buttonOutput.indexOf(";")); 338 } 339 _state.name = buttonOutput; 340 341 Form parentForm = getNearestForm(); 343 idScript = renderNameAndId(request, _state, parentForm); 344 } 345 else { 346 registerTagError(Bundle.getString("Tags_BadAction", _action), null); 348 } 349 } 350 else { 351 Form parentForm = getNearestForm(); 352 idScript = renderNameAndId(request, _state, parentForm); 353 } 354 355 boolean buttonDisableAndSubmit = false; 356 boolean buttonDisable = false; 357 358 if (getOnClick() == null) { 360 if (_disableSecondClick) { 361 Form parentForm = getNearestForm(); 362 String href = getActionUrl(request, response); 363 String entry; 364 if (parentForm != null && href != null && (_state.type == null || _state.type == INPUT_SUBMIT)) { 365 entry = ScriptRequestState.getString("buttonDisableAndSubmitFormAction", 366 new Object []{parentForm.getRealFormId(), href}); 367 buttonDisableAndSubmit = true; 368 } 369 else { 370 entry = ScriptRequestState.getString("buttonDisableAction", null); 371 buttonDisable = true; 372 } 373 _state.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONCLICK, entry); 374 if (parentForm != null) 375 parentForm.insureRealId(); 376 } 377 else if (_popupSupport != null) { 378 String href = getActionUrl(request, response); 379 380 if (href != null) { 381 href = response.encodeURL(href); 382 setOnClick(_popupSupport.getOnClick(request,href)); 383 } 384 } 385 } 386 387 if (hasErrors()) 389 return reportAndExit(EVAL_PAGE); 390 391 WriteRenderAppender writer = new WriteRenderAppender(pageContext); 392 TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.INPUT_SUBMIT_TAG, request); 393 br.doStartTag(writer, _state); 394 br.doEndTag(writer); 395 396 if (idScript != null || _popupSupport != null || buttonDisable || buttonDisableAndSubmit) { 398 ScriptRequestState srs = ScriptRequestState.getScriptRequestState(request); 399 InternalStringBuilder script = new InternalStringBuilder(32); 400 StringBuilderRenderAppender scriptWriter = new StringBuilderRenderAppender(script); 401 IScriptReporter sr = getScriptReporter(); 402 403 if (buttonDisableAndSubmit) 404 srs.writeFeature(sr, scriptWriter, CoreScriptFeature.BUTTON_DISABLE_AND_SUBMIT, true, false, null); 405 if (buttonDisable) 406 srs.writeFeature(sr, scriptWriter, CoreScriptFeature.BUTTON_DISABLE, true, false, null); 407 if (_popupSupport != null) 408 _popupSupport.writeScript(request, srs, getScriptReporter(), scriptWriter); 409 if (idScript != null) 410 scriptWriter.append(idScript); 411 write(script.toString()); 412 } 413 414 localRelease(); 416 return EVAL_PAGE; 417 } 418 419 private String getActionUrl(HttpServletRequest request, HttpServletResponse response) 420 throws JspException 421 { 422 String href = null; 423 424 if (_action != null) { 425 ServletContext servletContext = pageContext.getServletContext(); 426 boolean forXML = TagRenderingBase.Factory.isXHTML(request); 427 try { 428 href = PageFlowUtils.getRewrittenActionURI(servletContext, request, response, _action, _params, null, forXML); 429 } 430 catch (URISyntaxException e) { 431 logger.error(Bundle.getString("Tags_URISyntaxException")); 433 String s = Bundle.getString("Tags_Button_URLException", 434 new Object []{_action, e.getMessage()}); 435 registerTagError(s, e); 436 } 437 } 438 else { 439 Form parentForm = getNearestForm(); 440 if (parentForm != null) 441 href = HtmlUtils.addParams(parentForm.getActionUrl(), _params, response.getCharacterEncoding()); 442 } 443 444 return href; 445 } 446 447 450 protected void localRelease() 451 { 452 super.localRelease(); 453 454 _state.clear(); 455 _action = null; 456 _value = null; 457 _params = null; 458 _targetScope = null; 459 _popupSupport = null; 460 _disableSecondClick = false; 461 } 462 463 469 470 488 public void setAccessKey(char accessKey) 489 { 490 if (accessKey == 0x00) 491 return; 492 _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ACCESSKEY, Character.toString(accessKey)); 493 } 494 495 504 public void setAlt(String alt) 505 { 506 _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ALT, alt); 507 } 508 509 520 public void setTabindex(int tabindex) 521 { 522 _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, TABINDEX, Integer.toString(tabindex)); 523 } 524 525 public PopupSupport getPopupSupport() 526 { 527 return _popupSupport; 528 } 529 } 530 | Popular Tags |