myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m..@apache.org
Subject svn commit: r267219 - in /myfaces/api/trunk/src/java/javax/faces/component: UISelectMany.java UISelectOne.java _SelectItemsUtil.java
Date Fri, 02 Sep 2005 14:57:58 GMT
Author: mbr
Date: Fri Sep  2 07:57:54 2005
New Revision: 267219

URL: http://svn.apache.org/viewcvs?rev=267219&view=rev
Log:
fix for MYFACES-471

Added:
    myfaces/api/trunk/src/java/javax/faces/component/_SelectItemsUtil.java   (with props)
Modified:
    myfaces/api/trunk/src/java/javax/faces/component/UISelectMany.java
    myfaces/api/trunk/src/java/javax/faces/component/UISelectOne.java

Modified: myfaces/api/trunk/src/java/javax/faces/component/UISelectMany.java
URL: http://svn.apache.org/viewcvs/myfaces/api/trunk/src/java/javax/faces/component/UISelectMany.java?rev=267219&r1=267218&r2=267219&view=diff
==============================================================================
--- myfaces/api/trunk/src/java/javax/faces/component/UISelectMany.java (original)
+++ myfaces/api/trunk/src/java/javax/faces/component/UISelectMany.java Fri Sep  2 07:57:54
2005
@@ -15,14 +15,17 @@
  */
 package javax.faces.component;
 
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
 import javax.faces.application.FacesMessage;
 import javax.faces.context.FacesContext;
 import javax.faces.convert.ConverterException;
 import javax.faces.el.ValueBinding;
-
 import javax.faces.render.Renderer;
-import java.lang.reflect.Array;
-import java.util.List;
 
 /**
  * see Javadoc of JSF Specification
@@ -30,14 +33,13 @@
  * @author Manfred Geiler (latest modification by $Author$)
  * @version $Revision$ $Date$
  */
-public class UISelectMany
-        extends UIInput
+public class UISelectMany extends UIInput
 {
     public static final String INVALID_MESSAGE_ID = "javax.faces.component.UISelectMany.INVALID";
 
     public Object[] getSelectedValues()
     {
-        return (Object[])getValue();
+        return (Object[]) getValue();
     }
 
     public void setSelectedValues(Object[] selectedValues)
@@ -47,7 +49,8 @@
 
     public ValueBinding getValueBinding(String name)
     {
-        if (name == null) throw new NullPointerException("name");
+        if (name == null)
+            throw new NullPointerException("name");
         if (name.equals("selectedValues"))
         {
             return super.getValueBinding("value");
@@ -58,10 +61,10 @@
         }
     }
 
-    public void setValueBinding(String name,
-                                ValueBinding binding)
+    public void setValueBinding(String name, ValueBinding binding)
     {
-        if (name == null) throw new NullPointerException("name");
+        if (name == null)
+            throw new NullPointerException("name");
         if (name.equals("selectedValues"))
         {
             super.setValueBinding("value", binding);
@@ -75,8 +78,7 @@
     /**
      * @return true if Objects are different (!)
      */
-    protected boolean compareValues(Object previous,
-                                    Object value)
+    protected boolean compareValues(Object previous, Object value)
     {
         if (previous == null)
         {
@@ -90,18 +92,17 @@
         }
         else
         {
-            if (previous instanceof Object[] &&
-                value instanceof Object[])
+            if (previous instanceof Object[] && value instanceof Object[])
             {
-                return compareObjectArrays((Object[])previous, (Object[])value);
+                return compareObjectArrays((Object[]) previous,
+                                (Object[]) value);
             }
-            else if (previous instanceof List &&
-                     value instanceof List)
+            else if (previous instanceof List && value instanceof List)
             {
-                return compareLists((List)previous, (List)value);
+                return compareLists((List) previous, (List) value);
             }
-            else if (previous.getClass().isArray() &&
-                     value.getClass().isArray())
+            else if (previous.getClass().isArray()
+                            && value.getClass().isArray())
             {
                 return comparePrimitiveArrays(previous, value);
             }
@@ -113,11 +114,10 @@
         }
     }
 
-    private boolean compareObjectArrays(Object[] previous,
-                                        Object[] value)
+    private boolean compareObjectArrays(Object[] previous, Object[] value)
     {
-        int length = ((Object[])value).length;
-        if (((Object[])previous).length != length)
+        int length = ((Object[]) value).length;
+        if (((Object[]) previous).length != length)
         {
             //different length
             return true;
@@ -133,8 +133,8 @@
                 if (scoreBoard[j] == false)
                 {
                     Object v = value[j];
-                    if ((p == null && v == null) ||
-                        (p != null && v != null && p.equals(v)))
+                    if ((p == null && v == null)
+                                    || (p != null && v != null && p.equals(v)))
                     {
                         scoreBoard[j] = true;
                         found = true;
@@ -144,11 +144,11 @@
             }
             if (!found)
             {
-                return true;    //current element of previous array not found in new array
+                return true; //current element of previous array not found in new array
             }
         }
 
-        return false;   // arrays are identical
+        return false; // arrays are identical
     }
 
     private boolean compareLists(List previous, List value)
@@ -170,8 +170,8 @@
                 if (scoreBoard[j] == false)
                 {
                     Object v = value.get(j);
-                    if ((p == null && v == null) ||
-                        (p != null && v != null && p.equals(v)))
+                    if ((p == null && v == null)
+                                    || (p != null && v != null && p.equals(v)))
                     {
                         scoreBoard[j] = true;
                         found = true;
@@ -181,11 +181,11 @@
             }
             if (!found)
             {
-                return true;    //current element of previous List not found in new List
+                return true; //current element of previous List not found in new List
             }
         }
 
-        return false;   // Lists are identical
+        return false; // Lists are identical
     }
 
     private boolean comparePrimitiveArrays(Object previous, Object value)
@@ -207,8 +207,8 @@
                 if (scoreBoard[j] == false)
                 {
                     Object v = Array.get(value, j);
-                    if ((p == null && v == null) ||
-                        (p != null && v != null && p.equals(v)))
+                    if ((p == null && v == null)
+                                    || (p != null && v != null && p.equals(v)))
                     {
                         scoreBoard[j] = true;
                         found = true;
@@ -218,22 +218,23 @@
             }
             if (!found)
             {
-                return true;    //current element of previous array not found in new array
+                return true; //current element of previous array not found in new array
             }
         }
 
-        return false;   // arrays are identical
+        return false; // arrays are identical
     }
 
     protected void validateValue(FacesContext context, Object convertedValue)
     {
-        boolean empty =
-            convertedValue == null
-                || ((convertedValue instanceof Object[]) && (((Object[]) convertedValue).length
== 0))
-                || ((convertedValue instanceof List) && ((List) convertedValue).isEmpty());
+        boolean empty = convertedValue == null
+                        || ((convertedValue instanceof Object[]) && (((Object[])
convertedValue).length == 0))
+                        || ((convertedValue instanceof List) && ((List) convertedValue)
+                                        .isEmpty());
         if (isRequired() && empty)
         {
-            _MessageUtils.addErrorMessage(context, this, REQUIRED_MESSAGE_ID,new Object[]{getId()});
+            _MessageUtils.addErrorMessage(context, this, REQUIRED_MESSAGE_ID,
+                            new Object[] {getId()});
             setValid(false);
             return;
         }
@@ -243,8 +244,31 @@
             _ComponentUtils.callValidators(context, this, convertedValue);
         }
 
-        //TODO: see javadoc: iterate through UISelectItem and UISelectItems and check
-        //current values against these items
+        if (isValid() && convertedValue != null)
+        {
+            // all selected values must match to the values of the available options
+            Object[] values = (Object[]) convertedValue;
+            if (values.length > 0)
+            {
+                Collection items = new ArrayList();
+                for (Iterator iter = new _SelectItemsIterator(this); iter
+                                .hasNext();)
+                {
+                    items.add(iter.next());
+                }
+                for (int i = 0, size = values.length; i < size; i++)
+                {
+                    if (!_SelectItemsUtil.matchValue(context, values[i],
+                                    items.iterator()))
+                    {
+                        _MessageUtils.addErrorMessage(context, this,
+                                        INVALID_MESSAGE_ID,
+                                        new Object[] {getId()});
+                        setValid(false);
+                    }
+                }
+            }
+        }
     }
 
     /**
@@ -259,15 +283,17 @@
         super.validate(context);
     }
 
-
-    protected Object getConvertedValue(FacesContext context, Object submittedValue)
+    protected Object getConvertedValue(FacesContext context,
+                    Object submittedValue)
     {
         try
         {
             Renderer renderer = getRenderer(context);
             if (renderer != null)
             {
-                return renderer.getConvertedValue(context, this, submittedValue);
+                return renderer
+                                .getConvertedValue(context, this,
+                                                submittedValue);
             }
             else if (submittedValue == null)
             {
@@ -275,8 +301,8 @@
             }
             else if (submittedValue instanceof String[])
             {
-                return _SharedRendererUtils.getConvertedUISelectManyValue(context, this,
-                                                                          (String[])submittedValue);
+                return _SharedRendererUtils.getConvertedUISelectManyValue(
+                                context, this, (String[]) submittedValue);
             }
         }
         catch (ConverterException e)
@@ -288,23 +314,20 @@
             }
             else
             {
-                _MessageUtils.addErrorMessage(context, this, CONVERSION_MESSAGE_ID,new Object[]{getId()});
+                _MessageUtils.addErrorMessage(context, this,
+                                CONVERSION_MESSAGE_ID, new Object[] {getId()});
             }
             setValid(false);
         }
         return submittedValue;
     }
 
-
-
-
     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
 
     public static final String COMPONENT_TYPE = "javax.faces.SelectMany";
     public static final String COMPONENT_FAMILY = "javax.faces.SelectMany";
     private static final String DEFAULT_RENDERER_TYPE = "javax.faces.Listbox";
 
-
     public UISelectMany()
     {
         setRendererType(DEFAULT_RENDERER_TYPE);
@@ -314,7 +337,6 @@
     {
         return COMPONENT_FAMILY;
     }
-
 
     //------------------ GENERATED CODE END ---------------------------------------
 }

Modified: myfaces/api/trunk/src/java/javax/faces/component/UISelectOne.java
URL: http://svn.apache.org/viewcvs/myfaces/api/trunk/src/java/javax/faces/component/UISelectOne.java?rev=267219&r1=267218&r2=267219&view=diff
==============================================================================
--- myfaces/api/trunk/src/java/javax/faces/component/UISelectOne.java (original)
+++ myfaces/api/trunk/src/java/javax/faces/component/UISelectOne.java Fri Sep  2 07:57:54
2005
@@ -45,44 +45,12 @@
         }
 
         // selected value must match to one of the available options
-        if (!matchValue(context, value, new _SelectItemsIterator(this)))
+        if (!_SelectItemsUtil.matchValue(context, value, new _SelectItemsIterator(this)))
         {
             _MessageUtils.addErrorMessage(context, this, INVALID_MESSAGE_ID,
                             new Object[] {getId()});
             setValid(false);
         }
-    }
-
-    /**
-     * @param context
-     * @param value 
-     * @param iterator
-     * @return
-     */
-    private boolean matchValue(FacesContext context, Object value,
-                    Iterator selectItemsIter)
-    {
-        while (selectItemsIter.hasNext())
-        {
-            SelectItem item = (SelectItem) selectItemsIter.next();
-            if (value.equals(item.getValue()))
-            {
-                return true;
-            }
-            if (item instanceof SelectItemGroup)
-            {
-                SelectItemGroup itemgroup = (SelectItemGroup) item;
-                SelectItem[] selectItems = itemgroup.getSelectItems();
-                if (selectItems != null
-                                && selectItems.length > 0
-                                && matchValue(context, value, Arrays.asList(
-                                                selectItems).iterator()))
-                {
-                    return true;
-                }
-            }
-        }
-        return false;
     }
 
     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------

Added: myfaces/api/trunk/src/java/javax/faces/component/_SelectItemsUtil.java
URL: http://svn.apache.org/viewcvs/myfaces/api/trunk/src/java/javax/faces/component/_SelectItemsUtil.java?rev=267219&view=auto
==============================================================================
--- myfaces/api/trunk/src/java/javax/faces/component/_SelectItemsUtil.java (added)
+++ myfaces/api/trunk/src/java/javax/faces/component/_SelectItemsUtil.java Fri Sep  2 07:57:54
2005
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package javax.faces.component;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import javax.faces.context.FacesContext;
+import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
+
+/**
+ * @author Mathias Broekelmann (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+class _SelectItemsUtil
+{
+    /**
+     * @param context the faces context
+     * @param value the value to check
+     * @param iterator contains instances of SelectItem
+     * @return if the value of a selectitem is equal to the given value
+     */
+    public static boolean matchValue(FacesContext context, Object value,
+                    Iterator selectItemsIter)
+    {
+        while (selectItemsIter.hasNext())
+        {
+            SelectItem item = (SelectItem) selectItemsIter.next();
+            if (value.equals(item.getValue()))
+            {
+                return true;
+            }
+            if (item instanceof SelectItemGroup)
+            {
+                SelectItemGroup itemgroup = (SelectItemGroup) item;
+                SelectItem[] selectItems = itemgroup.getSelectItems();
+                if (selectItems != null
+                                && selectItems.length > 0
+                                && matchValue(context, value, Arrays.asList(
+                                                selectItems).iterator()))
+                {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+}

Propchange: myfaces/api/trunk/src/java/javax/faces/component/_SelectItemsUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/api/trunk/src/java/javax/faces/component/_SelectItemsUtil.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision Date Author



Mime
View raw message