1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.taglib.ui;
21  
22  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.HtmlUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  
28  import java.util.List;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.jsp.JspException;
32  import javax.servlet.jsp.tagext.TagSupport;
33  
34  /**
35   * <a href="TableIteratorTag.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class TableIteratorTag extends TagSupport {
41  
42      public int doStartTag() throws JspException {
43          try {
44              if (_list.size() > 0) {
45                  HttpServletRequest request =
46                      (HttpServletRequest)pageContext.getRequest();
47  
48                  request.setAttribute("liferay-ui:table-iterator:list", _list);
49                  request.setAttribute(
50                      "liferay-ui:table-iterator:rowLength",
51                      String.valueOf(_rowLength));
52                  request.setAttribute(
53                      "liferay-ui:table-iterator:rowPadding", _rowPadding);
54                  request.setAttribute(
55                      "liferay-ui:table-iterator:rowValign", _rowValign);
56                  request.setAttribute(
57                      "liferay-ui:table-iterator:rowBreak", _rowBreak);
58                  request.setAttribute("liferay-ui:table-iterator:width", _width);
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              HttpServletRequest request =
81                  (HttpServletRequest)pageContext.getRequest();
82  
83              request.setAttribute(
84                  "liferay-ui:table-iterator:listPos", String.valueOf(_listPos));
85  
86              PortalIncludeUtil.include(pageContext, getBodyPage());
87  
88              _listPos++;
89  
90              if (_listPos < _list.size()) {
91                  pageContext.setAttribute(
92                      "tableIteratorObj", _list.get(_listPos));
93                  pageContext.setAttribute(
94                      "tableIteratorPos", new Integer(_listPos));
95  
96                  return EVAL_BODY_AGAIN;
97              }
98              else {
99                  return SKIP_BODY;
100             }
101         }
102         catch (Exception e) {
103             throw new JspException(e);
104         }
105     }
106 
107     public int doEndTag() throws JspException {
108         try {
109             if (_list.size() > 0) {
110                 PortalIncludeUtil.include(pageContext, getEndPage());
111             }
112 
113             return EVAL_PAGE;
114         }
115         catch (Exception e) {
116             throw new JspException(e);
117         }
118         finally {
119             _startPage = null;
120             _bodyPage = null;
121             _endPage = null;
122             _list = null;
123             _listPos = 0;
124             _rowLength = 0;
125             _rowPadding = "0";
126             _rowValign = "middle";
127             _rowBreak = null;
128         }
129     }
130 
131     public String getStartPage() {
132         if (Validator.isNull(_startPage)) {
133             return _START_PAGE;
134         }
135         else {
136             return _startPage;
137         }
138     }
139 
140     public void setStartPage(String startPage) {
141         _startPage = startPage;
142     }
143 
144     public String getBodyPage() {
145         if (Validator.isNull(_bodyPage)) {
146             return _BODY_PAGE;
147         }
148         else {
149             return _bodyPage;
150         }
151     }
152 
153     public void setBodyPage(String bodyPage) {
154         _bodyPage = bodyPage;
155     }
156 
157     public String getEndPage() {
158         if (Validator.isNull(_endPage)) {
159             return _END_PAGE;
160         }
161         else {
162             return _endPage;
163         }
164     }
165 
166     public void setEndPage(String endPage) {
167         _endPage = endPage;
168     }
169 
170     public void setList(List<?> list) {
171         _list = list;
172     }
173 
174     public void setListType(String listType) {
175     }
176 
177     public void setRowLength(String rowLength) {
178         _rowLength = GetterUtil.getInteger(rowLength);
179     }
180 
181     public void setRowPadding(String rowPadding) {
182         _rowPadding = rowPadding;
183     }
184 
185     public void setRowValign(String rowValign) {
186         _rowValign = rowValign;
187     }
188 
189     public void setRowBreak(String rowBreak) {
190         _rowBreak = HtmlUtil.unescape(rowBreak);
191     }
192 
193     public void setWidth(String width) {
194         _width = width;
195     }
196 
197     private static final String _START_PAGE =
198         "/html/taglib/ui/table_iterator/start.jsp";
199 
200     private static final String _BODY_PAGE =
201         "/html/taglib/ui/table_iterator/body.jsp";
202 
203     private static final String _END_PAGE =
204         "/html/taglib/ui/table_iterator/end.jsp";
205 
206     private String _startPage;
207     private String _bodyPage;
208     private String _endPage;
209     private List<?> _list;
210     private int _listPos;
211     private int _rowLength;
212     private String _rowPadding = "0";
213     private String _rowValign = "middle";
214     private String _rowBreak = "<br />";
215     private String _width = StringPool.BLANK;
216 
217 }