You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by co...@apache.org on 2004/02/11 00:35:32 UTC

cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel DataWidget.java SelectableWidget.java Field.java MultiValueField.java Output.java

coliver     2004/02/10 15:35:32

  Modified:    src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2
                        ScriptableWidget.java
               src/blocks/woody/java/org/apache/cocoon/woody/formmodel
                        Field.java MultiValueField.java Output.java
  Added:       src/blocks/woody/java/org/apache/cocoon/woody/formmodel
                        DataWidget.java SelectableWidget.java
  Log:
  Added interfaces DataWidget and SelectableWidget to unify getDatatype() and setSelectionList() methods
  
  Revision  Changes    Path
  1.8       +33 -57    cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2/ScriptableWidget.java
  
  Index: ScriptableWidget.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2/ScriptableWidget.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ScriptableWidget.java	6 Feb 2004 15:55:32 -0000	1.7
  +++ ScriptableWidget.java	10 Feb 2004 23:35:32 -0000	1.8
  @@ -60,8 +60,11 @@
   import org.apache.cocoon.woody.formmodel.Submit;
   import org.apache.cocoon.woody.formmodel.Upload;
   import org.apache.cocoon.woody.formmodel.Widget;
  +import org.apache.cocoon.woody.formmodel.DataWidget;
  +import org.apache.cocoon.woody.formmodel.SelectableWidget;
   import org.apache.cocoon.woody.datatype.Datatype;
  -import org.apache.cocoon.woody.datatype.ValidationError;
  +import org.apache.cocoon.woody.validation.ValidationError;
  +import org.apache.cocoon.woody.validation.ValidationErrorAware;
   import org.apache.cocoon.woody.datatype.SelectionList;
   import org.apache.cocoon.woody.event.FormHandler;
   import org.apache.cocoon.woody.event.ActionEvent;
  @@ -84,6 +87,8 @@
   
   public class ScriptableWidget extends ScriptableObject {
   
  +    final static String WIDGETS_PROPERTY = "__widgets__";
  +
       Widget delegate;
       ScriptableWidget formWidget;
   
  @@ -110,7 +115,7 @@
               formWidget = this;
               Map widgetMap = new HashMap();
               widgetMap.put(delegate, this);
  -            put("__widgets__", this, widgetMap);
  +            defineProperty(WIDGETS_PROPERTY, widgetMap, DONTENUM|PERMANENT);
           }
       }
   
  @@ -126,7 +131,7 @@
   
       private void deleteWrapper(Widget w) {
           if (delegate instanceof Form) {
  -            Map widgetMap = (Map)super.get("__widgets__", this);
  +            Map widgetMap = (Map)super.get(WIDGETS_PROPERTY, this);
               widgetMap.remove(w);
           }
       }
  @@ -134,13 +139,13 @@
       private ScriptableWidget wrap(Widget w) {
           if (w == null) return null;
           if (delegate instanceof Form) {
  -            Map widgetMap = (Map)super.get("__widgets__", this);
  +            Map widgetMap = (Map)super.get(WIDGETS_PROPERTY, this);
               ScriptableWidget result = null;
               result = (ScriptableWidget)widgetMap.get(w);
               if (result == null) {
                   result = new ScriptableWidget(w);
                   result.formWidget = this;
  -                result.setPrototype(getClassPrototype(this, "Widget"));
  +                result.setPrototype(getClassPrototype(this, getClassName()));
                   result.setParentScope(getParentScope());
                   widgetMap.put(w, result);
               }
  @@ -246,7 +251,7 @@
                       newValues[i] = values[i];
                   }
                   i++;
  -                for (;i < values.length;i++) {
  +                for (;i < values.length; i++) {
                       newValues[i-1] = values[i];
                   }
                   field.setValues(newValues);
  @@ -284,25 +289,19 @@
           }
       }
   
  -    public Integer jsGet_length() {
  +    public Object jsGet_length() {
           if (delegate instanceof Repeater) {
               Repeater repeater = (Repeater)delegate;
               return new Integer(repeater.getSize());
           }
  -        return null;
  +        return Undefined.instance;
       }
   
       public void jsSet_value(Object value) throws JavaScriptException {
  -        if (delegate instanceof Field || delegate instanceof Output) {
  -            // fix me: Unify Field and Output with a "DataTypeWidget" interface
  +        if (delegate instanceof DataWidget) {
               value = unwrap(value);
               if (value != null) {
  -                Datatype datatype;
  -                if (delegate instanceof Field) {
  -                    datatype = ((Field)delegate).getFieldDefinition().getDatatype();
  -                } else {
  -                    datatype = ((Output)delegate).getOutputDefinition().getDatatype();
  -                }
  +                Datatype datatype = ((DataWidget)delegate).getDatatype();
                   Class typeClass = datatype.getTypeClass();
                   if (typeClass == String.class) {
                       value = Context.toString(value);
  @@ -419,7 +418,6 @@
               }
               field.setValues(values);
           } else {
  -            System.out.println("setting value of " + delegate.getClass());
               delegate.setValue(value);
           }
       }
  @@ -468,40 +466,30 @@
           return wrap(sub);
       }
   
  -    public void jsFunction_setValidationError(String message,
  -                                              boolean i18n,
  +    public void jsFunction_setValidationError(String message, 
                                                 Object parameters) {
  -        if (delegate instanceof Field || 
  -            delegate instanceof Upload) {
  +        if (delegate instanceof ValidationErrorAware) {
               String[] parms = null;
               if (parameters != null && parameters != Undefined.instance) {
                   Scriptable obj = Context.toObject(parameters, this);
                   int len = (int)
                       Context.toNumber(getProperty(obj, "length"));
  -                if (len > 0) {
  -                    parms = new String[len];
  -                    for (int i = 0; i < len; i++) {
  -                        parms[i] = Context.toString(getProperty(obj, i));
  -                    }
  +                parms = new String[len];
  +                for (int i = 0; i < len; i++) {
  +                    parms[i] = Context.toString(getProperty(obj, i));
                   }
               }
               ValidationError validationError = null;
               if (message != null) {
  -                if (parms != null) {
  +                if (parms != null && parms.length > 0) {
                       validationError = 
                           new ValidationError(message, parms);
                   } else {
                       validationError = 
  -                        new ValidationError(message, i18n);
  +                        new ValidationError(message, parms != null);
                   }
               }
  -            // fix me: unify Upload and Field into one interface:
  -            // Validatable or whatever
  -            if (delegate instanceof Upload) {
  -                ((Upload)delegate).setValidationError(validationError);
  -            } else {
  -                ((Field)delegate).setValidationError(validationError);
  -            }
  +            ((ValidationErrorAware)delegate).setValidationError(validationError);
               formWidget.notifyValidationErrorListener(this, validationError);
           }
       }
  @@ -524,8 +512,6 @@
           }
       }
   
  -
  -
       public Widget jsFunction_unwrap() {
           return delegate;
       }
  @@ -580,7 +566,7 @@
               }
           }
       }
  -    
  +
       private void handleEvent(WidgetEvent e) {
           if (e instanceof ActionEvent) {
               Object obj = super.get("onClick", this);
  @@ -608,7 +594,7 @@
                       Scriptable thisObj = scope;
                       Context cx = Context.getCurrentContext();
                       args[0] = vce.getOldValue();
  -                    args[1] = vce.getNewValue();
  +                    args[0] = vce.getNewValue();
                       fun.call(cx, scope, thisObj, args);
                   } catch (Exception exc) {
                       throw Context.reportRuntimeError(exc.getMessage());
  @@ -617,33 +603,23 @@
           }
       }
   
  -    public void jsFunction_setSelectionList(Object arg, Object valuePathArg, Object labelPathArg) throws Exception {
  -        if (delegate instanceof Field ||
  -            delegate instanceof MultiValueField) {
  +    public void jsFunction_setSelectionList(Object arg, 
  +                                            Object valuePathArg, 
  +                                            Object labelPathArg) 
  +        throws Exception {
  +        if (delegate instanceof SelectableWidget) {
               arg = unwrap(arg);
               if (valuePathArg != Undefined.instance && labelPathArg != Undefined.instance) {
                   String valuePath = Context.toString(valuePathArg);
                   String labelPath = Context.toString(labelPathArg);
  -                if (delegate instanceof Field) {
  -                    ((Field)delegate).setSelectionList(arg, valuePath, labelPath);
  -                } else {
  -                    ((MultiValueField)delegate).setSelectionList(arg, valuePath, labelPath);
  -                }
  +                ((SelectableWidget)delegate).setSelectionList(arg, valuePath, labelPath);
               } else {
                   if (arg instanceof SelectionList) {
                       SelectionList selectionList = (SelectionList)arg;
  -                    if (delegate instanceof Field) {
  -                        ((Field)delegate).setSelectionList(selectionList);
  -                    } else {
  -                        ((MultiValueField)delegate).setSelectionList(selectionList);
  -                    }
  +                    ((SelectableWidget)delegate).setSelectionList(selectionList);
                   } else {
                       String str = Context.toString(arg);
  -                    if (delegate instanceof Field) {
  -                        ((Field)delegate).setSelectionList(str);
  -                    } else {
  -                        ((MultiValueField)delegate).setSelectionList(str);
  -                    }
  +                    ((SelectableWidget)delegate).setSelectionList(str);
                   }
               }
           }
  
  
  
  1.23      +2 -2      cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Field.java
  
  Index: Field.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Field.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- Field.java	4 Feb 2004 17:25:57 -0000	1.22
  +++ Field.java	10 Feb 2004 23:35:32 -0000	1.23
  @@ -79,7 +79,7 @@
    * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
    * @version CVS $Id$
    */
  -public class Field extends AbstractWidget implements ValidationErrorAware {
  +public class Field extends AbstractWidget implements ValidationErrorAware, DataWidget, SelectableWidget {
       private SelectionList selectionList;
       private FieldDefinition fieldDefinition;
       
  
  
  
  1.15      +1 -1      cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/MultiValueField.java
  
  Index: MultiValueField.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/MultiValueField.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- MultiValueField.java	6 Feb 2004 21:27:08 -0000	1.14
  +++ MultiValueField.java	10 Feb 2004 23:35:32 -0000	1.15
  @@ -79,7 +79,7 @@
    * can be used with the Datatype (see {@link org.apache.cocoon.woody.datatype.Datatype Datatype}
    * description for more information).
    */
  -public class MultiValueField extends AbstractWidget implements ValidationErrorAware {
  +public class MultiValueField extends AbstractWidget implements ValidationErrorAware, SelectableWidget {
       private SelectionList selectionList;
       private MultiValueFieldDefinition fieldDefinition;
       private String[] enteredValues;
  
  
  
  1.7       +6 -1      cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Output.java
  
  Index: Output.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/Output.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Output.java	30 Jan 2004 19:19:53 -0000	1.6
  +++ Output.java	10 Feb 2004 23:35:32 -0000	1.7
  @@ -2,6 +2,7 @@
   
   import org.apache.cocoon.woody.FormContext;
   import org.apache.cocoon.woody.Constants;
  +import org.apache.cocoon.woody.datatype.Datatype;
   import org.apache.cocoon.xml.AttributesImpl;
   import org.xml.sax.ContentHandler;
   import org.xml.sax.SAXException;
  @@ -15,12 +16,16 @@
    *
    * <p>An Output widget is always valid and never required.
    */
  -public class Output extends AbstractWidget {
  +public class Output extends AbstractWidget implements DataWidget {
       private OutputDefinition definition;
       private Object value;
   
       public OutputDefinition getOutputDefinition() {
           return definition;
  +    }
  +
  +    public Datatype getDatatype() {
  +        return definition.getDatatype();
       }
   
       protected Output(OutputDefinition definition) {
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/DataWidget.java
  
  Index: DataWidget.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.woody.formmodel;
  
  import org.apache.cocoon.woody.datatype.Datatype;
  
  public interface DataWidget extends Widget {
  
      public Datatype getDatatype();
  
  }
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/SelectableWidget.java
  
  Index: SelectableWidget.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.woody.formmodel;
  
  import org.apache.cocoon.woody.datatype.SelectionList;
  
  public interface SelectableWidget extends Widget {
  
      public void setSelectionList(SelectionList selectionList);
  
      public void setSelectionList(String uri);
  
      public void setSelectionList(Object model, String valuePath, String labelPath);
  }