Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UISelectOne |
|
| 4.0;4 |
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.ArrayList; | |
22 | import java.util.Collection; | |
23 | import java.util.Iterator; | |
24 | import javax.faces.context.FacesContext; | |
25 | import javax.faces.convert.Converter; | |
26 | import javax.faces.model.SelectItem; | |
27 | ||
28 | import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent; | |
29 | import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty; | |
30 | ||
31 | /** | |
32 | * Component for choosing one option out of a set of possibilities. | |
33 | * <p> | |
34 | * This component is expected to have children of type UISelectItem or UISelectItems; these define the set of possible | |
35 | * options that the user can choose from. | |
36 | * </p> | |
37 | * <p> | |
38 | * See the javadoc for this class in the <a | |
39 | * href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a> for further details. | |
40 | * </p> | |
41 | */ | |
42 | @JSFComponent(defaultRendererType = "javax.faces.Menu") | |
43 | @JSFJspProperty(name="hideNoSelectionOption", returnType="boolean") | |
44 | public class UISelectOne extends UIInput | |
45 | { | |
46 | public static final String COMPONENT_TYPE = "javax.faces.SelectOne"; | |
47 | public static final String COMPONENT_FAMILY = "javax.faces.SelectOne"; | |
48 | ||
49 | public static final String INVALID_MESSAGE_ID = "javax.faces.component.UISelectOne.INVALID"; | |
50 | ||
51 | public UISelectOne() | |
52 | 22 | { |
53 | 22 | setRendererType("javax.faces.Menu"); |
54 | 22 | } |
55 | ||
56 | @Override | |
57 | public String getFamily() | |
58 | { | |
59 | 0 | return COMPONENT_FAMILY; |
60 | } | |
61 | ||
62 | /** | |
63 | * Verify that the result of converting the newly submitted value is <i>equal</i> to the value property of one of | |
64 | * the child SelectItem objects. If this is not true, a validation error is reported. | |
65 | * | |
66 | * @see javax.faces.component.UIInput#validateValue(javax.faces.context.FacesContext,java.lang.Object) | |
67 | */ | |
68 | @Override | |
69 | protected void validateValue(FacesContext context, Object value) | |
70 | { | |
71 | 10 | super.validateValue(context, value); |
72 | ||
73 | 10 | if (!isValid() || value == null) |
74 | { | |
75 | 2 | return; |
76 | } | |
77 | ||
78 | // selected value must match to one of the available options | |
79 | // and if required is true it must not match an option with noSelectionOption set to true (since 2.0) | |
80 | 8 | Converter converter = getConverter(); |
81 | ||
82 | // Since the iterator is used twice, it has sense to traverse it only once. | |
83 | 8 | Collection<SelectItem> items = new ArrayList<SelectItem>(); |
84 | 8 | for (Iterator<SelectItem> iter = new _SelectItemsIterator(this, context); iter.hasNext();) |
85 | { | |
86 | 24 | items.add(iter.next()); |
87 | } | |
88 | ||
89 | 8 | if (_SelectItemsUtil.matchValue(context, this, value, items.iterator(), converter)) |
90 | { | |
91 | 6 | if (! this.isRequired()) |
92 | { | |
93 | 4 | return; // Matched & Required false, so return ok. |
94 | } | |
95 | 2 | if (! _SelectItemsUtil.isNoSelectionOption(context, this, value, |
96 | items.iterator(), converter)) | |
97 | { | |
98 | 0 | return; // Matched & Required true & No-selection did NOT match, so return ok. |
99 | } | |
100 | } | |
101 | 4 | _MessageUtils.addErrorMessage(context, this, INVALID_MESSAGE_ID, |
102 | new Object[] {_MessageUtils.getLabel(context, this) }); | |
103 | 4 | setValid(false); |
104 | 4 | } |
105 | } |