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

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