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

http://struts2yuiplugin.googlecode.com/ · Java · 127 lines · 76 code · 20 blank · 31 comment · 5 complexity · acbc3951af6113571a2bc2e411cc2a98 MD5 · raw file

  1. package com.googlecode.struts2yuiplugin.components;
  2. import com.googlecode.struts2yuiplugin.tools.YUITools;
  3. import com.opensymphony.xwork2.util.ValueStack;
  4. import org.apache.struts2.views.annotations.StrutsTag;
  5. import org.apache.struts2.views.annotations.StrutsTagAttribute;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. /**
  9. * An anchor that can load the content of a target div via XHR
  10. */
  11. @StrutsTag(name = "a", tldTagClass = "com.googlecode.struts2yuiplugin.views.jsp.ui.AnchorTag", description = "Renders an anchor tag with an onclick listener that performs an XHR")
  12. public class Anchor extends ClosingYUIBean implements XHRComponent, OGNLEvaluator {
  13. public static final String OPEN_TEMPLATE = "yuianchor";
  14. public static final String TEMPLATE = OPEN_TEMPLATE +"-close";
  15. private XHRSupport xhrSupport;
  16. private String target;
  17. private String clickListener;
  18. public Anchor(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. public String getDefaultOpenTemplate() {
  32. return OPEN_TEMPLATE;
  33. }
  34. protected String getDefaultTemplate() {
  35. return TEMPLATE;
  36. }
  37. /**
  38. * Generate the javacscript click listener name if not explictly specified
  39. * The name is generated from the id but sanitized for javascript
  40. */
  41. private void populateClickListenerName() {
  42. String listenerName;
  43. if (clickListener != null) {
  44. listenerName = evaluateExpression(clickListener);
  45. addParameter("customClickListener", true);
  46. } else {
  47. listenerName = YUITools.sanitizeForJavascript(getId()+"Click");
  48. addParameter("customClickListener", false);
  49. }
  50. addParameter("clickListener", listenerName);
  51. }
  52. @StrutsTagAttribute(description = "Mandatory id attribute", required = true, rtexprvalue = true)
  53. public void setId(String id) {
  54. super.setId(id);
  55. }
  56. /**
  57. * The optional ID of the form to include in the request
  58. */
  59. @StrutsTagAttribute(description = "The ID of the form to include in the request", required = false)
  60. public void setFormId(String formId) {
  61. xhrSupport.setFormId(formId);
  62. }
  63. /**
  64. * If optional the href to send the request to. I not provided, the form's action will used
  65. */
  66. @StrutsTagAttribute(description = "The URL to make the request to if other than the form's action", required = false, rtexprvalue = true)
  67. public void setHref(String href) {
  68. xhrSupport.setHref(href);
  69. }
  70. /**
  71. * HTTP method to use when HREF is specified. Default is GET
  72. */
  73. @StrutsTagAttribute(description = "The HTTP method to use", required = false, defaultValue = "GET")
  74. public void setMethod(String method) {
  75. xhrSupport.setMethod(method);
  76. }
  77. /**
  78. * ID of the target div
  79. */
  80. @StrutsTagAttribute(description = "The ID of the target div to render the result", required = false)
  81. public void setTarget(String target) {
  82. this.target = target;
  83. }
  84. /**
  85. * The name of an alternative javascript callback for the YUI Connection Manager's response
  86. */
  87. @StrutsTagAttribute(description = "An alternative javascript callback for the YUI Connection Manager's response", required = false)
  88. public void setCallback(String callback) {
  89. xhrSupport.setCallback(callback);
  90. }
  91. /**
  92. * An alternateive javascript listener function for the click event
  93. */
  94. @StrutsTagAttribute(description = "An alternative listener for the click event", required = false)
  95. public void setClickListener(String listener) {
  96. this.clickListener = listener;
  97. }
  98. /**
  99. * Evaluates the OGNL expression
  100. *
  101. * @param expr OGNL expression.
  102. * @return the String value found.
  103. */
  104. public String evaluateExpression(String expr) {
  105. return super.findString(expr);
  106. }
  107. }