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

http://struts2yuiplugin.googlecode.com/ · Java · 93 lines · 56 code · 15 blank · 22 comment · 10 complexity · f872b8e17e9ed3c2dea965d62353d9ad MD5 · raw file

  1. package com.googlecode.struts2yuiplugin.components;
  2. import com.googlecode.struts2yuiplugin.tools.YUITools;
  3. import org.apache.struts2.components.UIBean;
  4. /**
  5. * Delegatable implementation of an XHRComponent. Provides common behaviour for evaluating the parameters
  6. */
  7. public class XHRSupport implements XHRComponent {
  8. private static final String DEFAULT_METHOD = "GET";
  9. private OGNLEvaluator eval;
  10. private String callback;
  11. private String formId;
  12. private String href;
  13. private String method;
  14. public XHRSupport(OGNLEvaluator eval) {
  15. this.eval = eval;
  16. }
  17. /**
  18. * Adds the common XHR parameters to the UIBean
  19. * @param uiBean bean to be updated
  20. */
  21. public void evaluateParams(UIBean uiBean) {
  22. populateCallbackName(uiBean);
  23. populateMethod(uiBean);
  24. if (formId != null) {
  25. uiBean.addParameter("formId", eval.evaluateExpression(formId));
  26. }
  27. if (href != null) {
  28. uiBean.addParameter("href", eval.evaluateExpression(href));
  29. }
  30. }
  31. /**
  32. * Generate the javascript onclick callback name for this widget if not explictly specified.
  33. * The name is generated from the id but sanitized for javascript
  34. */
  35. private void populateCallbackName(UIBean uiBean) {
  36. String callbackName;
  37. if (callback != null) {
  38. callbackName = eval.evaluateExpression(callback);
  39. uiBean.addParameter("customCallback", true);
  40. } else {
  41. callbackName = YUITools.sanitizeForJavascript(uiBean.getId()+"Callback");
  42. uiBean.addParameter("customCallback", false);
  43. }
  44. uiBean.addParameter("callback", callbackName);
  45. }
  46. /** Populates the http method property */
  47. private void populateMethod(UIBean uiBean) {
  48. String methodName;
  49. if (method != null) {
  50. methodName = eval.evaluateExpression(method);
  51. } else {
  52. methodName = DEFAULT_METHOD;
  53. }
  54. uiBean.addParameter("method", methodName);
  55. }
  56. /** An alterative javascript callback function for the YUI Connection Manager's response */
  57. public void setCallback(String callback) {
  58. this.callback = callback;
  59. }
  60. /**
  61. * The ID Of the form to include in the request
  62. */
  63. public void setFormId(String formId) {
  64. this.formId = formId;
  65. }
  66. /**
  67. * If the href is provided the widget will send the request to the specified URL.
  68. */
  69. public void setHref(String href) {
  70. this.href = href;
  71. }
  72. /**
  73. * HTTP method to use when HREF is specified. Default is GET
  74. */
  75. public void setMethod(String method) {
  76. this.method = method;
  77. }
  78. }