View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.web.page;
24  
25  import java.io.IOException;
26  import java.util.Collection;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.jsp.JspException;
29  import javax.servlet.jsp.JspTagException;
30  import javax.servlet.jsp.PageContext;
31  import org.apache.struts.taglib.TagUtils;
32  
33  /**
34   * TODO documentation
35   * 
36   * @author rlogiacco
37   * @jsp.tag name="paginate" body-content="JSP"
38   *          tei-class="net.smartlab.web.page.PaginateTei" display-name=""
39   *          description=""
40   */
41  public class PaginateTag extends AbstractTag {
42  
43  	/* # net.smartlab.web.page.PaginateTei tei */
44  	/**
45  	 * TODO documentation Comment for <code>PARAMETER</code>
46  	 */
47  	public final static String PARAMETER = "net.smartlab.web.page";
48  
49  	/**
50  	 * TODO documentation Comment for <code>paginator</code>
51  	 * 
52  	 * @directed true
53  	 */
54  	private Paginator paginator = null;
55  
56  	/**
57  	 * Attribute name.
58  	 */
59  	private String name = null;
60  
61  	/**
62  	 * Attribute property name.
63  	 */
64  	private String property = null;
65  
66  	/**
67  	 * HTML request parameter name.
68  	 */
69  	private String parameter;
70  
71  	/**
72  	 * HTML <code>href</code>
73  	 */
74  	private String href = "";
75  
76  	/**
77  	 * HTML form name.
78  	 */
79  	private String form = null;
80  
81  	/**
82  	 * Elements per page.
83  	 */
84  	private int size = Paginator.UNLIMITED_ITEMS;
85  
86  	/**
87  	 * Showed pages references.
88  	 */
89  	private int pages = Paginator.UNLIMITED_PAGES;
90  
91  	/**
92  	 * HTML anchor <code>href</code> filtering.
93  	 */
94  	private boolean filter = false;
95  
96  
97  	/**
98  	 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
99  	 */
100 	public int doStartTag() throws JspException {
101 		this.parameter = PARAMETER + '.' + name;
102 		if (property != null) {
103 			this.parameter += '.' + property;
104 		}
105 		String index = ((HttpServletRequest)context.getRequest()).getParameter(parameter);
106 		Object attribute = TagUtils.getInstance().lookup(context, name, property, "request");
107 		if (attribute instanceof Paginator) {
108 			this.paginator = (Paginator)attribute;
109 		} else if (attribute instanceof Collection) {
110 			this.paginator = new CollectionPaginator((Collection)attribute);
111 			context.setAttribute(name, this.paginator, PageContext.PAGE_SCOPE);
112 		}
113 		context.setAttribute("paginator", this.paginator, PageContext.PAGE_SCOPE);
114 		if (size == Paginator.UNLIMITED_ITEMS) {
115 			paginator.setPageSize(paginator.getCount());
116 		} else {
117 			paginator.setPageSize(size);
118 		}
119 		paginator.setPages(pages);
120 		if (index != null && index.length() > 0) {
121 			paginator.setPage(Integer.parseInt(index));
122 		} else {
123 			paginator.setPage(1);
124 		}
125 		if (form != null) {
126 			try {
127 				context.getOut().println("<input type='hidden' name='" + parameter + "'/>");
128 				context.getOut().println("<script type=\"text/javascript\">");
129 				context.getOut().println("//<![CDATA[");
130 				context.getOut().println("  function paginate(page) {");
131 				context.getOut().println(
132 						"    document.forms['" + form + "'].elements['" + parameter + "'].value = page;");
133 				context.getOut().println("    document.forms['" + form + "'].action = '';");
134 				context.getOut().println("    document.forms['" + form + "'].submit();");
135 				context.getOut().println("  }");
136 				context.getOut().println("//]]>");
137 				context.getOut().println("</script>");
138 			} catch (IOException ioe) {
139 				throw new JspTagException(ioe);
140 			}
141 		}
142 		return EVAL_BODY_INCLUDE;
143 	}
144 
145 	/**
146 	 * Sets the name of the attribute containing the paginator instance.
147 	 * 
148 	 * @param name the name of the attribute containing the paginator instance.
149 	 * @jsp.attribute required="true" rtexprvalue="false" type="String"
150 	 *                description="Name of the attribute containing the
151 	 *                paginator instance"
152 	 */
153 	public void setName(String name) {
154 		this.name = name;
155 	}
156 
157 	/**
158 	 * Sets the name of the attribute property containing the paginator
159 	 * instance.
160 	 * 
161 	 * @param property the name of the attribute property containing the
162 	 *        paginator instance
163 	 * @jsp.attribute required="false" rtexprvalue="false" type="String"
164 	 *                description="Name of the attribute property containing the
165 	 *                paginator instance"
166 	 */
167 	public void setProperty(String property) {
168 		this.property = property;
169 	}
170 
171 	/**
172 	 * Returns the href part to create an anchor.
173 	 * 
174 	 * @return the href part to create an anchor.
175 	 */
176 	protected String getHref() {
177 		if (form == null) {
178 			String query = ((HttpServletRequest)context.getRequest()).getQueryString();
179 			if (query != null) {
180 				int start = query.indexOf(parameter + '=');
181 				int end = query.length();
182 				if (start > -1) {
183 					end = query.indexOf("&", start);
184 					if (end > -1) {
185 						query = query.substring(0, start) + query.substring(end + 1) + '&' + parameter + '=';
186 					} else {
187 						query = query.substring(0, start + parameter.length() + 1);
188 					}
189 				} else {
190 					query = query + '&' + parameter + '=';
191 				}
192 			} else {
193 				query = parameter + '=';
194 			}
195 			if (filter) {
196 				return TagUtils.getInstance().filter(href + '?' + query);
197 			} else {
198 				return href + '?' + query;
199 			}
200 		} else {
201 			return href.substring(1);
202 		}
203 	}
204 
205 	/**
206 	 * Sets the url where pagination actions are submitted: leave blank or not
207 	 * specified to use the same page.
208 	 * 
209 	 * @param href
210 	 * @jsp.attribute required="false" rtexprvalue="true" type="String"
211 	 *                description="The url where pagination actions are
212 	 *                submitted: leave blank or not specified to use the same
213 	 *                page"
214 	 */
215 	public void setHref(String href) {
216 		this.href = href;
217 	}
218 
219 	/**
220 	 * Sets the name of form to use to vehicle pagination informations.
221 	 * 
222 	 * @param form
223 	 * @jsp.attribute required="false" rtexprvalue="true" type="String"
224 	 *                description="The name of form to use to vehicle pagination
225 	 *                informations"
226 	 */
227 	public void setForm(String form) {
228 		this.form = form;
229 	}
230 
231 	/**
232 	 * Sets the number of items per page.
233 	 * 
234 	 * @param size
235 	 * @jsp.attribute required="false" rtexprvalue="true" type="int"
236 	 *                description="Number of items per page"
237 	 */
238 	public void setSize(int size) {
239 		this.size = size;
240 	}
241 
242 	/**
243 	 * Sets the maximum number of pages displayed in a row.
244 	 * 
245 	 * @param pages
246 	 * @jsp.attribute required="false" rtexprvalue="true" type="int"
247 	 *                description="Maximum number of pages displayed in a row"
248 	 */
249 	public void setPages(int pages) {
250 		this.pages = pages;
251 	}
252 
253 	/**
254 	 * @see net.smartlab.web.page.AbstractTag#getPaginator()
255 	 */
256 	protected Paginator getPaginator() {
257 		return paginator;
258 	}
259 
260 	/**
261 	 * Checks if the pagination involves a form. Pagination with forms uses an
262 	 * hidden field to pass pagination infos, otherwise a request parameter is
263 	 * used.
264 	 * 
265 	 * @return <code>true</code> if the pagination involves a form,
266 	 *         <code>false</code> otherwise.
267 	 */
268 	protected boolean isForm() {
269 		return form != null;
270 	}
271 
272 	/**
273 	 * Requires HTML compliance on href query part.
274 	 * 
275 	 * @param filter <code>true</code> if HTML compliance on href query part is
276 	 *        required.
277 	 * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
278 	 *                description="Requires HTML compliance on href query part"
279 	 */
280 	public void setFilter(boolean filter) {
281 		this.filter = filter;
282 	}
283 }