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