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.ExtensionTag;
23  import org.apache.myfaces.tobago.apt.annotation.Tag;
24  import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
25  import org.apache.myfaces.tobago.apt.annotation.UIComponentTagAttribute;
26  import org.apache.myfaces.tobago.component.Facets;
27  import org.apache.myfaces.tobago.internal.layout.LayoutUtils;
28  import org.apache.myfaces.tobago.internal.taglib.GridLayoutTag;
29  import org.apache.myfaces.tobago.internal.taglib.LabelTag;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import javax.el.ValueExpression;
34  import javax.faces.webapp.FacetTag;
35  import javax.servlet.jsp.JspException;
36  
37  /**
38   * Renders a label to any component.
39   * <br />
40   * Short syntax of:
41   * <br />
42   * <pre>
43   * &lt;tc:panel>
44   *   &lt;f:facet name="layout">
45   *     &lt;tc:gridLayout columns="auto;*"/>
46   *   &lt;/f:facet>
47   *   &lt;tc:label value="#{label}" for="@auto"/>
48   *     ...
49   * &lt;/tc:panel>
50   * </pre>
51   * This is the universal version of the special versions: &lt;tx:in>, etc.
52   * In other words:
53   * <pre>
54   * &lt;tx:label>
55   *   &lt;tc:in/>
56   * &lt;/tx:label>
57   * </pre>
58   * does the same like
59   * <pre>
60   *   &lt;tx:in/>
61   * </pre>
62   */
63  
64  @Tag(name = "label")
65  @ExtensionTag(
66      baseClassName = "org.apache.myfaces.tobago.internal.taglib.LabelTag",
67      faceletHandler = "org.apache.myfaces.tobago.facelets.extension.LabelExtensionHandler")
68  public class LabelExtensionTag extends TobagoExtensionBodyTagSupport {
69  
70    private static final Logger LOG = LoggerFactory.getLogger(LabelExtensionTag.class);
71  
72    public static final String DEFAULT_COLUMNS = "auto;*";
73  
74    private javax.el.ValueExpression value;
75    private javax.el.ValueExpression accessKey;
76    private javax.el.ValueExpression tip;
77    private javax.el.ValueExpression rendered;
78    private javax.el.ValueExpression columns;
79    private String rows = "auto";
80    private javax.el.ValueExpression labelWidth;
81    private javax.el.ValueExpression markup;
82  
83    private ExtensionPanelTag panelTag;
84  
85    @Override
86    public int doStartTag() throws JspException {
87  
88      panelTag = new ExtensionPanelTag();
89      panelTag.setPageContext(pageContext);
90      panelTag.setParent(getParent());
91      if (rendered != null) {
92        panelTag.setRendered(rendered);
93      }
94      if (tip != null) {
95        panelTag.setTip(tip);
96      }
97      if (id != null) {
98        panelTag.setId(id);
99      }
100     panelTag.setJspId(nextJspId());
101     panelTag.doStartTag();
102 
103     final FacetTag facetTag = new FacetTag();
104     facetTag.setPageContext(pageContext);
105     facetTag.setName(Facets.LAYOUT);
106     facetTag.setParent(panelTag);
107     facetTag.doStartTag();
108 
109     if (columns == null) {
110       if (labelWidth != null) {
111         setColumns(createStringValueExpression(labelWidth.getExpressionString() + ";*"));
112       } else {
113         setColumns(createStringValueExpression(DEFAULT_COLUMNS));
114       }
115     }
116     final GridLayoutTag gridLayoutTag = new GridLayoutTag();
117     gridLayoutTag.setPageContext(pageContext);
118     gridLayoutTag.setColumns(columns);
119     final javax.el.ValueExpression ve = createStringValueExpression(rows);
120     gridLayoutTag.setRows(ve);
121     gridLayoutTag.setParent(facetTag);
122     gridLayoutTag.setJspId(nextJspId());
123     gridLayoutTag.doStartTag();
124     gridLayoutTag.doEndTag();
125 
126     facetTag.doEndTag();
127 
128     final LabelTag labelTag = new LabelTag();
129     labelTag.setPageContext(pageContext);
130     if (value != null) {
131       labelTag.setValue(value);
132     }
133     if (accessKey != null) {
134       labelTag.setAccessKey(accessKey);
135     }
136     if (markup != null) {
137       labelTag.setMarkup(markup);
138     }
139     labelTag.setFor("@auto");
140     labelTag.setParent(panelTag);
141     labelTag.setJspId(nextJspId());
142     labelTag.doStartTag();
143     labelTag.doEndTag();
144 
145     return super.doStartTag();
146   }
147 
148   @Override
149   public int doEndTag() throws JspException {
150     panelTag.doEndTag();
151     return super.doEndTag();
152   }
153 
154   @Override
155   public void release() {
156     super.release();
157     value = null;
158     accessKey = null;
159     tip = null;
160     rendered = null;
161     columns = null;
162     rows = "auto";
163     panelTag = null;
164     labelWidth = null;
165     markup = null;
166   }
167 
168    /**
169    * The current value of this component.
170    */
171   @TagAttribute
172   @UIComponentTagAttribute(type = "java.lang.Object")
173   public void setValue(final javax.el.ValueExpression value) {
174     this.value = value;
175   }
176 
177    /**
178    * The accessKey of this component.
179    */
180   @TagAttribute
181   @UIComponentTagAttribute(type = "java.lang.Character")
182   public void setAccessKey(final javax.el.ValueExpression accessKey) {
183     this.accessKey = accessKey;
184   }
185 
186   /**
187    * Text value to display as tooltip.
188    */
189   @TagAttribute
190   @UIComponentTagAttribute()
191   public void setTip(final javax.el.ValueExpression tip) {
192     this.tip = tip;
193   }
194 
195   /**
196    * Flag indicating whether or not this component should be rendered
197    * (during Render Response Phase), or processed on any subsequent form submit.
198    */
199   @TagAttribute
200   @UIComponentTagAttribute(type = "boolean", defaultValue = "true")
201   public void setRendered(final javax.el.ValueExpression rendered) {
202     this.rendered = rendered;
203   }
204 
205   void setColumns(final javax.el.ValueExpression columns) {
206     if (!(!columns.isLiteralText() || LayoutUtils.checkTokens(columns.getExpressionString()))) {
207       LOG.warn("Illegal value for columns = \"" + columns.getExpressionString()
208           + "\" replacing with default: \"" + DEFAULT_COLUMNS + "\"");
209       this.columns = createStringValueExpression(DEFAULT_COLUMNS);
210     } else {
211       this.columns = columns;
212     }
213   }
214 
215   void setRows(final String rows) {
216     this.rows = rows;
217   }
218 
219    /**
220    * The width for the label component. Default: 'auto'.
221    * This value is used in the gridLayouts columns attribute.
222    * See gridLayout tag for valid values.
223    */
224   @TagAttribute
225   @UIComponentTagAttribute()
226   public void setLabelWidth(final ValueExpression labelWidth) {
227     this.labelWidth = labelWidth;
228   }
229 
230  /**
231    * Indicate markup of this component.
232    * Possible value is 'none'. But this can be overridden in the theme.
233    */
234   @TagAttribute
235   @UIComponentTagAttribute(defaultValue = "none", type = "java.lang.String[]")
236   public void setMarkup(final javax.el.ValueExpression markup) {
237     this.markup = markup;
238   }
239 }