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.ServletRequest;
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                  ServletRequest req = pageContext.getRequest();
49  
50                  req.setAttribute("liferay-ui:table-iterator:list", _list);
51                  req.setAttribute(
52                      "liferay-ui:table-iterator:rowLength",
53                      String.valueOf(_rowLength));
54                  req.setAttribute(
55                      "liferay-ui:table-iterator:rowPadding", _rowPadding);
56                  req.setAttribute(
57                      "liferay-ui:table-iterator:rowValign", _rowValign);
58                  req.setAttribute(
59                      "liferay-ui:table-iterator:rowBreak", _rowBreak);
60                  req.setAttribute("liferay-ui:table-iterator:width", _width);
61  
62                  PortalIncludeUtil.include(pageContext, getStartPage());
63  
64                  pageContext.setAttribute(
65                      "tableIteratorObj", _list.get(_listPos));
66                  pageContext.setAttribute(
67                      "tableIteratorPos", new Integer(_listPos));
68  
69                  return EVAL_BODY_INCLUDE;
70              }
71              else {
72                  return SKIP_BODY;
73              }
74          }
75          catch (Exception e) {
76              throw new JspException(e);
77          }
78      }
79  
80      public int doAfterBody() throws JspException {
81          try {
82              ServletRequest req = pageContext.getRequest();
83  
84              req.setAttribute(
85                  "liferay-ui:table-iterator:listPos", String.valueOf(_listPos));
86  
87              PortalIncludeUtil.include(pageContext, getBodyPage());
88  
89              _listPos++;
90  
91              if (_listPos < _list.size()) {
92                  pageContext.setAttribute(
93                      "tableIteratorObj", _list.get(_listPos));
94                  pageContext.setAttribute(
95                      "tableIteratorPos", new Integer(_listPos));
96  
97                  return EVAL_BODY_AGAIN;
98              }
99              else {
100                 return SKIP_BODY;
101             }
102         }
103         catch (Exception e) {
104             throw new JspException(e);
105         }
106     }
107 
108     public int doEndTag() throws JspException {
109         try {
110             if (_list.size() > 0) {
111                 PortalIncludeUtil.include(pageContext, getEndPage());
112             }
113 
114             return EVAL_PAGE;
115         }
116         catch (Exception e) {
117             throw new JspException(e);
118         }
119         finally {
120             _startPage = null;
121             _bodyPage = null;
122             _endPage = null;
123             _list = null;
124             _listPos = 0;
125             _rowLength = 0;
126             _rowPadding = "0";
127             _rowValign = "middle";
128             _rowBreak = null;
129         }
130     }
131 
132     public String getStartPage() {
133         if (Validator.isNull(_startPage)) {
134             return _START_PAGE;
135         }
136         else {
137             return _startPage;
138         }
139     }
140 
141     public void setStartPage(String startPage) {
142         _startPage = startPage;
143     }
144 
145     public String getBodyPage() {
146         if (Validator.isNull(_bodyPage)) {
147             return _BODY_PAGE;
148         }
149         else {
150             return _bodyPage;
151         }
152     }
153 
154     public void setBodyPage(String bodyPage) {
155         _bodyPage = bodyPage;
156     }
157 
158     public String getEndPage() {
159         if (Validator.isNull(_endPage)) {
160             return _END_PAGE;
161         }
162         else {
163             return _endPage;
164         }
165     }
166 
167     public void setEndPage(String endPage) {
168         _endPage = endPage;
169     }
170 
171     public void setList(List<?> list) {
172         _list = list;
173     }
174 
175     public void setListType(String listType) {
176     }
177 
178     public void setRowLength(String rowLength) {
179         _rowLength = GetterUtil.getInteger(rowLength);
180     }
181 
182     public void setRowPadding(String rowPadding) {
183         _rowPadding = rowPadding;
184     }
185 
186     public void setRowValign(String rowValign) {
187         _rowValign = rowValign;
188     }
189 
190     public void setRowBreak(String rowBreak) {
191         _rowBreak = HtmlUtil.unescape(rowBreak);
192     }
193 
194     public void setWidth(String width) {
195         _width = width;
196     }
197 
198     private static final String _START_PAGE =
199         "/html/taglib/ui/table_iterator/start.jsp";
200 
201     private static final String _BODY_PAGE =
202         "/html/taglib/ui/table_iterator/body.jsp";
203 
204     private static final String _END_PAGE =
205         "/html/taglib/ui/table_iterator/end.jsp";
206 
207     private String _startPage;
208     private String _bodyPage;
209     private String _endPage;
210     private List<?> _list;
211     private int _listPos;
212     private int _rowLength;
213     private String _rowPadding = "0";
214     private String _rowValign = "middle";
215     private String _rowBreak = "<br />";
216     private String _width = StringPool.BLANK;
217 
218 }