001    /**
002     * Copyright (c) 2000-present 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.util.JavaConstants;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.taglib.util.IncludeTag;
020    
021    import java.util.Map;
022    
023    import javax.portlet.PortletResponse;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.jsp.JspException;
027    import javax.servlet.jsp.JspWriter;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class SectionTag extends IncludeTag {
033    
034            @Override
035            public int doStartTag() throws JspException {
036                    try {
037                            _tabsTag = (TabsTag)findAncestorWithClass(this, TabsTag.class);
038    
039                            if (_tabsTag == null) {
040                                    throw new JspException();
041                            }
042    
043                            HttpServletRequest request =
044                                    (HttpServletRequest)pageContext.getRequest();
045    
046                            PortletResponse portletResponse =
047                                    (PortletResponse)request.getAttribute(
048                                            JavaConstants.JAVAX_PORTLET_RESPONSE);
049    
050                            String namespace = StringPool.BLANK;
051    
052                            if (portletResponse != null) {
053                                    namespace = portletResponse.getNamespace();
054                            }
055    
056                            String sectionParam = _tabsTag.getParam();
057                            String sectionName = _tabsTag.getSectionName();
058                            _sectionSelected = Boolean.valueOf(_tabsTag.getSectionSelected());
059                            String sectionScroll = namespace + sectionParam + "TabsScroll";
060                            String sectionRedirectParams =
061                                    "&scroll=" + sectionScroll + "&" + sectionParam + "=" +
062                                            sectionName;
063    
064                            _tabsTag.incrementSection();
065    
066                            request.setAttribute("liferay-ui:section:data", _data);
067                            request.setAttribute("liferay-ui:section:param", sectionParam);
068                            request.setAttribute("liferay-ui:section:name", sectionName);
069                            request.setAttribute(
070                                    "liferay-ui:section:selected", _sectionSelected);
071                            request.setAttribute("liferay-ui:section:scroll", sectionScroll);
072    
073                            pageContext.setAttribute("sectionSelected", _sectionSelected);
074                            pageContext.setAttribute("sectionParam", sectionParam);
075                            pageContext.setAttribute("sectionName", sectionName);
076                            pageContext.setAttribute("sectionScroll", sectionScroll);
077                            pageContext.setAttribute(
078                                    "sectionRedirectParams", sectionRedirectParams);
079    
080                            include(getStartPage(), true);
081    
082                            if (!_tabsTag.isRefresh() || _sectionSelected.booleanValue()) {
083                                    return EVAL_BODY_INCLUDE;
084                            }
085                            else {
086                                    return EVAL_PAGE;
087                            }
088                    }
089                    catch (Exception e) {
090                            throw new JspException(e);
091                    }
092            }
093    
094            public void setData(Map<String, Object> data) {
095                    _data = data;
096            }
097    
098            @Override
099            protected void cleanUp() {
100                    _data = null;
101                    _sectionSelected = Boolean.FALSE;
102                    _tabsTag = null;
103            }
104    
105            @Override
106            protected String getEndPage() {
107                    return _END_PAGE;
108            }
109    
110            @Override
111            protected String getStartPage() {
112                    return _START_PAGE;
113            }
114    
115            @Override
116            protected int processEndTag() throws Exception {
117                    JspWriter jspWriter = pageContext.getOut();
118    
119                    jspWriter.write("</div>");
120    
121                    return EVAL_PAGE;
122            }
123    
124            private static final String _END_PAGE = "/html/taglib/ui/section/end.jsp";
125    
126            private static final String _START_PAGE =
127                    "/html/taglib/ui/section/start.jsp";
128    
129            private Map<String, Object> _data;
130            private Boolean _sectionSelected = Boolean.FALSE;
131            private TabsTag _tabsTag = null;
132    
133    }