001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
018    import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
019    import com.liferay.portal.kernel.util.IntegerWrapper;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.util.PwdGenerator;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.jsp.JspException;
026    import javax.servlet.jsp.tagext.BodyTag;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class PanelContainerTag extends BaseBodyTagSupport implements BodyTag {
032    
033            @Override
034            public int doAfterBody() {
035                    HttpServletRequest request =
036                            (HttpServletRequest)pageContext.getRequest();
037    
038                    IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
039                            "liferay-ui:panel-container:panelCount" + _id);
040    
041                    if ((panelCount != null) && (panelCount.getValue() == 1)) {
042    
043                            bodyContent.clearBody();
044    
045                            return EVAL_BODY_AGAIN;
046                    }
047                    else {
048                            return SKIP_BODY;
049                    }
050            }
051    
052            @Override
053            public int doEndTag() throws JspException {
054                    try {
055                            HttpServletRequest request =
056                                    (HttpServletRequest)pageContext.getRequest();
057    
058                            IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
059                                    "liferay-ui:panel-container:panelCount" + _id);
060    
061                            request.removeAttribute(
062                                    "liferay-ui:panel-container:panelCount" + _id);
063    
064                            if ((panelCount != null) && (panelCount.getValue() >= 1)) {
065                                    PortalIncludeUtil.include(pageContext, getStartPage());
066                            }
067    
068                            writeBodyContent(pageContext.getOut());
069    
070                            if ((panelCount != null) && (panelCount.getValue() >= 1)) {
071                                    PortalIncludeUtil.include(pageContext, getEndPage());
072                            }
073    
074                            request.removeAttribute("liferay-ui:panel-container:id");
075                            request.removeAttribute("liferay-ui:panel-container:accordion");
076                            request.removeAttribute("liferay-ui:panel-container:persistState");
077                            request.removeAttribute("liferay-ui:panel-container:extended");
078                            request.removeAttribute("liferay-ui:panel-container:cssClass");
079    
080                            return EVAL_PAGE;
081                    }
082                    catch (Exception e) {
083                            throw new JspException(e);
084                    }
085            }
086    
087            @Override
088            public int doStartTag() {
089                    HttpServletRequest request =
090                            (HttpServletRequest)pageContext.getRequest();
091    
092                    if (Validator.isNull(_id)) {
093                            _id = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
094                    }
095    
096                    request.setAttribute("liferay-ui:panel-container:id", _id);
097                    request.setAttribute(
098                            "liferay-ui:panel-container:accordion", String.valueOf(_accordion));
099                    request.setAttribute(
100                            "liferay-ui:panel-container:persistState",
101                            String.valueOf(_persistState));
102                    request.setAttribute("liferay-ui:panel-container:extended", _extended);
103                    request.setAttribute("liferay-ui:panel-container:cssClass", _cssClass);
104                    request.setAttribute(
105                            "liferay-ui:panel-container:panelCount" + _id,
106                            new IntegerWrapper());
107    
108                    return EVAL_BODY_BUFFERED;
109            }
110    
111            public String getId() {
112                    return _id;
113            }
114    
115            public void setAccordion(boolean accordion) {
116                    _accordion = accordion;
117            }
118    
119            public void setCssClass(String cssClass) {
120                    _cssClass = cssClass;
121            }
122    
123            public void setEndPage(String endPage) {
124                    _endPage = endPage;
125            }
126    
127            public void setExtended(Boolean extended) {
128                    _extended = extended;
129            }
130    
131            public void setId(String id) {
132                    _id = id;
133            }
134    
135            public void setPersistState(boolean persistState) {
136                    _persistState = persistState;
137            }
138    
139            public void setStartPage(String startPage) {
140                    _startPage = startPage;
141            }
142    
143            protected String getEndPage() {
144                    if (Validator.isNull(_endPage)) {
145                            return _END_PAGE;
146                    }
147                    else {
148                            return _endPage;
149                    }
150            }
151    
152            protected String getStartPage() {
153                    if (Validator.isNull(_startPage)) {
154                            return _START_PAGE;
155                    }
156                    else {
157                            return _startPage;
158                    }
159            }
160    
161            private static final String _END_PAGE =
162                    "/html/taglib/ui/panel_container/end.jsp";
163    
164            private static final String _START_PAGE =
165                    "/html/taglib/ui/panel_container/start.jsp";
166    
167            private boolean _accordion;
168            private String _cssClass = StringPool.BLANK;
169            private String _endPage;
170            private Boolean _extended;
171            private String _id;
172            private boolean _persistState;
173            private String _startPage;
174    
175    }