KickJava   Java API By Example, From Geeks To Geeks.

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


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 first 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="FirstPageTag.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.9 $ $Date: 2005/09/21 13:45:59 $
41  * @see PrevPageTag
42  * @see NextPageTag
43  * @see LastPageTag
44  * @jsp.tag name="firstPage"
45  * body-content="scriptless"
46  * @see com.blandware.atleap.webapp.taglib.core.grid.PagerTag
47  */

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

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

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