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