001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public int doStartTag() throws JspException {
036                    try {
037                            if (_list.size() > 0) {
038                                    HttpServletRequest request =
039                                            (HttpServletRequest)pageContext.getRequest();
040    
041                                    request.setAttribute("liferay-ui:table-iterator:list", _list);
042                                    request.setAttribute(
043                                            "liferay-ui:table-iterator:rowLength",
044                                            String.valueOf(_rowLength));
045                                    request.setAttribute(
046                                            "liferay-ui:table-iterator:rowPadding", _rowPadding);
047                                    request.setAttribute(
048                                            "liferay-ui:table-iterator:rowValign", _rowValign);
049                                    request.setAttribute(
050                                            "liferay-ui:table-iterator:rowBreak", _rowBreak);
051                                    request.setAttribute("liferay-ui:table-iterator:width", _width);
052    
053                                    PortalIncludeUtil.include(pageContext, getStartPage());
054    
055                                    pageContext.setAttribute(
056                                            "tableIteratorObj", _list.get(_listPos));
057                                    pageContext.setAttribute(
058                                            "tableIteratorPos", new Integer(_listPos));
059    
060                                    return EVAL_BODY_INCLUDE;
061                            }
062                            else {
063                                    return SKIP_BODY;
064                            }
065                    }
066                    catch (Exception e) {
067                            throw new JspException(e);
068                    }
069            }
070    
071            public int doAfterBody() throws JspException {
072                    try {
073                            HttpServletRequest request =
074                                    (HttpServletRequest)pageContext.getRequest();
075    
076                            request.setAttribute(
077                                    "liferay-ui:table-iterator:listPos", String.valueOf(_listPos));
078    
079                            PortalIncludeUtil.include(pageContext, getBodyPage());
080    
081                            _listPos++;
082    
083                            if (_listPos < _list.size()) {
084                                    pageContext.setAttribute(
085                                            "tableIteratorObj", _list.get(_listPos));
086                                    pageContext.setAttribute(
087                                            "tableIteratorPos", new Integer(_listPos));
088    
089                                    return EVAL_BODY_AGAIN;
090                            }
091                            else {
092                                    return SKIP_BODY;
093                            }
094                    }
095                    catch (Exception e) {
096                            throw new JspException(e);
097                    }
098            }
099    
100            public int doEndTag() throws JspException {
101                    try {
102                            if (_list.size() > 0) {
103                                    PortalIncludeUtil.include(pageContext, getEndPage());
104                            }
105    
106                            return EVAL_PAGE;
107                    }
108                    catch (Exception e) {
109                            throw new JspException(e);
110                    }
111                    finally {
112                            if (!ServerDetector.isResin()) {
113                                    _startPage = null;
114                                    _bodyPage = null;
115                                    _endPage = null;
116                                    _list = null;
117                                    _listPos = 0;
118                                    _rowLength = 0;
119                                    _rowPadding = "0";
120                                    _rowValign = "middle";
121                                    _rowBreak = null;
122                            }
123                    }
124            }
125    
126            protected String getStartPage() {
127                    if (Validator.isNull(_startPage)) {
128                            return _START_PAGE;
129                    }
130                    else {
131                            return _startPage;
132                    }
133            }
134    
135            public void setStartPage(String startPage) {
136                    _startPage = startPage;
137            }
138    
139            public String getBodyPage() {
140                    if (Validator.isNull(_bodyPage)) {
141                            return _BODY_PAGE;
142                    }
143                    else {
144                            return _bodyPage;
145                    }
146            }
147    
148            public void setBodyPage(String bodyPage) {
149                    _bodyPage = bodyPage;
150            }
151    
152            protected String getEndPage() {
153                    if (Validator.isNull(_endPage)) {
154                            return _END_PAGE;
155                    }
156                    else {
157                            return _endPage;
158                    }
159            }
160    
161            public void setEndPage(String endPage) {
162                    _endPage = endPage;
163            }
164    
165            public void setList(List<?> list) {
166                    _list = list;
167            }
168    
169            public void setListType(String listType) {
170            }
171    
172            public void setRowLength(String rowLength) {
173                    _rowLength = GetterUtil.getInteger(rowLength);
174            }
175    
176            public void setRowPadding(String rowPadding) {
177                    _rowPadding = rowPadding;
178            }
179    
180            public void setRowValign(String rowValign) {
181                    _rowValign = rowValign;
182            }
183    
184            public void setRowBreak(String rowBreak) {
185                    _rowBreak = HtmlUtil.unescape(rowBreak);
186            }
187    
188            public void setWidth(String width) {
189                    _width = width;
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            private String _width = StringPool.BLANK;
211    
212    }