KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > grid > LastPageTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.grid;
17
18 import com.blandware.atleap.webapp.taglib.core.BaseHandlerTag;
19 import com.blandware.atleap.webapp.util.core.RequestUtil;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.struts.taglib.TagUtils;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspTagException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.util.Map JavaDoc;
30
31
32 /**
33  * <p>This tag renders to hyperlink to last page. Body of link will be body of this tag.
34  * <br />
35  * This tag is only valid when nested within <em>pager</em> tag.
36  * </p>
37  * <p><a HREF="LastPageTag.java.htm"><i>View Source</i></a></p>
38  *
39  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
40  * @version $Revision: 1.10 $ $Date: 2005/09/21 13:45:59 $
41  * @see FirstPageTag
42  * @see PrevPageTag
43  * @see NextPageTag
44  * @jsp.tag name="lastPage"
45  * body-content="scriptless"
46  * @see com.blandware.atleap.webapp.taglib.core.grid.PagerTag
47  */

48 public class LastPageTag extends BaseHandlerTag {
49     protected transient final Log log = LogFactory.getLog(LastPageTag.class);
50
51     /**
52      * Processes the tag
53      *
54      * @throws JspException
55      * @throws IOException
56      */

57     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
58
59         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
60
61         // This tag is only valid when nested within 'pager' tag, so check this
62
PagerTag parentPagerTag = (PagerTag) findAncestorWithClass(this, PagerTag.class);
63
64         if ( parentPagerTag == null ) {
65             JspTagException JavaDoc e = new JspTagException JavaDoc("Parent tag is invalid! This tag is only valid when nested within 'pager' tag");
66             throw e;
67         }
68
69         GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);
70
71         if ( log.isDebugEnabled() ) {
72             log.debug("Current page number: " + parentGridTag.getCurrentPageNumber());
73             log.debug("Last page number: " + parentPagerTag.getLastPageNumber());
74         }
75
76         // If current page is already last, skip body, otherwise generate hyperlink
77

78         if ( parentGridTag.getCurrentPageNumber().intValue() < parentPagerTag.getLastPageNumber().intValue() ) {
79
80             String JavaDoc pageUrl = parentGridTag.getPageUrl();
81
82             Map JavaDoc parameterMap = RequestUtil.prepareParameterMap(parentGridTag.getParameterMap());
83             parameterMap.put("pageNumber", parentPagerTag.getLastPageNumber().toString());
84             parameterMap.put("gridName", parentGridTag.getName());
85             pageUrl += "?" + RequestUtil.createQueryStringFromMap(parameterMap, "&");
86
87             StringWriter JavaDoc sw = new StringWriter JavaDoc();
88             StringBuffer JavaDoc sb = sw.getBuffer();
89             sb.append("<a HREF=\"").append(pageUrl).append("\"").append(prepareAttributes()).append(">");
90             getJspBody().invoke(sw);
91             sb.append("</a>");
92
93             TagUtils.getInstance().write(pageContext, sw.toString());
94         }
95     }
96
97 }
98
Popular Tags