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  
20  package org.apache.myfaces.tobago.internal.taglib.extension;
21  
22  import org.apache.myfaces.tobago.apt.annotation.DynamicExpression;
23  import org.apache.myfaces.tobago.apt.annotation.ExtensionTag;
24  import org.apache.myfaces.tobago.apt.annotation.Tag;
25  import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
26  import org.apache.myfaces.tobago.apt.annotation.UIComponentTagAttribute;
27  import org.apache.myfaces.tobago.internal.taglib.SelectOneChoiceTag;
28  
29  import javax.el.MethodExpression;
30  import javax.el.ValueExpression;
31  import javax.servlet.jsp.JspException;
32  
33  /**
34   * Render a single selection dropdown list with a label.
35   */
36  
37  @Tag(name = "selectOneChoice")
38  @ExtensionTag(
39      baseClassName = "org.apache.myfaces.tobago.internal.taglib.SelectOneChoiceTag",
40      faceletHandler = "org.apache.myfaces.tobago.facelets.extension.SelectOneChoiceExtensionHandler")
41  public class SelectOneChoiceExtensionTag extends TobagoExtensionBodyTagSupport {
42  
43    private ValueExpression required;
44    private ValueExpression value;
45    private MethodExpression valueChangeListener;
46    private ValueExpression disabled;
47    private ValueExpression readonly;
48    private ValueExpression onchange;
49    private ValueExpression label;
50    private ValueExpression accessKey;
51    private ValueExpression rendered;
52    private ValueExpression binding;
53    private ValueExpression tip;
54    private MethodExpression validator;
55    private ValueExpression converter;
56    private ValueExpression labelWidth;
57    private ValueExpression tabIndex;
58    private ValueExpression focus;
59    private ValueExpression markup;
60    private ValueExpression validatorMessage;
61    private ValueExpression converterMessage;
62    private ValueExpression requiredMessage;
63    private String fieldId;
64  
65    private LabelExtensionTag labelTag;
66    private SelectOneChoiceTag selectOneChoiceTag;
67  
68    @Override
69    public int doStartTag() throws JspException {
70  
71      labelTag = new LabelExtensionTag();
72      labelTag.setPageContext(pageContext);
73      if (id != null) {
74        labelTag.setId(id);
75      }
76      if (label != null) {
77        labelTag.setValue(label);
78      }
79      if (accessKey != null) {
80        labelTag.setAccessKey(accessKey);
81      }
82      if (tip != null) {
83        labelTag.setTip(tip);
84      }
85      if (rendered != null) {
86        labelTag.setRendered(rendered);
87      }
88      if (labelWidth != null) {
89        labelTag.setColumns(createStringValueExpression(labelWidth.getExpressionString() + ";*"));
90      }
91      if (markup != null) {
92        labelTag.setMarkup(markup);
93      }
94      labelTag.setParent(getParent());
95      labelTag.setJspId(nextJspId());
96      labelTag.doStartTag();
97  
98      selectOneChoiceTag = new SelectOneChoiceTag();
99      selectOneChoiceTag.setPageContext(pageContext);
100     if (value != null) {
101       selectOneChoiceTag.setValue(value);
102     }
103     if (valueChangeListener != null) {
104       selectOneChoiceTag.setValueChangeListener(valueChangeListener);
105     }
106     if (validator != null) {
107       selectOneChoiceTag.setValidator(validator);
108     }
109     if (converter != null) {
110       selectOneChoiceTag.setConverter(converter);
111     }
112     if (binding != null) {
113       selectOneChoiceTag.setBinding(binding);
114     }
115     if (onchange != null) {
116       selectOneChoiceTag.setOnchange(onchange);
117     }
118     if (disabled != null) {
119       selectOneChoiceTag.setDisabled(disabled);
120     }
121     if (markup != null) {
122       selectOneChoiceTag.setMarkup(markup);
123     }
124     if (focus != null) {
125       selectOneChoiceTag.setFocus(focus);
126     }
127     if (fieldId != null) {
128       selectOneChoiceTag.setId(fieldId);
129     }
130     if (label != null) {
131       selectOneChoiceTag.setLabel(label);
132     }
133     if (readonly != null) {
134       selectOneChoiceTag.setReadonly(readonly);
135     }
136     if (required != null) {
137       selectOneChoiceTag.setRequired(required);
138     }
139     if (tabIndex != null) {
140       selectOneChoiceTag.setTabIndex(tabIndex);
141     }
142     if (validatorMessage != null) {
143       selectOneChoiceTag.setValidatorMessage(validatorMessage);
144     }
145     if (converterMessage != null) {
146       selectOneChoiceTag.setConverterMessage(converterMessage);
147     }
148     if (requiredMessage != null) {
149       selectOneChoiceTag.setRequiredMessage(requiredMessage);
150     }
151     selectOneChoiceTag.setParent(labelTag);
152     selectOneChoiceTag.setJspId(nextJspId());
153     selectOneChoiceTag.doStartTag();
154 
155     return super.doStartTag();
156   }
157 
158   @Override
159   public int doEndTag() throws JspException {
160     selectOneChoiceTag.doEndTag();
161     labelTag.doEndTag();
162     return super.doEndTag();
163   }
164 
165   @Override
166   public void release() {
167     super.release();
168     binding = null;
169     onchange = null;
170     disabled = null;
171     label = null;
172     accessKey = null;
173     labelWidth = null;
174     converter = null;
175     validator = null;
176     readonly = null;
177     rendered = null;
178     required = null;
179     tip = null;
180     value = null;
181     valueChangeListener = null;
182     tabIndex = null;
183     selectOneChoiceTag = null;
184     labelTag = null;
185     focus = null;
186     markup = null;
187     validatorMessage = null;
188     converterMessage = null;
189     requiredMessage = null;
190     fieldId = null;
191   }
192 
193   /**
194    * Flag indicating that a value is required.
195    * If the value is an empty string a
196    * ValidationError occurs and a Error Message is rendered.
197    */
198   @TagAttribute
199   @UIComponentTagAttribute(type = "boolean", defaultValue = "false")
200   public void setRequired(final ValueExpression required) {
201     this.required = required;
202   }
203 
204   /**
205    * The current value of this component.
206    */
207   @TagAttribute
208   @UIComponentTagAttribute(type = "java.lang.Object")
209   public void setValue(final ValueExpression value) {
210     this.value = value;
211   }
212 
213   /**
214    * MethodBinding representing a value change listener method
215    * that will be notified when a new value has been set for this input component.
216    * The expression must evaluate to a public method that takes a ValueChangeEvent
217    * parameter, with a return type of void.
218    *
219    * @param valueChangeListener
220    */
221   @TagAttribute
222   @UIComponentTagAttribute(
223           type = {},
224           expression = DynamicExpression.METHOD_EXPRESSION_REQUIRED,
225           methodSignature = "javax.faces.event.ValueChangeEvent")
226   public void setValueChangeListener(final MethodExpression valueChangeListener) {
227     this.valueChangeListener = valueChangeListener;
228   }
229 
230   /**
231    * A method binding EL expression,
232    * accepting FacesContext, UIComponent,
233    * and Object parameters, and returning void, that validates
234    * the component's local value.
235    */
236   @TagAttribute
237   @UIComponentTagAttribute(type = {},
238       expression = DynamicExpression.METHOD_EXPRESSION,
239       methodSignature = { "javax.faces.context.FacesContext", "javax.faces.component.UIComponent", "java.lang.Object" })
240   public void setValidator(final MethodExpression validator) {
241     this.validator = validator;
242   }
243 
244   /**
245    * Flag indicating that this element is disabled.
246    */
247   @TagAttribute()
248   @UIComponentTagAttribute(type = "boolean", defaultValue = "false")
249   public void setDisabled(final ValueExpression disabled) {
250     this.disabled = disabled;
251   }
252 
253   /**
254    * Flag indicating that this component will prohibit changes by the user.
255    */
256   @TagAttribute
257   @UIComponentTagAttribute(type = "boolean", defaultValue = "false")
258   public void setReadonly(final ValueExpression readonly) {
259     this.readonly = readonly;
260   }
261 
262   /**
263    * Clientside script function to add to this component's onchange handler.
264    */
265   @TagAttribute
266   @UIComponentTagAttribute()
267   public void setOnchange(final ValueExpression onchange) {
268     this.onchange = onchange;
269   }
270 
271   /**
272    * An expression that specifies the Converter for this component.
273    * If the value binding expression is a String,
274    * the String is used as an ID to look up a Converter.
275    * If the value binding expression is a Converter,
276    * uses that instance as the converter.
277    * The value can either be a static value (ID case only)
278    * or an EL expression.
279    */
280   @TagAttribute
281   @UIComponentTagAttribute(type = "javax.faces.convert.Converter",
282       expression = DynamicExpression.VALUE_EXPRESSION)
283   public void setConverter(final ValueExpression converter) {
284     this.converter = converter;
285   }
286 
287   /**
288    * Text value to display as label.
289    * If text contains an underscore the next character is used as accesskey.
290    */
291   @TagAttribute
292   @UIComponentTagAttribute()
293   public void setLabel(final ValueExpression label) {
294     this.label = label;
295   }
296 
297   /**
298    * The accessKey of this component.
299    */
300   @TagAttribute
301   @UIComponentTagAttribute(type = "java.lang.Character")
302   public void setAccessKey(final javax.el.ValueExpression accessKey) {
303     this.accessKey = accessKey;
304   }
305 
306   /**
307    * Flag indicating whether or not this component should be rendered
308    * (during Render Response Phase), or processed on any subsequent form submit.
309    */
310   @TagAttribute
311   @UIComponentTagAttribute(type = "boolean", defaultValue = "true")
312   public void setRendered(final ValueExpression rendered) {
313     this.rendered = rendered;
314   }
315 
316   /**
317    * The value binding expression linking this
318    * component to a property in a backing bean.
319    */
320   @TagAttribute
321   @UIComponentTagAttribute(type = "javax.faces.component.UIComponent")
322   public void setBinding(final ValueExpression binding) {
323     this.binding = binding;
324   }
325 
326   /**
327    * Text value to display as tooltip.
328    */
329   @TagAttribute
330   @UIComponentTagAttribute()
331   public void setTip(final ValueExpression tip) {
332     this.tip = tip;
333   }
334 
335   /**
336    * The width for the label component. Default: 'auto'.
337    * This value is used in the gridLayouts columns attribute.
338    * See gridLayout tag for valid values.
339    */
340   @TagAttribute
341   @UIComponentTagAttribute()
342   public void setLabelWidth(final ValueExpression labelWidth) {
343     this.labelWidth = labelWidth;
344   }
345 
346   @TagAttribute
347   @UIComponentTagAttribute(type = "java.lang.Integer")
348   public void setTabIndex(final ValueExpression tabIndex) {
349     this.tabIndex = tabIndex;
350   }
351 
352   /**
353    * Flag indicating this component should receive the focus.
354    */
355   @TagAttribute
356   @UIComponentTagAttribute(type = "boolean", defaultValue = "false")
357   public void setFocus(final ValueExpression focus) {
358     this.focus = focus;
359   }
360 
361   /**
362    * Indicate markup of this component.
363    * Possible value is 'none'. But this can be overridden in the theme.
364    */
365   @TagAttribute
366   @UIComponentTagAttribute(defaultValue = "none", type = "java.lang.String[]")
367   public void setMarkup(final ValueExpression markup) {
368     this.markup = markup;
369   }
370 
371   /**
372    * An expression that specifies the validator message
373    */
374   @TagAttribute
375   @UIComponentTagAttribute()
376   public void setValidatorMessage(final ValueExpression validatorMessage) {
377     this.validatorMessage = validatorMessage;
378   }
379 
380   /**
381    * An expression that specifies the converter message
382    */
383   @TagAttribute
384   @UIComponentTagAttribute()
385   public void setConverterMessage(final ValueExpression converterMessage) {
386     this.converterMessage = converterMessage;
387   }
388 
389   /**
390    * An expression that specifies the required message
391    */
392   @TagAttribute
393   @UIComponentTagAttribute()
394   public void setRequiredMessage(final ValueExpression requiredMessage) {
395     this.requiredMessage = requiredMessage;
396   }
397 
398   /**
399    * The component identifier for the input field component inside of the container.
400    * This value must be unique within the closest parent component that is a naming container.
401    */
402   @TagAttribute(rtexprvalue = true)
403   @UIComponentTagAttribute
404   public void setFieldId(final String fieldId) {
405     this.fieldId = fieldId;
406   }
407 
408   /**
409    * The component identifier for this component.
410    * This value must be unique within the closest parent component that is a naming container.
411    * For tx components the id will be set to the container (e. g. the panel).
412    * To set the id of the input field, you have to use the attribute "fieldId".
413    */
414   @TagAttribute(rtexprvalue = true)
415   @UIComponentTagAttribute
416   public void setId(final String id) {
417     super.setId(id);
418   }
419 }