1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.jsp.JspException;
31  import javax.servlet.jsp.tagext.TagSupport;
32  
33  /**
34   * <a href="PageIteratorTag.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class PageIteratorTag extends TagSupport {
40  
41      public int doStartTag() throws JspException {
42          try {
43              _pages = (int)Math.ceil((double)_total / _delta);
44  
45              HttpServletRequest request =
46                  (HttpServletRequest)pageContext.getRequest();
47  
48              request.setAttribute(
49                  "liferay-ui:page-iterator:formName", _formName);
50              request.setAttribute(
51                  "liferay-ui:page-iterator:curParam", _curParam);
52              request.setAttribute(
53                  "liferay-ui:page-iterator:curValue", String.valueOf(_curValue));
54              request.setAttribute(
55                  "liferay-ui:page-iterator:delta", String.valueOf(_delta));
56              request.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
57              request.setAttribute(
58                  "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
59              request.setAttribute("liferay-ui:page-iterator:target", _target);
60              request.setAttribute(
61                  "liferay-ui:page-iterator:total", String.valueOf(_total));
62              request.setAttribute("liferay-ui:page-iterator:url", _url);
63              request.setAttribute(
64                  "liferay-ui:page-iterator:urlAnchor", _urlAnchor);
65              request.setAttribute(
66                  "liferay-ui:page-iterator:pages", String.valueOf(_pages));
67              request.setAttribute("liferay-ui:page-iterator:type", _type);
68  
69              PortalIncludeUtil.include(pageContext, getStartPage());
70  
71              return EVAL_BODY_INCLUDE;
72          }
73          catch (Exception e) {
74              throw new JspException(e);
75          }
76      }
77  
78      public int doEndTag() throws JspException {
79          try {
80              if (_pages > 1) {
81                  PortalIncludeUtil.include(pageContext, getEndPage());
82              }
83  
84              return EVAL_PAGE;
85          }
86          catch (Exception e) {
87              throw new JspException(e);
88          }
89      }
90  
91      public String getStartPage() {
92          if (Validator.isNull(_startPage)) {
93              return _START_PAGE;
94          }
95          else {
96              return _startPage;
97          }
98      }
99  
100     public void setStartPage(String startPage) {
101         _startPage = startPage;
102     }
103 
104     public String getEndPage() {
105         if (Validator.isNull(_endPage)) {
106             return _END_PAGE;
107         }
108         else {
109             return _endPage;
110         }
111     }
112 
113     public void setEndPage(String endPage) {
114         _endPage = endPage;
115     }
116 
117     public void setFormName(String formName) {
118         _formName = formName;
119     }
120 
121     public void setCurParam(String curParam) {
122         _curParam = curParam;
123     }
124 
125     public void setCurValue(int curValue) {
126         _curValue = curValue;
127     }
128 
129     public void setDelta(int delta) {
130         _delta = delta;
131     }
132 
133     public void setJsCall(String jsCall) {
134         _jsCall = jsCall;
135     }
136 
137     public void setMaxPages(int maxPages) {
138         _maxPages = maxPages;
139     }
140 
141     public void setTarget(String target) {
142         _target = target;
143     }
144 
145     public void setTotal(int total) {
146         _total = total;
147     }
148 
149     public void setType(String type) {
150         _type = type;
151     }
152 
153     public void setUrl(String url) {
154         _url = url;
155         _urlAnchor = StringPool.BLANK;
156 
157         int pos = _url.indexOf("#");
158 
159         if (pos != -1) {
160             _url = url.substring(0, pos);
161             _urlAnchor = url.substring(pos, url.length());
162         }
163 
164         if (_url.indexOf("?") == -1) {
165             _url += "?";
166         }
167         else if (!_url.endsWith("&")) {
168             _url += "&";
169         }
170     }
171 
172     private static final String _START_PAGE =
173         "/html/taglib/ui/page_iterator/start.jsp";
174 
175     private static final String _END_PAGE =
176         "/html/taglib/ui/page_iterator/end.jsp";
177 
178     private String _startPage;
179     private String _endPage;
180     private String _formName = "fm";
181     private String _curParam;
182     private int _curValue;
183     private int _delta = 10;
184     private String _jsCall;
185     private int _maxPages = 10;
186     private String _target = "_self";
187     private int _total;
188     private String _type = "regular";
189     private String _url;
190     private String _urlAnchor;
191     private int _pages;
192 
193 }