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