01: /*
02: * Copyright 2005 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package javax.faces.component;
17:
18: import java.util.Arrays;
19: import java.util.Iterator;
20:
21: import javax.faces.context.FacesContext;
22: import javax.faces.model.SelectItem;
23: import javax.faces.model.SelectItemGroup;
24:
25: /**
26: * @author Mathias Broekelmann (latest modification by $Author: mbr $)
27: * @version $Revision: 511124 $ $Date: 2007-02-23 22:56:42 +0100 (Fr, 23 Feb 2007) $
28: */
29: class _SelectItemsUtil {
30: public static interface _ValueConverter {
31: Object getConvertedValue(FacesContext context, String value);
32: }
33:
34: /**
35: * @param context the faces context
36: * @param value the value to check
37: * @param converter
38: * @param iterator contains instances of SelectItem
39: * @return if the value of a selectitem is equal to the given value
40: */
41: public static boolean matchValue(FacesContext context,
42: Object value, Iterator<SelectItem> selectItemsIter,
43: _ValueConverter converter) {
44: while (selectItemsIter.hasNext()) {
45: SelectItem item = selectItemsIter.next();
46: if (item instanceof SelectItemGroup) {
47: SelectItemGroup itemgroup = (SelectItemGroup) item;
48: SelectItem[] selectItems = itemgroup.getSelectItems();
49: if (selectItems != null
50: && selectItems.length > 0
51: && matchValue(context, value, Arrays.asList(
52: selectItems).iterator(), converter)) {
53: return true;
54: }
55: } else {
56: Object itemValue = item.getValue();
57: if (converter != null && itemValue instanceof String) {
58: itemValue = converter.getConvertedValue(context,
59: (String) itemValue);
60: }
61: if (value == itemValue || value.equals(itemValue)) {
62: return true;
63: }
64: }
65: }
66: return false;
67: }
68: }
|