001    /**
002     * Copyright (c) 2000-2013 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 doAfterBody() throws JspException {
037                    try {
038                            HttpServletRequest request =
039                                    (HttpServletRequest)pageContext.getRequest();
040    
041                            request.setAttribute(
042                                    "liferay-ui:table-iterator:listPos", String.valueOf(_listPos));
043    
044                            PortalIncludeUtil.include(pageContext, getBodyPage());
045    
046                            _listPos++;
047    
048                            if (_listPos < _list.size()) {
049                                    pageContext.setAttribute(
050                                            "tableIteratorObj", _list.get(_listPos));
051                                    pageContext.setAttribute(
052                                            "tableIteratorPos", new Integer(_listPos));
053    
054                                    return EVAL_BODY_AGAIN;
055                            }
056                            else {
057                                    return SKIP_BODY;
058                            }
059                    }
060                    catch (Exception e) {
061                            throw new JspException(e);
062                    }
063            }
064    
065            @Override
066            public int doEndTag() throws JspException {
067                    try {
068                            if (_list.size() > 0) {
069                                    PortalIncludeUtil.include(pageContext, getEndPage());
070                            }
071    
072                            return EVAL_PAGE;
073                    }
074                    catch (Exception e) {
075                            throw new JspException(e);
076                    }
077                    finally {
078                            if (!ServerDetector.isResin()) {
079                                    _startPage = null;
080                                    _bodyPage = null;
081                                    _endPage = null;
082                                    _list = null;
083                                    _listPos = 0;
084                                    _rowLength = 0;
085                                    _rowPadding = "0";
086                                    _rowValign = "middle";
087                                    _rowBreak = null;
088                            }
089                    }
090            }
091    
092            @Override
093            public int doStartTag() throws JspException {
094                    try {
095                            if (_list.size() > 0) {
096                                    HttpServletRequest request =
097                                            (HttpServletRequest)pageContext.getRequest();
098    
099                                    request.setAttribute("liferay-ui:table-iterator:list", _list);
100                                    request.setAttribute(
101                                            "liferay-ui:table-iterator:rowLength",
102                                            String.valueOf(_rowLength));
103                                    request.setAttribute(
104                                            "liferay-ui:table-iterator:rowPadding", _rowPadding);
105                                    request.setAttribute(
106                                            "liferay-ui:table-iterator:rowValign", _rowValign);
107                                    request.setAttribute(
108                                            "liferay-ui:table-iterator:rowBreak", _rowBreak);
109                                    request.setAttribute("liferay-ui:table-iterator:width", _width);
110    
111                                    PortalIncludeUtil.include(pageContext, getStartPage());
112    
113                                    pageContext.setAttribute(
114                                            "tableIteratorObj", _list.get(_listPos));
115                                    pageContext.setAttribute(
116                                            "tableIteratorPos", new Integer(_listPos));
117    
118                                    return EVAL_BODY_INCLUDE;
119                            }
120                            else {
121                                    return SKIP_BODY;
122                            }
123                    }
124                    catch (Exception e) {
125                            throw new JspException(e);
126                    }
127            }
128    
129            public String getBodyPage() {
130                    if (Validator.isNull(_bodyPage)) {
131                            return _BODY_PAGE;
132                    }
133                    else {
134                            return _bodyPage;
135                    }
136            }
137    
138            public void setBodyPage(String bodyPage) {
139                    _bodyPage = bodyPage;
140            }
141    
142            public void setEndPage(String endPage) {
143                    _endPage = endPage;
144            }
145    
146            public void setList(List<?> list) {
147                    _list = list;
148            }
149    
150            public void setListType(String listType) {
151            }
152    
153            public void setRowBreak(String rowBreak) {
154                    _rowBreak = HtmlUtil.unescape(rowBreak);
155            }
156    
157            public void setRowLength(String rowLength) {
158                    _rowLength = GetterUtil.getInteger(rowLength);
159            }
160    
161            public void setRowPadding(String rowPadding) {
162                    _rowPadding = rowPadding;
163            }
164    
165            public void setRowValign(String rowValign) {
166                    _rowValign = rowValign;
167            }
168    
169            public void setStartPage(String startPage) {
170                    _startPage = startPage;
171            }
172    
173            public void setWidth(String width) {
174                    _width = width;
175            }
176    
177            protected String getEndPage() {
178                    if (Validator.isNull(_endPage)) {
179                            return _END_PAGE;
180                    }
181                    else {
182                            return _endPage;
183                    }
184            }
185    
186            protected String getStartPage() {
187                    if (Validator.isNull(_startPage)) {
188                            return _START_PAGE;
189                    }
190                    else {
191                            return _startPage;
192                    }
193            }
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 static final String _START_PAGE =
202                    "/html/taglib/ui/table_iterator/start.jsp";
203    
204            private String _bodyPage;
205            private String _endPage;
206            private List<?> _list;
207            private int _listPos;
208            private String _rowBreak = "<br />";
209            private int _rowLength;
210            private String _rowPadding = "0";
211            private String _rowValign = "middle";
212            private String _startPage;
213            private String _width = StringPool.BLANK;
214    
215    }