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