001    /**
002     * Copyright (c) 2000-2010 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    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PanelContainerTag extends BaseBodyTagSupport {
031    
032            public int doStartTag() {
033                    HttpServletRequest request =
034                            (HttpServletRequest)pageContext.getRequest();
035    
036                    if (Validator.isNull(_id)) {
037                            _id = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
038                    }
039    
040                    request.setAttribute(
041                            "liferay-ui:panel-container:id", _id);
042                    request.setAttribute(
043                            "liferay-ui:panel-container:accordion", String.valueOf(_accordion));
044                    request.setAttribute(
045                            "liferay-ui:panel-container:persistState",
046                            String.valueOf(_persistState));
047                    request.setAttribute("liferay-ui:panel-container:extended", _extended);
048                    request.setAttribute("liferay-ui:panel-container:cssClass", _cssClass);
049                    request.setAttribute(
050                            "liferay-ui:panel-container:panelCount" + _id,
051                            new IntegerWrapper());
052    
053                    return EVAL_BODY_BUFFERED;
054            }
055    
056            public int doAfterBody() {
057                    HttpServletRequest request =
058                            (HttpServletRequest)pageContext.getRequest();
059    
060                    IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
061                            "liferay-ui:panel-container:panelCount" + _id);
062    
063                    if ((panelCount != null) && (panelCount.getValue() == 1)) {
064    
065                            bodyContent.clearBody();
066    
067                            return EVAL_BODY_AGAIN;
068                    }
069                    else {
070                            return SKIP_BODY;
071                    }
072            }
073    
074            public int doEndTag() throws JspException {
075                    try {
076                            HttpServletRequest request =
077                                    (HttpServletRequest)pageContext.getRequest();
078    
079                            IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
080                                    "liferay-ui:panel-container:panelCount" + _id);
081    
082                            request.removeAttribute(
083                                    "liferay-ui:panel-container:panelCount" + _id);
084    
085                            if ((panelCount != null) && (panelCount.getValue() >= 1)) {
086                                    PortalIncludeUtil.include(pageContext, getStartPage());
087                            }
088    
089                            writeBodyContent(pageContext.getOut());
090    
091                            if ((panelCount != null) && (panelCount.getValue() >= 1)) {
092                                    PortalIncludeUtil.include(pageContext, getEndPage());
093                            }
094    
095                            request.removeAttribute("liferay-ui:panel-container:id");
096                            request.removeAttribute("liferay-ui:panel-container:accordion");
097                            request.removeAttribute("liferay-ui:panel-container:persistState");
098                            request.removeAttribute("liferay-ui:panel-container:extended");
099                            request.removeAttribute("liferay-ui:panel-container:cssClass");
100    
101                            return EVAL_PAGE;
102                    }
103                    catch (Exception e) {
104                            throw new JspException(e);
105                    }
106            }
107    
108            public String getId() {
109                    return _id;
110            }
111    
112            protected String getStartPage() {
113                    if (Validator.isNull(_startPage)) {
114                            return _START_PAGE;
115                    }
116                    else {
117                            return _startPage;
118                    }
119            }
120    
121            public void setStartPage(String startPage) {
122                    _startPage = startPage;
123            }
124    
125            protected String getEndPage() {
126                    if (Validator.isNull(_endPage)) {
127                            return _END_PAGE;
128                    }
129                    else {
130                            return _endPage;
131                    }
132            }
133    
134            public void setEndPage(String endPage) {
135                    _endPage = endPage;
136            }
137    
138            public void setId(String id) {
139                    _id = id;
140            }
141    
142            public void setAccordion(boolean accordion) {
143                    _accordion = accordion;
144            }
145    
146            public void setPersistState(boolean persistState) {
147                    _persistState = persistState;
148            }
149    
150            public void setExtended(Boolean extended) {
151                    _extended = extended;
152            }
153    
154            public void setCssClass(String cssClass) {
155                    _cssClass = cssClass;
156            }
157    
158            private static final String _START_PAGE =
159                    "/html/taglib/ui/panel_container/start.jsp";
160    
161            private static final String _END_PAGE =
162                    "/html/taglib/ui/panel_container/end.jsp";
163    
164            private String _startPage;
165            private String _endPage;
166            private String _id;
167            private boolean _accordion;
168            private boolean _persistState;
169            private Boolean _extended;
170            private String _cssClass = StringPool.BLANK;
171    
172    }