1   /**
2    * Copyright (c) 2000-2008 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.GetterUtil;
27  import com.liferay.portal.kernel.util.HtmlUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  
31  import java.util.List;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.jsp.JspException;
35  import javax.servlet.jsp.tagext.TagSupport;
36  
37  /**
38   * <a href="TableIteratorTag.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
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             _startPage = null;
123             _bodyPage = null;
124             _endPage = null;
125             _list = null;
126             _listPos = 0;
127             _rowLength = 0;
128             _rowPadding = "0";
129             _rowValign = "middle";
130             _rowBreak = null;
131         }
132     }
133 
134     public String getStartPage() {
135         if (Validator.isNull(_startPage)) {
136             return _START_PAGE;
137         }
138         else {
139             return _startPage;
140         }
141     }
142 
143     public void setStartPage(String startPage) {
144         _startPage = startPage;
145     }
146 
147     public String getBodyPage() {
148         if (Validator.isNull(_bodyPage)) {
149             return _BODY_PAGE;
150         }
151         else {
152             return _bodyPage;
153         }
154     }
155 
156     public void setBodyPage(String bodyPage) {
157         _bodyPage = bodyPage;
158     }
159 
160     public String getEndPage() {
161         if (Validator.isNull(_endPage)) {
162             return _END_PAGE;
163         }
164         else {
165             return _endPage;
166         }
167     }
168 
169     public void setEndPage(String endPage) {
170         _endPage = endPage;
171     }
172 
173     public void setList(List<?> list) {
174         _list = list;
175     }
176 
177     public void setListType(String listType) {
178     }
179 
180     public void setRowLength(String rowLength) {
181         _rowLength = GetterUtil.getInteger(rowLength);
182     }
183 
184     public void setRowPadding(String rowPadding) {
185         _rowPadding = rowPadding;
186     }
187 
188     public void setRowValign(String rowValign) {
189         _rowValign = rowValign;
190     }
191 
192     public void setRowBreak(String rowBreak) {
193         _rowBreak = HtmlUtil.unescape(rowBreak);
194     }
195 
196     public void setWidth(String width) {
197         _width = width;
198     }
199 
200     private static final String _START_PAGE =
201         "/html/taglib/ui/table_iterator/start.jsp";
202 
203     private static final String _BODY_PAGE =
204         "/html/taglib/ui/table_iterator/body.jsp";
205 
206     private static final String _END_PAGE =
207         "/html/taglib/ui/table_iterator/end.jsp";
208 
209     private String _startPage;
210     private String _bodyPage;
211     private String _endPage;
212     private List<?> _list;
213     private int _listPos;
214     private int _rowLength;
215     private String _rowPadding = "0";
216     private String _rowValign = "middle";
217     private String _rowBreak = "<br />";
218     private String _width = StringPool.BLANK;
219 
220 }