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