KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxanywhere > LinkTag


1 /*
2  * Copyright (c) Constantin Mircea Moisei 2005 for AjaxAnywhere
3  *
4  * $Header: /cvsroot/ajaxanywhere/ajaxAnywhere/src/java/org/ajaxanywhere/LinkTag.java,v 1.6 2006/05/28 22:47:36 shevit Exp $
5  * $Revision: 1.6 $
6  * $Date: 2006/05/28 22:47:36 $
7  *
8  */

9 package org.ajaxanywhere;
10
11
12 import java.text.MessageFormat JavaDoc;
13
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16 import javax.servlet.jsp.JspException JavaDoc;
17 import javax.servlet.jsp.JspWriter JavaDoc;
18 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
19 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
20
21 /**
22  * <b>org.ajaxanywhere.LinkTag</b> <br>
23  *
24  * It's for the failover mechanism. It's going to be transparent for the user how it works.
25  *
26  * <p><u>Usage</u></p>
27  * <code>
28  * <aa:link HREF="../../../some.url" zones="content,menu"/>Ajax Link<aa:link><br>
29  * <aa:link HREF="../../../some.url" zones="content,menu" functionName="ajaxanywhere.getAJAXAndClean" />Ajax Link<aa:link><br>
30  * <aa:link HREF="../../../some.url" zones="zone1, zone2" functionName="openLinkTop" style="color:red" styleClass="class">Ajax Link</aa:link>
31  * </code>
32  *
33  * <p>The tag will expand to <br>
34  * <a HREF="../../../some.url" onclick="ajaxanywhere.getAjax( '/some.url', 'content,menu');return false;">Ajax Link</a><br>
35   * <a HREF="../../../some.url" onclick="ajaxanywhere.getAJAXAndClean( '/some.url', 'content,menu');return false;">Ajax Link</a><br>
36  * <a HREF="../../../some.url" onclick="openLinkTop( '/some.url', 'content,menu');return false;" style="color:red" class="class">Ajax Link</a><br>
37  *
38  * </p>
39  *
40  * <p>getAJAXAndClean and openLinkTop are given as an example and are not a part of AjajxAnywhere distribution</p>
41  *
42  * @author Constantin Moisei
43  * @version $Revision: 1.6 $
44  * @since Feb 18, 2006 10:48:52 AM
45  *
46  * @jsp.tag name = "link"
47  *
48  */

49 public class LinkTag extends BodyTagSupport JavaDoc //org.apache.taglibs.standard.tag.el.core.UrlTag
50
{
51
52     /**
53      * Comment for <code>serialVersionUID</code> long
54      */

55     private static final long serialVersionUID = 1927146406417618943L;
56
57     public static final String JavaDoc LINK_HTML_START = "<a HREF=\"{0}\" onclick=\"{1}\" {2} >";
58     public static final String JavaDoc LINK_HTML_END = "</a>";
59     public static final String JavaDoc DEFAULT_FUNCTION_NAME = "ajaxAnywhere.getAJAX";
60
61     //attributes
62
private String JavaDoc functionName;
63     private String JavaDoc zones;
64     private String JavaDoc href;
65     private String JavaDoc styleClass;
66     private String JavaDoc style;
67     
68
69     public LinkTag( )
70     {
71         super();
72         functionName = DEFAULT_FUNCTION_NAME;
73     }
74
75     /**
76      * @return Returns the function String.
77      */

78     public String JavaDoc getFunctionName()
79     {
80         return functionName;
81     }
82
83     /**
84      * @param function
85      * The function to set. Type String
86      * @jsp.attribute required="false"
87      * rtexprvalue="true"
88      * type="java.lang.String"
89      */

90     public void setFunctionName( String JavaDoc function )
91     {
92         this.functionName = function;
93     }
94
95     private Object JavaDoc buildOnClick()
96     {
97         StringBuffer JavaDoc strbuff = new StringBuffer JavaDoc( this.getFunctionName() );
98         strbuff.append( "('" );
99         strbuff.append( buildUrl() );
100         strbuff.append( "', '" );
101         strbuff.append( this.getZones() );
102         strbuff.append( "')" );
103         return strbuff.toString();
104     }
105     /**
106      *
107      * @return
108      * Object
109      */

110     private Object JavaDoc buildOtherAttributes()
111     {
112         StringBuffer JavaDoc strbuff = new StringBuffer JavaDoc( );
113
114         if ( style != null && style.trim().length() > 0 )
115         {
116             strbuff.append( "style=\"" );
117             strbuff.append( style );
118             strbuff.append( "\"" );
119             strbuff.append( " " );
120         }
121         
122         if ( styleClass != null && styleClass.trim().length() > 0 )
123         {
124             strbuff.append( "class=\"" );
125             strbuff.append( styleClass );
126             strbuff.append( "\"" );
127             strbuff.append( " " );
128         }
129        
130         return strbuff.toString();
131     }
132
133     /**
134      * @return Returns the zones String.
135      */

136     public String JavaDoc getZones()
137     {
138         return zones;
139     }
140
141     /**
142      * @param zones
143      * The zones to set. Type String
144      * @jsp.attribute required="true"
145      * rtexprvalue="true"
146      * type="java.lang.String"
147      */

148     public void setZones( String JavaDoc zones )
149     {
150         this.zones = zones;
151     }
152     
153     /**
154      *
155      */

156     public int doStartTag() throws JspException JavaDoc
157     {
158         try
159         {
160             Object JavaDoc[] arguments = { buildUrl(), buildOnClick(), buildOtherAttributes() };
161             String JavaDoc html = MessageFormat.format( LINK_HTML_START, arguments );
162             pageContext.getOut().print( html );
163         }
164         catch ( Exception JavaDoc ex )
165         {
166             throw new JspException JavaDoc( ex );
167         }
168         return EVAL_BODY_BUFFERED;//EVAL_BODY_TAG;
169
}
170     
171     
172    
173
174      /**
175       * Build url using encodeURL and <code>href</code>
176       * @return
177       * Object
178       */

179     private Object JavaDoc buildUrl()
180     {
181         HttpServletRequest JavaDoc request = ( HttpServletRequest JavaDoc ) pageContext.getRequest();
182         return ( ( HttpServletResponse JavaDoc ) this.pageContext.getResponse() ).encodeURL( request.getContextPath() + "/" + href );
183     }
184
185     /*
186      * (non-Javadoc)
187      *
188      * @see org.apache.taglibs.standard.tag.common.core.UrlSupport#doEndTag()
189      *
190      * @return @throws JspException
191      *
192      */

193     public int doEndTag() throws JspException JavaDoc
194  {
195         
196         try
197         {
198             BodyContent JavaDoc bc = getBodyContent();
199             // get the bodycontent as string
200
String JavaDoc body = bc.getString();
201             // getJspWriter to output content
202
JspWriter JavaDoc out = bc.getEnclosingWriter();
203             if ( body != null )
204             {
205                 out.print( body );
206                 pageContext.getOut().print( LINK_HTML_END );
207             }
208         }
209         catch ( Exception JavaDoc e )
210         {
211             throw new JspException JavaDoc( e );
212         }
213         // return SKIP_BODY;
214
return EVAL_PAGE;
215     }
216
217     
218
219
220     
221     /**
222      * @return Returns the style String.
223      */

224     public String JavaDoc getStyle()
225     {
226         return style;
227     }
228
229     
230     /**
231      * @jsp.attribute required="false"
232      * rtexprvalue="true"
233      * type="java.lang.String"
234      * @param style The style to set. Type String
235      */

236     public void setStyle( String JavaDoc style )
237     {
238         this.style = style;
239     }
240
241     
242     /**
243      * @return Returns the styleClass String.
244      */

245     public String JavaDoc getStyleClass()
246     {
247         return styleClass;
248     }
249
250     
251     /**
252      * @jsp.attribute required="false"
253      * rtexprvalue="true"
254      * type="java.lang.String"
255      * @param styleClass The styleClass to set. Type String
256      */

257     public void setStyleClass( String JavaDoc styleClass )
258     {
259         this.styleClass = styleClass;
260     }
261
262     
263     /**
264      * @return Returns the href String.
265      */

266     public String JavaDoc getHref()
267     {
268         return href;
269     }
270
271     
272     /**
273      * @param href The href to set. Type String
274      * @jsp.attribute required="false"
275      * rtexprvalue="true"
276      * type="java.lang.String"
277      */

278     public void setHref( String JavaDoc href )
279     {
280         this.href = href;
281     }
282
283 }
284
285
Popular Tags