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.ServletRequest;
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              ServletRequest req = pageContext.getRequest();
46  
47              req.setAttribute("liferay-ui:page-iterator:formName", _formName);
48              req.setAttribute("liferay-ui:page-iterator:curParam", _curParam);
49              req.setAttribute(
50                  "liferay-ui:page-iterator:curValue", String.valueOf(_curValue));
51              req.setAttribute(
52                  "liferay-ui:page-iterator:delta", String.valueOf(_delta));
53              req.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
54              req.setAttribute(
55                  "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
56              req.setAttribute("liferay-ui:page-iterator:target", _target);
57              req.setAttribute(
58                  "liferay-ui:page-iterator:total", String.valueOf(_total));
59              req.setAttribute("liferay-ui:page-iterator:url", _url);
60              req.setAttribute("liferay-ui:page-iterator:urlAnchor", _urlAnchor);
61              req.setAttribute(
62                  "liferay-ui:page-iterator:pages", String.valueOf(_pages));
63              req.setAttribute("liferay-ui:page-iterator:type", _type);
64  
65              PortalIncludeUtil.include(pageContext, getStartPage());
66  
67              return EVAL_BODY_INCLUDE;
68          }
69          catch (Exception e) {
70              throw new JspException(e);
71          }
72      }
73  
74      public int doEndTag() throws JspException {
75          try {
76              if (_pages > 1) {
77                  PortalIncludeUtil.include(pageContext, getEndPage());
78              }
79  
80              return EVAL_PAGE;
81          }
82          catch (Exception e) {
83              throw new JspException(e);
84          }
85      }
86  
87      public String getStartPage() {
88          if (Validator.isNull(_startPage)) {
89              return _START_PAGE;
90          }
91          else {
92              return _startPage;
93          }
94      }
95  
96      public void setStartPage(String startPage) {
97          _startPage = startPage;
98      }
99  
100     public String getEndPage() {
101         if (Validator.isNull(_endPage)) {
102             return _END_PAGE;
103         }
104         else {
105             return _endPage;
106         }
107     }
108 
109     public void setEndPage(String endPage) {
110         _endPage = endPage;
111     }
112 
113     public void setFormName(String formName) {
114         _formName = formName;
115     }
116 
117     public void setCurParam(String curParam) {
118         _curParam = curParam;
119     }
120 
121     public void setCurValue(int curValue) {
122         _curValue = curValue;
123     }
124 
125     public void setDelta(int delta) {
126         _delta = delta;
127     }
128 
129     public void setJsCall(String jsCall) {
130         _jsCall = jsCall;
131     }
132 
133     public void setMaxPages(int maxPages) {
134         _maxPages = maxPages;
135     }
136 
137     public void setTarget(String target) {
138         _target = target;
139     }
140 
141     public void setTotal(int total) {
142         _total = total;
143     }
144 
145     public void setType(String type) {
146         _type = type;
147     }
148 
149     public void setUrl(String url) {
150         _url = url;
151         _urlAnchor = StringPool.BLANK;
152 
153         int pos = _url.indexOf("#");
154 
155         if (pos != -1) {
156             _url = url.substring(0, pos);
157             _urlAnchor = url.substring(pos, url.length());
158         }
159 
160         if (_url.indexOf("?") == -1) {
161             _url += "?";
162         }
163         else if (!_url.endsWith("&")) {
164             _url += "&";
165         }
166     }
167 
168     private static final String _START_PAGE =
169         "/html/taglib/ui/page_iterator/start.jsp";
170 
171     private static final String _END_PAGE =
172         "/html/taglib/ui/page_iterator/end.jsp";
173 
174     private String _startPage;
175     private String _endPage;
176     private String _formName = "fm";
177     private String _curParam;
178     private int _curValue;
179     private int _delta = 10;
180     private String _jsCall;
181     private int _maxPages = 10;
182     private String _target = "_self";
183     private int _total;
184     private String _type = "regular";
185     private String _url;
186     private String _urlAnchor;
187     private int _pages;
188 
189 }