1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.dao.search.SearchContainer;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.taglib.util.IncludeTag;
20  
21  import javax.servlet.http.HttpServletRequest;
22  
23  /**
24   * <a href="PageIteratorTag.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   */
28  public class PageIteratorTag extends IncludeTag {
29  
30      public void setCur(int cur) {
31          _cur = cur;
32      }
33  
34      public void setCurParam(String curParam) {
35          _curParam = curParam;
36      }
37  
38      public void setDelta(int delta) {
39          _delta = delta;
40      }
41  
42      public void setDeltaConfigurable(boolean deltaConfigurable) {
43          _deltaConfigurable = deltaConfigurable;
44      }
45  
46      public void setDeltaParam(String deltaParam) {
47          _deltaParam = deltaParam;
48      }
49  
50      public void setFormName(String formName) {
51          _formName = formName;
52      }
53  
54      public void setJsCall(String jsCall) {
55          _jsCall = jsCall;
56      }
57  
58      public void setMaxPages(int maxPages) {
59          _maxPages = maxPages;
60      }
61  
62      public void setTarget(String target) {
63          _target = target;
64      }
65  
66      public void setTotal(int total) {
67          _total = total;
68      }
69  
70      public void setType(String type) {
71          _type = type;
72      }
73  
74      public void setUrl(String url) {
75          _url = url;
76          _urlAnchor = StringPool.BLANK;
77  
78          int pos = _url.indexOf("#");
79  
80          if (pos != -1) {
81              _url = url.substring(0, pos);
82              _urlAnchor = url.substring(pos, url.length());
83          }
84  
85          if (_url.indexOf("?") == -1) {
86              _url += "?";
87          }
88          else if (!_url.endsWith("&")) {
89              _url += "&";
90          }
91      }
92  
93      protected void cleanUp() {
94          _cur = 0;
95          _curParam = null;
96          _delta = SearchContainer.DEFAULT_DELTA;
97          _deltaConfigurable = SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
98          _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
99          _formName = "fm";
100         _jsCall = null;
101         _maxPages = 10;
102         _pages = 0;
103         _target = "_self";
104         _total = 0;
105         _type = "regular";
106         _url = null;
107         _urlAnchor = null;
108     }
109 
110     protected String getEndPage() {
111         if (_pages > 1) {
112             return _END_PAGE;
113         }
114         else {
115             return null;
116         }
117     }
118 
119     protected String getStartPage() {
120         return _START_PAGE;
121     }
122 
123     protected void setAttributes(HttpServletRequest request) {
124         _pages = (int)Math.ceil((double)_total / _delta);
125 
126         request.setAttribute(
127             "liferay-ui:page-iterator:cur", String.valueOf(_cur));
128         request.setAttribute(
129             "liferay-ui:page-iterator:curParam", _curParam);
130         request.setAttribute(
131             "liferay-ui:page-iterator:delta", String.valueOf(_delta));
132         request.setAttribute(
133             "liferay-ui:page-iterator:deltaConfigurable",
134             String.valueOf(_deltaConfigurable));
135         request.setAttribute(
136             "liferay-ui:page-iterator:deltaParam", _deltaParam);
137         request.setAttribute(
138             "liferay-ui:page-iterator:formName", _formName);
139         request.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
140         request.setAttribute(
141             "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
142         request.setAttribute(
143             "liferay-ui:page-iterator:pages", String.valueOf(_pages));
144         request.setAttribute("liferay-ui:page-iterator:target", _target);
145         request.setAttribute(
146             "liferay-ui:page-iterator:total", String.valueOf(_total));
147         request.setAttribute("liferay-ui:page-iterator:type", _type);
148         request.setAttribute("liferay-ui:page-iterator:url", _url);
149         request.setAttribute(
150             "liferay-ui:page-iterator:urlAnchor", _urlAnchor);
151     }
152 
153     private static final String _END_PAGE =
154         "/html/taglib/ui/page_iterator/end.jsp";
155 
156     private static final String _START_PAGE =
157         "/html/taglib/ui/page_iterator/start.jsp";
158 
159     private int _cur;
160     private String _curParam;
161     private int _delta = SearchContainer.DEFAULT_DELTA;
162     private boolean _deltaConfigurable =
163         SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
164     private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
165     private String _formName = "fm";
166     private String _jsCall;
167     private int _maxPages = 10;
168     private int _pages;
169     private String _target = "_self";
170     private int _total;
171     private String _type = "regular";
172     private String _url;
173     private String _urlAnchor;
174 
175 }