KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ditchnet > jsp > taglib > tabs > handler > TabLinkTag


1 /*
2  * The contents of this file are subject to the GNU Lesser General Public
3  * License Version 2.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.gnu.org/copyleft/lesser.html
6  *
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  *
12  * Developer:
13  * Todd Ditchendorf, todd@ditchnet.org
14  *
15  */

16
17 /**
18  * @author Todd Ditchendorf
19  * @version 0.8
20  * @since 0.8
21  */

22 package org.ditchnet.jsp.taglib.tabs.handler;
23
24 import java.io.StringWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.JspContext JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import javax.servlet.jsp.JspException JavaDoc;
31 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
32
33
34 /**
35  * @author Todd Ditchendorf
36  * @since 0.8
37  *
38  * JSP Tag that renders an XHTML </code>a</code> element that handles targeting
39  * a specific tab on the current page or another page.
40  */

41 public final class TabLinkTag extends SimpleTagSupport JavaDoc {
42     
43     static final String JavaDoc QUESTION_MARK = "?";
44     static final String JavaDoc EQUALS = "=";
45     static final String JavaDoc AMPERSAND = "&amp;";
46     
47     public static final String JavaDoc PARAM_NAME_TAB_PANE_ID
48         = "orgDitchnetTabPaneId";
49     
50     private String JavaDoc id,href,selectedTabPaneId;
51     
52     public void setId(final String JavaDoc id) {
53         this.id = id;
54     }
55     
56     public void setHref(final String JavaDoc href) {
57         this.href = href;
58     }
59     
60     public void setSelectedTabPaneId(final String JavaDoc selectedTabPaneId) {
61         this.selectedTabPaneId = selectedTabPaneId;
62     }
63     
64     private boolean hrefHasQueryString() {
65         return href.indexOf( QUESTION_MARK ) > -1;
66     }
67     
68     private String JavaDoc getUrlParamString() {
69         String JavaDoc prefix;
70         if (hrefHasQueryString()) {
71             prefix = AMPERSAND;
72         } else {
73             prefix = QUESTION_MARK;
74         }
75         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
76         buff.append(prefix).append(PARAM_NAME_TAB_PANE_ID).append(EQUALS)
77             .append(selectedTabPaneId);
78         return buff.toString();
79     }
80
81     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
82                 
83         StringWriter JavaDoc evalResult = new StringWriter JavaDoc();
84         StringBuffer JavaDoc buff = evalResult.getBuffer();
85         
86         buff.append("\n<a ");
87         if (isHrefSameAsRequestURI()) {
88             buff.append("onclick=\"org.ditchnet.jsp.")
89                 .append("TabUtils.tabLinkClicked(event,'")
90                 .append(selectedTabPaneId)
91                 .append("'); return false;\" HREF=\"")
92                 .append(getRequest().getRequestURL());
93         } else {
94             buff.append("href=\"").append(href).append(getUrlParamString());
95         }
96         if (null != id && 0 != id.length()) {
97             buff.append(" id=\"").append(id).append("\"");
98         }
99         buff.append("\">");
100         
101         getJspBody().invoke(evalResult);
102         
103         buff.append("</a>\n");
104         
105         getJspContext().getOut().print(buff);
106         
107     }
108     
109     private boolean isHrefSameAsRequestURI() {
110         return null == href;
111     }
112     
113     private HttpServletRequest JavaDoc getRequest() {
114         PageContext JavaDoc pageContext = (PageContext JavaDoc)getJspContext();
115         HttpServletRequest JavaDoc request =
116             (HttpServletRequest JavaDoc)pageContext.getRequest();
117         return request;
118     }
119     
120 }
121
Popular Tags