click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r919964 - in /click/trunk/click/examples: src/org/apache/click/examples/page/control/ src/org/apache/click/examples/page/form/dynamic/ webapp/WEB-INF/ webapp/control/ webapp/form/dynamic/
Date Sun, 07 Mar 2010 11:05:18 GMT
Author: sabob
Date: Sun Mar  7 11:05:17 2010
New Revision: 919964

URL: http://svn.apache.org/viewvc?rev=919964&view=rev
Log:
added dynamic form demos

Added:
    click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/
    click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicFieldSet.java
    click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicForm.java
    click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicSelect.java
    click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/PopulateOnSelect.java
      - copied, changed from r918936, click/trunk/click/examples/src/org/apache/click/examples/page/control/PopulateOnSelect.java
    click/trunk/click/examples/webapp/form/dynamic/
    click/trunk/click/examples/webapp/form/dynamic/dynamic-field-set.htm
    click/trunk/click/examples/webapp/form/dynamic/dynamic-form.htm
    click/trunk/click/examples/webapp/form/dynamic/dynamic-select.htm
    click/trunk/click/examples/webapp/form/dynamic/populate-on-select.htm
      - copied unchanged from r906895, click/trunk/click/examples/webapp/control/populate-on-select.htm
    click/trunk/click/examples/webapp/form/dynamic/populate-on-select.js
      - copied, changed from r906895, click/trunk/click/examples/webapp/control/populate-on-select.js
Removed:
    click/trunk/click/examples/src/org/apache/click/examples/page/control/PopulateOnSelect.java
    click/trunk/click/examples/webapp/control/populate-on-select.htm
    click/trunk/click/examples/webapp/control/populate-on-select.js
Modified:
    click/trunk/click/examples/webapp/WEB-INF/menu.xml

Added: click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicFieldSet.java
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicFieldSet.java?rev=919964&view=auto
==============================================================================
--- click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicFieldSet.java
(added)
+++ click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicFieldSet.java
Sun Mar  7 11:05:17 2010
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.examples.page.form.dynamic;
+
+import org.apache.click.ActionListener;
+import org.apache.click.Control;
+import org.apache.click.control.Checkbox;
+import org.apache.click.control.FieldSet;
+import org.apache.click.control.Form;
+import org.apache.click.control.Submit;
+import org.apache.click.control.TextField;
+import org.apache.click.examples.page.BorderPage;
+import org.apache.click.extras.control.EmailField;
+import org.apache.click.extras.control.IntegerField;
+import org.apache.click.util.ClickUtils;
+
+/**
+ * Demonstrates dynamic Form behavior.
+ */
+public class DynamicFieldSet extends BorderPage {
+
+    private Form form = new Form("dynamicForm");
+
+    private FieldSet customerFS = new FieldSet("customer");
+
+    private FieldSet addressFS = new FieldSet("address");
+
+    private TextField nameField = new TextField("name", true);
+
+    private IntegerField ageField = new IntegerField("age");
+
+    private Checkbox addressChk = new Checkbox("address");
+
+    private EmailField emailField = new EmailField("email", true);
+
+    private Submit submit = new Submit("ok");
+
+    @Override
+    public void onInit() {
+        super.onInit();
+
+        form.add(customerFS);
+        nameField.setLabelStyleClass("label-style");
+        nameField.setParentStyleClass("parent-style");
+        nameField.setLabelStyle("color: blue; font-weight: bold");
+        nameField.setParentStyle("padding-bottom: 20px");
+        customerFS.add(nameField);
+        customerFS.add(ageField);
+        customerFS.add(addressChk);
+
+        // The Click script, '/click/control.js', provides the JavaScript
+        // function Click.submit(formName, validate). To bypass validation
+        // specify 'false' as the second argument.
+        addressChk.setAttribute("onclick", "Click.submit(dynamicForm, false)");
+
+        form.add(submit);
+
+        addControl(form);
+
+        // NB: Bind and validate form fields *before* the onProcess event,
+        // enabling us to inspect Field values in the onInit event or even
+        // the Page constructor
+        boolean valid = ClickUtils.bindAndValidate(form);
+
+        // We can safely check whether the user checked the addressChk
+        if (addressChk.isChecked()) {
+            // Dynamically add a new Field and FieldSet to the form
+            addressFS.add(emailField);
+            form.add(addressFS);
+        }
+
+        form.setActionListener(new ActionListener() {
+            public boolean onAction(Control source) {
+                return onFormSubmit();
+            }
+        });
+    }
+
+    public boolean onFormSubmit() {
+        // onFormSubmit listens on Form itself and will be invoked whenever the
+        // form is submitted.
+        if (form.isValid()) {
+
+            // Check isBypassValidation() flag whether the form validation occurred
+            if (form.isBypassValidation()) {
+                addModel("msg", "Validation bypassed");
+            } else {
+                addModel("msg", "Form is valid after validation");
+            }
+        }
+        return true;
+    }
+}

Added: click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicForm.java
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicForm.java?rev=919964&view=auto
==============================================================================
--- click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicForm.java
(added)
+++ click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicForm.java
Sun Mar  7 11:05:17 2010
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.examples.page.form.dynamic;
+
+import org.apache.click.ActionListener;
+import org.apache.click.Control;
+import org.apache.click.control.Checkbox;
+import org.apache.click.control.Form;
+import org.apache.click.control.Submit;
+import org.apache.click.control.TextField;
+import org.apache.click.examples.page.BorderPage;
+import org.apache.click.extras.control.EmailField;
+import org.apache.click.util.ClickUtils;
+
+/**
+ * Demonstrates dynamic Form behavior.
+ */
+public class DynamicForm extends BorderPage {
+
+    private Form form = new Form("dynamicForm");
+
+    private TextField nameField = new TextField("name", true);
+
+    private Checkbox checkbox = new Checkbox("subscribe");
+
+    private Submit submit = new Submit("ok");
+
+    @Override
+    public void onInit() {
+        super.onInit();
+
+        form.add(nameField);
+        form.add(checkbox);
+
+        // The Click script, '/click/control.js', provides the JavaScript
+        // function Click.submit(formName, validate). To bypass validation
+        // specify 'false' as the second argument.
+        checkbox.setAttribute("onclick", "Click.submit(dynamicForm, false)");
+
+        form.add(submit);
+
+        addControl(form);
+
+        // NB: Bind the checkbox *before* the onProcess event, enabling us to
+        // inspect the checkbox value inside the onInit event, or even the
+        // page constructor
+        ClickUtils.bind(checkbox);
+
+        // We could also bind *all* form fields in one go
+        // ClickUtils.bind(form);
+
+        // We can safely check whether the user checked the checkbox
+        if (checkbox.isChecked()) {
+            // Dynamically add the email Field to the form
+            form.add(new EmailField("email", true));
+        }
+
+        form.setActionListener(new ActionListener() {
+            public boolean onAction(Control source) {
+                return onFormSubmit();
+            }
+        });
+    }
+
+    public boolean onFormSubmit() {
+        // onFormSubmit listens on Form itself and will be invoked whenever the
+        // form is submitted.
+        if (form.isValid()) {
+
+            // Check isBypassValidation() flag whether the form validation occurred
+            if (form.isBypassValidation()) {
+                addModel("msg", "Validation was bypassed");
+            } else {
+                addModel("msg", "Form is valid after validation");
+            }
+        }
+        return true;
+    }
+}

Added: click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicSelect.java
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicSelect.java?rev=919964&view=auto
==============================================================================
--- click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicSelect.java
(added)
+++ click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicSelect.java
Sun Mar  7 11:05:17 2010
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.examples.page.form.dynamic;
+
+import org.apache.click.ActionListener;
+import org.apache.click.Control;
+import org.apache.click.control.FieldSet;
+import org.apache.click.control.Form;
+import org.apache.click.control.Option;
+import org.apache.click.control.Select;
+import org.apache.click.control.Submit;
+import org.apache.click.control.TextField;
+import org.apache.click.examples.page.BorderPage;
+import org.apache.click.extras.control.DoubleField;
+import org.apache.click.util.ClickUtils;
+
+/**
+ * Demonstrates dynamic Form behavior using a Select control.
+ */
+public class DynamicSelect extends BorderPage {
+
+    private Form form = new Form("dynamicForm");
+
+    private TextField nameField = new TextField("name", true);
+
+    private Select select = new Select("investments", true);
+
+    private Submit submit = new Submit("ok");
+
+    // Investment options
+    private static final String PROPERTY = "Property";
+    private static final String STOCKS = "Stocks";
+    private static final String[] INVESTMENTS = {PROPERTY, STOCKS};
+
+    public DynamicSelect() {
+
+        form.setActionListener(new ActionListener() {
+
+            public boolean onAction(Control source) {
+                return onFormSubmit();
+            }
+        });
+
+        form.add(nameField);
+
+        select.add(Option.EMPTY_OPTION);
+        select.addAll(INVESTMENTS);
+        form.add(select);
+
+        // The Click script, '/click/control.js', provides the JavaScript
+        // function Click.submit(formName, validate). To bypass validation
+        // specify 'false' as the second argument.
+        select.setAttribute("onchange", "Click.submit(dynamicForm, false)");
+
+        form.add(submit);
+
+        addControl(form);
+
+        // Bind the form field request values
+        ClickUtils.bind(form);
+
+        if (STOCKS.equals(select.getValue())) {
+            form.add(new DoubleField("amount", true));
+        } else if (PROPERTY.equals(select.getValue())) {
+            FieldSet address = new FieldSet("address");
+            address.add(new TextField("street", true));
+            address.add(new DoubleField("amount", true));
+            form.add(address);
+        }
+    }
+
+    public boolean onFormSubmit() {
+        // onFormSubmit listens on Form itself and will be invoked whenever the
+        // form is submitted.
+        if (form.isValid()) {
+
+            // Check isBypassValidation() flag whether the form validation occurred
+            if (form.isBypassValidation()) {
+                addModel("msg", "Validation bypassed");
+            } else {
+                addModel("msg", "Form is valid after validation");
+            }
+        }
+        return true;
+    }
+}

Copied: click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/PopulateOnSelect.java
(from r918936, click/trunk/click/examples/src/org/apache/click/examples/page/control/PopulateOnSelect.java)
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/PopulateOnSelect.java?p2=click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/PopulateOnSelect.java&p1=click/trunk/click/examples/src/org/apache/click/examples/page/control/PopulateOnSelect.java&r1=918936&r2=919964&rev=919964&view=diff
==============================================================================
--- click/trunk/click/examples/src/org/apache/click/examples/page/control/PopulateOnSelect.java
(original)
+++ click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/PopulateOnSelect.java
Sun Mar  7 11:05:17 2010
@@ -16,18 +16,25 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.click.examples.page.control;
+package org.apache.click.examples.page.form.dynamic;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.click.control.FieldSet;
 import org.apache.click.control.Select;
+import org.apache.click.control.Submit;
+import org.apache.click.element.JsScript;
 import org.apache.click.examples.page.BorderPage;
 import org.apache.click.extras.control.TabbedForm;
 import org.apache.click.util.Bindable;
+import org.apache.click.util.ClickUtils;
 import org.apache.commons.lang.StringUtils;
 
+/**
+ * Demonstrate how to populate Select controls.
+ */
 public class PopulateOnSelect extends BorderPage {
 
     private static final String EASTERN_CAPE = "EC";
@@ -37,10 +44,12 @@
 
     @Bindable protected TabbedForm form = new TabbedForm("form");
 
-    private Select state = new Select("state");
-    private Select city = new Select("city");
-    private Select suburb = new Select("suburb");
+    private Select state = new Select("state", true);
+    private Select city = new Select("city", true);
+    private Select suburb = new Select("suburb", true);
+    private Submit save = new Submit("save");
 
+    @Override
     public void onInit() {
         super.onInit();
 
@@ -64,6 +73,8 @@
 
         // build the Selects in the onInit phase
         buildSelects();
+
+        form.add(save);
     }
 
     public void buildSelects() {
@@ -74,8 +85,8 @@
         // Populate the States. Do this before binding requests
         populateStateData();
 
-        // Bind the state to its request value
-        state.bindRequestValue();
+        // Bind the form field request values
+        ClickUtils.bind(form);
 
         if (StringUtils.isEmpty(state.getValue())) {
             // No state selected, exit early
@@ -85,9 +96,6 @@
         // If state is selected, proceed to populate city
         populateCityData(state.getValue());
 
-        // Bind the city to its request value
-        city.bindRequestValue();
-
         if (StringUtils.isEmpty(city.getValue())) {
             // No city selected, exit early
             return;
@@ -98,20 +106,21 @@
     }
 
     @Override
-    public String getHtmlImports() {
-        Map model = new HashMap();
-        model.put("stateId", state.getId());
-        model.put("cityId", city.getId());
-        model.put("suburbId", suburb.getId());
-
-        // populate-on-select.js is a Velocity template which is rendered directly
-        // from this Page
-        String templatePath = "/control/populate-on-select.js";
-        String template = getContext().renderTemplate(templatePath, model);
-
-        // Click's JavaScript and CSS import parser does not yet handle multiline
-        // imports so coerce it into a single line
-        return "<script type=\"text/javascript\">" + template.replace('\n', ' ') +
"</script>\n";
+    public List getHeadElements() {
+        if (headElements == null) {
+            headElements = super.getHeadElements();
+
+            Map model = new HashMap();
+            model.put("stateId", state.getId());
+            model.put("cityId", city.getId());
+            model.put("suburbId", suburb.getId());
+
+            // populate-on-select.js is a Velocity template which is rendered directly
+            // from this Page
+            JsScript script = new JsScript("/form/dynamic/populate-on-select.js", model);
+            headElements.add(script);
+        }
+        return headElements;
     }
 
     // -------------------------------------------------------- Private Methods

Modified: click/trunk/click/examples/webapp/WEB-INF/menu.xml
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/WEB-INF/menu.xml?rev=919964&r1=919963&r2=919964&view=diff
==============================================================================
--- click/trunk/click/examples/webapp/WEB-INF/menu.xml (original)
+++ click/trunk/click/examples/webapp/WEB-INF/menu.xml Sun Mar  7 11:05:17 2010
@@ -81,7 +81,6 @@
     <menu label="Link Controls" path="control/link-demo.htm"/>
     <menu label="SubmitLink Control" path="control/submit-link-demo.htm"/>
     <menu label="Select" path="control/select-demo.htm"/>
-    <menu label="Populate on select" path="control/populate-on-select.htm"/>
     <menu separator="true"/>
     <menu label="Menu" path="control/menu-demo.htm"/>
     <menu label="PickList" path="control/pick-list-demo.htm"/>
@@ -100,6 +99,11 @@
     <menu separator="true"/>
     <menu label="Tabbed Form" path="form/tabbed-form-demo.htm"/>
     <menu label="Custom Form Layout" path="form/contact-details.htm"/>
+    <menu separator="true"/>
+    <menu label="Dynamic Form" path="form/dynamic/dynamic-form.htm"/>
+    <menu label="Dynamic FieldSet" path="form/dynamic/dynamic-field-set.htm"/>
+    <menu label="Dynamic Select" path="form/dynamic/dynamic-select.htm"/>
+    <menu label="Populate on select" path="form/dynamic/populate-on-select.htm"/>
   </menu>
 
   <menu label=" Table" path="#" imageSrc="/assets/images/table.png">

Added: click/trunk/click/examples/webapp/form/dynamic/dynamic-field-set.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/form/dynamic/dynamic-field-set.htm?rev=919964&view=auto
==============================================================================
--- click/trunk/click/examples/webapp/form/dynamic/dynamic-field-set.htm (added)
+++ click/trunk/click/examples/webapp/form/dynamic/dynamic-field-set.htm Sun Mar  7 11:05:17
2010
@@ -0,0 +1,5 @@
+$dynamicForm
+
+#if ($msg)
+  <div id="msgDiv"> $msg </div>
+#end

Added: click/trunk/click/examples/webapp/form/dynamic/dynamic-form.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/form/dynamic/dynamic-form.htm?rev=919964&view=auto
==============================================================================
--- click/trunk/click/examples/webapp/form/dynamic/dynamic-form.htm (added)
+++ click/trunk/click/examples/webapp/form/dynamic/dynamic-form.htm Sun Mar  7 11:05:17 2010
@@ -0,0 +1,5 @@
+$dynamicForm
+
+#if ($msg)
+  <div id="msgDiv"> $msg </div>
+#end

Added: click/trunk/click/examples/webapp/form/dynamic/dynamic-select.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/form/dynamic/dynamic-select.htm?rev=919964&view=auto
==============================================================================
--- click/trunk/click/examples/webapp/form/dynamic/dynamic-select.htm (added)
+++ click/trunk/click/examples/webapp/form/dynamic/dynamic-select.htm Sun Mar  7 11:05:17
2010
@@ -0,0 +1,5 @@
+$dynamicForm
+
+#if ($msg)
+  <div id="msgDiv"> $msg </div>
+#end

Copied: click/trunk/click/examples/webapp/form/dynamic/populate-on-select.js (from r906895,
click/trunk/click/examples/webapp/control/populate-on-select.js)
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/form/dynamic/populate-on-select.js?p2=click/trunk/click/examples/webapp/form/dynamic/populate-on-select.js&p1=click/trunk/click/examples/webapp/control/populate-on-select.js&r1=906895&r2=919964&rev=919964&view=diff
==============================================================================
--- click/trunk/click/examples/webapp/control/populate-on-select.js (original)
+++ click/trunk/click/examples/webapp/form/dynamic/populate-on-select.js Sun Mar  7 11:05:17
2010
@@ -38,13 +38,15 @@
 /**
  * This method is invoked when a Select control is changed.
  *
- * @id specifies the Select control that must be reset
+ * @id specifies the dependent Select control which value to clear
  * @form specified the form that must be submitted
  */
 function handleChange(id, form) {
     var select=document.getElementById(id);
     if(select != null) {
-        select.selectedIndex=-1;
+        // Clear dependent Select value
+        select.selectedIndex=0;
     }
-    form.submit();
+    // Submit form and bypass server-side validation
+    Click.submit(form, false);
 }
\ No newline at end of file



Mime
View raw message