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