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