PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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