You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2009/03/28 16:47:01 UTC

svn commit: r759486 - in /incubator/click/trunk/click: extras/src/org/apache/click/extras/control/ framework/src/org/apache/click/control/

Author: sabob
Date: Sat Mar 28 15:47:00 2009
New Revision: 759486

URL: http://svn.apache.org/viewvc?rev=759486&view=rev
Log:
moved isFormSubmission check to Field. CLK-508

Modified:
    incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AbstractContainerField.java
    incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/ActionButton.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/Field.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/FieldSet.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/Radio.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/RadioGroup.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/Submit.java

Modified: incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AbstractContainerField.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AbstractContainerField.java?rev=759486&r1=759485&r2=759486&view=diff
==============================================================================
--- incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AbstractContainerField.java (original)
+++ incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AbstractContainerField.java Sat Mar 28 15:47:00 2009
@@ -257,9 +257,13 @@
      * @return true to continue Page event processing or false otherwise
      */
     public boolean onProcess() {
-        boolean continueProcessing = super.onProcess();
-        if (!container.onProcess()) {
-            continueProcessing = false;
+        boolean continueProcessing = true;
+
+        if (canProcess()) {
+            continueProcessing = super.onProcess();
+            if (!container.onProcess()) {
+                continueProcessing = false;
+            }
         }
         return continueProcessing;
     }

Modified: incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java?rev=759486&r1=759485&r2=759486&view=diff
==============================================================================
--- incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java (original)
+++ incubator/click/trunk/click/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java Sat Mar 28 15:47:00 2009
@@ -20,14 +20,17 @@
 
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.text.MessageFormat;
 import java.util.List;
 
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.click.Context;
+import org.apache.click.Page;
 import org.apache.click.control.TextField;
+import org.apache.click.element.CssImport;
+import org.apache.click.element.JsImport;
+import org.apache.click.element.JsScript;
 import org.apache.click.util.ClickUtils;
 import org.apache.click.util.HtmlStringBuffer;
 
@@ -78,15 +81,6 @@
         "/org/apache/click/extras/control/prototype/prototype.js",
     };
 
-    /** The JavaScript sorting HTML import statements. */
-    public static final String HTML_IMPORTS =
-        "<link type=\"text/css\" rel=\"stylesheet\" href=\"{0}/click/extras-control{1}.css\"/>\n"
-        + "<script type=\"text/javascript\" src=\"{0}/click/control{1}.js\"></script>\n"
-        + "<script type=\"text/javascript\" src=\"{0}/click/prototype/prototype{1}.js\"></script>\n"
-        + "<script type=\"text/javascript\" src=\"{0}/click/prototype/effects{1}.js\"></script>\n"
-        + "<script type=\"text/javascript\" src=\"{0}/click/prototype/controls{1}.js\"></script>\n"
-        + "<script type=\"text/javascript\">addLoadEvent(function() '{'new Ajax.Autocompleter( ''{2}'', ''{2}_auto_complete_div'', ''{0}{3}'', {4} );'}');</script>\n";
-
     // ----------------------------------------------------- Instance Variables
 
     /**
@@ -221,23 +215,57 @@
     }
 
     /**
-     * Return the HTML CSS and JavaScript includes.
+     * Return the list of HEAD elements to be included in the page.
      *
-     * @see org.apache.click.Control#getHtmlImports()
+     * @see org.apache.click.Control#getHeadElements()
      *
-     * @return the HTML CSS and JavaScript includes
-     */
-    public String getHtmlImports() {
+     * @return the list of HEAD elements to be included in the page
+     * @throws IllegalStateException if the field's name has not been set
+     * @throws IllegalStateException if the field is not attached to the Page
+     */
+    public List getHeadElements() {
+        // Check that the field name and parent Page has been set
+        String fieldName = getName();
+        if (fieldName == null) {
+            throw new IllegalStateException("AutoCompleteTextField name"
+                + " is not defined. Set the name before calling"
+                + " getHeadElements().");
+        }
+
+        Page page = getPage();
+        if (page == null) {
+            throw new IllegalStateException("The AutoCompleteTextField, '"
+                + fieldName + "', is not attached to the Page. Add"
+                + " AutoCompleteTextField to a parent form or container and"
+                + " attach the parent to the Page before calling"
+                + " getHeadElements().");
+        }
+
         Context context = getContext();
-        String[] args = {
-            context.getRequest().getContextPath(),
-            ClickUtils.getResourceVersionIndicator(context),
-            getId(),
-            getPage().getPath(),
-            getAutoCompleteOptions()
-        };
 
-        return MessageFormat.format(HTML_IMPORTS, args);
+        if (headElements == null) {
+            headElements = super.getHeadElements();
+            headElements.add(new CssImport("/click/extras-control.css"));
+            headElements.add(new JsImport("/click/control.js"));
+            headElements.add(new JsImport("/click/prototype/prototype.js"));
+            headElements.add(new JsImport("/click/prototype/effects.js"));
+            headElements.add(new JsImport("/click/prototype/controls.js"));
+
+            String fieldId = getId();
+            String contextPath = context.getRequest().getContextPath();
+
+            JsScript script = new JsScript();
+            script.setId(fieldName + "_autocomplete");
+            HtmlStringBuffer buffer = new HtmlStringBuffer(150);
+            buffer.append("addLoadEvent(function() { new Ajax.Autocompleter(");
+            buffer.append("'").append(fieldId).append("'");
+            buffer.append(",'").append(fieldId).append("_auto_complete_div'");
+            buffer.append(",'").append(contextPath).append(page.getPath()).append("'");
+            buffer.append(",").append(getAutoCompleteOptions()).append(");})");
+
+            headElements.add(script);
+        }
+        return headElements;
     }
 
     /**
@@ -259,31 +287,6 @@
     // --------------------------------------------------------- Event Handlers
 
     /**
-     * Register the field with the parent page to intercept POST autocompletion
-     * requests.
-     *
-     * @see org.apache.click.Control#onInit()
-     */
-    public void onInit() {
-        super.onInit();
-        // See whether control has been registered at Page level.
-        Object control = getPage().getModel().get(getName());
-
-        // If not registered, then register control
-        if (control == null) {
-            getPage().addControl(this);
-
-        } else if (!(control instanceof AutoCompleteTextField)) {
-            String message =
-                "Non AutoCompleteTextField object '"
-                + control.getClass().toString()
-                + "' already registered in Page as: "
-                + getName();
-            throw new IllegalStateException(message);
-        }
-    }
-
-    /**
      * Process the page request and if an auto completion POST request then
      * render an list of suggested values.
      *
@@ -293,18 +296,16 @@
      */
     public boolean onProcess() {
         Context context = getContext();
-        if (context.isPost()) {
-            // If an auto complete POST request then render suggested list,
-            // otherwise continue as normal
-            if (getForm().isFormSubmission()) {
-                return super.onProcess();
-            } else if (context.isAjaxRequest()) {
-                String criteria = context.getRequestParameter(getName());
-                if (criteria != null) {
-                    List autoCompleteList = getAutoCompleteList(criteria);
-                    renderAutoCompleteList(autoCompleteList);
-                    return false;
-                }
+        // If an auto complete POST request, render suggestion list,
+        // otherwise continue as normal
+        if (getForm().isFormSubmission()) {
+            return super.onProcess();
+        } else {
+            String criteria = context.getRequestParameter(getName());
+            if (criteria != null) {
+                List autoCompleteList = getAutoCompleteList(criteria);
+                renderAutoCompleteList(autoCompleteList);
+                return false;
             }
         }
         return true;

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/ActionButton.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/ActionButton.java?rev=759486&r1=759485&r2=759486&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/ActionButton.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/ActionButton.java Sat Mar 28 15:47:00 2009
@@ -490,10 +490,12 @@
      * @return true to continue Page event processing or false otherwise
      */
     public boolean onProcess() {
-        bindRequestValue();
+        if (canProcess()) {
+            bindRequestValue();
 
-        if (isClicked()) {
-            registerActionEvent();
+            if (isClicked()) {
+                registerActionEvent();
+            }
         }
         return true;
     }

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/Field.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/Field.java?rev=759486&r1=759485&r2=759486&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/Field.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/Field.java Sat Mar 28 15:47:00 2009
@@ -374,12 +374,11 @@
      * @return the parent Form containing the Field
      */
     public Form getForm() {
-        if (form != null) {
-            return form;
-        } else {
+        if (form == null) {
             // Find form in parent hierarchy
-            return ContainerUtils.findForm(this);
+            form = ContainerUtils.findForm(this);
         }
+        return form;
     }
 
     /**
@@ -896,14 +895,15 @@
      * @return true to continue Page event processing or false otherwise
      */
     public boolean onProcess() {
-        bindRequestValue();
-
-        if (getValidate()) {
-            validate();
-        }
+        if (canProcess()) {
+            bindRequestValue();
 
-        registerActionEvent();
+            if (getValidate()) {
+                validate();
+            }
 
+            registerActionEvent();
+        }
         return true;
     }
 
@@ -921,6 +921,40 @@
     // ------------------------------------------------------ Protected Methods
 
     /**
+     * Returns true if the Field's onProcess event handler should be invoked,
+     * false otherwise.
+     * <p/>
+     * By default this method will return true if:
+     * <ul>
+     * <li>the Field's Form is submitted. This method invokes
+     * {@link Form#isFormSubmission()} to check if the Form is submitted or not
+     * </li>
+     * <li>the request is an Ajax request. This method invoked
+     * {@link org.apache.click.Context#isAjaxRequest()} to check if the request
+     * is an Ajax request or not
+     * </li>
+     * </ul>
+     *
+     * @return true if the Field's onProcess event handler should be invoked,
+     * false otherwise
+     */
+    protected boolean canProcess() {
+        // An Ajax request forces the Field to process itself. Otherwise, if
+        // the Form is not submitted, an Ajax request targeting this field won't
+        // be processed.
+        if (getContext().isAjaxRequest()) {
+            return true;
+        }
+
+        Form form = getForm();
+        if (form != null) {
+            return form.isFormSubmission();
+        } else {
+            return true;
+        }
+    }
+
+    /**
      * Return a normalized label for display in error messages.
      * <p/>
      * The error label is a normalized version of {@link #getLabel()}.

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/FieldSet.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/FieldSet.java?rev=759486&r1=759485&r2=759486&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/FieldSet.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/FieldSet.java Sat Mar 28 15:47:00 2009
@@ -746,14 +746,17 @@
      * returned false
      */
     public boolean onProcess() {
-        if (hasControls()) {
-            for (Iterator it = getControls().iterator(); it.hasNext();) {
-                Control control = (Control) it.next();
-                String controlName = control.getName();
-                if (controlName == null || !controlName.startsWith(Form.SUBMIT_CHECK)) {
-                    boolean continueProcessing = control.onProcess();
-                    if (!continueProcessing) {
-                        return false;
+        if (canProcess()) {
+            if (hasControls()) {
+                for (Iterator it = getControls().iterator(); it.hasNext();) {
+                    Control control = (Control) it.next();
+                    String controlName = control.getName();
+                    if (controlName == null || !controlName.startsWith(
+                        Form.SUBMIT_CHECK)) {
+                        boolean continueProcessing = control.onProcess();
+                        if (!continueProcessing) {
+                            return false;
+                        }
                     }
                 }
             }

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java?rev=759486&r1=759485&r2=759486&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java Sat Mar 28 15:47:00 2009
@@ -431,6 +431,7 @@
  *  </li>
  * </ul>
  *
+ * <a name="post-redirect"></a>
  * <h3>Preventing Accidental Form Posts</h3>
  *
  * Users may accidentally make multiple form submissions by refreshing a page
@@ -625,6 +626,9 @@
     /** The label &lt;td&gt; "style" attribute value. */
     protected String labelStyle;
 
+    /** The form submission flag. */
+    protected Boolean formSubmission;
+
     // ----------------------------------------------------------- Constructors
 
     /**
@@ -1124,14 +1128,20 @@
      * @return true if the page request is a submission from this form
      */
     public boolean isFormSubmission() {
+        if (formSubmission != null) {
+            return formSubmission.booleanValue();
+        }
+
         Context context = getContext();
         String requestMethod = context.getRequest().getMethod();
 
-        if (!getMethod().equalsIgnoreCase(requestMethod)) {
-            return false;
+        if (getMethod().equalsIgnoreCase(requestMethod)
+            && getName().equals(context.getRequestParameter(FORM_NAME))) {
+            formSubmission = Boolean.TRUE;
+        } else {
+            formSubmission = Boolean.FALSE;
         }
-
-        return getName().equals(context.getRequestParameter(FORM_NAME));
+        return formSubmission.booleanValue();
     }
 
     /**
@@ -1740,19 +1750,20 @@
         }
 
         boolean continueProcessing = true;
-        if (isFormSubmission()) {
 
-            for (int i = 0, size = getControls().size(); i < size; i++) {
-                Control control = (Control) getControls().get(i);
-                String controlName = control.getName();
-                if (controlName == null || !controlName.startsWith(Form.SUBMIT_CHECK)) {
+        for (int i = 0, size = getControls().size(); i < size; i++) {
+            Control control = (Control) getControls().get(i);
+            String controlName = control.getName();
+            if (controlName == null ||
+                !controlName.startsWith(Form.SUBMIT_CHECK)) {
 
-                    if (!control.onProcess()) {
-                        continueProcessing = false;
-                    }
+                if (!control.onProcess()) {
+                    continueProcessing = false;
                 }
             }
+        }
 
+        if (isFormSubmission()) {
             registerActionEvent();
         }
 
@@ -1768,6 +1779,7 @@
     public void onDestroy() {
         super.onDestroy();
 
+        formSubmission = null;
         setError(null);
     }
 

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/Radio.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/Radio.java?rev=759486&r1=759485&r2=759486&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/Radio.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/Radio.java Sat Mar 28 15:47:00 2009
@@ -313,12 +313,13 @@
      * @return true to continue Page event processing or false otherwise
      */
     public boolean onProcess() {
-        bindRequestValue();
+        if (canProcess()) {
+            bindRequestValue();
 
-        if (isChecked()) {
-            registerActionEvent();
+            if (isChecked()) {
+                registerActionEvent();
+            }
         }
-
         return true;
     }
 

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/RadioGroup.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/RadioGroup.java?rev=759486&r1=759485&r2=759486&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/RadioGroup.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/RadioGroup.java Sat Mar 28 15:47:00 2009
@@ -422,22 +422,24 @@
      * @return true to continue Page event processing or false otherwise
      */
     public boolean onProcess() {
-        bindRequestValue();
-
         boolean continueProcessing = true;
-        for (int i = 0, size = getRadioList().size(); i < size; i++) {
-            Radio radio = (Radio) getRadioList().get(i);
-            if (!radio.onProcess()) {
-                continueProcessing = false;
-            }
-        }
 
-        if (getValidate()) {
-            validate();
-        }
+        if (canProcess()) {
+            bindRequestValue();
 
-        registerActionEvent();
+            for (int i = 0, size = getRadioList().size(); i < size; i++) {
+                Radio radio = (Radio) getRadioList().get(i);
+                if (!radio.onProcess()) {
+                    continueProcessing = false;
+                }
+            }
 
+            if (getValidate()) {
+                validate();
+            }
+
+            registerActionEvent();
+        }
         return continueProcessing;
     }
 

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/Submit.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/Submit.java?rev=759486&r1=759485&r2=759486&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/Submit.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/Submit.java Sat Mar 28 15:47:00 2009
@@ -202,10 +202,12 @@
      * @return true to continue Page event processing or false otherwise
      */
     public boolean onProcess() {
-        bindRequestValue();
+        if (canProcess()) {
+            bindRequestValue();
 
-        if (isClicked()) {
-            registerActionEvent();
+            if (isClicked()) {
+                registerActionEvent();
+            }
         }
 
         return true;