001    /**
002     * Copyright (c) 2000-2010 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.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.taglib.util.IncludeTag;
020    
021    import javax.servlet.http.HttpServletRequest;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class PageIteratorTag extends IncludeTag {
027    
028            public void setCur(int cur) {
029                    _cur = cur;
030            }
031    
032            public void setCurParam(String curParam) {
033                    _curParam = curParam;
034            }
035    
036            public void setDelta(int delta) {
037                    _delta = delta;
038            }
039    
040            public void setDeltaConfigurable(boolean deltaConfigurable) {
041                    _deltaConfigurable = deltaConfigurable;
042            }
043    
044            public void setDeltaParam(String deltaParam) {
045                    _deltaParam = deltaParam;
046            }
047    
048            public void setFormName(String formName) {
049                    _formName = formName;
050            }
051    
052            public void setJsCall(String jsCall) {
053                    _jsCall = jsCall;
054            }
055    
056            public void setMaxPages(int maxPages) {
057                    _maxPages = maxPages;
058            }
059    
060            public void setTarget(String target) {
061                    _target = target;
062            }
063    
064            public void setTotal(int total) {
065                    _total = total;
066            }
067    
068            public void setType(String type) {
069                    _type = type;
070            }
071    
072            public void setUrl(String url) {
073                    _url = url;
074                    _urlAnchor = StringPool.BLANK;
075    
076                    int pos = _url.indexOf("#");
077    
078                    if (pos != -1) {
079                            _url = url.substring(0, pos);
080                            _urlAnchor = url.substring(pos, url.length());
081                    }
082    
083                    if (_url.indexOf("?") == -1) {
084                            _url += "?";
085                    }
086                    else if (!_url.endsWith("&")) {
087                            _url += "&";
088                    }
089            }
090    
091            protected void cleanUp() {
092                    _cur = 0;
093                    _curParam = null;
094                    _delta = SearchContainer.DEFAULT_DELTA;
095                    _deltaConfigurable = SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
096                    _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
097                    _formName = "fm";
098                    _jsCall = null;
099                    _maxPages = 10;
100                    _pages = 0;
101                    _target = "_self";
102                    _total = 0;
103                    _type = "regular";
104                    _url = null;
105                    _urlAnchor = null;
106            }
107    
108            protected String getEndPage() {
109                    if (_pages > 1) {
110                            return _END_PAGE;
111                    }
112                    else {
113                            return null;
114                    }
115            }
116    
117            protected String getStartPage() {
118                    return _START_PAGE;
119            }
120    
121            protected void setAttributes(HttpServletRequest request) {
122                    _pages = (int)Math.ceil((double)_total / _delta);
123    
124                    request.setAttribute(
125                            "liferay-ui:page-iterator:cur", String.valueOf(_cur));
126                    request.setAttribute(
127                            "liferay-ui:page-iterator:curParam", _curParam);
128                    request.setAttribute(
129                            "liferay-ui:page-iterator:delta", String.valueOf(_delta));
130                    request.setAttribute(
131                            "liferay-ui:page-iterator:deltaConfigurable",
132                            String.valueOf(_deltaConfigurable));
133                    request.setAttribute(
134                            "liferay-ui:page-iterator:deltaParam", _deltaParam);
135                    request.setAttribute(
136                            "liferay-ui:page-iterator:formName", _formName);
137                    request.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
138                    request.setAttribute(
139                            "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
140                    request.setAttribute(
141                            "liferay-ui:page-iterator:pages", String.valueOf(_pages));
142                    request.setAttribute("liferay-ui:page-iterator:target", _target);
143                    request.setAttribute(
144                            "liferay-ui:page-iterator:total", String.valueOf(_total));
145                    request.setAttribute("liferay-ui:page-iterator:type", _type);
146                    request.setAttribute("liferay-ui:page-iterator:url", _url);
147                    request.setAttribute(
148                            "liferay-ui:page-iterator:urlAnchor", _urlAnchor);
149            }
150    
151            private static final String _END_PAGE =
152                    "/html/taglib/ui/page_iterator/end.jsp";
153    
154            private static final String _START_PAGE =
155                    "/html/taglib/ui/page_iterator/start.jsp";
156    
157            private int _cur;
158            private String _curParam;
159            private int _delta = SearchContainer.DEFAULT_DELTA;
160            private boolean _deltaConfigurable =
161                    SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
162            private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
163            private String _formName = "fm";
164            private String _jsCall;
165            private int _maxPages = 10;
166            private int _pages;
167            private String _target = "_self";
168            private int _total;
169            private String _type = "regular";
170            private String _url;
171            private String _urlAnchor;
172    
173    }