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

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