1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.GetterUtil;
27  import com.liferay.portal.kernel.util.HtmlUtil;
28  import com.liferay.portal.kernel.util.ServerDetector;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  
32  import java.util.List;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.jsp.JspException;
36  import javax.servlet.jsp.tagext.TagSupport;
37  
38  /**
39   * <a href="TableIteratorTag.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class TableIteratorTag extends TagSupport {
44  
45      public int doStartTag() throws JspException {
46          try {
47              if (_list.size() > 0) {
48                  HttpServletRequest request =
49                      (HttpServletRequest)pageContext.getRequest();
50  
51                  request.setAttribute("liferay-ui:table-iterator:list", _list);
52                  request.setAttribute(
53                      "liferay-ui:table-iterator:rowLength",
54                      String.valueOf(_rowLength));
55                  request.setAttribute(
56                      "liferay-ui:table-iterator:rowPadding", _rowPadding);
57                  request.setAttribute(
58                      "liferay-ui:table-iterator:rowValign", _rowValign);
59                  request.setAttribute(
60                      "liferay-ui:table-iterator:rowBreak", _rowBreak);
61                  request.setAttribute("liferay-ui:table-iterator:width", _width);
62  
63                  PortalIncludeUtil.include(pageContext, getStartPage());
64  
65                  pageContext.setAttribute(
66                      "tableIteratorObj", _list.get(_listPos));
67                  pageContext.setAttribute(
68                      "tableIteratorPos", new Integer(_listPos));
69  
70                  return EVAL_BODY_INCLUDE;
71              }
72              else {
73                  return SKIP_BODY;
74              }
75          }
76          catch (Exception e) {
77              throw new JspException(e);
78          }
79      }
80  
81      public int doAfterBody() throws JspException {
82          try {
83              HttpServletRequest request =
84                  (HttpServletRequest)pageContext.getRequest();
85  
86              request.setAttribute(
87                  "liferay-ui:table-iterator:listPos", String.valueOf(_listPos));
88  
89              PortalIncludeUtil.include(pageContext, getBodyPage());
90  
91              _listPos++;
92  
93              if (_listPos < _list.size()) {
94                  pageContext.setAttribute(
95                      "tableIteratorObj", _list.get(_listPos));
96                  pageContext.setAttribute(
97                      "tableIteratorPos", new Integer(_listPos));
98  
99                  return EVAL_BODY_AGAIN;
100             }
101             else {
102                 return SKIP_BODY;
103             }
104         }
105         catch (Exception e) {
106             throw new JspException(e);
107         }
108     }
109 
110     public int doEndTag() throws JspException {
111         try {
112             if (_list.size() > 0) {
113                 PortalIncludeUtil.include(pageContext, getEndPage());
114             }
115 
116             return EVAL_PAGE;
117         }
118         catch (Exception e) {
119             throw new JspException(e);
120         }
121         finally {
122             if (!ServerDetector.isResin()) {
123                 _startPage = null;
124                 _bodyPage = null;
125                 _endPage = null;
126                 _list = null;
127                 _listPos = 0;
128                 _rowLength = 0;
129                 _rowPadding = "0";
130                 _rowValign = "middle";
131                 _rowBreak = null;
132             }
133         }
134     }
135 
136     public String getStartPage() {
137         if (Validator.isNull(_startPage)) {
138             return _START_PAGE;
139         }
140         else {
141             return _startPage;
142         }
143     }
144 
145     public void setStartPage(String startPage) {
146         _startPage = startPage;
147     }
148 
149     public String getBodyPage() {
150         if (Validator.isNull(_bodyPage)) {
151             return _BODY_PAGE;
152         }
153         else {
154             return _bodyPage;
155         }
156     }
157 
158     public void setBodyPage(String bodyPage) {
159         _bodyPage = bodyPage;
160     }
161 
162     public String getEndPage() {
163         if (Validator.isNull(_endPage)) {
164             return _END_PAGE;
165         }
166         else {
167             return _endPage;
168         }
169     }
170 
171     public void setEndPage(String endPage) {
172         _endPage = endPage;
173     }
174 
175     public void setList(List<?> list) {
176         _list = list;
177     }
178 
179     public void setListType(String listType) {
180     }
181 
182     public void setRowLength(String rowLength) {
183         _rowLength = GetterUtil.getInteger(rowLength);
184     }
185 
186     public void setRowPadding(String rowPadding) {
187         _rowPadding = rowPadding;
188     }
189 
190     public void setRowValign(String rowValign) {
191         _rowValign = rowValign;
192     }
193 
194     public void setRowBreak(String rowBreak) {
195         _rowBreak = HtmlUtil.unescape(rowBreak);
196     }
197 
198     public void setWidth(String width) {
199         _width = width;
200     }
201 
202     private static final String _START_PAGE =
203         "/html/taglib/ui/table_iterator/start.jsp";
204 
205     private static final String _BODY_PAGE =
206         "/html/taglib/ui/table_iterator/body.jsp";
207 
208     private static final String _END_PAGE =
209         "/html/taglib/ui/table_iterator/end.jsp";
210 
211     private String _startPage;
212     private String _bodyPage;
213     private String _endPage;
214     private List<?> _list;
215     private int _listPos;
216     private int _rowLength;
217     private String _rowPadding = "0";
218     private String _rowValign = "middle";
219     private String _rowBreak = "<br />";
220     private String _width = StringPool.BLANK;
221 
222 }