/src/main/java/com/googlecode/struts2yuiplugin/components/PushButton.java

http://struts2yuiplugin.googlecode.com/ · Java · 124 lines · 73 code · 19 blank · 32 comment · 5 complexity · 488810bd7ba74f62b59e6c41022f2fa1 MD5 · raw file

  1. package com.googlecode.struts2yuiplugin.components;
  2. import com.opensymphony.xwork2.util.ValueStack;
  3. import com.googlecode.struts2yuiplugin.tools.YUITools;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.struts2.views.annotations.StrutsTagAttribute;
  7. import org.apache.struts2.views.annotations.StrutsTag;
  8. /**
  9. * A YUI PushButton
  10. * Unlike a submit buton, a plain old pushbutton does not need to be associated with a form (it can be via FormId though)
  11. */
  12. @StrutsTag(name = "pushbutton", tldTagClass = "com.googlecode.struts2yuiplugin.views.jsp.ui.PushButtonTag", description = "Renders a YUI button of type PushButton")
  13. public class PushButton extends YUIBean implements XHRComponent, OGNLEvaluator {
  14. private static final String TEMPLATE = "yuipushbutton";
  15. private XHRSupport xhrSupport;
  16. private String target;
  17. private String clickListener;
  18. public PushButton(ValueStack valueStack, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
  19. super(valueStack, httpServletRequest, httpServletResponse);
  20. xhrSupport = new XHRSupport(this);
  21. }
  22. @Override
  23. public void evaluateParams() {
  24. super.evaluateParams();
  25. xhrSupport.evaluateParams(this);
  26. populateClickListenerName();
  27. if (target != null) {
  28. addParameter("target", evaluateExpression(target));
  29. }
  30. }
  31. protected String getDefaultTemplate() {
  32. return TEMPLATE;
  33. }
  34. /**
  35. * Generate the javacscript click listener name if not explictly specified
  36. * The name is generated from the id but sanitized for javascript
  37. */
  38. private void populateClickListenerName() {
  39. String listenerName;
  40. if (clickListener != null) {
  41. listenerName = evaluateExpression(clickListener);
  42. addParameter("customClickListener", true);
  43. } else {
  44. listenerName = YUITools.sanitizeForJavascript(getId()+"Click");
  45. addParameter("customClickListener", false);
  46. }
  47. addParameter("clickListener", listenerName);
  48. }
  49. @Override
  50. @StrutsTagAttribute(description = "Mandatory id attribute", required = true, rtexprvalue = true)
  51. public void setId(String id) {
  52. super.setId(id);
  53. }
  54. /**
  55. * The optional ID of the form to include in the request
  56. */
  57. @StrutsTagAttribute(description = "The ID of the form to include in the request", required = false)
  58. public void setFormId(String formId) {
  59. xhrSupport.setFormId(formId);
  60. }
  61. /**
  62. * Optional the href to send the request to. If not provided, the form's action will used
  63. */
  64. @StrutsTagAttribute(description = "The URL to make the request to if other than the form's action", required = false)
  65. public void setHref(String href) {
  66. xhrSupport.setHref(href);
  67. }
  68. /**
  69. * HTTP method to use when HREF is specified. Default is GET
  70. */
  71. @StrutsTagAttribute(description = "The HTTP method to use", required = false, defaultValue = "GET")
  72. public void setMethod(String method) {
  73. xhrSupport.setMethod(method);
  74. }
  75. /**
  76. * ID of the target div
  77. */
  78. @StrutsTagAttribute(description = "The ID of the target div to render the result", required = false)
  79. public void setTarget(String target) {
  80. this.target = target;
  81. }
  82. /**
  83. * The name of an alternative javascript callback for the YUI Connection Manager's respons
  84. */
  85. @StrutsTagAttribute(description = "An alternative javascript callback for the YUI Connection Manager's response", required = false)
  86. public void setCallback(String callback) {
  87. xhrSupport.setCallback(callback);
  88. }
  89. /**
  90. * An alternative javascript listener function for the click event
  91. */
  92. @StrutsTagAttribute(description = "An alternative listener for the click event", required = false)
  93. public void setClickListener(String listener) {
  94. this.clickListener = listener;
  95. }
  96. /**
  97. * Evaluates the OGNL expression
  98. *
  99. * @param expr OGNL expression.
  100. * @return the String value found.
  101. */
  102. public String evaluateExpression(String expr) {
  103. return super.findString(expr);
  104. }
  105. }