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