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.faces.context.FacesContext;
22  import javax.faces.el.ValueBinding;
23  
24  /**
25   * A component representing a single option that the user can choose.
26   * <p>
27   * The option attributes can either be defined directly on this component
28   * (via the itemValue, itemLabel, itemDescription properties) or the value
29   * property can reference a SelectItem object (directly or via an EL expression).
30   * </p><p>
31   * The value expression (if defined) is read-only; the parent select component
32   * will have a value attribute specifying where the value for the chosen
33   * selection will be stored.
34   * </p>
35   * See Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
36   *
37   * @JSFComponent
38   *   name = "f:selectItem"
39   *   bodyContent = "empty"
40   *   tagClass = "org.apache.myfaces.taglib.core.SelectItemTag"
41   *   desc = "UISelectItem"
42   *
43   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
44   * @version $Revision: 684004 $ $Date: 2008-08-08 10:53:07 -0500 (Fri, 08 Aug 2008) $
45   */
46  public class UISelectItem
47          extends UIComponentBase
48  {
49      //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
50  
51      public static final String COMPONENT_TYPE = "javax.faces.SelectItem";
52      public static final String COMPONENT_FAMILY = "javax.faces.SelectItem";
53      private static final boolean DEFAULT_ITEMDISABLED = false;
54  
55      private String _itemDescription = null;
56      private Boolean _itemDisabled = null;
57      private String _itemLabel = null;
58      private Object _itemValue = null;
59      private Object _value = null;
60  
61      public UISelectItem()
62      {
63      }
64  
65      public String getFamily()
66      {
67          return COMPONENT_FAMILY;
68      }
69      
70      /**
71       * Disable this property; although this class extends a base-class that
72       * defines a read/write rendered property, this particular subclass
73       * does not support setting it. Yes, this is broken OO design: direct
74       * all complaints to the JSF spec group.
75       *
76       * @JSFProperty tagExcluded="true"
77       */
78      public void setRendered(boolean state) {
79         //throw new UnsupportedOperationException();
80          //Restored due to compatibility with TCK tests.
81          super.setRendered(state);
82      }
83  
84      public boolean isRendered() {
85          //return true;
86          //Restored due to compatibility with TCK tests.
87          return super.isRendered();
88      }
89  
90      public void setItemDescription(String itemDescription)
91      {
92          _itemDescription = itemDescription;
93      }
94  
95      /**
96       * An optional description for this item.
97       * For use in development tools.
98       * 
99       * @JSFProperty 
100      */
101     public String getItemDescription()
102     {
103         //Q: what use is an EL expression for this???
104         if (_itemDescription != null) return _itemDescription;
105         ValueBinding vb = getValueBinding("itemDescription");
106         return vb != null ? _ComponentUtils.getStringValue(getFacesContext(), vb) : null;
107     }
108 
109     /**
110      * When true, this item cannot be chosen by the user. If this method is
111      * ever called, then any EL-binding for the disabled property will be
112      * ignored.
113      */
114     public void setItemDisabled(boolean itemDisabled)
115     {
116         _itemDisabled = Boolean.valueOf(itemDisabled);
117     }
118 
119     /**
120      * Determine whether this item can be chosen by the user.
121      * 
122      * @JSFProperty
123      */
124     public boolean isItemDisabled()
125     {
126         if (_itemDisabled != null) return _itemDisabled.booleanValue();
127         ValueBinding vb = getValueBinding("itemDisabled");
128         Boolean v = vb != null ? (Boolean)vb.getValue(getFacesContext()) : null;
129         return v != null ? v.booleanValue() : DEFAULT_ITEMDISABLED;
130     }
131 
132     public void setItemLabel(String itemLabel)
133     {
134         _itemLabel = itemLabel;
135     }
136 
137     /**
138      * Get the string which will be presented to the user for this option.
139      * 
140      * @JSFProperty
141      */
142     public String getItemLabel()
143     {
144         if (_itemLabel != null) return _itemLabel;
145         ValueBinding vb = getValueBinding("itemLabel");
146         return vb != null ? _ComponentUtils.getStringValue(getFacesContext(), vb) : null;
147     }
148 
149     public void setItemValue(Object itemValue)
150     {
151         _itemValue = itemValue;
152     }
153 
154     /**
155      * The value of this item, of the same type as the parent component's value.
156      * 
157      * @JSFProperty
158      */
159     public Object getItemValue()
160     {
161         if (_itemValue != null) return _itemValue;
162         ValueBinding vb = getValueBinding("itemValue");
163         return vb != null ? vb.getValue(getFacesContext()) : null;
164     }
165 
166     public void setValue(Object value)
167     {
168         _value = value;
169     }
170 
171     /**
172      * An EL expression that refers to a javax.faces.model.SelectItem instance.
173      * 
174      * @JSFProperty
175      */
176     public Object getValue()
177     {
178         if (_value != null) return _value;
179         ValueBinding vb = getValueBinding("value");
180         return vb != null ? vb.getValue(getFacesContext()) : null;
181     }
182 
183 
184     public Object saveState(FacesContext context)
185     {
186         Object values[] = new Object[6];
187         values[0] = super.saveState(context);
188         values[1] = _itemDescription;
189         values[2] = _itemDisabled;
190         values[3] = _itemLabel;
191         values[4] = _itemValue;
192         values[5] = _value;
193         return values;
194     }
195 
196     public void restoreState(FacesContext context, Object state)
197     {
198         Object values[] = (Object[])state;
199         super.restoreState(context, values[0]);
200         _itemDescription = (String)values[1];
201         _itemDisabled = (Boolean)values[2];
202         _itemLabel = (String)values[3];
203         _itemValue = values[4];
204         _value = values[5];
205     }
206     //------------------ GENERATED CODE END ---------------------------------------
207 }