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 javax.el.ValueExpression;
22  import javax.faces.el.ValueBinding;
23  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
24  
25  /**
26   * A component that allows the user to select or unselect an object.
27   * <p>
28   * This can also be used to choose between two states such as true/false or on/off.
29   * </p>
30   * <p>
31   * See the javadoc for this class in the
32   * <a href="http://java.sun.com/j2ee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
33   * for further details.
34   * </p>
35   */
36  @JSFComponent(defaultRendererType = "javax.faces.Checkbox")
37  public class UISelectBoolean extends UIInput
38  {
39      public static final String COMPONENT_TYPE = "javax.faces.SelectBoolean";
40      public static final String COMPONENT_FAMILY = "javax.faces.SelectBoolean";
41  
42      public UISelectBoolean()
43      {
44          setRendererType("javax.faces.Checkbox");
45      }
46  
47      @Override
48      public String getFamily()
49      {
50          return COMPONENT_FAMILY;
51      }
52  
53      public void setSelected(boolean selected)
54      {
55          setValue(Boolean.valueOf(selected));
56      }
57  
58      public boolean isSelected()
59      {
60          Boolean value = (Boolean) getSubmittedValue();
61          if (value == null)
62          {
63              value = (Boolean) getValue();
64          }
65  
66          return value != null ? value.booleanValue() : false;
67      }
68  
69      /**
70       * @deprecated Use getValueExpression instead
71       */
72      public ValueBinding getValueBinding(String name)
73      {
74          if (name == null)
75          {
76              throw new NullPointerException("name");
77          }
78          if (name.equals("selected"))
79          {
80              return super.getValueBinding("value");
81          }
82          else
83          {
84              return super.getValueBinding(name);
85          }
86      }
87  
88      /**
89       * @deprecated Use setValueExpression instead
90       */
91      public void setValueBinding(String name, ValueBinding binding)
92      {
93          if (name == null)
94          {
95              throw new NullPointerException("name");
96          }
97          if (name.equals("selected"))
98          {
99              super.setValueBinding("value", binding);
100         }
101         else
102         {
103             super.setValueBinding(name, binding);
104         }
105     }
106 
107     public ValueExpression getValueExpression(String name)
108     {
109         if (name == null)
110         {
111             throw new NullPointerException("name");
112         }
113         if (name.equals("selected"))
114         {
115             return super.getValueExpression("value");
116         }
117         else
118         {
119             return super.getValueExpression(name);
120         }
121     }
122 
123     public void setValueExpression(String name, ValueExpression binding)
124     {
125         if (name == null)
126         {
127             throw new NullPointerException("name");
128         }
129         if (name.equals("selected"))
130         {
131             super.setValueExpression("value", binding);
132         }
133         else
134         {
135             super.setValueExpression(name, binding);
136         }
137     }
138 }