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