001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
043                            "liferay-ui:panel-container:id", _id);
044                    request.setAttribute(
045                            "liferay-ui:panel-container:accordion", String.valueOf(_accordion));
046                    request.setAttribute(
047                            "liferay-ui:panel-container:persistState",
048                            String.valueOf(_persistState));
049                    request.setAttribute("liferay-ui:panel-container:extended", _extended);
050                    request.setAttribute("liferay-ui:panel-container:cssClass", _cssClass);
051                    request.setAttribute(
052                            "liferay-ui:panel-container:panelCount" + _id,
053                            new IntegerWrapper());
054    
055                    return EVAL_BODY_BUFFERED;
056            }
057    
058            @Override
059            public int doAfterBody() {
060                    HttpServletRequest request =
061                            (HttpServletRequest)pageContext.getRequest();
062    
063                    IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
064                            "liferay-ui:panel-container:panelCount" + _id);
065    
066                    if ((panelCount != null) && (panelCount.getValue() == 1)) {
067    
068                            bodyContent.clearBody();
069    
070                            return EVAL_BODY_AGAIN;
071                    }
072                    else {
073                            return SKIP_BODY;
074                    }
075            }
076    
077            @Override
078            public int doEndTag() throws JspException {
079                    try {
080                            HttpServletRequest request =
081                                    (HttpServletRequest)pageContext.getRequest();
082    
083                            IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
084                                    "liferay-ui:panel-container:panelCount" + _id);
085    
086                            request.removeAttribute(
087                                    "liferay-ui:panel-container:panelCount" + _id);
088    
089                            if ((panelCount != null) && (panelCount.getValue() >= 1)) {
090                                    PortalIncludeUtil.include(pageContext, getStartPage());
091                            }
092    
093                            writeBodyContent(pageContext.getOut());
094    
095                            if ((panelCount != null) && (panelCount.getValue() >= 1)) {
096                                    PortalIncludeUtil.include(pageContext, getEndPage());
097                            }
098    
099                            request.removeAttribute("liferay-ui:panel-container:id");
100                            request.removeAttribute("liferay-ui:panel-container:accordion");
101                            request.removeAttribute("liferay-ui:panel-container:persistState");
102                            request.removeAttribute("liferay-ui:panel-container:extended");
103                            request.removeAttribute("liferay-ui:panel-container:cssClass");
104    
105                            return EVAL_PAGE;
106                    }
107                    catch (Exception e) {
108                            throw new JspException(e);
109                    }
110            }
111    
112            public String getId() {
113                    return _id;
114            }
115    
116            protected String getStartPage() {
117                    if (Validator.isNull(_startPage)) {
118                            return _START_PAGE;
119                    }
120                    else {
121                            return _startPage;
122                    }
123            }
124    
125            public void setStartPage(String startPage) {
126                    _startPage = startPage;
127            }
128    
129            protected String getEndPage() {
130                    if (Validator.isNull(_endPage)) {
131                            return _END_PAGE;
132                    }
133                    else {
134                            return _endPage;
135                    }
136            }
137    
138            public void setEndPage(String endPage) {
139                    _endPage = endPage;
140            }
141    
142            public void setId(String id) {
143                    _id = id;
144            }
145    
146            public void setAccordion(boolean accordion) {
147                    _accordion = accordion;
148            }
149    
150            public void setPersistState(boolean persistState) {
151                    _persistState = persistState;
152            }
153    
154            public void setExtended(Boolean extended) {
155                    _extended = extended;
156            }
157    
158            public void setCssClass(String cssClass) {
159                    _cssClass = cssClass;
160            }
161    
162            private static final String _START_PAGE =
163                    "/html/taglib/ui/panel_container/start.jsp";
164    
165            private static final String _END_PAGE =
166                    "/html/taglib/ui/panel_container/end.jsp";
167    
168            private String _startPage;
169            private String _endPage;
170            private String _id;
171            private boolean _accordion;
172            private boolean _persistState;
173            private Boolean _extended;
174            private String _cssClass = StringPool.BLANK;
175    
176    }