You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by jk...@apache.org on 2007/11/06 15:30:13 UTC

svn commit: r592437 - in /tapestry/tapestry4/trunk/tapestry-framework/src: java/org/apache/tapestry/form/BeanPropertySelectionModel.java test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java

Author: jkuhnert
Date: Tue Nov  6 06:30:12 2007
New Revision: 592437

URL: http://svn.apache.org/viewvc?rev=592437&view=rev
Log:
Fixes TAPESTRY-1885.  Added "null label" functionality to BeanPropertySelectionModel.

Modified:
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java
    tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java?rev=592437&r1=592436&r2=592437&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java Tue Nov  6 06:30:12 2007
@@ -13,14 +13,14 @@
 // limitations under the License.
 package org.apache.tapestry.form;
 
+import org.apache.commons.beanutils.BeanUtils;
+
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
-import org.apache.commons.beanutils.BeanUtils;
-
 /**
  * This class is a property selection model for an object list. This is used in PropertySelection,
  * MultiplePropertySelection or Palette tapestry components. For example, to use for a Hospital
@@ -29,7 +29,7 @@
  * return new BeanPropertySelectionModel(hospitals, "name");
  * </code>
  * This will use getName() on the Hospital object, as its display.
- * 
+ *
  * @author Gabriel Handford
  */
 public class BeanPropertySelectionModel implements IPropertySelectionModel, Serializable
@@ -37,8 +37,9 @@
 
     /** Comment for <code>serialVersionUID</code>. */
     private static final long serialVersionUID = 3763091973006766644L;
-    private List _list;
-    private String _labelField;
+    protected List _list;
+    protected String _labelField;
+    protected String _nullLabel;
 
     /**
      * Build an empty property selection model.
@@ -50,7 +51,7 @@
 
     /**
      * Build a bean property selection model.
-     * 
+     *
      * @param list
      *            The list
      * @param labelField
@@ -64,11 +65,11 @@
 
     /**
      * Build a bean property selection model.
-     * 
+     *
      * @param c
-     *            Collection
+     *          Collection
      * @param labelField
-     *            The label field
+     *          The label field
      */
     public BeanPropertySelectionModel(Collection c, String labelField)
     {
@@ -77,48 +78,93 @@
     }
 
     /**
+     * Same as {@link #BeanPropertySelectionModel(java.util.List, String)} - with the added
+     * functionality of using the specified <code>nullLabel</code> field as a pseudo item in
+     * the list of options that stores a null value.   This is useful for situations where you
+     * want to present a "Choose.." option or similar invalid option to prompt users for input.
+     *
+     * @param list
+     *          The list of options.
+     * @param labelField
+     *          The string expression to be used on each object to get the label value
+     *          for the option - such as "user.name".
+     * @param nullLabel
+     *          Will be treated as a pseudo option that always resolves to a null value but
+     *          is properly displayed in the options list as the first element.
+     */
+    public BeanPropertySelectionModel(List list, String labelField, String nullLabel)
+    {
+        this(list, labelField);
+
+        _nullLabel = nullLabel;
+    }
+
+    /**
      * Get the number of options.
-     * 
+     *
      * @return option count
      */
     public int getOptionCount()
     {
-        return _list.size();
+        return _nullLabel != null ? _list.size() + 1 : _list.size();
     }
 
     /**
      * Get the option at index.
-     * 
+     *
      * @param index
      *            Index
      * @return object Object at index
      */
     public Object getOption(int index)
     {
+        if (_nullLabel != null && index == 0)
+        {
+            return null;
+        }
+
+        if (_nullLabel != null)
+            index--;
+        
+        if (index > (_list.size() - 1))
+        {
+            return null;
+        }
+
         return _list.get(index);
     }
 
     /**
      * Get the label at index.
-     * 
+     *
      * @param index
      *            Index
      * @return label Label at index
      */
     public String getLabel(int index)
     {
-        Object obj = _list.get(index);
-        try {
+        if (index == 0 && _nullLabel != null)
+        {
+            return _nullLabel;
+        }
 
+        if (_nullLabel != null)
+            index--;
+        
+        Object obj = _list.get(index);
+        
+        try
+        {
             return BeanUtils.getProperty(obj, _labelField);
-        } catch (Exception e) {
+        } catch (Exception e)
+        {
             throw new RuntimeException("Error getting property", e);
         }
     }
 
     /**
      * Get the value at index.
-     * 
+     *
      * @param index
      *            Index
      * @return value Value at index
@@ -130,18 +176,29 @@
 
     public boolean isDisabled(int index)
     {
-        return false;
+        return index == 0 && _nullLabel != null;
     }
-    
+
     /**
      * Translate value to object.
-     * 
+     *
      * @param value
      *            Value
      * @return object Object from value
      */
     public Object translateValue(String value)
     {
-        return getOption(Integer.parseInt(value));
+        if (value == null)
+        {
+            return null;
+        }
+
+        int index = Integer.parseInt(value);
+        if (index == 0 && _nullLabel != null)
+        {
+            return null;
+        }
+
+        return getOption(index);
     }
 }

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java?rev=592437&r1=592436&r2=592437&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java Tue Nov  6 06:30:12 2007
@@ -13,12 +13,12 @@
 // limitations under the License.
 package org.apache.tapestry.form;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import org.apache.tapestry.BaseComponentTestCase;
 import org.testng.annotations.Test;
 
+import java.util.ArrayList;
+import java.util.List;
+
 
 /**
  * Tests the functionality of {@link BeanPropertySelectionModel}.
@@ -29,28 +29,20 @@
 public class BeanPropertySelectionModelTest extends BaseComponentTestCase
 {
     
-    /**
-     * Tests using a null arg constuctor.
-     */
-    public void testNullModel()
+    public void test_Null_Model()
     {
         BeanPropertySelectionModel model = new BeanPropertySelectionModel();
         assertEquals(model.getOptionCount(), 0);
     }
     
-    /**
-     * Uses {@link BeanPropertySelectionModelTest} as the 
-     * model.
-     */
-    public void testBasicModel()
+    public void test_Basic_Model()
     {
         List<SimpleBean> list = new ArrayList();
         list.add(new SimpleBean(1, "Name 1", "Description 1"));
         list.add(new SimpleBean(2, "Name 2", "Description 2"));
         list.add(new SimpleBean(3, "Name 3", "Description 3"));
         
-        BeanPropertySelectionModel model =
-            new BeanPropertySelectionModel(list, "name");
+        BeanPropertySelectionModel model = new BeanPropertySelectionModel(list, "name");
         
         assertEquals(model.getOptionCount(), 3);
         
@@ -59,5 +51,30 @@
         assertEquals(model.getLabel(2), "Name 3");
         
         assertEquals(model.translateValue("1"), b2);
+    }
+
+    public void test_Invalid_Option_Index()
+    {
+        BeanPropertySelectionModel model = new BeanPropertySelectionModel();
+
+        assertEquals(model.getOptionCount(), 0);
+        assertEquals(model.getOption(3), null);
+    }
+
+    public void test_Null_Label()
+    {
+        List<SimpleBean> list = new ArrayList();
+        list.add(new SimpleBean(1, "Name 1", "Description 1"));
+        list.add(new SimpleBean(2, "Name 2", "Description 2"));
+        list.add(new SimpleBean(3, "Name 3", "Description 3"));
+
+        BeanPropertySelectionModel model = new BeanPropertySelectionModel(list, "name", "test");
+
+        assertEquals(model.getOptionCount(), 4);
+        assert model.getOption(3) != null;
+        
+        assertEquals(model.getOption(0), null);
+        assertEquals(model.getLabel(0), "test");
+        assertEquals(model.getLabel(1), "Name 1");
     }
 }



Re: svn commit: r592437 - in /tapestry/tapestry4/trunk/tapestry-framework/src: java/org/apache/tapestry/form/BeanPropertySelectionModel.java test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java

Posted by Jesse Kuhnert <jk...@gmail.com>.
Hmmm...It does appear as if it does the same thing.  Never even
noticed it there....Yeah I'll back those changes out later then
(besides one bug fix).   Thanks for noticing.  ;)

On Nov 6, 2007 1:56 PM, Andreas Andreou <an...@gmail.com> wrote:
> just wondering..., isn't the same as decorating with
> org.apache.tapestry.form.LabeledPropertySelectionModel ?
>
>
> On 11/6/07, jkuhnert@apache.org <jk...@apache.org> wrote:
> > Author: jkuhnert
> > Date: Tue Nov  6 06:30:12 2007
> > New Revision: 592437
> >
> > URL: http://svn.apache.org/viewvc?rev=592437&view=rev
> > Log:
> > Fixes TAPESTRY-1885.  Added "null label" functionality to BeanPropertySelectionModel.
> >
> > Modified:
> >     tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java
> >     tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java
> >
> > Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java
> > URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java?rev=592437&r1=592436&r2=592437&view=diff
> > ==============================================================================
> > --- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java (original)
> > +++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java Tue Nov  6 06:30:12 2007
> > @@ -13,14 +13,14 @@
> >  // limitations under the License.
> >  package org.apache.tapestry.form;
> >
> > +import org.apache.commons.beanutils.BeanUtils;
> > +
> >  import java.io.Serializable;
> >  import java.util.ArrayList;
> >  import java.util.Arrays;
> >  import java.util.Collection;
> >  import java.util.List;
> >
> > -import org.apache.commons.beanutils.BeanUtils;
> > -
> >  /**
> >   * This class is a property selection model for an object list. This is used in PropertySelection,
> >   * MultiplePropertySelection or Palette tapestry components. For example, to use for a Hospital
> > @@ -29,7 +29,7 @@
> >   * return new BeanPropertySelectionModel(hospitals, "name");
> >   * </code>
> >   * This will use getName() on the Hospital object, as its display.
> > - *
> > + *
> >   * @author Gabriel Handford
> >   */
> >  public class BeanPropertySelectionModel implements IPropertySelectionModel, Serializable
> > @@ -37,8 +37,9 @@
> >
> >      /** Comment for <code>serialVersionUID</code>. */
> >      private static final long serialVersionUID = 3763091973006766644L;
> > -    private List _list;
> > -    private String _labelField;
> > +    protected List _list;
> > +    protected String _labelField;
> > +    protected String _nullLabel;
> >
> >      /**
> >       * Build an empty property selection model.
> > @@ -50,7 +51,7 @@
> >
> >      /**
> >       * Build a bean property selection model.
> > -     *
> > +     *
> >       * @param list
> >       *            The list
> >       * @param labelField
> > @@ -64,11 +65,11 @@
> >
> >      /**
> >       * Build a bean property selection model.
> > -     *
> > +     *
> >       * @param c
> > -     *            Collection
> > +     *          Collection
> >       * @param labelField
> > -     *            The label field
> > +     *          The label field
> >       */
> >      public BeanPropertySelectionModel(Collection c, String labelField)
> >      {
> > @@ -77,48 +78,93 @@
> >      }
> >
> >      /**
> > +     * Same as {@link #BeanPropertySelectionModel(java.util.List, String)} - with the added
> > +     * functionality of using the specified <code>nullLabel</code> field as a pseudo item in
> > +     * the list of options that stores a null value.   This is useful for situations where you
> > +     * want to present a "Choose.." option or similar invalid option to prompt users for input.
> > +     *
> > +     * @param list
> > +     *          The list of options.
> > +     * @param labelField
> > +     *          The string expression to be used on each object to get the label value
> > +     *          for the option - such as "user.name".
> > +     * @param nullLabel
> > +     *          Will be treated as a pseudo option that always resolves to a null value but
> > +     *          is properly displayed in the options list as the first element.
> > +     */
> > +    public BeanPropertySelectionModel(List list, String labelField, String nullLabel)
> > +    {
> > +        this(list, labelField);
> > +
> > +        _nullLabel = nullLabel;
> > +    }
> > +
> > +    /**
> >       * Get the number of options.
> > -     *
> > +     *
> >       * @return option count
> >       */
> >      public int getOptionCount()
> >      {
> > -        return _list.size();
> > +        return _nullLabel != null ? _list.size() + 1 : _list.size();
> >      }
> >
> >      /**
> >       * Get the option at index.
> > -     *
> > +     *
> >       * @param index
> >       *            Index
> >       * @return object Object at index
> >       */
> >      public Object getOption(int index)
> >      {
> > +        if (_nullLabel != null && index == 0)
> > +        {
> > +            return null;
> > +        }
> > +
> > +        if (_nullLabel != null)
> > +            index--;
> > +
> > +        if (index > (_list.size() - 1))
> > +        {
> > +            return null;
> > +        }
> > +
> >          return _list.get(index);
> >      }
> >
> >      /**
> >       * Get the label at index.
> > -     *
> > +     *
> >       * @param index
> >       *            Index
> >       * @return label Label at index
> >       */
> >      public String getLabel(int index)
> >      {
> > -        Object obj = _list.get(index);
> > -        try {
> > +        if (index == 0 && _nullLabel != null)
> > +        {
> > +            return _nullLabel;
> > +        }
> >
> > +        if (_nullLabel != null)
> > +            index--;
> > +
> > +        Object obj = _list.get(index);
> > +
> > +        try
> > +        {
> >              return BeanUtils.getProperty(obj, _labelField);
> > -        } catch (Exception e) {
> > +        } catch (Exception e)
> > +        {
> >              throw new RuntimeException("Error getting property", e);
> >          }
> >      }
> >
> >      /**
> >       * Get the value at index.
> > -     *
> > +     *
> >       * @param index
> >       *            Index
> >       * @return value Value at index
> > @@ -130,18 +176,29 @@
> >
> >      public boolean isDisabled(int index)
> >      {
> > -        return false;
> > +        return index == 0 && _nullLabel != null;
> >      }
> > -
> > +
> >      /**
> >       * Translate value to object.
> > -     *
> > +     *
> >       * @param value
> >       *            Value
> >       * @return object Object from value
> >       */
> >      public Object translateValue(String value)
> >      {
> > -        return getOption(Integer.parseInt(value));
> > +        if (value == null)
> > +        {
> > +            return null;
> > +        }
> > +
> > +        int index = Integer.parseInt(value);
> > +        if (index == 0 && _nullLabel != null)
> > +        {
> > +            return null;
> > +        }
> > +
> > +        return getOption(index);
> >      }
> >  }
> >
> > Modified: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java
> > URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java?rev=592437&r1=592436&r2=592437&view=diff
> > ==============================================================================
> > --- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java (original)
> > +++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java Tue Nov  6 06:30:12 2007
> > @@ -13,12 +13,12 @@
> >  // limitations under the License.
> >  package org.apache.tapestry.form;
> >
> > -import java.util.ArrayList;
> > -import java.util.List;
> > -
> >  import org.apache.tapestry.BaseComponentTestCase;
> >  import org.testng.annotations.Test;
> >
> > +import java.util.ArrayList;
> > +import java.util.List;
> > +
> >
> >  /**
> >   * Tests the functionality of {@link BeanPropertySelectionModel}.
> > @@ -29,28 +29,20 @@
> >  public class BeanPropertySelectionModelTest extends BaseComponentTestCase
> >  {
> >
> > -    /**
> > -     * Tests using a null arg constuctor.
> > -     */
> > -    public void testNullModel()
> > +    public void test_Null_Model()
> >      {
> >          BeanPropertySelectionModel model = new BeanPropertySelectionModel();
> >          assertEquals(model.getOptionCount(), 0);
> >      }
> >
> > -    /**
> > -     * Uses {@link BeanPropertySelectionModelTest} as the
> > -     * model.
> > -     */
> > -    public void testBasicModel()
> > +    public void test_Basic_Model()
> >      {
> >          List<SimpleBean> list = new ArrayList();
> >          list.add(new SimpleBean(1, "Name 1", "Description 1"));
> >          list.add(new SimpleBean(2, "Name 2", "Description 2"));
> >          list.add(new SimpleBean(3, "Name 3", "Description 3"));
> >
> > -        BeanPropertySelectionModel model =
> > -            new BeanPropertySelectionModel(list, "name");
> > +        BeanPropertySelectionModel model = new BeanPropertySelectionModel(list, "name");
> >
> >          assertEquals(model.getOptionCount(), 3);
> >
> > @@ -59,5 +51,30 @@
> >          assertEquals(model.getLabel(2), "Name 3");
> >
> >          assertEquals(model.translateValue("1"), b2);
> > +    }
> > +
> > +    public void test_Invalid_Option_Index()
> > +    {
> > +        BeanPropertySelectionModel model = new BeanPropertySelectionModel();
> > +
> > +        assertEquals(model.getOptionCount(), 0);
> > +        assertEquals(model.getOption(3), null);
> > +    }
> > +
> > +    public void test_Null_Label()
> > +    {
> > +        List<SimpleBean> list = new ArrayList();
> > +        list.add(new SimpleBean(1, "Name 1", "Description 1"));
> > +        list.add(new SimpleBean(2, "Name 2", "Description 2"));
> > +        list.add(new SimpleBean(3, "Name 3", "Description 3"));
> > +
> > +        BeanPropertySelectionModel model = new BeanPropertySelectionModel(list, "name", "test");
> > +
> > +        assertEquals(model.getOptionCount(), 4);
> > +        assert model.getOption(3) != null;
> > +
> > +        assertEquals(model.getOption(0), null);
> > +        assertEquals(model.getLabel(0), "test");
> > +        assertEquals(model.getLabel(1), "Name 1");
> >      }
> >  }
> >
> >
> >
>
>
> --
> Andreas Andreou - andyhot@apache.org - http://blog.andyhot.gr
> Tapestry / Tacos developer
> Open Source / JEE Consulting
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: dev-help@tapestry.apache.org
>
>



-- 
Jesse Kuhnert
Tapestry/Dojo team member/developer

Open source based consulting work centered around
dojo/tapestry/tacos/hivemind. http://blog.opencomponentry.com

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org


Re: svn commit: r592437 - in /tapestry/tapestry4/trunk/tapestry-framework/src: java/org/apache/tapestry/form/BeanPropertySelectionModel.java test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java

Posted by Andreas Andreou <an...@gmail.com>.
just wondering..., isn't the same as decorating with
org.apache.tapestry.form.LabeledPropertySelectionModel ?

On 11/6/07, jkuhnert@apache.org <jk...@apache.org> wrote:
> Author: jkuhnert
> Date: Tue Nov  6 06:30:12 2007
> New Revision: 592437
>
> URL: http://svn.apache.org/viewvc?rev=592437&view=rev
> Log:
> Fixes TAPESTRY-1885.  Added "null label" functionality to BeanPropertySelectionModel.
>
> Modified:
>     tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java
>     tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java
>
> Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java
> URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java?rev=592437&r1=592436&r2=592437&view=diff
> ==============================================================================
> --- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java (original)
> +++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/BeanPropertySelectionModel.java Tue Nov  6 06:30:12 2007
> @@ -13,14 +13,14 @@
>  // limitations under the License.
>  package org.apache.tapestry.form;
>
> +import org.apache.commons.beanutils.BeanUtils;
> +
>  import java.io.Serializable;
>  import java.util.ArrayList;
>  import java.util.Arrays;
>  import java.util.Collection;
>  import java.util.List;
>
> -import org.apache.commons.beanutils.BeanUtils;
> -
>  /**
>   * This class is a property selection model for an object list. This is used in PropertySelection,
>   * MultiplePropertySelection or Palette tapestry components. For example, to use for a Hospital
> @@ -29,7 +29,7 @@
>   * return new BeanPropertySelectionModel(hospitals, "name");
>   * </code>
>   * This will use getName() on the Hospital object, as its display.
> - *
> + *
>   * @author Gabriel Handford
>   */
>  public class BeanPropertySelectionModel implements IPropertySelectionModel, Serializable
> @@ -37,8 +37,9 @@
>
>      /** Comment for <code>serialVersionUID</code>. */
>      private static final long serialVersionUID = 3763091973006766644L;
> -    private List _list;
> -    private String _labelField;
> +    protected List _list;
> +    protected String _labelField;
> +    protected String _nullLabel;
>
>      /**
>       * Build an empty property selection model.
> @@ -50,7 +51,7 @@
>
>      /**
>       * Build a bean property selection model.
> -     *
> +     *
>       * @param list
>       *            The list
>       * @param labelField
> @@ -64,11 +65,11 @@
>
>      /**
>       * Build a bean property selection model.
> -     *
> +     *
>       * @param c
> -     *            Collection
> +     *          Collection
>       * @param labelField
> -     *            The label field
> +     *          The label field
>       */
>      public BeanPropertySelectionModel(Collection c, String labelField)
>      {
> @@ -77,48 +78,93 @@
>      }
>
>      /**
> +     * Same as {@link #BeanPropertySelectionModel(java.util.List, String)} - with the added
> +     * functionality of using the specified <code>nullLabel</code> field as a pseudo item in
> +     * the list of options that stores a null value.   This is useful for situations where you
> +     * want to present a "Choose.." option or similar invalid option to prompt users for input.
> +     *
> +     * @param list
> +     *          The list of options.
> +     * @param labelField
> +     *          The string expression to be used on each object to get the label value
> +     *          for the option - such as "user.name".
> +     * @param nullLabel
> +     *          Will be treated as a pseudo option that always resolves to a null value but
> +     *          is properly displayed in the options list as the first element.
> +     */
> +    public BeanPropertySelectionModel(List list, String labelField, String nullLabel)
> +    {
> +        this(list, labelField);
> +
> +        _nullLabel = nullLabel;
> +    }
> +
> +    /**
>       * Get the number of options.
> -     *
> +     *
>       * @return option count
>       */
>      public int getOptionCount()
>      {
> -        return _list.size();
> +        return _nullLabel != null ? _list.size() + 1 : _list.size();
>      }
>
>      /**
>       * Get the option at index.
> -     *
> +     *
>       * @param index
>       *            Index
>       * @return object Object at index
>       */
>      public Object getOption(int index)
>      {
> +        if (_nullLabel != null && index == 0)
> +        {
> +            return null;
> +        }
> +
> +        if (_nullLabel != null)
> +            index--;
> +
> +        if (index > (_list.size() - 1))
> +        {
> +            return null;
> +        }
> +
>          return _list.get(index);
>      }
>
>      /**
>       * Get the label at index.
> -     *
> +     *
>       * @param index
>       *            Index
>       * @return label Label at index
>       */
>      public String getLabel(int index)
>      {
> -        Object obj = _list.get(index);
> -        try {
> +        if (index == 0 && _nullLabel != null)
> +        {
> +            return _nullLabel;
> +        }
>
> +        if (_nullLabel != null)
> +            index--;
> +
> +        Object obj = _list.get(index);
> +
> +        try
> +        {
>              return BeanUtils.getProperty(obj, _labelField);
> -        } catch (Exception e) {
> +        } catch (Exception e)
> +        {
>              throw new RuntimeException("Error getting property", e);
>          }
>      }
>
>      /**
>       * Get the value at index.
> -     *
> +     *
>       * @param index
>       *            Index
>       * @return value Value at index
> @@ -130,18 +176,29 @@
>
>      public boolean isDisabled(int index)
>      {
> -        return false;
> +        return index == 0 && _nullLabel != null;
>      }
> -
> +
>      /**
>       * Translate value to object.
> -     *
> +     *
>       * @param value
>       *            Value
>       * @return object Object from value
>       */
>      public Object translateValue(String value)
>      {
> -        return getOption(Integer.parseInt(value));
> +        if (value == null)
> +        {
> +            return null;
> +        }
> +
> +        int index = Integer.parseInt(value);
> +        if (index == 0 && _nullLabel != null)
> +        {
> +            return null;
> +        }
> +
> +        return getOption(index);
>      }
>  }
>
> Modified: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java
> URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java?rev=592437&r1=592436&r2=592437&view=diff
> ==============================================================================
> --- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java (original)
> +++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/BeanPropertySelectionModelTest.java Tue Nov  6 06:30:12 2007
> @@ -13,12 +13,12 @@
>  // limitations under the License.
>  package org.apache.tapestry.form;
>
> -import java.util.ArrayList;
> -import java.util.List;
> -
>  import org.apache.tapestry.BaseComponentTestCase;
>  import org.testng.annotations.Test;
>
> +import java.util.ArrayList;
> +import java.util.List;
> +
>
>  /**
>   * Tests the functionality of {@link BeanPropertySelectionModel}.
> @@ -29,28 +29,20 @@
>  public class BeanPropertySelectionModelTest extends BaseComponentTestCase
>  {
>
> -    /**
> -     * Tests using a null arg constuctor.
> -     */
> -    public void testNullModel()
> +    public void test_Null_Model()
>      {
>          BeanPropertySelectionModel model = new BeanPropertySelectionModel();
>          assertEquals(model.getOptionCount(), 0);
>      }
>
> -    /**
> -     * Uses {@link BeanPropertySelectionModelTest} as the
> -     * model.
> -     */
> -    public void testBasicModel()
> +    public void test_Basic_Model()
>      {
>          List<SimpleBean> list = new ArrayList();
>          list.add(new SimpleBean(1, "Name 1", "Description 1"));
>          list.add(new SimpleBean(2, "Name 2", "Description 2"));
>          list.add(new SimpleBean(3, "Name 3", "Description 3"));
>
> -        BeanPropertySelectionModel model =
> -            new BeanPropertySelectionModel(list, "name");
> +        BeanPropertySelectionModel model = new BeanPropertySelectionModel(list, "name");
>
>          assertEquals(model.getOptionCount(), 3);
>
> @@ -59,5 +51,30 @@
>          assertEquals(model.getLabel(2), "Name 3");
>
>          assertEquals(model.translateValue("1"), b2);
> +    }
> +
> +    public void test_Invalid_Option_Index()
> +    {
> +        BeanPropertySelectionModel model = new BeanPropertySelectionModel();
> +
> +        assertEquals(model.getOptionCount(), 0);
> +        assertEquals(model.getOption(3), null);
> +    }
> +
> +    public void test_Null_Label()
> +    {
> +        List<SimpleBean> list = new ArrayList();
> +        list.add(new SimpleBean(1, "Name 1", "Description 1"));
> +        list.add(new SimpleBean(2, "Name 2", "Description 2"));
> +        list.add(new SimpleBean(3, "Name 3", "Description 3"));
> +
> +        BeanPropertySelectionModel model = new BeanPropertySelectionModel(list, "name", "test");
> +
> +        assertEquals(model.getOptionCount(), 4);
> +        assert model.getOption(3) != null;
> +
> +        assertEquals(model.getOption(0), null);
> +        assertEquals(model.getLabel(0), "test");
> +        assertEquals(model.getLabel(1), "Name 1");
>      }
>  }
>
>
>


-- 
Andreas Andreou - andyhot@apache.org - http://blog.andyhot.gr
Tapestry / Tacos developer
Open Source / JEE Consulting

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org