View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package javax.faces.component;
20  
21  import java.util.Arrays;
22  import java.util.Iterator;
23  
24  import javax.faces.context.FacesContext;
25  import javax.faces.model.SelectItem;
26  import javax.faces.model.SelectItemGroup;
27  
28  /**
29   * @author Mathias Broekelmann (latest modification by $Author: skitching $)
30   * @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (Sun, 13 Jul 2008) $
31   */
32  class _SelectItemsUtil
33  {
34      public static interface _ValueConverter
35      {
36          Object getConvertedValue(FacesContext context, String value);
37      }
38      
39      /**
40       * @param context the faces context
41       * @param value the value to check
42       * @param converter 
43       * @param iterator contains instances of SelectItem
44       * @return if the value of a selectitem is equal to the given value
45       */
46      public static boolean matchValue(FacesContext context, Object value,
47                      Iterator<SelectItem> selectItemsIter, _ValueConverter converter)
48      {
49          while (selectItemsIter.hasNext())
50          {
51              SelectItem item = selectItemsIter.next();
52              if (item instanceof SelectItemGroup)
53              {
54                  SelectItemGroup itemgroup = (SelectItemGroup) item;
55                  SelectItem[] selectItems = itemgroup.getSelectItems();
56                  if (selectItems != null
57                                  && selectItems.length > 0
58                                  && matchValue(context, value, Arrays.asList(
59                                                  selectItems).iterator(), converter))
60                  {
61                      return true;
62                  }
63              }
64              else
65              {
66                  Object itemValue = item.getValue();
67                  if(converter != null && itemValue instanceof String)
68                  {
69                      itemValue = converter.getConvertedValue(context, (String)itemValue);
70                  }
71                  if (value==itemValue || value.equals(itemValue))
72                  {
73                      return true;
74                  }
75              }
76          }
77          return false;
78      }
79  }