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.servlet.PortalIncludeUtil;
26  import com.liferay.portal.kernel.util.IntegerWrapper;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.jsp.JspException;
32  import javax.servlet.jsp.tagext.BodyContent;
33  import javax.servlet.jsp.tagext.BodyTagSupport;
34  
35  /**
36   * <a href="PanelContainerTag.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PanelContainerTag extends BodyTagSupport {
42  
43      public int doStartTag() {
44          HttpServletRequest request =
45              (HttpServletRequest)pageContext.getRequest();
46  
47          request.setAttribute(
48              "liferay-ui:panel-container:id", _id);
49          request.setAttribute(
50              "liferay-ui:panel-container:accordion", String.valueOf(_accordion));
51          request.setAttribute(
52              "liferay-ui:panel-container:persistState",
53              String.valueOf(_persistState));
54          request.setAttribute("liferay-ui:panel-container:extended", _extended);
55          request.setAttribute("liferay-ui:panel-container:cssClass", _cssClass);
56          request.setAttribute(
57              "liferay-ui:panel-container:panel-count", new IntegerWrapper());
58  
59          return EVAL_BODY_BUFFERED;
60      }
61  
62      public int doAfterBody() {
63          BodyContent bodyContent = getBodyContent();
64  
65          _bodyContentString = bodyContent.getString();
66  
67          HttpServletRequest request =
68              (HttpServletRequest)pageContext.getRequest();
69  
70          IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
71              "liferay-ui:panel-container:panel-count");
72  
73          if ((panelCount != null) && (panelCount.getValue() == 1)) {
74  
75              bodyContent.clearBody();
76  
77              return EVAL_BODY_AGAIN;
78          }
79          else {
80              return SKIP_BODY;
81          }
82      }
83  
84      public int doEndTag() throws JspException {
85          try {
86              HttpServletRequest request =
87                  (HttpServletRequest)pageContext.getRequest();
88  
89              IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
90                  "liferay-ui:panel-container:panel-count");
91  
92              request.removeAttribute("liferay-ui:panel-container:panel-count");
93  
94              if ((panelCount != null) && (panelCount.getValue() >= 1)) {
95                  PortalIncludeUtil.include(pageContext, getStartPage());
96              }
97  
98              pageContext.getOut().print(_bodyContentString);
99  
100             if ((panelCount != null) && (panelCount.getValue() >= 1)) {
101                 PortalIncludeUtil.include(pageContext, getEndPage());
102             }
103 
104             request.removeAttribute("liferay-ui:panel-container:id");
105             request.removeAttribute("liferay-ui:panel-container:accordion");
106             request.removeAttribute("liferay-ui:panel-container:persistState");
107             request.removeAttribute("liferay-ui:panel-container:extended");
108             request.removeAttribute("liferay-ui:panel-container:cssClass");
109 
110             return EVAL_PAGE;
111         }
112         catch (Exception e) {
113             throw new JspException(e);
114         }
115     }
116 
117     public String getStartPage() {
118         if (Validator.isNull(_startPage)) {
119             return _START_PAGE;
120         }
121         else {
122             return _startPage;
123         }
124     }
125 
126     public void setStartPage(String startPage) {
127         _startPage = startPage;
128     }
129 
130     public String getEndPage() {
131         if (Validator.isNull(_endPage)) {
132             return _END_PAGE;
133         }
134         else {
135             return _endPage;
136         }
137     }
138 
139     public void setEndPage(String endPage) {
140         _endPage = endPage;
141     }
142 
143     public void setId(String id) {
144         _id = id;
145     }
146 
147     public void setAccordion(boolean accordion) {
148         _accordion = accordion;
149     }
150 
151     public void setPersistState(boolean persistState) {
152         _persistState = persistState;
153     }
154 
155     public void setExtended(Boolean extended) {
156         _extended = extended;
157     }
158 
159     public void setCssClass(String cssClass) {
160         _cssClass = cssClass;
161     }
162 
163     private static final String _START_PAGE =
164         "/html/taglib/ui/panel_container/start.jsp";
165 
166     private static final String _END_PAGE =
167         "/html/taglib/ui/panel_container/end.jsp";
168 
169     private String _startPage;
170     private String _endPage;
171     private String _id;
172     private boolean _accordion;
173     private boolean _persistState;
174     private Boolean _extended;
175     private String _cssClass = StringPool.BLANK;
176     private String _bodyContentString = StringPool.BLANK;
177 
178 }