tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject cvs commit: jakarta-tapestry/framework/src/net/sf/tapestry/form ListEdit.jwc ListEdit.java
Date Fri, 07 Feb 2003 04:21:34 GMT
hlship      2003/02/06 20:21:34

  Modified:    framework/src/net/sf/tapestry/form ListEdit.jwc
                        ListEdit.java
  Log:
  Simplify and enhance ListEdit component.
  
  Revision  Changes    Path
  1.8       +6 -7      jakarta-tapestry/framework/src/net/sf/tapestry/form/ListEdit.jwc
  
  Index: ListEdit.jwc
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/net/sf/tapestry/form/ListEdit.jwc,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ListEdit.jwc	26 Jan 2003 02:59:16 -0000	1.7
  +++ ListEdit.jwc	7 Feb 2003 04:21:34 -0000	1.8
  @@ -12,16 +12,15 @@
     because it stores each element as a hidden field.
     </description>
   	
  -  <parameter name="source" required="yes" java-type="java.lang.Object"
  -  direction="in"/>
  +  <parameter name="source" required="yes" type="java.lang.Object"/>
     
  -  <parameter name="listener" java-type="net.sf.tapestry.IActionListener" direction="in"/>
  +  <parameter name="listener" type="net.sf.tapestry.IActionListener" direction="in"/>
     
  -  <parameter name="value" required="yes" java-type="java.lang.Object"/>
  +  <parameter name="value" required="yes" type="java.lang.Object"/>
     
  -  <parameter name="index" java-type="int"/>
  +  <parameter name="index" type="int"/>
     
  -  <parameter name="element" java-type="java.lang.String" direction="in"/>
  +  <parameter name="element" type="java.lang.String" direction="in"/>
     
   </component-specification>
   
  
  
  
  1.16      +56 -130   jakarta-tapestry/framework/src/net/sf/tapestry/form/ListEdit.java
  
  Index: ListEdit.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/net/sf/tapestry/form/ListEdit.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- ListEdit.java	17 Jan 2003 17:40:27 -0000	1.15
  +++ ListEdit.java	7 Feb 2003 04:21:34 -0000	1.16
  @@ -85,76 +85,10 @@
   
   public class ListEdit extends AbstractComponent
   {
  -    /**
  -     *  Interface that allows a ListEdit to treat an Object[] array or
  -     *  a {@link List} identically.
  -     *
  -     **/
  -
  -    private interface ISource
  -    {
  -        public int getCount();
  -
  -        public Object get(int index);
  -    }
  -
  -    private static class ArraySource implements ISource
  -    {
  -        Object[] _array;
  -
  -        ArraySource(Object[] array)
  -        {
  -            _array = array;
  -        }
  -
  -        public int getCount()
  -        {
  -            return _array.length;
  -        }
  -
  -        public Object get(int index)
  -        {
  -            return _array[index];
  -        }
  -    }
  -
  -    private static class ListSource implements ISource
  -    {
  -        List _list;
  -
  -        ListSource(List list)
  -        {
  -            _list = list;
  -        }
  -
  -        public int getCount()
  -        {
  -            return _list.size();
  -        }
  -
  -        public Object get(int index)
  -        {
  -            return _list.get(index);
  -        }
  -    }
  -
  -    private static class EmptySource implements ISource
  -    {
  -        public int getCount()
  -        {
  -            return 0;
  -        }
  -
  -        public Object get(int index)
  -        {
  -            throw new IndexOutOfBoundsException("ListEdit.EmptySource contains no values.");
  -        }
  -    }
  -
       private IBinding _valueBinding;
       private IBinding _indexBinding;
  +    private IBinding _sourceBinding;
   
  -    private Object _source;
       private String _element;
   
       /** @since 2.2 **/
  @@ -181,19 +115,19 @@
           return _indexBinding;
       }
   
  -    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) throws RequestCycleException
  +    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
  +        throws RequestCycleException
       {
  -        ISource source = null;
  -        int count;
  -        RequestContext context = null;
  -        Object value = null;
  +        Iterator i = null;
   
           IForm form = Form.get(cycle);
           if (form == null)
  -            throw new RequestCycleException(Tapestry.getString("must-be-wrapped-by-form",
"ListEdit"), this);
  +            throw new RequestCycleException(
  +                Tapestry.getString("must-be-wrapped-by-form", "ListEdit"),
  +                this);
   
           boolean cycleRewinding = cycle.isRewinding();
  -    
  +
           // If the cycle is rewinding, but not this particular form,
           // then do nothing (don't even render the body).
   
  @@ -204,32 +138,38 @@
   
           if (!cycleRewinding)
           {
  -            source = getSourceData();
  -            count = source.getCount();
  -
  -            writer.beginEmpty("input");
  -            writer.attribute("type", "hidden");
  -            writer.attribute("name", name);
  -            writer.attribute("value", count);
  -            writer.println();
  +            i = Tapestry.coerceToIterator(_sourceBinding.getObject());
           }
           else
           {
  -            context = cycle.getRequestContext();
  -            count = Integer.parseInt(context.getParameter(name));
  +            RequestContext context = cycle.getRequestContext();
  +            String[] submittedValues = context.getParameters(name);
  +
  +            i = Tapestry.coerceToIterator(submittedValues);
           }
   
  -        for (int i = 0; i < count; i++)
  +		// If the source (when rendering), or the submitted values (on submit)
  +		// are null, then skip the remainder (nothing to update, nothing to
  +		// render).
  +		
  +		if (i == null)
  +			return;
  +			
  +        int index = 0;
  +
  +        while (i.hasNext())
           {
  +            Object value = null;
  +
               if (_indexBinding != null)
  -                _indexBinding.setInt(i);
  +                _indexBinding.setInt(index++);
   
               if (cycleRewinding)
  -                value = extractValue(context, form.getElementId(this));
  +                value = convertValue((String) i.next());
               else
               {
  -                value = source.get(i);
  -                writeValue(writer, form.getElementId(this), value);
  +                value = i.next();
  +                writeValue(writer, name, value);
               }
   
               _valueBinding.setObject(value);
  @@ -247,10 +187,12 @@
   
               if (_element != null)
                   writer.end();
  +
           }
       }
   
  -    private void writeValue(IMarkupWriter writer, String name, Object value) throws RequestCycleException
  +    private void writeValue(IMarkupWriter writer, String name, Object value)
  +        throws RequestCycleException
       {
           String externalValue;
   
  @@ -260,7 +202,10 @@
           }
           catch (IOException ex)
           {
  -            throw new RequestCycleException(Tapestry.getString("ListEdit.unable-to-convert-value",
value), this, ex);
  +            throw new RequestCycleException(
  +                Tapestry.getString("ListEdit.unable-to-convert-value", value),
  +                this,
  +                ex);
           }
   
           writer.beginEmpty("input");
  @@ -270,44 +215,21 @@
           writer.println();
       }
   
  -    private Object extractValue(RequestContext context, String name) throws RequestCycleException
  +    private Object convertValue(String value) throws RequestCycleException
       {
  -        String value = context.getParameter(name);
  -
           try
           {
               return getDataSqueezer().unsqueeze(value);
           }
           catch (IOException ex)
           {
  -            throw new RequestCycleException(Tapestry.getString("ListEdit.unable-to-convert-string",
value), this, ex);
  +            throw new RequestCycleException(
  +                Tapestry.getString("ListEdit.unable-to-convert-string", value),
  +                this,
  +                ex);
           }
       }
   
  -    private ISource getSourceData() throws RequestCycleException
  -    {
  -        if (_source == null)
  -            return new EmptySource();
  -
  -        if (_source instanceof List)
  -            return new ListSource((List) _source);
  -
  -        if (_source.getClass().isArray())
  -            return new ArraySource((Object[]) _source);
  -
  -        if (_source instanceof Iterator)
  -        {
  -            Iterator i = (Iterator) _source;
  -            List list = new ArrayList();
  -            while (i.hasNext())
  -                list.add(i.next());
  -
  -            return new ListSource(list);
  -        }
  -
  -        throw new RequestCycleException(Tapestry.getString("ListEdit.unable-to-convert-source",
_source), this);
  -    }
  -
       public String getElement()
       {
           return _element;
  @@ -318,16 +240,6 @@
           _element = element;
       }
   
  -    public void setSource(Object source)
  -    {
  -        _source = source;
  -    }
  -
  -    public Object getSource()
  -    {
  -        return _source;
  -    }
  -
       private DataSqueezer getDataSqueezer()
       {
           return getPage().getEngine().getDataSqueezer();
  @@ -345,6 +257,20 @@
       public void setListener(IActionListener listener)
       {
           _listener = listener;
  +    }
  +
  +	/** @since 2.4 **/
  +	
  +    public IBinding getSourceBinding()
  +    {
  +        return _sourceBinding;
  +    }
  +
  +	/** @since 2.4 **/
  +	
  +    public void setSourceBinding(IBinding sourceBinding)
  +    {
  +        _sourceBinding = sourceBinding;
       }
   
   }
  
  
  

Mime
View raw message