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