cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sylv...@apache.org
Subject svn commit: r327146 - in /cocoon/blocks/forms/trunk: ./ java/org/apache/cocoon/forms/binding/ java/org/apache/cocoon/forms/event/impl/ java/org/apache/cocoon/forms/validation/impl/ samples/forms/
Date Fri, 21 Oct 2005 10:38:51 GMT
Author: sylvain
Date: Fri Oct 21 03:38:37 2005
New Revision: 327146

URL: http://svn.apache.org/viewcvs?rev=327146&view=rev
Log:
Ensure all JavaScript snippets have the "this" and "widget" variables set to the current widget

Modified:
    cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/binding/JavaScriptJXPathBinding.java
    cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/event/impl/JavaScriptWidgetListener.java
    cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/event/impl/JavaScriptWidgetListenerBuilder.java
    cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/validation/impl/JavaScriptValidator.java
    cocoon/blocks/forms/trunk/samples/forms/carselector_form.xml
    cocoon/blocks/forms/trunk/status.xml

Modified: cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/binding/JavaScriptJXPathBinding.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/binding/JavaScriptJXPathBinding.java?rev=327146&r1=327145&r2=327146&view=diff
==============================================================================
--- cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/binding/JavaScriptJXPathBinding.java
(original)
+++ cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/binding/JavaScriptJXPathBinding.java
Fri Oct 21 03:38:37 2005
@@ -86,7 +86,7 @@
                 JXPathContext newCtx = pointer.getNode() == null ? null :
                 	    jctx.getRelativeContext(pointer);
 
-                JavaScriptHelper.callFunction(this.loadScript, frmModel,
+                JavaScriptHelper.callFunction(this.loadScript, widget,
                 		new Object[] {widget, pointer, newCtx, this.childBindings}, objectModel);
     
             } catch(RuntimeException re) {
@@ -112,7 +112,7 @@
             try {
                 Map objectModel = ContextHelper.getObjectModel(this.avalonContext);
 
-                JavaScriptHelper.callFunction(this.saveScript, frmModel,
+                JavaScriptHelper.callFunction(this.saveScript, widget,
                 		new Object[] {widget, pointer, widgetCtx, this.childBindings}, objectModel);
 
             } catch(RuntimeException re) {

Modified: cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/event/impl/JavaScriptWidgetListener.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/event/impl/JavaScriptWidgetListener.java?rev=327146&r1=327145&r2=327146&view=diff
==============================================================================
--- cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/event/impl/JavaScriptWidgetListener.java
(original)
+++ cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/event/impl/JavaScriptWidgetListener.java
Fri Oct 21 03:38:37 2005
@@ -29,10 +29,11 @@
 import org.apache.cocoon.forms.event.ValueChangedEvent;
 import org.apache.cocoon.forms.event.ValueChangedListener;
 import org.apache.cocoon.forms.event.WidgetEvent;
+import org.apache.cocoon.forms.formmodel.Widget;
 import org.apache.cocoon.forms.formmodel.tree.TreeSelectionEvent;
 import org.apache.cocoon.forms.formmodel.tree.TreeSelectionListener;
 import org.apache.cocoon.forms.util.JavaScriptHelper;
-import org.mozilla.javascript.Script;
+import org.mozilla.javascript.Function;
 
 /**
  * Listeners built by {@link org.apache.cocoon.forms.event.impl.JavaScriptWidgetListenerBuilder}
@@ -42,11 +43,11 @@
  */
 public abstract class JavaScriptWidgetListener {
     
-    private Script script;
+    private Function func;
     private Context context;
 
-    public JavaScriptWidgetListener(Script script, Context context) {
-        this.script = script;
+    public JavaScriptWidgetListener(Function func, Context context) {
+        this.func = func;
         this.context = context;
     }
     
@@ -69,7 +70,9 @@
                 values.put("viewData", viewData);
             }
             
-            JavaScriptHelper.execScript(this.script, values, objectModel);
+            Widget w = event.getSourceWidget();
+            
+            JavaScriptHelper.callFunction(this.func, w, new Object[]{w, event}, objectModel);
             
         } catch(RuntimeException re) {
             // rethrow
@@ -81,8 +84,8 @@
     
     public static class JSActionListener extends JavaScriptWidgetListener implements ActionListener
{
 
-        public JSActionListener(Script script, Context context) {
-            super(script, context);
+        public JSActionListener(Function func, Context context) {
+            super(func, context);
         }
 
         public void actionPerformed(ActionEvent event) {
@@ -92,8 +95,8 @@
     
     public static class JSValueChangedListener extends JavaScriptWidgetListener implements
ValueChangedListener {
 
-        public JSValueChangedListener(Script script, Context context) {
-            super(script, context);
+        public JSValueChangedListener(Function func, Context context) {
+            super(func, context);
         }
 
         public void valueChanged(ValueChangedEvent event) {
@@ -103,8 +106,8 @@
     
     public static class JSCreateListener extends JavaScriptWidgetListener implements CreateListener
{
 
-        public JSCreateListener(Script script, Context context) {
-            super(script, context);
+        public JSCreateListener(Function func, Context context) {
+            super(func, context);
         }
 
         public void widgetCreated(CreateEvent event) {
@@ -114,8 +117,8 @@
     
     public static class JSTreeSelectionListener extends JavaScriptWidgetListener implements
TreeSelectionListener {
 
-        public JSTreeSelectionListener(Script script, Context context) {
-            super(script, context);
+        public JSTreeSelectionListener(Function func, Context context) {
+            super(func, context);
         }
 
         public void selectionChanged(TreeSelectionEvent event) {

Modified: cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/event/impl/JavaScriptWidgetListenerBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/event/impl/JavaScriptWidgetListenerBuilder.java?rev=327146&r1=327145&r2=327146&view=diff
==============================================================================
--- cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/event/impl/JavaScriptWidgetListenerBuilder.java
(original)
+++ cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/event/impl/JavaScriptWidgetListenerBuilder.java
Fri Oct 21 03:38:37 2005
@@ -15,6 +15,10 @@
  */
 package org.apache.cocoon.forms.event.impl;
 
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.thread.ThreadSafe;
 import org.apache.cocoon.forms.event.ActionListener;
 import org.apache.cocoon.forms.event.CreateListener;
 import org.apache.cocoon.forms.event.ValueChangedListener;
@@ -22,11 +26,7 @@
 import org.apache.cocoon.forms.event.WidgetListenerBuilder;
 import org.apache.cocoon.forms.formmodel.tree.TreeSelectionListener;
 import org.apache.cocoon.forms.util.JavaScriptHelper;
-import org.apache.avalon.framework.thread.ThreadSafe;
-import org.apache.avalon.framework.context.Contextualizable;
-import org.apache.avalon.framework.context.Context;
-import org.apache.avalon.framework.context.ContextException;
-import org.mozilla.javascript.Script;
+import org.mozilla.javascript.Function;
 import org.w3c.dom.Element;
 
 /**
@@ -55,16 +55,16 @@
 
     public WidgetListener buildListener(Element element, Class listenerClass) throws Exception
{
 
-        Script script = JavaScriptHelper.buildScript(element);
+        Function func = JavaScriptHelper.buildFunction(element, "handleEvent", new String[]{"widget",
"event"});
 
         if (listenerClass == ActionListener.class) {
-            return new JavaScriptWidgetListener.JSActionListener(script, context);
+            return new JavaScriptWidgetListener.JSActionListener(func, context);
         } else if (listenerClass == CreateListener.class) {
-            return new JavaScriptWidgetListener.JSCreateListener(script, context);
+            return new JavaScriptWidgetListener.JSCreateListener(func, context);
         } else if (listenerClass == ValueChangedListener.class) {
-            return new JavaScriptWidgetListener.JSValueChangedListener(script, context);
+            return new JavaScriptWidgetListener.JSValueChangedListener(func, context);
         } else if (listenerClass == TreeSelectionListener.class) {
-            return new JavaScriptWidgetListener.JSTreeSelectionListener(script, context);
+            return new JavaScriptWidgetListener.JSTreeSelectionListener(func, context);
         } else {
             throw new Exception("Unkonwn event class: " + listenerClass);
         }

Modified: cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/validation/impl/JavaScriptValidator.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/validation/impl/JavaScriptValidator.java?rev=327146&r1=327145&r2=327146&view=diff
==============================================================================
--- cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/validation/impl/JavaScriptValidator.java
(original)
+++ cocoon/blocks/forms/trunk/java/org/apache/cocoon/forms/validation/impl/JavaScriptValidator.java
Fri Oct 21 03:38:37 2005
@@ -22,16 +22,31 @@
 import org.apache.cocoon.components.ContextHelper;
 import org.apache.cocoon.forms.formmodel.Widget;
 import org.apache.cocoon.forms.util.JavaScriptHelper;
+import org.apache.cocoon.forms.validation.ValidationError;
+import org.apache.cocoon.forms.validation.ValidationErrorAware;
 import org.apache.cocoon.forms.validation.WidgetValidator;
+import org.apache.excalibur.xml.sax.XMLizable;
 import org.mozilla.javascript.Function;
 
 /**
  * A {@link org.apache.cocoon.forms.validation.WidgetValidator} implemented as a JavaScript
snippet.
- * This snippet must return a boolean value. If it returns <code>false</code>,
it <strong>must</strong> have
- * set a validation error on the validated widget or one of its children.
  * <p>
- * The JavaScript snippet has the "this" variable set to the validated widget, and, if the
form is used in a
- * flowscript, can use the flow's global values and fonctions and the <code>cocoon</code>
object.
+ * This snippet must return a value which can be of different types. The only way to indicate
+ * successfull validation is to return a boolean <code>true</code> value.
+ * <p>
+ * To indicate validation error, a number of result types are possible:
+ * <ul>
+ * <li>A boolean <code>false</code>: the validator <strong>must</strong>
then have
+ *     set a validation error on the validated widget or one of its children.</li>
+ * <li>A {@link ValidationError}: this error is then set on the validated widget.</li>
+ * <li>A <code>String</code>: a validation error using that string as a
non-i18nized message is
+ *     then set on the validated widget</li>
+ * <li>An <code>XMLizable</code> such as {@link org.apache.cocoon.forms.util.I18nMessage}:
this
+ *     xmlizable is used to build a validation error that is set on the validated widget.</li>
+ * </ul>
+ * <p>
+ * The JavaScript snippet has the "this" and "widget" variables set to the validated widget,
and, if the form
+ * is used in a flowscript, can use the flow's global values and fonctions and the <code>cocoon</code>
object.
  * 
  * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
  * @version $Id$
@@ -62,12 +77,29 @@
 
         if (result == null) {
             throw new RuntimeException("Validation script did not return a value");
-            
-        } else if (result instanceof Boolean) {
+        }
+
+        if (result instanceof Boolean) {
             return ((Boolean)result).booleanValue();
-            
-        } else {
-            throw new RuntimeException("Validation script returned an unexpected value of
type " + result.getClass());
         }
+        
+        if (result instanceof ValidationError) {
+            // Set the validation error on the widget
+            ((ValidationErrorAware)widget).setValidationError((ValidationError)result);
+        }
+
+        if (result instanceof String) {
+            // Set a non-i18n error on the current widget
+            ((ValidationErrorAware)widget).setValidationError(new ValidationError((String)result,
false));
+            return false;
+        }
+
+        if (result instanceof XMLizable) {
+            // Set a xmlizable error (e.g. I18nMessage) on the current widget
+            ((ValidationErrorAware)widget).setValidationError(new ValidationError((XMLizable)result));
+            return false;
+        }
+
+        throw new RuntimeException("Validation script returned an unexpected value of type
" + result.getClass());
     }
 }

Modified: cocoon/blocks/forms/trunk/samples/forms/carselector_form.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/forms/trunk/samples/forms/carselector_form.xml?rev=327146&r1=327145&r2=327146&view=diff
==============================================================================
--- cocoon/blocks/forms/trunk/samples/forms/carselector_form.xml (original)
+++ cocoon/blocks/forms/trunk/samples/forms/carselector_form.xml Fri Oct 21 03:38:37 2005
@@ -27,24 +27,27 @@
       <fd:selection-list src="cocoon:/cars" dynamic="true"/>
       <fd:on-value-changed>
         <javascript>
+	  // The following variables are available in event listeners:
+	  // - "widget" and "this" are set to the current widget
+	  // - "event" is the event that was raised
           java.lang.System.err.println("maker changed from " + event.oldValue + " to " +
event.newValue);
-          var value = event.source.value;
-          var typewidget = event.source.lookupWidget("../type");
+          var value = this.value;
+          var typewidget = widget.lookupWidget("../type");
           if (value != null) {
             // Get the corresponding type list
             typewidget.setSelectionList("cocoon:/cars/" + value);
-            typewidget.setState(Packages.org.apache.cocoon.forms.formmodel.WidgetState.ACTIVE);
+            typewidget.state = Packages.org.apache.cocoon.forms.formmodel.WidgetState.ACTIVE;
           } else {
             // Set an empty selection list
             typewidget.setSelectionList(new Packages.org.apache.cocoon.forms.datatype.EmptySelectionList("Select
a maker first"));
-            typewidget.setState(Packages.org.apache.cocoon.forms.formmodel.WidgetState.DISABLED);
+            typewidget.state = Packages.org.apache.cocoon.forms.formmodel.WidgetState.DISABLED;
           }
           // Always set the type value to null. Note that it will also fire an event on the
"type"
           // widget if it already had a value.
-          typewidget.setValue(null);
+          typewidget.value = null;
           
           // Fun with messages...
-          var msg = event.source.lookupWidget("../message");
+          var msg = this.lookupWidget("../message");
           if (value == null) {
             msg.value = "Yep. Choosing a maker is not that easy...";
           } else {
@@ -68,10 +71,10 @@
       </fd:selection-list>
       <fd:on-value-changed>
         <javascript>
-          java.lang.System.err.println("type changed to " + event.source.value);
-          var value = event.source.value;
-          var modelwidget = event.source.lookupWidget("../model");
-          var makewidget = event.source.lookupWidget("../make");
+          java.lang.System.err.println("type changed to " + event.newValue);
+          var value = event.newValue;
+          var modelwidget = this.lookupWidget("../model");
+          var makewidget = this.lookupWidget("../make");
           if (value != null) {
             modelwidget.setSelectionList("cocoon:/cars/" + makewidget.value + "/" + value);
             modelwidget.setState(Packages.org.apache.cocoon.forms.formmodel.WidgetState.ACTIVE);
@@ -83,10 +86,10 @@
           
           // Always set the model value to null. Note that it will also fire an event on
the "model"
           // widget if it already had a value.
-          modelwidget.setValue(null);
+          modelwidget.value = null;
           
           // Fun with messages...
-          var msg = event.source.lookupWidget("../message");
+          var msg = this.lookupWidget("../message");
           if (value != null) {
             if (event.oldValue == null) {
               msg.value = "A " + makewidget.value + " " + value + " is a very good choice.";
@@ -110,12 +113,14 @@
       </fd:selection-list>
       <fd:on-value-changed>
         <javascript>
-          var value = event.source.value;
+          var value = this.value;
           if (value != null) {
-            event.source.lookupWidget("../message").setValue("Model " + value + " is a great
car!");
+            this.lookupWidget("../message").value = "Model " + value + " is a great car!";
+	    this.lookupWidget("../buy").state = Packages.org.apache.cocoon.forms.formmodel.WidgetState.ACTIVE;
           } else {
             // Reset value
-            event.source.value = null;
+            this.value = null;
+	    this.lookupWidget("../buy").state = Packages.org.apache.cocoon.forms.formmodel.WidgetState.DISABLED;
           }
         </javascript>
       </fd:on-value-changed>
@@ -125,7 +130,8 @@
       <fd:datatype base="string"/>
     </fd:output>
 
-    <fd:submit id="buy">
+    <!-- submit button, initially disabled as no model has been chosen -->
+    <fd:submit id="buy" state="disabled">
       <fd:label>Buy it!</fd:label>
     </fd:submit>
   

Modified: cocoon/blocks/forms/trunk/status.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/forms/trunk/status.xml?rev=327146&r1=327145&r2=327146&view=diff
==============================================================================
--- cocoon/blocks/forms/trunk/status.xml (original)
+++ cocoon/blocks/forms/trunk/status.xml Fri Oct 21 03:38:37 2005
@@ -65,6 +65,11 @@
  <!-- The following list contains the changes since the latest 2.1.x version. -->
  <changes>
   <release version="@version@" date="@date@">
+    <action dev="none">
+      The Cocoon Forms code base is shared between the 2.1 and 2.2 branches. Until this
+      changes or 2.1 also adopts a per-block status file, all changes must be reported
+      in the main status.xml file.
+    </action>
   </release>
  </changes>
 



Mime
View raw message