click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r1029965 - in /click/trunk/click/framework: src/org/apache/click/control/Select.java test/org/apache/click/control/SelectTest.java
Date Tue, 02 Nov 2010 07:36:55 GMT
Author: sabob
Date: Tue Nov  2 07:36:54 2010
New Revision: 1029965

URL: http://svn.apache.org/viewvc?rev=1029965&view=rev
Log:
added stateful Select. CLK-715

Modified:
    click/trunk/click/framework/src/org/apache/click/control/Select.java
    click/trunk/click/framework/test/org/apache/click/control/SelectTest.java

Modified: click/trunk/click/framework/src/org/apache/click/control/Select.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/control/Select.java?rev=1029965&r1=1029964&r2=1029965&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/control/Select.java (original)
+++ click/trunk/click/framework/src/org/apache/click/control/Select.java Tue Nov  2 07:36:54
2010
@@ -20,6 +20,7 @@ package org.apache.click.control;
 
 import java.text.MessageFormat;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -931,6 +932,54 @@ public class Select extends Field {
     }
 
     /**
+     * Return the Select state. The following state is returned, depending on
+     * whether {@link #isMultiple()} is <tt>true</tt> or <tt>false</tt>:
+     * <ul>
+     * <li>{@link #getValue()} if {@link #isMultiple()} is <tt>false</tt></li>
+     * <li>{@link #getSelectedValues()} if {@link #isMultiple()} is <tt>true</tt></li>
+     * </ul>
+     *
+     * @return the Select state
+     */
+    @Override
+    public Object getState() {
+        if (isMultiple()) {
+            List selectedState = getSelectedValues();
+            if (selectedState.isEmpty()) {
+                return null;
+            } else {
+                return selectedState.toArray(new String[0]);
+            }
+        } else {
+            return super.getState();
+        }
+    }
+
+    /**
+     * Set the Select state.
+     *
+     * @param state the Select state to set
+     */
+    @Override
+    public void setState(Object state) {
+        if (state == null) {
+            return;
+        }
+
+        List<String> localSelectedState = new ArrayList<String>(5);
+
+        if (state instanceof String) {
+            String selectState = (String) state;
+            setValue(selectState);
+            localSelectedState.add(selectState);
+        } else {
+            String[] selectState = (String[]) state;
+            localSelectedState = Arrays.asList(selectState);
+        }
+        setSelectedValues(localSelectedState);
+    }
+
+    /**
      * This method invokes {@link #getOptionList()} to ensure exceptions thrown
      * while retrieving options will be handled by the error page.
      *

Modified: click/trunk/click/framework/test/org/apache/click/control/SelectTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/control/SelectTest.java?rev=1029965&r1=1029964&r2=1029965&view=diff
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/control/SelectTest.java (original)
+++ click/trunk/click/framework/test/org/apache/click/control/SelectTest.java Tue Nov  2 07:36:54
2010
@@ -18,6 +18,9 @@
  */
 package org.apache.click.control;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 import junit.framework.TestCase;
 import org.apache.click.MockContext;
 
@@ -42,4 +45,100 @@ public class SelectTest extends TestCase
         // Check that the value <script> is not rendered
         assertTrue(select.toString().indexOf(value) < 0);
     }
+
+    /**
+     * Test that Select.getState contains the select value.
+     * CLK-715
+     */
+    public void testGetState() {
+        // Setup Select
+        Select select  = new Select("gender");
+        select.add(new Option("male"));
+        select.add(new Option("female"));
+
+        String expectedState = "female";
+        select.setValue(expectedState);
+
+        // Setup Select value
+        List<String> selectedValues = new ArrayList<String>();
+        selectedValues.add(expectedState);
+        select.setSelectedValues(selectedValues);
+
+        Object state = select.getState();
+
+        // Perform tests
+        assertEquals(expectedState, state);
+        assertEquals(state, select.getValue());
+        assertEquals(state, select.getSelectedValues().get(0));
+    }
+
+    /**
+     * Test that Select.getState contains the select values.
+     * CLK-715
+     */
+    public void testGetStateMultiple() {
+        // Setup Select
+        Select select  = new Select("gender");
+        select.add(new Option("male"));
+        select.add(new Option("female"));
+                select.setMultiple(true);
+
+        // Setup Select values
+        List<String> selectedValues = new ArrayList<String>();
+        selectedValues.add("male");
+        selectedValues.add("female");
+        select.setSelectedValues(selectedValues);
+
+        String[] state = (String[]) select.getState();
+
+        Object expectedState = "male";
+        assertEquals(expectedState, state[0]);
+
+        // Perform tests
+        String[] expectedStateArray = new String[] {"male", "female"};
+        assertTrue(Arrays.equals(expectedStateArray, state));
+
+        assertTrue(Arrays.equals(state, select.getSelectedValues().toArray()));
+    }
+
+    /**
+     * Test that Select.setState set the select value.
+     *
+     * CLK-715
+     */
+    public void testSetState() {
+        // Setup Select
+        Select select  = new Select("gender");
+        select.add(new Option("male"));
+        select.add(new Option("female"));
+
+        String expectedState = "female";
+
+        select.setState(expectedState);
+
+        // Perform tests
+        assertEquals(expectedState, select.getValue());
+        assertEquals(expectedState, select.getSelectedValues().get(0));
+    }
+
+    /**
+     * Test that Select.setState set the select value if multiple is true.
+     *
+     * CLK-715
+     */
+    public void testSetStateMultiple() {
+        // Setup Select
+        Select select  = new Select("gender");
+        select.setMultiple(true);
+        select.add(new Option("male"));
+        select.add(new Option("female"));
+
+        String[] expectedState = {"male", "female"};
+
+        select.setState(expectedState);
+
+        // Perform tests
+        assertEquals(expectedState[0], select.getValue());
+        assertTrue(Arrays.equals(expectedState, select.getSelectedValues().toArray()));
+    }
 }



Mime
View raw message