1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.kernel.util.JavaConstants;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
29  
30  import javax.portlet.RenderResponse;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.jsp.JspException;
34  
35  /**
36   * <a href="SectionTag.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class SectionTag extends ParamAndPropertyAncestorTagImpl {
42  
43      public int doStartTag() throws JspException {
44          _tabsTag = (TabsTag)findAncestorWithClass(this, TabsTag.class);
45  
46          if (_tabsTag == null) {
47              throw new JspException();
48          }
49  
50          try {
51              HttpServletRequest request =
52                  (HttpServletRequest)pageContext.getRequest();
53  
54              RenderResponse renderResponse =
55                  (RenderResponse)request.getAttribute(
56                      JavaConstants.JAVAX_PORTLET_RESPONSE);
57  
58              String namespace = StringPool.BLANK;
59  
60              if (renderResponse != null) {
61                  namespace = renderResponse.getNamespace();
62              }
63  
64              String sectionParam = _tabsTag.getParam();
65              String sectionName = _tabsTag.getSectionName();
66              _sectionSelected = Boolean.valueOf(_tabsTag.getSectionSelected());
67              String sectionScroll = namespace + sectionParam + "TabsScroll";
68              String sectionRedirectParams =
69                  "&scroll=" + sectionScroll + "&" + sectionParam + "=" +
70                      sectionName;
71  
72              _tabsTag.incrementSection();
73  
74              request.setAttribute("liferay-ui:section:param", sectionParam);
75              request.setAttribute("liferay-ui:section:name", sectionName);
76              request.setAttribute(
77                  "liferay-ui:section:selected", _sectionSelected);
78              request.setAttribute("liferay-ui:section:scroll", sectionScroll);
79  
80              pageContext.setAttribute("sectionSelected", _sectionSelected);
81              pageContext.setAttribute("sectionParam", sectionParam);
82              pageContext.setAttribute("sectionName", sectionName);
83              pageContext.setAttribute("sectionScroll", sectionScroll);
84              pageContext.setAttribute(
85                  "sectionRedirectParams", sectionRedirectParams);
86  
87              if (!_tabsTag.isRefresh() || _sectionSelected.booleanValue()) {
88                  if (!_tabsTag.isRefresh()) {
89                      include(getStartPage());
90                  }
91  
92                  return EVAL_BODY_INCLUDE;
93              }
94              else {
95                  return EVAL_PAGE;
96              }
97          }
98          catch (Exception e) {
99              throw new JspException(e);
100         }
101     }
102 
103     public int doEndTag() throws JspException {
104         try {
105             if (!_tabsTag.isRefresh() || _sectionSelected.booleanValue()) {
106                 if (!_tabsTag.isRefresh()) {
107                     include(getEndPage());
108                 }
109 
110                 return EVAL_BODY_INCLUDE;
111             }
112             else {
113                 return EVAL_PAGE;
114             }
115         }
116         catch (Exception e) {
117             throw new JspException(e);
118         }
119     }
120 
121     public String getStartPage() {
122         if (Validator.isNull(_startPage)) {
123             return _START_PAGE;
124         }
125         else {
126             return _startPage;
127         }
128     }
129 
130     public void setStartPage(String startPage) {
131         _startPage = startPage;
132     }
133 
134     public String getEndPage() {
135         if (Validator.isNull(_endPage)) {
136             return _END_PAGE;
137         }
138         else {
139             return _endPage;
140         }
141     }
142 
143     public void setEndPage(String endPage) {
144         _endPage = endPage;
145     }
146 
147     private static final String _START_PAGE =
148         "/html/taglib/ui/section/start.jsp";
149 
150     private static final String _END_PAGE = "/html/taglib/ui/section/end.jsp";
151 
152     private String _startPage;
153     private String _endPage;
154     private TabsTag _tabsTag = null;
155     private Boolean _sectionSelected = Boolean.FALSE;
156 
157 }