001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.ServerDetector;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.util.List;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.jsp.JspException;
028    import javax.servlet.jsp.tagext.TagSupport;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class TableIteratorTag extends TagSupport {
034    
035            @Override
036            public int doStartTag() throws JspException {
037                    try {
038                            if (_list.size() > 0) {
039                                    HttpServletRequest request =
040                                            (HttpServletRequest)pageContext.getRequest();
041    
042                                    request.setAttribute("liferay-ui:table-iterator:list", _list);
043                                    request.setAttribute(
044                                            "liferay-ui:table-iterator:rowLength",
045                                            String.valueOf(_rowLength));
046                                    request.setAttribute(
047                                            "liferay-ui:table-iterator:rowPadding", _rowPadding);
048                                    request.setAttribute(
049                                            "liferay-ui:table-iterator:rowValign", _rowValign);
050                                    request.setAttribute(
051                                            "liferay-ui:table-iterator:rowBreak", _rowBreak);
052                                    request.setAttribute("liferay-ui:table-iterator:width", _width);
053    
054                                    PortalIncludeUtil.include(pageContext, getStartPage());
055    
056                                    pageContext.setAttribute(
057                                            "tableIteratorObj", _list.get(_listPos));
058                                    pageContext.setAttribute(
059                                            "tableIteratorPos", new Integer(_listPos));
060    
061                                    return EVAL_BODY_INCLUDE;
062                            }
063                            else {
064                                    return SKIP_BODY;
065                            }
066                    }
067                    catch (Exception e) {
068                            throw new JspException(e);
069                    }
070            }
071    
072            @Override
073            public int doAfterBody() throws JspException {
074                    try {
075                            HttpServletRequest request =
076                                    (HttpServletRequest)pageContext.getRequest();
077    
078                            request.setAttribute(
079                                    "liferay-ui:table-iterator:listPos", String.valueOf(_listPos));
080    
081                            PortalIncludeUtil.include(pageContext, getBodyPage());
082    
083                            _listPos++;
084    
085                            if (_listPos < _list.size()) {
086                                    pageContext.setAttribute(
087                                            "tableIteratorObj", _list.get(_listPos));
088                                    pageContext.setAttribute(
089                                            "tableIteratorPos", new Integer(_listPos));
090    
091                                    return EVAL_BODY_AGAIN;
092                            }
093                            else {
094                                    return SKIP_BODY;
095                            }
096                    }
097                    catch (Exception e) {
098                            throw new JspException(e);
099                    }
100            }
101    
102            @Override
103            public int doEndTag() throws JspException {
104                    try {
105                            if (_list.size() > 0) {
106                                    PortalIncludeUtil.include(pageContext, getEndPage());
107                            }
108    
109                            return EVAL_PAGE;
110                    }
111                    catch (Exception e) {
112                            throw new JspException(e);
113                    }
114                    finally {
115                            if (!ServerDetector.isResin()) {
116                                    _startPage = null;
117                                    _bodyPage = null;
118                                    _endPage = null;
119                                    _list = null;
120                                    _listPos = 0;
121                                    _rowLength = 0;
122                                    _rowPadding = "0";
123                                    _rowValign = "middle";
124                                    _rowBreak = null;
125                            }
126                    }
127            }
128    
129            protected String getStartPage() {
130                    if (Validator.isNull(_startPage)) {
131                            return _START_PAGE;
132                    }
133                    else {
134                            return _startPage;
135                    }
136            }
137    
138            public void setStartPage(String startPage) {
139                    _startPage = startPage;
140            }
141    
142            public String getBodyPage() {
143                    if (Validator.isNull(_bodyPage)) {
144                            return _BODY_PAGE;
145                    }
146                    else {
147                            return _bodyPage;
148                    }
149            }
150    
151            public void setBodyPage(String bodyPage) {
152                    _bodyPage = bodyPage;
153            }
154    
155            protected String getEndPage() {
156                    if (Validator.isNull(_endPage)) {
157                            return _END_PAGE;
158                    }
159                    else {
160                            return _endPage;
161                    }
162            }
163    
164            public void setEndPage(String endPage) {
165                    _endPage = endPage;
166            }
167    
168            public void setList(List<?> list) {
169                    _list = list;
170            }
171    
172            public void setListType(String listType) {
173            }
174    
175            public void setRowLength(String rowLength) {
176                    _rowLength = GetterUtil.getInteger(rowLength);
177            }
178    
179            public void setRowPadding(String rowPadding) {
180                    _rowPadding = rowPadding;
181            }
182    
183            public void setRowValign(String rowValign) {
184                    _rowValign = rowValign;
185            }
186    
187            public void setRowBreak(String rowBreak) {
188                    _rowBreak = HtmlUtil.unescape(rowBreak);
189            }
190    
191            public void setWidth(String width) {
192                    _width = width;
193            }
194    
195            private static final String _START_PAGE =
196                    "/html/taglib/ui/table_iterator/start.jsp";
197    
198            private static final String _BODY_PAGE =
199                    "/html/taglib/ui/table_iterator/body.jsp";
200    
201            private static final String _END_PAGE =
202                    "/html/taglib/ui/table_iterator/end.jsp";
203    
204            private String _startPage;
205            private String _bodyPage;
206            private String _endPage;
207            private List<?> _list;
208            private int _listPos;
209            private int _rowLength;
210            private String _rowPadding = "0";
211            private String _rowValign = "middle";
212            private String _rowBreak = "<br />";
213            private String _width = StringPool.BLANK;
214    
215    }