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    @Tag(name = "label")
039    @ExtensionTag(baseClassName = "org.apache.myfaces.tobago.taglib.component.LabelTag")
040    public class LabelExtensionTag extends BodyTagSupport
041        implements HasValue, HasTip {
042    
043      private static final Log LOG = LogFactory.getLog(LabelExtensionTag.class);
044    
045      public static final String DEFAULT_COLUMNS = "fixed;*";
046    
047      private String value;
048      private String tip;
049      private String rendered;
050      private String columns = DEFAULT_COLUMNS;
051      private String rows = "fixed";
052      private String markup;
053    
054      private PanelTag panelTag;
055    
056      @Override
057      public int doStartTag() throws JspException {
058    
059        panelTag = new PanelTag();
060        panelTag.setPageContext(pageContext);
061        panelTag.setParent(getParent());
062        if (rendered != null) {
063          panelTag.setRendered(rendered);
064        }
065        if (tip != null) {
066          panelTag.setTip(tip);
067        }
068        panelTag.doStartTag();
069    
070        FacetTag facetTag = new FacetTag();
071        facetTag.setPageContext(pageContext);
072        facetTag.setName(FACET_LAYOUT);
073        facetTag.setParent(panelTag);
074        facetTag.doStartTag();
075    
076        GridLayoutTag gridLayoutTag = new GridLayoutTag();
077        gridLayoutTag.setPageContext(pageContext);
078        gridLayoutTag.setColumns(columns);
079        gridLayoutTag.setRows(rows);
080        gridLayoutTag.setParent(facetTag);
081        gridLayoutTag.doStartTag();
082        gridLayoutTag.doEndTag();
083    
084        facetTag.doEndTag();
085    
086        LabelTag labelTag = new LabelTag();
087        labelTag.setPageContext(pageContext);
088        if (value != null) {
089          labelTag.setValue(value);
090        }
091        if (markup != null) {
092          labelTag.setMarkup(markup);
093        }
094        labelTag.setFor("@auto");
095        labelTag.setParent(panelTag);
096        labelTag.doStartTag();
097        labelTag.doEndTag();
098    
099        return super.doStartTag();
100      }
101    
102      @Override
103      public int doEndTag() throws JspException {
104        panelTag.doEndTag();
105        return super.doEndTag();
106      }
107    
108      @Override
109      public void release() {
110        super.release();
111        value = null;
112        tip = null;
113        rendered = null;
114        columns = DEFAULT_COLUMNS;
115        rows = "fixed";
116        panelTag = null;
117        markup = null;
118      }
119    
120      public void setValue(String value) {
121        this.value = value;
122      }
123    
124      public void setTip(String tip) {
125        this.tip = tip;
126      }
127    
128      public void setRendered(String rendered) {
129        this.rendered = rendered;
130      }
131    
132      void setColumns(String columns) {
133        if (!(UIComponentTag.isValueReference(columns) || LayoutUtil.checkTokens(columns))) {
134          LOG.warn("Illegal value for columns = \"" + columns + "\" replacing with default: \"" + DEFAULT_COLUMNS + "\"");
135          this.columns = DEFAULT_COLUMNS;
136        } else {
137          this.columns = columns;
138        }
139      }
140    
141      void setRows(String rows) {
142        this.rows = rows;
143      }
144    
145      public void setMarkup(String markup) {
146        this.markup = markup;
147      }
148    }