KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxtags > tags > AjaxAreaTag


1 /**
2  * Copyright 2005 Darren L. Spurgeon
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 org.ajaxtags.tags;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.JspWriter JavaDoc;
22 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
23
24 import org.ajaxtags.helpers.AjaxHtmlHelper;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
27
28 /**
29  * Wraps any area on the page (with a DIV element) so that actions within that
30  * area refresh/load inside the defined DIV region rather than inside the whole
31  * browser window.
32  *
33  * @author Darren Spurgeon
34  * @version $Revision: 1.2 $ $Date: 2007/06/20 20:55:56 $ $Author: jenskapitza $
35  */

36 public class AjaxAreaTag extends BodyTagSupport JavaDoc {
37
38     /**
39      * serialVersionUID
40      */

41     private static final long serialVersionUID = -7940387487602588115L;
42
43     private String JavaDoc ajaxFlag = "ajax";
44
45     private String JavaDoc style;
46
47     private String JavaDoc styleClass;
48
49     private String JavaDoc ajaxAnchors = "false";
50
51     private boolean ajax = false;
52
53     /**
54      * @return Returns the ajaxFlag.
55      */

56     public String JavaDoc getAjaxFlag() {
57         return this.ajaxFlag;
58     }
59
60     /**
61      * @param ajaxFlag
62      * The ajaxFlag to set.
63      */

64     public void setAjaxFlag(String JavaDoc ajaxFlag) {
65         this.ajaxFlag = ajaxFlag;
66     }
67
68     /**
69      * @return Returns the style.
70      */

71     public String JavaDoc getStyle() {
72         return this.style;
73     }
74
75     /**
76      * @param style
77      * The style to set.
78      */

79     public void setStyle(String JavaDoc style) {
80         this.style = style;
81     }
82
83     /**
84      * @return Returns the styleClass.
85      */

86     public String JavaDoc getStyleClass() {
87         return this.styleClass;
88     }
89
90     /**
91      * @param styleClass
92      * The styleClass to set.
93      */

94     public void setStyleClass(String JavaDoc styleClass) {
95         this.styleClass = styleClass;
96     }
97
98     /**
99      * @return Returns the ajaxAnchors.
100      */

101     public String JavaDoc getAjaxAnchors() {
102         return this.ajaxAnchors;
103     }
104
105     /**
106      * @param ajaxAnchors
107      * The ajaxAnchors to set.
108      */

109     public void setAjaxAnchors(String JavaDoc ajaxAnchors) {
110         this.ajaxAnchors = ajaxAnchors;
111     }
112
113     /**
114      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
115      */

116     @Override JavaDoc
117     public int doStartTag() throws JspException JavaDoc {
118
119         initParameters();
120
121         if (this.ajax) {
122             try {
123                 pageContext.getOut().clearBuffer();
124                 // pageContext.getOut().clear();
125
// pageContext.getOut().flush();
126
} catch (IOException JavaDoc ex) {
127                 throw new JspException JavaDoc(ex.getMessage());
128             }
129         } else {
130             JspWriter JavaDoc out = pageContext.getOut();
131             try {
132                 out.print("<div");
133                 if (StringUtils.isNotBlank(id)) {
134                     out.print(" id='" + this.id + "'");
135                 }
136                 if (StringUtils.isNotBlank(styleClass)) {
137                     out.print(" class='" + this.styleClass + "'");
138                 }
139                 if (StringUtils.isNotBlank(style)) {
140                     out.print(" style='" + this.style + "'");
141                 }
142                 out.print(">");
143             } catch (IOException JavaDoc ex) {
144                 throw new JspException JavaDoc(ex.getMessage());
145             }
146         }
147         return EVAL_BODY_BUFFERED;
148     }
149
150     /**
151      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
152      */

153     @Override JavaDoc
154     public int doEndTag() throws JspException JavaDoc {
155         JspWriter JavaDoc out = pageContext.getOut();
156         String JavaDoc body = bodyContent.getString();
157         try {
158             body = processContent(body);
159             out.println(body);
160             if (!this.ajax) {
161                 out.println("</div>");
162             }
163         } catch (IOException JavaDoc ex) {
164             throw new JspException JavaDoc(ex.getMessage());
165         }
166         return this.ajax ? SKIP_PAGE : EVAL_PAGE;
167     }
168
169     /**
170      * @see javax.servlet.jsp.tagext.Tag#release()
171      */

172     @Override JavaDoc
173     public void release() {
174         this.ajaxFlag = null;
175         this.style = null;
176         this.ajaxAnchors = null;
177         this.styleClass = null;
178         super.release();
179     }
180
181     /**
182      * Set initial parameters.
183      *
184      * @throws JspException
185      */

186     protected void initParameters() throws JspException JavaDoc {
187         // Optional Properties
188
if (this.ajaxFlag != null) {
189             this.ajaxFlag = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
190                     "ajaxFlag", this.ajaxFlag, String JavaDoc.class, this,
191                     super.pageContext);
192         }
193         if (this.style != null) {
194             this.style = (String JavaDoc) ExpressionEvaluatorManager.evaluate("style",
195                     this.style, String JavaDoc.class, this, super.pageContext);
196         }
197         if (this.styleClass != null) {
198             this.styleClass = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
199                     "styleClass", this.styleClass, String JavaDoc.class, this,
200                     super.pageContext);
201         }
202         if (this.ajaxAnchors != null) {
203             this.ajaxAnchors = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
204                     "ajaxAnchors", this.ajaxAnchors, String JavaDoc.class, this,
205                     super.pageContext);
206         }
207
208         this.ajax = Boolean.valueOf(
209                 pageContext.getRequest().getParameter(this.ajaxFlag))
210                 .booleanValue();
211     }
212
213     /**
214      * Process content.
215      *
216      * @param content
217      * @return
218      */

219     protected String JavaDoc processContent(String JavaDoc content) {
220         return Boolean.valueOf(ajaxAnchors).booleanValue() ? AjaxHtmlHelper
221                 .ajaxAnchors(content, this.id, this.ajaxFlag) : content;
222     }
223
224 }
225
Popular Tags