001    package org.apache.myfaces.tobago.taglib.extension;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.myfaces.tobago.apt.annotation.ExtensionTag;
023    import org.apache.myfaces.tobago.apt.annotation.Tag;
024    import org.apache.myfaces.tobago.taglib.component.GridLayoutTag;
025    import org.apache.myfaces.tobago.taglib.component.LabelTag;
026    import org.apache.myfaces.tobago.taglib.component.PanelTag;
027    import org.apache.myfaces.tobago.taglib.decl.HasTip;
028    import org.apache.myfaces.tobago.taglib.decl.HasValue;
029    import org.apache.myfaces.tobago.util.LayoutUtil;
030    
031    import javax.faces.webapp.FacetTag;
032    import javax.faces.webapp.UIComponentTag;
033    import javax.servlet.jsp.JspException;
034    import javax.servlet.jsp.tagext.BodyTagSupport;
035    
036    import static org.apache.myfaces.tobago.TobagoConstants.FACET_LAYOUT;
037    
038    /**
039     * Renders a label to any component.
040     * <br />
041     * Short syntax of:
042     * <br />
043     * <pre>
044     * &lt;tc:panel>
045     *   &lt;f:facet name="layout">
046     *     &lt;tc:gridLayout columns="fixed;*"/>
047     *   &lt;/f:facet>
048     *   &lt;tc:label value="#{label}" for="@auto"/>
049     *     ...
050     * &lt;/tc:panel>
051     * </pre>
052     * This is the universal version of the special versions: &lt;tx:in>, etc.
053     * In other words:
054     * <pre>
055     * &lt;tx:label>
056     *   &lt;tc:in/>
057     * &lt;/tx:label>
058     * </pre>
059     * does the same like
060     * <pre>
061     *   &lt;tx:in/>
062     * </pre>
063     */
064    
065    @Tag(name = "label")
066    @ExtensionTag(baseClassName = "org.apache.myfaces.tobago.taglib.component.LabelTag")
067    public class LabelExtensionTag extends BodyTagSupport
068        implements HasValue, HasTip {
069    
070      private static final Log LOG = LogFactory.getLog(LabelExtensionTag.class);
071    
072      public static final String DEFAULT_COLUMNS = "fixed;*";
073    
074      private String value;
075      private String tip;
076      private String rendered;
077      private String columns = DEFAULT_COLUMNS;
078      private String rows = "fixed";
079      private String markup;
080    
081      private PanelTag panelTag;
082    
083      @Override
084      public int doStartTag() throws JspException {
085    
086        panelTag = new PanelTag();
087        panelTag.setPageContext(pageContext);
088        panelTag.setParent(getParent());
089        if (rendered != null) {
090          panelTag.setRendered(rendered);
091        }
092        if (tip != null) {
093          panelTag.setTip(tip);
094        }
095        panelTag.doStartTag();
096    
097        FacetTag facetTag = new FacetTag();
098        facetTag.setPageContext(pageContext);
099        facetTag.setName(FACET_LAYOUT);
100        facetTag.setParent(panelTag);
101        facetTag.doStartTag();
102    
103        GridLayoutTag gridLayoutTag = new GridLayoutTag();
104        gridLayoutTag.setPageContext(pageContext);
105        gridLayoutTag.setColumns(columns);
106        gridLayoutTag.setRows(rows);
107        gridLayoutTag.setParent(facetTag);
108        gridLayoutTag.doStartTag();
109        gridLayoutTag.doEndTag();
110    
111        facetTag.doEndTag();
112    
113        LabelTag labelTag = new LabelTag();
114        labelTag.setPageContext(pageContext);
115        if (value != null) {
116          labelTag.setValue(value);
117        }
118        if (markup != null) {
119          labelTag.setMarkup(markup);
120        }
121        labelTag.setFor("@auto");
122        labelTag.setParent(panelTag);
123        labelTag.doStartTag();
124        labelTag.doEndTag();
125    
126        return super.doStartTag();
127      }
128    
129      @Override
130      public int doEndTag() throws JspException {
131        panelTag.doEndTag();
132        return super.doEndTag();
133      }
134    
135      @Override
136      public void release() {
137        super.release();
138        value = null;
139        tip = null;
140        rendered = null;
141        columns = DEFAULT_COLUMNS;
142        rows = "fixed";
143        panelTag = null;
144        markup = null;
145      }
146    
147      public void setValue(String value) {
148        this.value = value;
149      }
150    
151      public void setTip(String tip) {
152        this.tip = tip;
153      }
154    
155      public void setRendered(String rendered) {
156        this.rendered = rendered;
157      }
158    
159      void setColumns(String columns) {
160        if (!(UIComponentTag.isValueReference(columns) || LayoutUtil.checkTokens(columns))) {
161          LOG.warn("Illegal value for columns = \"" + columns + "\" replacing with default: \"" + DEFAULT_COLUMNS + "\"");
162          this.columns = DEFAULT_COLUMNS;
163        } else {
164          this.columns = columns;
165        }
166      }
167    
168      void setRows(String rows) {
169        this.rows = rows;
170      }
171    
172      public void setMarkup(String markup) {
173        this.markup = markup;
174      }
175    }