You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by pr...@apache.org on 2005/11/02 01:25:44 UTC

svn commit: r330161 - in /myfaces/sandbox/trunk: conf/ src/java/org/apache/myfaces/custom/ajax/ src/java/org/apache/myfaces/custom/ajax/api/ src/java/org/apache/myfaces/custom/ajax/util/ src/java/org/apache/myfaces/custom/inputAjax/ tld/

Author: prophecy
Date: Tue Nov  1 16:25:39 2005
New Revision: 330161

URL: http://svn.apache.org/viewcvs?rev=330161&view=rev
Log:
InputTextAjax component that updates through ajax onchange

Added:
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/AjaxCallbacks.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/util/
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/util/AjaxRendererUtils.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjax.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjaxRenderer.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjaxTag.java
Modified:
    myfaces/sandbox/trunk/conf/faces-config.xml
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxComponent.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxDecodePhaseListener.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxPhaseListener.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjax.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjaxRenderer.java
    myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjaxTag.java
    myfaces/sandbox/trunk/tld/myfaces_sandbox.tld

Modified: myfaces/sandbox/trunk/conf/faces-config.xml
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/conf/faces-config.xml?rev=330161&r1=330160&r2=330161&view=diff
==============================================================================
--- myfaces/sandbox/trunk/conf/faces-config.xml (original)
+++ myfaces/sandbox/trunk/conf/faces-config.xml Tue Nov  1 16:25:39 2005
@@ -34,6 +34,11 @@
     <component-class>org.apache.myfaces.custom.inputAjax.HtmlSelectManyCheckboxAjax</component-class>
   </component>
 
+    <component>
+    <component-type>org.apache.myfaces.InputTextAjax</component-type>
+    <component-class>org.apache.myfaces.custom.inputAjax.HtmlInputTextAjax</component-class>
+  </component>
+
   <component>
     <component-type>org.apache.myfaces.Planner</component-type>
     <component-class>org.apache.myfaces.custom.schedule.HtmlPlanner</component-class>
@@ -126,6 +131,11 @@
       <renderer-class>org.apache.myfaces.custom.inputAjax.HtmlSelectManyCheckboxAjaxRenderer</renderer-class>
     </renderer>
 
+    <renderer>
+      <component-family>javax.faces.Input</component-family>
+      <renderer-type>org.apache.myfaces.InputTextAjax</renderer-type>
+      <renderer-class>org.apache.myfaces.custom.inputAjax.HtmlInputTextAjaxRenderer</renderer-class>
+    </renderer>
 
 
     <renderer>

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/AjaxCallbacks.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/AjaxCallbacks.java?rev=330161&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/AjaxCallbacks.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/AjaxCallbacks.java Tue Nov  1 16:25:39 2005
@@ -0,0 +1,23 @@
+package org.apache.myfaces.custom.ajax;
+
+/**
+ * Could probably merge this interface with AjaxComponent
+ * 
+ * User: treeder
+ * Date: Oct 28, 2005
+ * Time: 7:35:57 PM
+ */
+public interface AjaxCallbacks
+{
+    String getOnSuccess();
+
+    void setOnSuccess(String onSuccess);
+
+    String getOnFailure();
+
+    void setOnFailure(String onFailure);
+
+    String getOnStart();
+
+    void setOnStart(String onStart);
+}

Modified: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxComponent.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxComponent.java?rev=330161&r1=330160&r2=330161&view=diff
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxComponent.java (original)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxComponent.java Tue Nov  1 16:25:39 2005
@@ -28,4 +28,5 @@
     void encodeAjax(FacesContext context) throws IOException;
 
     void decodeAjax(FacesContext context);
+    
 }

Modified: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxDecodePhaseListener.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxDecodePhaseListener.java?rev=330161&r1=330160&r2=330161&view=diff
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxDecodePhaseListener.java (original)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxDecodePhaseListener.java Tue Nov  1 16:25:39 2005
@@ -52,14 +52,13 @@
             //DebugUtils.printView(root, System.out);
             String affectedAjaxComponent = (String) context.getExternalContext().getRequestParameterMap().get("affectedAjaxComponent");
             UIComponent ajaxComponent = root.findComponent(affectedAjaxComponent);
-            log.debug("affectedAjaxComponent: " + ajaxComponent + " - " + ajaxComponent.getId());
-
             if (ajaxComponent == null)
             {
                 String msg = "Component with id [" + affectedAjaxComponent + "] not found in view tree.";
                 log.error(msg);
                 throw new ComponentNotFoundException(msg);
             }
+            log.debug("affectedAjaxComponent: " + ajaxComponent + " - " + ajaxComponent.getId());
             if (ajaxComponent instanceof AjaxComponent)
             {
                 try

Modified: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxPhaseListener.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxPhaseListener.java?rev=330161&r1=330160&r2=330161&view=diff
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxPhaseListener.java (original)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/api/AjaxPhaseListener.java Tue Nov  1 16:25:39 2005
@@ -93,12 +93,13 @@
 
                 try
                 {
-                    HtmlBufferResponseWriterWrapper wrapper = HtmlBufferResponseWriterWrapper.getInstance(null);
-                    context.setResponseWriter(wrapper);
+                    // TR - What is this for????  This caused me hours and hours of pain
+                    // HtmlBufferResponseWriterWrapper wrapper = HtmlBufferResponseWriterWrapper.getInstance(null);
+                    // context.setResponseWriter(wrapper);
 
                     ((AjaxComponent) ajaxComponent).encodeAjax(context);
 
-                    StringBuffer buf = new StringBuffer(wrapper.toString());
+                    /*StringBuffer buf = new StringBuffer(wrapper.toString());
 
                     buf.insert(0,Integer.toHexString(buf.length())+"\r\n");
                     buf.append("\r\n"+0+"\r\n\r\n");
@@ -114,7 +115,7 @@
                         //System.out.println("PhaseListener: buf = " + buf.toString());
                         writer.flush();
                         writer.close();
-                    }
+                    }*/
                 }
                 catch (IOException e)
                 {

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/util/AjaxRendererUtils.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/util/AjaxRendererUtils.java?rev=330161&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/util/AjaxRendererUtils.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/ajax/util/AjaxRendererUtils.java Tue Nov  1 16:25:39 2005
@@ -0,0 +1,247 @@
+package org.apache.myfaces.custom.ajax.util;
+
+import org.apache.myfaces.component.html.util.AddResource;
+import org.apache.myfaces.renderkit.JSFAttr;
+import org.apache.myfaces.renderkit.html.HTML;
+import org.apache.myfaces.custom.prototype.PrototypeResourceLoader;
+import org.apache.myfaces.custom.ajax.AjaxCallbacks;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.component.UIComponent;
+import javax.faces.application.ViewHandler;
+import javax.faces.application.FacesMessage;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Map;
+import java.util.Iterator;
+
+/**
+ * User: treeder
+ * Date: Oct 31, 2005
+ * Time: 1:52:07 PM
+ */
+public class AjaxRendererUtils
+{
+    private static final Log log = LogFactory.getLog(AjaxRendererUtils.class);
+
+    public static void addPrototypeScript(FacesContext context, UIComponent component, AddResource addResource)
+    {
+        String javascriptLocation = (String) component.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
+        if (javascriptLocation != null)
+        {
+            addResource.addJavaScriptToHeader(context, javascriptLocation + "/prototype.js");
+        }
+        else
+        {
+            addResource.addJavaScriptToHeader(context, PrototypeResourceLoader.class, "prototype.js");
+        }
+    }
+
+    public static void writeAjaxScript(FacesContext context, ResponseWriter out, AjaxCallbacks component)
+            throws IOException
+    {
+        writeAjaxScript(context, out, component, null);
+    }
+
+    /**
+     * Not really liking having the extraParams thing, seems to inflexible for altering other things
+     *
+     * @param context
+     * @param out
+     * @param component
+     * @param extraParams
+     * @throws IOException
+     */
+    public static void writeAjaxScript(FacesContext context, ResponseWriter out, AjaxCallbacks component, String extraParams) throws IOException
+    {
+        UIComponent uiComponent = (UIComponent) component;
+        String clientId = uiComponent.getClientId(context);
+        String viewId = context.getViewRoot().getViewId();
+        ViewHandler viewHandler = context.getApplication().getViewHandler();
+        String actionURL = viewHandler.getActionURL(context, viewId);
+        String ajaxURL = actionURL;
+
+        String jsNameSpace = uiComponent.getId() + "_MyFaces_inputAjax_";
+
+        out.startElement(HTML.SCRIPT_ELEM, null);
+        out.writeAttribute(HTML.TYPE_ATTR, "text/javascript", null);
+
+        // todo: only namespace the things that are specific to the component and only output those a second time, use comment below to limit
+        // // check to see if javascript has already been written
+        /*
+        if (context.getExternalContext().getRequestMap().containsKey(JAVASCRIPT_ENCODED))
+        {
+            return;
+        }*/
+
+        out.writeText("var " + jsNameSpace + "ajaxUrl = '" + ajaxURL + "';\n", null);
+        if (component.getOnSuccess() != null)
+            out.writeText("var " + jsNameSpace + "onSuccessFunction = " + component.getOnSuccess() + ";\n", null);
+        if (component.getOnFailure() != null)
+            out.writeText("var " + jsNameSpace + "onFailureFunction = " + component.getOnFailure() + ";\n", null);
+        if (component.getOnStart() != null)
+            out.writeText("var " + jsNameSpace + "onStartFunction = " + component.getOnStart() + ";\n", null);
+
+        // todo: stringbuffer this
+        StringBuffer buff = new StringBuffer();
+        buff.append("\n" +
+                "function " + jsNameSpace + "notifyElement(originalRequest, successful)\n" +
+                "{\n" +
+                "    //alert(\"originalRequest: \" + originalRequest + \" - \" + successful + \"\\ntext: \" + originalRequest.responseText);\n" +
+                "    var errorArray = originalRequest.responseXML.getElementsByTagName(\"response\")[0].getElementsByTagName(\"error\");\n" +
+                "    if(errorArray && errorArray.length > 0){\n" +
+                // could easily loop this and handle more than one error at a time, but that wouldn't happen now
+                "        var myObError = errorArray[0];\n" +
+                "        var errorClientId = myObError.getAttribute(\"elname\");\n" +
+                "        var errorSeverity = myObError.getAttribute(\"severity\");\n" +
+                "        var errorSummary = myObError.getAttribute(\"summary\");\n" +
+                "        " + jsNameSpace + "displayError(errorClientId, errorSeverity, errorSummary);\n" +
+                "    }\n" +
+                "    var myObElementArray = originalRequest.responseXML.getElementsByTagName(\"response\")[0].getElementsByTagName(\"elementUpdated\");\n" +
+                "    if(myObElementArray && myObElementArray.length > 0){" +
+                "       var myObElement = myObElementArray[0];\n" +
+                "       var elname = myObElement.getAttribute(\"elname\");\n" +
+                "       var elvalue = myObElement.getAttribute(\"elvalue\");\n" +
+                "       if (successful)\n" +
+                "       {\n");
+        if (component.getOnSuccess() != null)
+            buff.append("        " + jsNameSpace + "onSuccessFunction(elname, elvalue);\n");
+        buff.append("    }\n" +
+                "        else\n" +
+                "        {\n");
+        if (component.getOnFailure() != null)
+            buff.append("        " + jsNameSpace + "onFailureFunction(elname, elvalue);\n");
+        buff.append("        }\n" +
+                "     }\n" +
+                "}\n" +
+                "function " + jsNameSpace + "displayError(elname, severity, summary){\n" +
+                "    var msgSpan = document.getElementById(\"msg_\" + elname);\n" +
+                "    msgSpan.innerHTML = summary;\n" +
+                "}\n" +
+                "function " + jsNameSpace + "notifyElementFailure(originalRequest)\n" +
+                "{\n" +
+                " alert('failed');\n" +
+                "    " + jsNameSpace + "notifyElement(originalRequest, false);\n" +
+                "}\n" +
+                "function " + jsNameSpace + "notifyElementSuccess(originalRequest)\n" +
+                "{\n" +
+                "    " + jsNameSpace + "notifyElement(originalRequest, true);\n" +
+                "}\n" +
+                "function " + jsNameSpace + "complete(originalRequest)\n" +
+                "{\n" +
+                "    alert('completed'); \n" +
+                "}\n" +
+                "function " + jsNameSpace + "ajaxSubmit(f, elname)\n" +
+                "{\n" +
+                " var elvalue = f.value;\n");
+
+        if (component.getOnStart() != null)
+            buff.append("    " + jsNameSpace + "onStartFunction(elname, elvalue);\n");
+        buff.append("    var pars = \"affectedAjaxComponent=\" + elname + \"&elname=\" + elname + \"&elvalue=\" + elvalue + \"");
+        buff.append("&\" + elname + \"=\" + elvalue + \"");
+        if (extraParams != null)
+        {
+            buff.append(extraParams);
+        }
+        if (context.getApplication().getStateManager().isSavingStateInClient(context))
+        {
+            buff.append("&jsf_tree_64=\"+encodeURIComponent(document.getElementById(\"jsf_tree_64\").value)+\"&jsf_state_64=\"+encodeURIComponent(document.getElementById(\"jsf_state_64\").value)+\"&jsf_viewid=\"+encodeURIComponent(document.getElementById(\"jsf_viewid\").value)+\"");
+        }
+        buff.append("\";\n");
+        //buff.append("alert('pars: ' + pars);");
+
+        buff.append("    var "
+                //+ uiComponent.getId()
+                + "_ajaxRequest = new Ajax.Request(\n" +
+                "    " + jsNameSpace + "ajaxUrl,\n" +
+                "    {method: 'post'" +
+                ", parameters: pars");
+        buff.append(", onComplete: " + jsNameSpace + "complete");
+        //if (component.getOnSuccess() != null)
+         buff.append(", onSuccess: " + jsNameSpace + "notifyElementSuccess");
+        //if (component.getOnFailure() != null)
+          buff.append(", onFailure: " + jsNameSpace + "notifyElementFailure");
+        buff.append("} \n" +
+                "            );\n" +
+                "}");
+        out.writeText(buff.toString(), null);
+
+        out.endElement(HTML.SCRIPT_ELEM);
+    }
+
+
+    public static void encodeAjax(FacesContext context, UIComponent component)
+            throws IOException
+    {
+
+        encodeAjax(context, component, null);
+    }
+
+    public static void encodeAjax(FacesContext context, UIComponent component, Map extraReturnAttributes) throws IOException
+    {
+        String clientId = component.getClientId(context);
+        Object responseOb = context.getExternalContext().getResponse();
+        if (responseOb instanceof HttpServletResponse)
+        {
+            HttpServletResponse response = (HttpServletResponse) responseOb;
+            //response.setContentType("application/xml");
+            response.reset();
+            //response.setCharacterEncoding("UTF-8");
+            response.setContentType("text/xml");
+
+            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
+            // this is all pretty wacky because it wasn't returning anything, turns out to be the OutputWrapper in AjaxPhaseListener!!!  man oh man, countless hours wasted
+            StringWriter sw = new StringWriter();
+            PrintWriter buff = new PrintWriter(sw);
+            // todo: remove concats
+            buff.println("<?xml version=\"1.0\"?>");
+            buff.println("<response>");
+            buff.println();
+            Iterator iter = context.getMessages(clientId);
+            while (iter.hasNext())
+            {
+                FacesMessage msg = (FacesMessage) iter.next();
+                buff.append("<error elname=\"").append(request.getParameter("elname"))
+                        .append("\" severity=\"").append(msg.getSeverity().toString())
+                        .append("\" summary=\"").append(msg.getSummary())
+                        .append("\" />");
+                buff.println();
+            }
+            buff.append("<elementUpdated elname=\"").append(request.getParameter("elname"));
+            buff.append("\" elvalue=\"").append(request.getParameter("elvalue")).append("\"");
+            if (extraReturnAttributes != null)
+            {
+                Iterator iter2 = extraReturnAttributes.keySet().iterator();
+                while (iter2.hasNext())
+                {
+                    String key = (String) iter2.next();
+                    buff.append(" ").append(key).append("=\"").append(extraReturnAttributes.get(key).toString()).append("\"");
+                }
+            }
+
+            buff.append(" />");
+            buff.println();
+            buff.println("</response>");
+            String output = sw.toString();
+            log.debug(output);
+            PrintWriter out = response.getWriter();
+            out.print(output);
+            out.flush();
+
+
+           /* out.println("<?xml version=\"1.0\"?>");
+            out.println("<response>");
+            out.println("<elementUpdated elname=\"" + request.getParameter("elname") + "\" elvalue=\"" +
+                    request.getParameter("elvalue") + "\" checked=\"" + request.getParameter("checked") + "\"/>");
+            out.println("</response>");*/
+
+
+        }
+    }
+}

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjax.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjax.java?rev=330161&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjax.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjax.java Tue Nov  1 16:25:39 2005
@@ -0,0 +1,103 @@
+package org.apache.myfaces.custom.inputAjax;
+
+import org.apache.myfaces.component.html.ext.HtmlInputText;
+import org.apache.myfaces.custom.ajax.api.AjaxComponent;
+import org.apache.myfaces.custom.ajax.api.AjaxRenderer;
+import org.apache.myfaces.custom.ajax.AjaxCallbacks;
+import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.faces.context.FacesContext;
+import javax.faces.render.Renderer;
+import java.util.Map;
+import java.util.Collection;
+import java.util.Iterator;
+import java.io.IOException;
+
+/**
+ * User: treeder
+ * Date: Oct 28, 2005
+ * Time: 7:48:57 PM
+ */
+public class HtmlInputTextAjax extends HtmlInputText implements AjaxComponent, AjaxCallbacks
+{
+    private static final Log log = LogFactory.getLog(HtmlInputTextAjax.class);
+    public static final String COMPONENT_TYPE = "org.apache.myfaces.InputTextAjax";
+    public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.InputTextAjax";
+
+    private String onSuccess;
+    private String onFailure;
+    private String onStart;
+
+    public HtmlInputTextAjax()
+    {
+        super();
+        setRendererType(DEFAULT_RENDERER_TYPE);
+    }
+
+    /**
+     * Decode ajax request and apply value changes
+     *
+     * @param context
+     */
+    public void decodeAjax(FacesContext context)
+    {
+        log.debug("entering HtmlInputTextAjax.decodeAjax");
+
+        //Map requestParams = context.getExternalContext().getRequestParameterMap();
+
+        // now go through the required lifecycle phases
+        HtmlRendererUtils.decodeUIInput(context, this);
+        validate(context);
+        updateModel(context);
+        Object valOb = this.getValue();
+        log.debug("value object after decodeAjax: " + valOb);
+
+    }
+
+    public void encodeAjax(FacesContext context) throws IOException
+    {
+        log.debug("encodeAjax in HtmlInputTextAjax");
+        if (context == null) throw new NullPointerException("context");
+        if (!isRendered()) return;
+        Renderer renderer = getRenderer(context);
+
+        if (renderer != null && renderer instanceof AjaxRenderer)
+        {
+            ((AjaxRenderer) renderer).encodeAjax(context, this);
+
+        }
+    }
+
+
+    public String getOnSuccess()
+    {
+        return onSuccess;
+    }
+
+    public void setOnSuccess(String onSuccess)
+    {
+        this.onSuccess = onSuccess;
+    }
+
+    public String getOnFailure()
+    {
+        return onFailure;
+    }
+
+    public void setOnFailure(String onFailure)
+    {
+        this.onFailure = onFailure;
+    }
+
+    public String getOnStart()
+    {
+        return onStart;
+    }
+
+    public void setOnStart(String onStart)
+    {
+        this.onStart = onStart;
+    }
+}

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjaxRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjaxRenderer.java?rev=330161&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjaxRenderer.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjaxRenderer.java Tue Nov  1 16:25:39 2005
@@ -0,0 +1,96 @@
+package org.apache.myfaces.custom.inputAjax;
+
+import org.apache.myfaces.renderkit.html.HtmlTextRendererBase;
+import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
+import org.apache.myfaces.renderkit.html.HTML;
+import org.apache.myfaces.renderkit.RendererUtils;
+import org.apache.myfaces.custom.ajax.api.AjaxRenderer;
+import org.apache.myfaces.custom.ajax.util.AjaxRendererUtils;
+import org.apache.myfaces.component.html.util.AddResource;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.component.UIComponent;
+import javax.faces.application.FacesMessage;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Iterator;
+
+/**
+ * User: treeder
+ * Date: Oct 28, 2005
+ * Time: 7:54:08 PM
+ */
+public class HtmlInputTextAjaxRenderer extends HtmlTextRendererBase implements AjaxRenderer
+{
+    private static final Log log = LogFactory.getLog(HtmlInputTextAjaxRenderer.class);
+    private static final String JAVASCRIPT_ENCODED = "org.apache.myfaces.custom.inputAjax.HtmlInputTextAjax.JAVASCRIPT_ENCODED";
+
+
+    /**
+     * Encodes any stand-alone javascript functions that are needed.
+     * Uses either the extension filter, or a
+     * user-supplied location for the javascript files.
+     *
+     * @param context   FacesContext
+     * @param component UIComponent
+     * @throws java.io.IOException
+     */
+    private void encodeJavascript(FacesContext context, UIComponent component) throws IOException
+    {
+        HtmlInputTextAjax comp = (HtmlInputTextAjax) component;
+
+        AddResource addResource = AddResource.getInstance(context);
+
+        AjaxRendererUtils.addPrototypeScript(context, component, addResource);
+
+        ResponseWriter out = context.getResponseWriter();
+        AjaxRendererUtils.writeAjaxScript(context, out, comp);
+
+        context.getExternalContext().getRequestMap().put(JAVASCRIPT_ENCODED, Boolean.TRUE);
+    }
+
+
+
+
+    public void encodeEnd(FacesContext context, UIComponent component) throws IOException
+    {
+        log.debug("encodeEnd in HtmlInputTextAjaxRenderer");
+        RendererUtils.checkParamValidity(context, component, HtmlInputTextAjax.class);
+
+        if (HtmlRendererUtils.isDisplayValueOnly(component) || isDisabled(context, component))
+        {
+            super.encodeEnd(context, component);
+            return;
+        }
+
+        //String clientId = component.getClientId(context);
+
+        HtmlInputTextAjax comp = (HtmlInputTextAjax) component;
+        //log.debug("setting onclick");
+        comp.setOnchange(component.getId() + "_MyFaces_inputAjax_ajaxSubmit(this, '" + component.getId() + "');");
+
+        this.encodeJavascript(context, component);
+
+        super.encodeEnd(context, component);
+        
+        // output a span for error messages
+        ResponseWriter writer = context.getResponseWriter();        
+        writer.startElement(HTML.SPAN_ELEM, component);
+        writer.writeAttribute(HTML.ID_ATTR, "msg_" + component.getId(), null);
+        writer.writeText(" ", null);
+        writer.endElement(HTML.SPAN_ELEM);
+                
+
+    }
+
+    public void encodeAjax(FacesContext context, UIComponent component) throws IOException
+    {
+        log.debug("encodeAjax in HtmlInputTextAjaxRenderer");
+        AjaxRendererUtils.encodeAjax(context, component);
+    }
+}

Added: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjaxTag.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjaxTag.java?rev=330161&view=auto
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjaxTag.java (added)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlInputTextAjaxTag.java Tue Nov  1 16:25:39 2005
@@ -0,0 +1,81 @@
+package org.apache.myfaces.custom.inputAjax;
+
+import org.apache.myfaces.taglib.html.ext.HtmlInputTextTag;
+import org.apache.myfaces.custom.ajax.AjaxCallbacks;
+import org.apache.myfaces.component.html.ext.HtmlInputText;
+
+import javax.faces.component.UIComponent;
+
+/**
+ * User: treeder
+ * Date: Oct 28, 2005
+ * Time: 7:42:08 PM
+ */
+public class HtmlInputTextAjaxTag extends HtmlInputTextTag implements AjaxCallbacks
+{
+    private String onSuccess;
+    private String onFailure;
+    private String onStart;
+
+      public String getComponentType() {
+        return HtmlInputTextAjax.COMPONENT_TYPE;
+    }
+
+    /**
+     * @return the RendererType String
+     */
+    public String getRendererType() {
+        return HtmlInputTextAjax.DEFAULT_RENDERER_TYPE;
+    }
+
+
+    public void release()
+    {
+        super.release();
+        onSuccess = null;
+        onFailure = null;
+        onStart = null;
+    }
+
+    protected void setProperties(UIComponent component)
+    {
+        super.setProperties(component);
+
+        setStringProperty(component, "onSuccess", onSuccess);
+        setStringProperty(component, "onFailure", onFailure);
+        setStringProperty(component, "onStart", onStart);
+    }
+
+
+    public String getOnSuccess()
+    {
+        return onSuccess;
+    }
+
+    public void setOnSuccess(String onSuccess)
+    {
+        this.onSuccess = onSuccess;
+    }
+
+    public String getOnFailure()
+    {
+        return onFailure;
+    }
+
+    public void setOnFailure(String onFailure)
+    {
+        this.onFailure = onFailure;
+    }
+
+    public String getOnStart()
+    {
+        return onStart;
+    }
+
+    public void setOnStart(String onStart)
+    {
+        this.onStart = onStart;
+    }
+
+
+}

Modified: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjax.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjax.java?rev=330161&r1=330160&r2=330161&view=diff
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjax.java (original)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjax.java Tue Nov  1 16:25:39 2005
@@ -3,6 +3,8 @@
 import org.apache.myfaces.component.html.ext.HtmlSelectManyCheckbox;
 import org.apache.myfaces.custom.ajax.api.AjaxComponent;
 import org.apache.myfaces.custom.ajax.api.AjaxRenderer;
+import org.apache.myfaces.custom.ajax.AjaxCallbacks;
+import org.apache.myfaces.custom.ExtendedComponentBase;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -25,7 +27,7 @@
  * Date: Oct 20, 2005
  * Time: 1:40:15 PM
  */
-public class HtmlSelectManyCheckboxAjax extends HtmlSelectManyCheckbox implements AjaxComponent
+public class HtmlSelectManyCheckboxAjax extends HtmlSelectManyCheckbox implements AjaxComponent, ExtendedComponentBase, AjaxCallbacks
 {
     private static final Log log = LogFactory.getLog(HtmlSelectManyCheckboxAjax.class);
     public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlSelectManyCheckboxAjax";
@@ -54,13 +56,16 @@
 
         String elname = (String) requestParams.get("elname");
         String elvalue = (String) requestParams.get("elvalue");
-        boolean checked = Boolean.getBoolean(requestParams.get("checked")==null?"false":
-        (String)requestParams.get("checked"));
+        String checkedStr = (String) requestParams.get("checked");
+        log.debug("checkedStr: " + checkedStr);
+        boolean checked = Boolean.parseBoolean(checkedStr);
+        log.debug("checked: " + checked);
         // now apply this to the
         Object valOb = this.getValue();
         log.debug("valOb: " + valOb);
         if(valOb instanceof Collection){
             // then all good
+            log.debug("valOb is collection");
             Collection c = (Collection) valOb;
             updateChosenValue(c, elname, elvalue, checked);
         } else {
@@ -106,7 +111,7 @@
 
     public void encodeAjax(FacesContext context) throws IOException
     {
-        log.debug("encodeAjax in HtmlSelectManyCheckbox");
+        //log.debug("encodeAjax in HtmlSelectManyCheckbox");
         if (context == null) throw new NullPointerException("context");
         if (!isRendered()) return;
         Renderer renderer = getRenderer(context);

Modified: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjaxRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjaxRenderer.java?rev=330161&r1=330160&r2=330161&view=diff
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjaxRenderer.java (original)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjaxRenderer.java Tue Nov  1 16:25:39 2005
@@ -7,6 +7,7 @@
 import org.apache.myfaces.renderkit.JSFAttr;
 import org.apache.myfaces.renderkit.RendererUtils;
 import org.apache.myfaces.custom.ajax.api.AjaxRenderer;
+import org.apache.myfaces.custom.ajax.util.AjaxRendererUtils;
 import org.apache.myfaces.custom.prototype.PrototypeResourceLoader;
 import org.apache.myfaces.custom.inputsuggestajax.InputSuggestAjax;
 import org.apache.myfaces.component.html.util.AddResource;
@@ -23,6 +24,8 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
 
 /**
  * For onSuccess, onFailure, onStart functions, they should accept (elname, elvalue) attributes for the specific item
@@ -34,7 +37,7 @@
 public class HtmlSelectManyCheckboxAjaxRenderer extends HtmlCheckboxRenderer implements AjaxRenderer
 {
     private static final Log log = LogFactory.getLog(HtmlSelectManyCheckboxAjaxRenderer.class);
-    private static final String JAVASCRIPT_ENCODED = "org.apache.myfaces.custom.inputAjax.JAVASCRIPT_ENCODED";
+    private static final String JAVASCRIPT_ENCODED = "org.apache.myfaces.custom.inputAjax.HtmlSelectManyCheckbox.JAVASCRIPT_ENCODED";
 
 
     /**
@@ -48,101 +51,17 @@
      */
     private void encodeJavascript(FacesContext context, UIComponent component) throws IOException
     {
-        // check to see if javascript has already been written
-        // (which could happen if more than one inputSuggestAjax on the same page)
-        if (context.getExternalContext().getRequestMap().containsKey(JAVASCRIPT_ENCODED))
-        {
-            return;
-        }
-        HtmlSelectManyCheckboxAjax selectManyCheckbox = (HtmlSelectManyCheckboxAjax) component;
 
-        String javascriptLocation = (String) component.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
+        HtmlSelectManyCheckboxAjax selectManyCheckbox = (HtmlSelectManyCheckboxAjax) component;
 
         AddResource addResource = AddResource.getInstance(context);
-        addResource.addJavaScriptToHeader(context,
-                PrototypeResourceLoader.class, "prototype.js");
 
-        String clientId = component.getClientId(context);
-        String viewId = context.getViewRoot().getViewId();
-        ViewHandler viewHandler = context.getApplication().getViewHandler();
-        String actionURL = viewHandler.getActionURL(context, viewId);
-        // TODO: THIS SHOULD GO TO A SERVLET OR SOMETHING INSTEAD OF A JSP, NOT THE ORIGINAL PAGE I DON'T THINK BECAUSE YOU WOULDN'T WANT TO GO THROUGH THE WHOLE LIFE CYCLE
-        String ajaxURL = actionURL;
-        String viewId2 = context.getExternalContext().getRequestPathInfo();
+        AjaxRendererUtils.addPrototypeScript(context, component, addResource);
 
         ResponseWriter out = context.getResponseWriter();
 
-        out.startElement(HTML.SCRIPT_ELEM, null);
-        out.writeAttribute(HTML.TYPE_ATTR, "text/javascript", null);
-
-        out.writeText("var MyFaces_inputAjax_ajaxUrl = '" + ajaxURL + "';\n", null);
-        if (selectManyCheckbox.getOnSuccess() != null)
-            out.writeText("var MyFaces_inputAjax_onSuccessFunction = " + selectManyCheckbox.getOnSuccess() + ";\n", null);
-        if (selectManyCheckbox.getOnFailure() != null)
-            out.writeText("var MyFaces_inputAjax_onFailureFunction = " + selectManyCheckbox.getOnFailure() + ";\n", null);
-        if (selectManyCheckbox.getOnStart() != null)
-            out.writeText("var MyFaces_inputAjax_onStartFunction = " + selectManyCheckbox.getOnStart() + ";\n", null);
-
-        // todo: stringbuffer this
-        StringBuffer buff = new StringBuffer();
-        buff.append("\n" +
-                "function MyFaces_inputAjax_notifyElement(originalRequest, successful)\n" +
-                "{\n" +
-                // "    alert(\"originalRequest: \" + originalRequest + \" - \" + successful);\n" +
-                "    var myObElement = originalRequest.responseXML.getElementsByTagName(\"response\")[0].getElementsByTagName(\"elementUpdated\")[0];\n" +
-                "    var elname = myObElement.getAttribute(\"elname\");\n" +
-                "    var elvalue = myObElement.getAttribute(\"elvalue\");\n" +
-                "    if (successful)\n" +
-                "    {\n");
-        if (selectManyCheckbox.getOnSuccess() != null)
-            buff.append("        MyFaces_inputAjax_onSuccessFunction(elname, elvalue);\n");
-        buff.append("    }\n" +
-                "    else\n" +
-                "    {\n");
-        if (selectManyCheckbox.getOnFailure() != null)
-            buff.append("        MyFaces_inputAjax_onFailureFunction(elname, elvalue);\n");
-        buff.append("    }\n" +
-                "}\n" +
-                "function MyFaces_inputAjax_notifyElementFailure(originalRequest)\n" +
-                "{\n" +
-                "    MyFaces_inputAjax_notifyElement(originalRequest, false);\n" +
-                "}\n" +
-                "function MyFaces_inputAjax_notifyElementSuccess(originalRequest)\n" +
-                "{\n" +
-                "    MyFaces_inputAjax_notifyElement(originalRequest, true);\n" +
-                "}\n" +
-                "function MyFaces_inputAjax_ajaxSubmit(f, elname)\n" +
-                "{\n" +
-                " var elvalue = f.value;\n")
-                .append("var checked = f.checked;\n");
-        if (selectManyCheckbox.getOnStart() != null)
-            buff.append("    MyFaces_inputAjax_onStartFunction(elname, elvalue);\n");
-        buff.append("    var pars = \"affectedAjaxComponent=\" + elname + \"&elname=\" + elname + \"&elvalue=\" + elvalue + \"");
-        buff.append("&checked=\" + checked + \"");
-        buff.append("&vi=").append(context.getExternalContext().encodeActionURL(actionURL));
-        buff.append("&vi2=").append(context.getExternalContext().encodeActionURL(viewId2));
-        buff.append("&vi3=").append(context.getExternalContext().encodeActionURL(viewId));
-        if (context.getApplication().getStateManager().isSavingStateInClient(context))
-        {
-            buff.append("&jsf_tree_64=\"+encodeURIComponent(document.getElementById(\"jsf_tree_64\").value)+\"&jsf_state_64=\"+encodeURIComponent(document.getElementById(\"jsf_state_64\").value)+\"&jsf_viewid=\"+encodeURIComponent(document.getElementById(\"jsf_viewid\").value)+\"");
-        }
-        buff.append("\";\n");
-
-        buff.append(
-                "    var myAjax = new Ajax.Request(\n" +
-                        "    MyFaces_inputAjax_ajaxUrl,\n" +
-                        "    {method: 'post'" +
-                        ",   parameters: pars\n");
-        if (selectManyCheckbox.getOnSuccess() != null)
-            buff.append(", onComplete: MyFaces_inputAjax_notifyElementSuccess");
-        if (selectManyCheckbox.getOnFailure() != null)
-            buff.append(", onFailure: MyFaces_inputAjax_notifyElementFailure");
-        buff.append("} \n" +
-                "            );\n" +
-                "}");
-        out.writeText(buff.toString(), null);
-
-        out.endElement(HTML.SCRIPT_ELEM);
+        String extraParams =("&checked=\" + f.checked + \"");
+        AjaxRendererUtils.writeAjaxScript(context, out, selectManyCheckbox, extraParams);
 
         context.getExternalContext().getRequestMap().put(JAVASCRIPT_ENCODED, Boolean.TRUE);
     }
@@ -159,12 +78,9 @@
         }
 
         String clientId = component.getClientId(context);
-        String viewId = context.getViewRoot().getViewId();
-        ViewHandler viewHandler = context.getApplication().getViewHandler();
-        String actionURL = viewHandler.getActionURL(context, viewId);
 
         HtmlSelectManyCheckboxAjax selectManyCheckbox = (HtmlSelectManyCheckboxAjax) component;
-        selectManyCheckbox.setOnclick("MyFaces_inputAjax_ajaxSubmit(this, '" + clientId + "');");
+        selectManyCheckbox.setOnclick(component.getId() + "_MyFaces_inputAjax_ajaxSubmit(this, '" + clientId + "');");
 
         this.encodeJavascript(context, component);
 
@@ -175,22 +91,15 @@
     public void encodeAjax(FacesContext context, UIComponent component) throws IOException
     {
         log.debug("encodeAjax in HtmlSelectManyCheckbox");
-        HtmlSelectManyCheckboxAjax comp = (HtmlSelectManyCheckboxAjax) component;
-        Object responseOb = context.getExternalContext().getResponse();
-        if (responseOb instanceof HttpServletResponse)
-        {
-            HttpServletResponse response = (HttpServletResponse) responseOb;
-            response.setContentType("application/xml");
-            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
-            PrintWriter out = response.getWriter();
-            out.println("<?xml version=\"1.0\"?>");
-            out.println("<response>");
-            out.println("<elementUpdated elname=\"" + request.getParameter("elname") + "\" elvalue=\"" +
-                    request.getParameter("elvalue") + "\" checked=\"" + request.getParameter("checked") + "\"/>");
-            out.println("</response>");
-        }
+        // check for request type (portlet support)
+        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
+        Map extraReturnAttributes = new HashMap();
+        extraReturnAttributes.put("checked", request.getParameter("checked"));
+        AjaxRendererUtils.encodeAjax(context, component, extraReturnAttributes);
 
 
     }
+
+
 
 }

Modified: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjaxTag.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjaxTag.java?rev=330161&r1=330160&r2=330161&view=diff
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjaxTag.java (original)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/inputAjax/HtmlSelectManyCheckboxAjaxTag.java Tue Nov  1 16:25:39 2005
@@ -2,6 +2,8 @@
 
 import org.apache.myfaces.taglib.html.ext.HtmlSelectManyCheckboxTag;
 import org.apache.myfaces.custom.autoupdatedatatable.AutoUpdateDataTable;
+import org.apache.myfaces.custom.ajax.AjaxCallbacks;
+import org.apache.myfaces.custom.ExtendedComponentBase;
 
 import javax.faces.component.UIComponent;
 
@@ -10,7 +12,7 @@
  * Date: Oct 20, 2005
  * Time: 3:05:46 PM
  */
-public class HtmlSelectManyCheckboxAjaxTag extends HtmlSelectManyCheckboxTag
+public class HtmlSelectManyCheckboxAjaxTag extends HtmlSelectManyCheckboxTag implements AjaxCallbacks
 {
     private String onSuccess;
     private String onFailure;
@@ -71,4 +73,7 @@
     {
         this.onStart = onStart;
     }
+
+
+
 }

Modified: myfaces/sandbox/trunk/tld/myfaces_sandbox.tld
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/tld/myfaces_sandbox.tld?rev=330161&r1=330160&r2=330161&view=diff
==============================================================================
--- myfaces/sandbox/trunk/tld/myfaces_sandbox.tld (original)
+++ myfaces/sandbox/trunk/tld/myfaces_sandbox.tld Tue Nov  1 16:25:39 2005
@@ -725,6 +725,17 @@
         &ext_ajax_attributes;
     </tag>
 
+    <tag>
+        <name>inputTextAjax</name>
+        <tag-class>org.apache.myfaces.custom.inputAjax.HtmlInputTextAjaxTag</tag-class>
+        <body-content>JSP</body-content>
+        <description>
+            Extends standard inputText allowing for dynamic ajax submitting.
+        </description>
+        &standard_input_text_attributes;
+        &ext_forceId_attribute;
+        &ext_ajax_attributes;
+    </tag>
 
 
 </taglib>