001    /**
002     * Copyright (c) 2000-2013 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.ServerDetector;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
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                            bodyContent.clearBody();
043    
044                            return EVAL_BODY_AGAIN;
045                    }
046                    else {
047                            return SKIP_BODY;
048                    }
049            }
050    
051            @Override
052            public int doEndTag() throws JspException {
053                    try {
054                            HttpServletRequest request =
055                                    (HttpServletRequest)pageContext.getRequest();
056    
057                            IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
058                                    "liferay-ui:panel-container:panelCount" + _id);
059    
060                            request.removeAttribute(
061                                    "liferay-ui:panel-container:panelCount" + _id);
062    
063                            if ((panelCount != null) && (panelCount.getValue() >= 1)) {
064                                    PortalIncludeUtil.include(pageContext, getStartPage());
065                            }
066    
067                            writeBodyContent(pageContext.getOut());
068    
069                            if ((panelCount != null) && (panelCount.getValue() >= 1)) {
070                                    PortalIncludeUtil.include(pageContext, getEndPage());
071                            }
072    
073                            request.removeAttribute("liferay-ui:panel-container:id");
074                            request.removeAttribute("liferay-ui:panel-container:accordion");
075                            request.removeAttribute("liferay-ui:panel-container:persistState");
076                            request.removeAttribute("liferay-ui:panel-container:extended");
077                            request.removeAttribute("liferay-ui:panel-container:cssClass");
078    
079                            return EVAL_PAGE;
080                    }
081                    catch (Exception e) {
082                            throw new JspException(e);
083                    }
084                    finally {
085                            if (!ServerDetector.isResin()) {
086                                    cleanUp();
087                            }
088                    }
089            }
090    
091            @Override
092            public int doStartTag() {
093                    HttpServletRequest request =
094                            (HttpServletRequest)pageContext.getRequest();
095    
096                    if (Validator.isNull(_id)) {
097                            _id = StringUtil.randomId();
098                    }
099    
100                    request.setAttribute("liferay-ui:panel-container:id", _id);
101                    request.setAttribute(
102                            "liferay-ui:panel-container:accordion", String.valueOf(_accordion));
103                    request.setAttribute(
104                            "liferay-ui:panel-container:persistState",
105                            String.valueOf(_persistState));
106                    request.setAttribute("liferay-ui:panel-container:extended", _extended);
107                    request.setAttribute("liferay-ui:panel-container:cssClass", _cssClass);
108                    request.setAttribute(
109                            "liferay-ui:panel-container:panelCount" + _id,
110                            new IntegerWrapper());
111    
112                    return EVAL_BODY_BUFFERED;
113            }
114    
115            public String getId() {
116                    return _id;
117            }
118    
119            public void setAccordion(boolean accordion) {
120                    _accordion = accordion;
121            }
122    
123            public void setCssClass(String cssClass) {
124                    _cssClass = cssClass;
125            }
126    
127            public void setEndPage(String endPage) {
128                    _endPage = endPage;
129            }
130    
131            public void setExtended(Boolean extended) {
132                    _extended = extended;
133            }
134    
135            public void setId(String id) {
136                    _id = id;
137            }
138    
139            public void setPersistState(boolean persistState) {
140                    _persistState = persistState;
141            }
142    
143            public void setStartPage(String startPage) {
144                    _startPage = startPage;
145            }
146    
147            protected void cleanUp() {
148                    _accordion = false;
149                    _cssClass = null;
150                    _endPage = null;
151                    _extended = false;
152                    _id = null;
153                    _persistState = false;
154                    _startPage = null;
155            }
156    
157            protected String getEndPage() {
158                    if (Validator.isNull(_endPage)) {
159                            return _END_PAGE;
160                    }
161                    else {
162                            return _endPage;
163                    }
164            }
165    
166            protected String getStartPage() {
167                    if (Validator.isNull(_startPage)) {
168                            return _START_PAGE;
169                    }
170                    else {
171                            return _startPage;
172                    }
173            }
174    
175            private static final String _END_PAGE =
176                    "/html/taglib/ui/panel_container/end.jsp";
177    
178            private static final String _START_PAGE =
179                    "/html/taglib/ui/panel_container/start.jsp";
180    
181            private boolean _accordion;
182            private String _cssClass;
183            private String _endPage;
184            private Boolean _extended;
185            private String _id;
186            private boolean _persistState;
187            private String _startPage;
188    
189    }