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.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                            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            }
085    
086            @Override
087            public int doStartTag() {
088                    HttpServletRequest request =
089                            (HttpServletRequest)pageContext.getRequest();
090    
091                    if (Validator.isNull(_id)) {
092                            _id = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
093                    }
094    
095                    request.setAttribute("liferay-ui:panel-container:id", _id);
096                    request.setAttribute(
097                            "liferay-ui:panel-container:accordion", String.valueOf(_accordion));
098                    request.setAttribute(
099                            "liferay-ui:panel-container:persistState",
100                            String.valueOf(_persistState));
101                    request.setAttribute("liferay-ui:panel-container:extended", _extended);
102                    request.setAttribute("liferay-ui:panel-container:cssClass", _cssClass);
103                    request.setAttribute(
104                            "liferay-ui:panel-container:panelCount" + _id,
105                            new IntegerWrapper());
106    
107                    return EVAL_BODY_BUFFERED;
108            }
109    
110            public String getId() {
111                    return _id;
112            }
113    
114            public void setAccordion(boolean accordion) {
115                    _accordion = accordion;
116            }
117    
118            public void setCssClass(String cssClass) {
119                    _cssClass = cssClass;
120            }
121    
122            public void setEndPage(String endPage) {
123                    _endPage = endPage;
124            }
125    
126            public void setExtended(Boolean extended) {
127                    _extended = extended;
128            }
129    
130            public void setId(String id) {
131                    _id = id;
132            }
133    
134            public void setPersistState(boolean persistState) {
135                    _persistState = persistState;
136            }
137    
138            public void setStartPage(String startPage) {
139                    _startPage = startPage;
140            }
141    
142            protected String getEndPage() {
143                    if (Validator.isNull(_endPage)) {
144                            return _END_PAGE;
145                    }
146                    else {
147                            return _endPage;
148                    }
149            }
150    
151            protected String getStartPage() {
152                    if (Validator.isNull(_startPage)) {
153                            return _START_PAGE;
154                    }
155                    else {
156                            return _startPage;
157                    }
158            }
159    
160            private static final String _END_PAGE =
161                    "/html/taglib/ui/panel_container/end.jsp";
162    
163            private static final String _START_PAGE =
164                    "/html/taglib/ui/panel_container/start.jsp";
165    
166            private boolean _accordion;
167            private String _cssClass = StringPool.BLANK;
168            private String _endPage;
169            private Boolean _extended;
170            private String _id;
171            private boolean _persistState;
172            private String _startPage;
173    
174    }