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