KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
26
27 import au.id.jericho.lib.html.Attribute;
28 import au.id.jericho.lib.html.Attributes;
29 import au.id.jericho.lib.html.Element;
30 import au.id.jericho.lib.html.HTMLElementName;
31 import au.id.jericho.lib.html.OutputDocument;
32 import au.id.jericho.lib.html.Segment;
33 import au.id.jericho.lib.html.Source;
34 import au.id.jericho.lib.html.StartTag;
35
36 /**
37  * Wraps a DisplayTag (http://displaytag.org) table, enabling AJAX capabilities.
38  * In the process, anchors in the navigation are rewritten on the fly so that
39  * the DisplayTag table refreshes within the same region on the page without a
40  * full-page reload.
41  *
42  * @author Darren Spurgeon
43  * @version $Revision: 1.4 $ $Date: 2007/07/08 17:52:30 $ $Author: jenskapitza $
44  */

45 public class AjaxDisplayTag extends AjaxAreaTag {
46
47     private String JavaDoc pagelinksClass = "pagelinks";
48     private String JavaDoc tableClass = "displaytag";
49     private String JavaDoc columnClass = "sortable";
50     private String JavaDoc baseUrl = "";
51     private String JavaDoc postFunction = null;
52     private String JavaDoc preFunction = null;
53     private String JavaDoc parameters;
54
55     public String JavaDoc getPreFunction() {
56         return preFunction;
57     }
58
59     public void setPreFunction(String JavaDoc preFunction) {
60         this.preFunction = preFunction;
61     }
62
63     /**
64      * @return Returns the pagelinksClass.
65      */

66     public String JavaDoc getPagelinksClass() {
67         return this.pagelinksClass;
68     }
69
70     /**
71      * @param pagelinksClass
72      * The pagelinksClass to set.
73      */

74     public void setPagelinksClass(String JavaDoc pagelinksClass) {
75         this.pagelinksClass = pagelinksClass;
76     }
77
78     /**
79      * @return Returns the postFunction.
80      */

81     public String JavaDoc getPostFunction() {
82         return postFunction;
83     }
84
85     /**
86      * @param postFunction
87      * javascript function name to be called after the Ajax request.
88      */

89     public void setPostFunction(String JavaDoc postFunction) {
90         this.postFunction = postFunction;
91     }
92
93     /**
94      * @return Returns the tableClass.
95      */

96     public String JavaDoc getTableClass() {
97         return this.tableClass;
98     }
99
100     /**
101      * @param tableClass
102      * The tableClass to set.
103      */

104     public void setTableClass(String JavaDoc tableClass) {
105         this.tableClass = tableClass;
106     }
107
108     /**
109      * @return Returns the columnClass.
110      */

111     public String JavaDoc getColumnClass() {
112         return this.columnClass;
113     }
114
115     /**
116      * @param columnClass
117      * The columnClass to set.
118      */

119     public void setColumnClass(String JavaDoc columnClass) {
120         this.columnClass = columnClass;
121     }
122
123     /**
124      * @return Returns the baseUrl.
125      */

126     public String JavaDoc getBaseUrl() {
127         return this.baseUrl;
128     }
129
130     /**
131      * @param baseUrl
132      * The baseUrl to set.
133      */

134     public void setBaseUrl(String JavaDoc baseUrl) {
135         this.baseUrl = baseUrl;
136     }
137
138     public String JavaDoc getParameters() {
139         return parameters;
140     }
141
142     public void setParameters(String JavaDoc parameters) {
143         this.parameters = parameters;
144     }
145
146     /**
147      * @see javax.servlet.jsp.tagext.Tag#release()
148      */

149     @Override JavaDoc
150     public void release() {
151         this.pagelinksClass = null;
152         this.tableClass = null;
153         this.columnClass = null;
154         this.baseUrl = null;
155         this.postFunction = null;
156         this.parameters = null;
157         super.release();
158     }
159
160     /**
161      * @see org.ajaxtags.tags.AjaxAreaTag#initParameters()
162      */

163     @Override JavaDoc
164     protected void initParameters() throws JspException JavaDoc {
165         super.initParameters();
166         // Optional Properties
167
if (this.pagelinksClass != null) {
168             this.pagelinksClass = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
169                     "pagelinksClass", this.pagelinksClass, String JavaDoc.class, this,
170                     super.pageContext);
171         }
172         if (this.tableClass != null) {
173             this.tableClass = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
174                     "tableClass", this.tableClass, String JavaDoc.class, this,
175                     super.pageContext);
176         }
177         if (this.columnClass != null) {
178             this.columnClass = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
179                     "columnClass", this.columnClass, String JavaDoc.class, this,
180                     super.pageContext);
181         }
182         if (this.baseUrl != null) {
183             this.baseUrl = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
184                     "baseUrl", this.baseUrl, String JavaDoc.class, this,
185                     super.pageContext);
186         }
187         if (this.postFunction != null) {
188             this.postFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
189                     "postFunction", this.postFunction, String JavaDoc.class, this,
190                     super.pageContext);
191         }
192         if (this.preFunction != null) {
193             this.preFunction = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
194                     "preFunction", this.preFunction, String JavaDoc.class, this,
195                     super.pageContext);
196         }
197         if (this.parameters != null) {
198             this.parameters = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
199                     "parameters", this.parameters, String JavaDoc.class, this,
200                     super.pageContext);
201         }
202     }
203
204     /**
205      * @see org.ajaxtags.tags.AjaxAreaTag#processContent(java.lang.String)
206      */

207     @Override JavaDoc
208     protected String JavaDoc processContent(String JavaDoc content) {
209         Source source = new Source(content);
210         OutputDocument outputDocument = new OutputDocument(source);
211         // Rewrite pagination links
212
List JavaDoc<?> spans = source.findAllElements(HTMLElementName.SPAN);
213         for (Iterator JavaDoc<?> spanIter = spans.iterator(); spanIter.hasNext();) {
214             Element spanElement = (Element) spanIter.next();
215             Attributes spanAttributes = spanElement.getAttributes();
216             Attribute classAttribute = spanAttributes.get("class");
217             if (classAttribute != null
218                     && classAttribute.getValue() != null
219                     && classAttribute.getValue().equalsIgnoreCase(
220                             this.pagelinksClass)) {
221                 rewriteAnchors(spanElement, outputDocument);
222             }
223         }
224         // Rewrite Column sorting links
225
List JavaDoc<?> tables = source.findAllElements(HTMLElementName.TABLE);
226         for (Iterator JavaDoc<?> tableIter = tables.iterator(); tableIter.hasNext();) {
227             Element tableElement = (Element) tableIter.next();
228             Attributes tableAttributes = tableElement.getAttributes();
229             Attribute classAttribute = tableAttributes.get("class");
230             if (classAttribute != null
231                     && classAttribute.getValue() != null
232                     && classAttribute.getValue().equalsIgnoreCase(
233                             this.tableClass)) {
234                 List JavaDoc<?> theads = tableElement
235                         .findAllElements(HTMLElementName.THEAD);
236                 for (Iterator JavaDoc<?> theadIter = theads.iterator(); theadIter
237                         .hasNext();) {
238                     Element theadElement = (Element) theadIter.next();
239                     if (StringUtils.isNotBlank(this.columnClass)) {
240                         List JavaDoc<?> ths = theadElement
241                                 .findAllElements(HTMLElementName.TH);
242                         for (Iterator JavaDoc<?> thIter = ths.iterator(); thIter
243                                 .hasNext();) {
244                             Element columnElement = (Element) thIter.next();
245                             Attribute columnClassAttribute = columnElement
246                                     .getAttributes().get("class");
247                             if (columnClassAttribute != null
248                                     && columnClassAttribute.getValue() != null
249                                     && StringUtils.contains(
250                                             columnClassAttribute.getValue()
251                                                     .toLowerCase(),
252                                             this.columnClass.toLowerCase())) {
253                                 rewriteAnchors(columnElement, outputDocument);
254                             }
255                         }
256                     } else {
257                         rewriteAnchors(theadElement, outputDocument);
258                     }
259                 }
260             }
261         }
262
263         return outputDocument.toString();
264     }
265
266     /**
267      * Rewrite all anchors in this segment.
268      *
269      * @param segment
270      * @param outputDocument
271      */

272     @SuppressWarnings JavaDoc("unchecked")
273     protected void rewriteAnchors(Segment segment, OutputDocument outputDocument) {
274         List JavaDoc<?> anchors = segment.findAllStartTags(HTMLElementName.A);
275         for (Iterator JavaDoc<?> i = anchors.iterator(); i.hasNext();) {
276             StartTag anchorTag = (StartTag) i.next();
277             Attributes attributes = anchorTag.getAttributes();
278             Attribute hrefAttribute = attributes.get("href");
279             if (hrefAttribute == null)
280                 continue;
281             String JavaDoc href = hrefAttribute.getValue();
282             if (href == null)
283                 continue;
284             // include the baseURL so it loads the exact page with the table on
285
// it
286
if (StringUtils.isNotBlank(this.getBaseUrl())) {
287                 // Remove url, we are going to use baseUrl.
288
if (href.indexOf("?") > 0) {
289                     href = href.substring(href.indexOf("?"));
290                 }
291                 href = this.getBaseUrl() + href;
292             }
293
294             Map JavaDoc<String JavaDoc, String JavaDoc> attrreplace = outputDocument.replace(
295                     attributes, true);
296             // AttributesOutputSegment outputSegment = new
297
// AttributesOutputSegment(attributes, true);
298
OptionsBuilder onclickOptions = new OptionsBuilder();
299             if (this.postFunction != null) {
300                 onclickOptions.add("postFunction", this.postFunction, false);
301             }
302             if (this.parameters != null) {
303                 onclickOptions.add("parameters", this.parameters, true);
304             }
305
306             if (this.preFunction != null) {
307                 onclickOptions.add("preFunction", this.preFunction, false);
308             }
309
310             attrreplace.put("href", "javascript://nop/");
311
312             if (StringUtils.isNotBlank(this.getAjaxFlag())) {
313                 if (href.indexOf(this.getAjaxFlag() + "=") < 0) {
314                     if (href.indexOf("?") >= 0) {
315                         href += "&";
316                     } else {
317                         href += "?";
318                     }
319                     href += this.getAjaxFlag() + "=true";
320                 }
321             }
322
323             if (href != null) {
324                 onclickOptions.add("href", href, true);
325             }
326
327             if (this.getId() != null) {
328                 onclickOptions.add("id", this.getId(), true);
329             }
330             // attrreplace.put("onclick", AjaxHtmlHelper.getOnclickAjax(this
331
// .getId(), href, this.getAjaxFlag(), onclickOptions));
332
//
333
attrreplace.put("onclick",
334                     getOnclickAjaxInvokePreFunction(onclickOptions));
335             // outputDocument.add(outputSegment);
336
}
337     }
338
339     private static final String JavaDoc getOnclickAjaxInvokePreFunction(
340             OptionsBuilder options) {
341         StringBuffer JavaDoc onclick = new StringBuffer JavaDoc(
342                 "new AjaxJspTag.PreFunctionUpdateInvoke(");
343
344         onclick.append("{");
345         onclick.append(options.toString());
346         onclick.append("}");
347         onclick.append("); return false;");
348
349         return onclick.toString();
350     }
351
352 }
353
Popular Tags