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(baseClassName = "org.apache.myfaces.tobago.internal.taglib.LabelTag")
66  public class LabelExtensionTag extends TobagoExtensionBodyTagSupport {
67  
68    private static final Logger LOG = LoggerFactory.getLogger(LabelExtensionTag.class);
69  
70    public static final String DEFAULT_COLUMNS = "auto;*";
71  
72    private javax.el.ValueExpression value;
73    private javax.el.ValueExpression tip;
74    private javax.el.ValueExpression rendered;
75    private javax.el.ValueExpression columns;
76    private String rows = "auto";
77    private javax.el.ValueExpression labelWidth;
78    private javax.el.ValueExpression markup;
79  
80    private ExtensionPanelTag panelTag;
81  
82    @Override
83    public int doStartTag() throws JspException {
84  
85      panelTag = new ExtensionPanelTag();
86      panelTag.setPageContext(pageContext);
87      panelTag.setParent(getParent());
88      if (rendered != null) {
89        panelTag.setRendered(rendered);
90      }
91      if (tip != null) {
92        panelTag.setTip(tip);
93      }
94      if (id != null) {
95        panelTag.setId(id);
96      }
97      panelTag.setJspId(nextJspId());
98      panelTag.doStartTag();
99  
100     FacetTag facetTag = new FacetTag();
101     facetTag.setPageContext(pageContext);
102     facetTag.setName(Facets.LAYOUT);
103     facetTag.setParent(panelTag);
104     facetTag.doStartTag();
105 
106     if (columns == null) {
107       if (labelWidth != null) {
108         setColumns(createStringValueExpression(labelWidth.getExpressionString() + ";*"));
109       } else {
110         setColumns(createStringValueExpression(DEFAULT_COLUMNS));
111       }
112     }
113     GridLayoutTag gridLayoutTag = new GridLayoutTag();
114     gridLayoutTag.setPageContext(pageContext);
115     gridLayoutTag.setColumns(columns);
116     javax.el.ValueExpression ve = createStringValueExpression(rows);
117     gridLayoutTag.setRows(ve);
118     gridLayoutTag.setParent(facetTag);
119     gridLayoutTag.setJspId(nextJspId());
120     gridLayoutTag.doStartTag();
121     gridLayoutTag.doEndTag();
122 
123     facetTag.doEndTag();
124 
125     LabelTag labelTag = new LabelTag();
126     labelTag.setPageContext(pageContext);
127     if (value != null) {
128       labelTag.setValue(value);
129     }
130     if (markup != null) {
131       labelTag.setMarkup(markup);
132     }
133     labelTag.setFor("@auto");
134     labelTag.setParent(panelTag);
135     labelTag.setJspId(nextJspId());
136     labelTag.doStartTag();
137     labelTag.doEndTag();
138 
139     return super.doStartTag();
140   }
141 
142   @Override
143   public int doEndTag() throws JspException {
144     panelTag.doEndTag();
145     return super.doEndTag();
146   }
147 
148   @Override
149   public void release() {
150     super.release();
151     value = null;
152     tip = null;
153     rendered = null;
154     columns = null;
155     rows = "auto";
156     panelTag = null;
157     labelWidth = null;
158     markup = null;
159   }
160 
161    /**
162    * The current value of this component.
163    */
164   @TagAttribute
165   @UIComponentTagAttribute(type = "java.lang.Object")
166   public void setValue(javax.el.ValueExpression value) {
167     this.value = value;
168   }
169 
170   /**
171    * Text value to display as tooltip.
172    */
173   @TagAttribute
174   @UIComponentTagAttribute()
175   public void setTip(javax.el.ValueExpression tip) {
176     this.tip = tip;
177   }
178 
179   /**
180    * Flag indicating whether or not this component should be rendered
181    * (during Render Response Phase), or processed on any subsequent form submit.
182    */
183   @TagAttribute
184   @UIComponentTagAttribute(type = "boolean", defaultValue = "true")
185   public void setRendered(javax.el.ValueExpression rendered) {
186     this.rendered = rendered;
187   }
188 
189   void setColumns(javax.el.ValueExpression columns) {
190     if (!(!columns.isLiteralText() || LayoutUtils.checkTokens(columns.getExpressionString()))) {
191       LOG.warn("Illegal value for columns = \"" + columns.getExpressionString()
192           + "\" replacing with default: \"" + DEFAULT_COLUMNS + "\"");
193       this.columns = createStringValueExpression(DEFAULT_COLUMNS);
194     } else {
195       this.columns = columns;
196     }
197   }
198 
199   void setRows(String rows) {
200     this.rows = rows;
201   }
202 
203    /**
204    * The width for the label component. Default: 'auto'.
205    * This value is used in the gridLayouts columns attribute.
206    * See gridLayout tag for valid values.
207    */
208   @TagAttribute
209   @UIComponentTagAttribute()
210   public void setLabelWidth(ValueExpression labelWidth) {
211     this.labelWidth = labelWidth;
212   }
213 
214  /**
215    * Indicate markup of this component.
216    * Possible value is 'none'. But this can be overridden in the theme.
217    */
218   @TagAttribute
219   @UIComponentTagAttribute(defaultValue = "none", type = "java.lang.String[]")
220   public void setMarkup(javax.el.ValueExpression markup) {
221     this.markup = markup;
222   }
223 }