1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.HtmlUtil;
20  import com.liferay.portal.kernel.util.ServerDetector;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  
24  import java.util.List;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.jsp.JspException;
28  import javax.servlet.jsp.tagext.TagSupport;
29  
30  /**
31   * <a href="TableIteratorTag.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class TableIteratorTag extends TagSupport {
36  
37      public int doStartTag() throws JspException {
38          try {
39              if (_list.size() > 0) {
40                  HttpServletRequest request =
41                      (HttpServletRequest)pageContext.getRequest();
42  
43                  request.setAttribute("liferay-ui:table-iterator:list", _list);
44                  request.setAttribute(
45                      "liferay-ui:table-iterator:rowLength",
46                      String.valueOf(_rowLength));
47                  request.setAttribute(
48                      "liferay-ui:table-iterator:rowPadding", _rowPadding);
49                  request.setAttribute(
50                      "liferay-ui:table-iterator:rowValign", _rowValign);
51                  request.setAttribute(
52                      "liferay-ui:table-iterator:rowBreak", _rowBreak);
53                  request.setAttribute("liferay-ui:table-iterator:width", _width);
54  
55                  PortalIncludeUtil.include(pageContext, getStartPage());
56  
57                  pageContext.setAttribute(
58                      "tableIteratorObj", _list.get(_listPos));
59                  pageContext.setAttribute(
60                      "tableIteratorPos", new Integer(_listPos));
61  
62                  return EVAL_BODY_INCLUDE;
63              }
64              else {
65                  return SKIP_BODY;
66              }
67          }
68          catch (Exception e) {
69              throw new JspException(e);
70          }
71      }
72  
73      public int doAfterBody() throws JspException {
74          try {
75              HttpServletRequest request =
76                  (HttpServletRequest)pageContext.getRequest();
77  
78              request.setAttribute(
79                  "liferay-ui:table-iterator:listPos", String.valueOf(_listPos));
80  
81              PortalIncludeUtil.include(pageContext, getBodyPage());
82  
83              _listPos++;
84  
85              if (_listPos < _list.size()) {
86                  pageContext.setAttribute(
87                      "tableIteratorObj", _list.get(_listPos));
88                  pageContext.setAttribute(
89                      "tableIteratorPos", new Integer(_listPos));
90  
91                  return EVAL_BODY_AGAIN;
92              }
93              else {
94                  return SKIP_BODY;
95              }
96          }
97          catch (Exception e) {
98              throw new JspException(e);
99          }
100     }
101 
102     public int doEndTag() throws JspException {
103         try {
104             if (_list.size() > 0) {
105                 PortalIncludeUtil.include(pageContext, getEndPage());
106             }
107 
108             return EVAL_PAGE;
109         }
110         catch (Exception e) {
111             throw new JspException(e);
112         }
113         finally {
114             if (!ServerDetector.isResin()) {
115                 _startPage = null;
116                 _bodyPage = null;
117                 _endPage = null;
118                 _list = null;
119                 _listPos = 0;
120                 _rowLength = 0;
121                 _rowPadding = "0";
122                 _rowValign = "middle";
123                 _rowBreak = null;
124             }
125         }
126     }
127 
128     public String getStartPage() {
129         if (Validator.isNull(_startPage)) {
130             return _START_PAGE;
131         }
132         else {
133             return _startPage;
134         }
135     }
136 
137     public void setStartPage(String startPage) {
138         _startPage = startPage;
139     }
140 
141     public String getBodyPage() {
142         if (Validator.isNull(_bodyPage)) {
143             return _BODY_PAGE;
144         }
145         else {
146             return _bodyPage;
147         }
148     }
149 
150     public void setBodyPage(String bodyPage) {
151         _bodyPage = bodyPage;
152     }
153 
154     public String getEndPage() {
155         if (Validator.isNull(_endPage)) {
156             return _END_PAGE;
157         }
158         else {
159             return _endPage;
160         }
161     }
162 
163     public void setEndPage(String endPage) {
164         _endPage = endPage;
165     }
166 
167     public void setList(List<?> list) {
168         _list = list;
169     }
170 
171     public void setListType(String listType) {
172     }
173 
174     public void setRowLength(String rowLength) {
175         _rowLength = GetterUtil.getInteger(rowLength);
176     }
177 
178     public void setRowPadding(String rowPadding) {
179         _rowPadding = rowPadding;
180     }
181 
182     public void setRowValign(String rowValign) {
183         _rowValign = rowValign;
184     }
185 
186     public void setRowBreak(String rowBreak) {
187         _rowBreak = HtmlUtil.unescape(rowBreak);
188     }
189 
190     public void setWidth(String width) {
191         _width = width;
192     }
193 
194     private static final String _START_PAGE =
195         "/html/taglib/ui/table_iterator/start.jsp";
196 
197     private static final String _BODY_PAGE =
198         "/html/taglib/ui/table_iterator/body.jsp";
199 
200     private static final String _END_PAGE =
201         "/html/taglib/ui/table_iterator/end.jsp";
202 
203     private String _startPage;
204     private String _bodyPage;
205     private String _endPage;
206     private List<?> _list;
207     private int _listPos;
208     private int _rowLength;
209     private String _rowPadding = "0";
210     private String _rowValign = "middle";
211     private String _rowBreak = "<br />";
212     private String _width = StringPool.BLANK;
213 
214 }