[cvs] expresso commit by rimovm: New Radio EL tag

JCorporate Ltd jcorp at jcorporate.com
Thu Apr 28 14:11:13 UTC 2005


Log Message:
-----------
New Radio EL tag

Added Files:
-----------
    expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/services/taglib:
        Radio.java

Revision Data
-------------
--- /dev/null
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/services/taglib/Radio.java
@@ -0,0 +1,438 @@
+package com.jcorporate.expresso.services.taglib;
+
+import java.io.IOException;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.tagext.TagSupport;
+import com.jcorporate.expresso.core.controller.Input;
+import com.jcorporate.expresso.core.dbobj.ValidValue;
+import org.apache.log4j.Logger;
+
+/**
+ * EL-based radio button that assists in constructing radio buttons from
+ * multi-valued inputs.  Like the rest of the Expresso EL library, this is minimalisting
+ * in the sense that it only renders the <input> tag of type radio.  As
+ * the page designer, you are responsible for providing iteration to allow
+ * for all the formatting you could possibly want.
+ * <p>Example Usage:</p>
+ * <p>The following example takes a multivalued input and renders the radios as
+ * Label: radio with a non-breaking space between each radio.</p>
+ * <p>
+ * <pre>
+ * &lt;%-- Set the input variable for easy EL and probably faster lookups --%&gt;
+ * &lt;c:set var="input" value="${controllerResponse.namedInputs['myMultiValuedInput']}"/&gt;
+ * <br/>
+ * &lt;%-- Iterate through all the input's valid values --%&gt;
+ * &lt;c:forEach items="${input.validValues"} var="oneValidValue"&gt;
+ * &nbsp;&nbsp;&lt;%-- Write the label, a colon, and then the radio button proper. --%&gt;
+ * &nbsp;&nbsp;&lt;c:out value="${oneValidValue.Description}"/&gt;: &lt;exp:radio input="${input}" value="${oneValidValue}"/&gt;&amp;nbsp;
+ * &lt;/c:forEach&gt;
+ * </pre>
+ * </p>
+ * <p>This tags also supports additions of Javascript as defined by W3C DOM level 2</p>
+ * @author Michael Rimov
+ */
+public class Radio extends TagSupport
+{
+
+    /**
+     * String Buffer Initial Allocation Size.
+     */
+    private static final int BUFFER_SIZE = 128;
+
+    /**
+     * Log4j Logger.
+     */
+    private static final Logger log = Logger.getLogger(Radio.class);
+
+    /**
+     * Variable for the input EL expression.
+     */
+    private String input;
+
+    /**
+     * Variable for the value EL epxression.
+     */
+    private String value;
+
+    /**
+     * Javascript for onclick.
+     */
+    private String onClick;
+
+    /**
+     * Javascript for onblur.
+     */
+    private String onBlur;
+
+    /**
+     * Variable for style class EL expression.
+     */
+    private String styleClass;
+
+    /**
+     * Javascript for onFocus.
+     */
+    private String onFocus;
+
+    /**
+     * Javascript for onmousedown.
+     */
+    private String onMouseDown;
+
+    /**
+     * Javascript for onmousemove.
+     */
+    private String onMouseMove;
+
+    /**
+     * Javascript for onmouseout.
+     */
+    private String onMouseOut;
+
+    /**
+     * Javascript for onmouseover.
+     */
+    private String onMouseOver;
+
+    /**
+     * Javascript for onmouseup.
+     */
+    private String onMouseUp;
+
+    /**
+     * Default constructor.
+     */
+    public Radio() {
+        super();
+    }
+
+    /**
+     * Sets the input EL value.
+     * @param input String
+     */
+    public void setInput(final String input) {
+        this.input = input;
+    }
+
+    /**
+     * Sets the ValidValue EL value.
+     * @param value String
+     */
+    public void setValue(final String value) {
+        this.value = value;
+    }
+
+    /**
+     * Sets the onClick javascript (non-EL)
+     * @param onClick String
+     */
+    public void setOnClick(final String onClick) {
+        this.onClick = onClick;
+    }
+
+    /**
+     * Sets the onblur javascript (non-EL).
+     * @param onBlur String
+     */
+    public void setOnBlur(final String onBlur) {
+        this.onBlur = onBlur;
+    }
+
+    /**
+     * Sets the CSS Style Class EL Value.
+     * @param styleClass String
+     */
+    public void setStyleClass(final String styleClass) {
+        this.styleClass = styleClass;
+    }
+
+    /**
+     * Sets the onfocus javascript event handler (non-EL).
+     * @param onFocus String
+     */
+    public void setOnFocus(String onFocus) {
+        this.onFocus = onFocus;
+    }
+
+    /**
+     * Sets the onmousedown javascript event handler (non-EL).
+     * @param onMouseDown String
+     */
+    public void setOnMouseDown(String onMouseDown) {
+        this.onMouseDown = onMouseDown;
+    }
+
+    /**
+     * Sets the onmousemove javascript event handler (non-EL).
+     * @param onMouseMove String
+     */
+    public void setOnMouseMove(String onMouseMove) {
+        this.onMouseMove = onMouseMove;
+    }
+
+    /**
+     * Sets the onMouseOut javascript event handler (non-EL).
+     * @param onMouseOut String
+     */
+    public void setOnMouseOut(String onMouseOut) {
+        this.onMouseOut = onMouseOut;
+    }
+
+    /**
+     * Sets the onMouseOver javascript event handler (non-EL).
+     * @param onMouseOver String
+     */
+    public void setOnMouseOver(String onMouseOver) {
+        this.onMouseOver = onMouseOver;
+    }
+
+    /**
+     * Sets the onMouseUp javascript event handler (non-EL).
+     * @param onMouseUp String
+     */
+    public void setOnMouseUp(String onMouseUp) {
+        this.onMouseUp = onMouseUp;
+    }
+
+    /**
+     * Gets the input El value.
+     * @return String
+     */
+    public String getInput() {
+        return input;
+    }
+
+    /**
+     * Gets the ValidValue EL value.
+     * @return String
+     */
+    public String getValue() {
+        return value;
+    }
+
+    /**
+     * Get the onclick javascript (non-EL).
+     * @return String
+     */
+    public String getOnClick() {
+        return onClick;
+    }
+
+    /**
+     * Get the onclick javascript (non-EL).
+     * @return String
+     */
+    public String getOnBlur() {
+        return onBlur;
+    }
+
+    /**
+     * Get the onclick javascript (non-EL).
+     * @return String
+     */
+    public String getStyleClass() {
+        return styleClass;
+    }
+
+    /**
+    * Get the onfocus javascript (non-EL).
+    * @return String
+    */
+   public String getOnFocus() {
+        return onFocus;
+    }
+
+    /**
+    * Get the onmousedown javascript (non-EL).
+    * @return String
+    */
+   public String getOnMouseDown() {
+        return onMouseDown;
+    }
+
+    /**
+     * Get the onmousemove javascript (non-EL).
+     * @return String
+     */
+    public String getOnMouseMove() {
+        return onMouseMove;
+    }
+
+    /**
+     * Get the onclick javascript (non-EL).
+     * @return String
+     */
+    public String getOnMouseOut() {
+        return onMouseOut;
+    }
+
+    /**
+     * Get the onmouseover javascript (non-EL).
+     * @return String
+     */
+    public String getOnMouseOver() {
+        return onMouseOver;
+    }
+
+    /**
+    * Get the onmouseup javascript (non-EL).
+    * @return String
+    */
+   public String getOnMouseUp() {
+        return onMouseUp;
+    }
+
+    /**
+     * Does the actual grunt work.
+     *
+     * @return EVAL_PAGE
+     * @throws javax.servlet.jsp.JspException upon evaluation or I/O error.
+     */
+    public int doEndTag() throws javax.servlet.jsp.JspException
+    {
+        //Grab the EL engine.
+        final ELTagSupport support = ELTagSupport.getInstance();
+
+        //Evaluate the EL expressions.
+        final Input result = (Input)evaluateValue(support, Input.class, getInput(), "input");
+        final String cssStyle = (String)evaluateValue(support, String.class, getStyleClass(), "styleClass");
+        final ValidValue vv = (ValidValue)evaluateValue(support, ValidValue.class, getValue(), "value");
+
+        final StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
+
+        //Open the tag.
+        stringBuffer.append("<input type=\"radio\"");
+
+
+        //Write the name of the tag.
+        stringBuffer.append(" name=\"");
+        stringBuffer.append(result.getName());
+        stringBuffer.append("\"");
+
+        //Write the value of the tag.
+        stringBuffer.append(" value=\"");
+        stringBuffer.append(vv.getKey());
+        stringBuffer.append("\"");
+
+        //Write style if set.
+        if (cssStyle != null) {
+            stringBuffer.append(" class=\"");
+            stringBuffer.append(cssStyle);
+            stringBuffer.append("\"");
+        }
+
+        //Set it to checked if it is supposed to be.
+        renderChecked(stringBuffer, result, vv);
+
+        //Render all the javscript events (if any)
+        renderJavascript(stringBuffer);
+
+        //Close the tag
+        stringBuffer.append("/>");
+
+        //Create the string from the buffer.
+        String sendToClient = stringBuffer.toString();
+
+        if (log.isDebugEnabled()) {
+            log.debug("Writing the following radio code to the page: " + sendToClient);
+        }
+
+        try {
+            //Send it to the client.
+            pageContext.getOut().write(sendToClient);
+        } catch (IOException ex1) {
+            log.error("I/O exception writing output value", ex1);
+            throw new JspException("I/O error");
+        }
+
+        return EVAL_PAGE;
+    }
+
+    /**
+     * Renders whether the value is checked or not.
+     *
+     * @param stringBuffer StringBuffer
+     * @param currentInput Input
+     * @param currentValue ValidValue
+     */
+    private void renderChecked(final StringBuffer stringBuffer, final Input currentInput, final ValidValue currentValue) {
+        String currentInputValue = currentInput.getDefaultValue();
+        if (currentInputValue.equals(currentValue.getKey())) {
+            stringBuffer.append(" checked ");
+        }
+    }
+
+    /**
+     * Renders the javscript event handlers to the radio (if any are defined).
+     * @param bufferToRenderTo StringBuffer the buffer that the event handlers
+     * are written to.
+     */
+    public void renderJavascript(StringBuffer bufferToRenderTo) {
+        final int EVENT_NAME = 0;
+        final int EVENT_CODE = 1;
+
+        //All the javscript events and their appropriate values.
+        String javascriptValues[][] =
+        {
+            {"onblur", getOnBlur()},
+            {"onfocus", getOnFocus()},
+            {"onmousedown", getOnMouseDown()},
+            {"onmousemove", getOnMouseMove()},
+            {"onmouseout", getOnMouseOut()},
+            {"onmouseover", getOnMouseOver()},
+            {"onmouseup", getOnMouseUp()},
+        };
+
+        //Iterate over all the javascript event handlers and write their respective values
+        //to the buffer.
+        for (int i = 0; i < javascriptValues.length; i++) {
+
+            //If there is a value for the javascript event defined.
+            if (javascriptValues[i][EVENT_CODE] != null) {
+
+                //Write it.
+                bufferToRenderTo.append(" ");
+                bufferToRenderTo.append(javascriptValues[EVENT_NAME]);
+                bufferToRenderTo.append("=\"");
+                bufferToRenderTo.append(javascriptValues[EVENT_CODE]);
+                bufferToRenderTo.append("\"");
+            }
+        }
+    }
+
+    /**
+     * Evaluate a value through the EL engine.
+     * @param elEngine ELTagSupport the EL evaluation engine.
+     * @param expectedClass Class the expected resulting final class.
+     * @param parameterValue String the value set by the tag user on the JSP page.
+     * @param parameterName String the property we're using for evaluation purposes.
+     * @return Object of the specified expected class.
+     * @throws JspException if unable to evaluate the function or if for some
+     * reason null was returned from the expression.
+     */
+    protected Object evaluateValue(final ELTagSupport elEngine, final Class expectedClass, final String parameterValue, final String parameterName) throws JspException{
+        if (parameterValue == null) {
+            return null;
+        }
+
+        Object result =  elEngine.evaluate(parameterName,parameterValue,
+            expectedClass,
+            this, this.pageContext);
+
+        if (result == null) {
+            throw new JspException("Unable to locate a " + expectedClass.toString() + " using expression: " + parameterValue + " for property " + parameterName);
+        }
+
+        return result;
+
+    }
+
+    /**
+     * Returns a string representation of the object.
+     *
+     * @return a string representation of the object.
+     */
+    public String toString() {
+        return "Radio Tag [Input = " + getInput() + " Value = " + getValue() + "]";
+    }
+}


More information about the cvs mailing list