KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > renderer > FixedHeaderCollection


1 package fr.improve.struts.taglib.layout.renderer;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.jsp.JspException JavaDoc;
5 import javax.servlet.jsp.PageContext JavaDoc;
6 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
7
8 import fr.improve.struts.taglib.layout.collection.CollectionItemTag;
9 import fr.improve.struts.taglib.layout.collection.CollectionTag;
10 import fr.improve.struts.taglib.layout.el.Expression;
11 import fr.improve.struts.taglib.layout.util.BrowserUtil;
12 import fr.improve.struts.taglib.layout.util.CollectionInterface;
13 import fr.improve.struts.taglib.layout.util.IFooterRenderer;
14 import fr.improve.struts.taglib.layout.util.IMathCollectionRenderer;
15 import fr.improve.struts.taglib.layout.util.IMultiLevelHeaderRenderer;
16 import fr.improve.struts.taglib.layout.util.LayoutUtils;
17
18 /**
19  * New implementation of the CollectionInterface, with fix header for IE and Firefox.
20  * This solution generates different code for the two browsers, and don't use additional Javascript code.
21  *
22  * The height of the generated table are slightly different in IE and Firefox,
23  * and in IE, CSS border leads to strange displays...
24  *
25  * @author: Jean-NoŽl Ribette
26  */

27 public class FixedHeaderCollection implements CollectionInterface, IMathCollectionRenderer, IFooterRenderer, IMultiLevelHeaderRenderer {
28     protected CollectionTag collectionTag;
29     protected PageContext JavaDoc pageContext;
30     protected String JavaDoc styleClass;
31     protected boolean headerOpen = false;
32     
33     /**
34      * Initialize the renderer.
35      */

36     public void init(PageContext JavaDoc pg, String JavaDoc in_styleClass, TagSupport JavaDoc in_tag) throws JspException JavaDoc {
37         collectionTag = (CollectionTag) in_tag;
38         pageContext = pg;
39         styleClass = collectionTag.getStyleClass();
40     }
41     
42     /**
43      * Display a special message if the collection is empty.
44      */

45     public void doPrintEmptyCollection(StringBuffer JavaDoc out_buffer, String JavaDoc in_message) {
46         out_buffer.append("<tr><td colspan=\"");
47         out_buffer.append(collectionTag.getHeaders().size());
48         out_buffer.append("\" class=\"");
49         out_buffer.append(buildEmptyStyleClass(styleClass));
50         out_buffer.append("\">&nbsp;");
51         out_buffer.append(in_message);
52         out_buffer.append("</td></tr>");
53     }
54     /**
55      * Print the collection title.
56      */

57     public void doPrintTitle(StringBuffer JavaDoc buffer, String JavaDoc title) {
58         if (title!=null) {
59             if (styleClass!=null) {
60                 buffer.append("<p class=\"");
61                 buffer.append(styleClass);
62                 buffer.append("\">");
63             }
64             buffer.append(title);
65             if (styleClass!=null) {
66                 buffer.append("</p>");
67             }
68         }
69     }
70     /**
71      * Start the collection.
72      */

73     public void doStartPanel(StringBuffer JavaDoc buffer, String JavaDoc align, String JavaDoc width) {
74         // If height of width is set, generate a scrollar.
75
if (collectionTag.getWidth()!=null || collectionTag.getHeight()!=null) {
76             if (BrowserUtil.isIe((HttpServletRequest JavaDoc)pageContext.getRequest())) {
77                 buffer.append("<div style=\"");
78                 if (collectionTag.getWidth()!=null) {
79                     buffer.append("width:");
80                     buffer.append(collectionTag.getWidth());
81                     buffer.append("px;");
82                     if (!collectionTag.getWidth().endsWith("%")) {
83                         buffer.append("overflow-x:auto;");
84                     }
85                 }
86                 if (collectionTag.getHeight()!=null) {
87                     buffer.append("height:");
88                     buffer.append(collectionTag.getHeight());
89                     buffer.append("px;");
90                     buffer.append("overflow-y:auto;");
91                 }
92                 if (align!=null) {
93                     buffer.append("\" align=\"");
94                     buffer.append(align);
95                 }
96                 buffer.append("\">");
97             }
98         }
99         
100         // Start the table
101
buffer.append("<table width=\"100%\" cellspacing=\"0\"");
102         if (styleClass!=null) {
103             buffer.append(" class=\"");
104             buffer.append(styleClass);
105             buffer.append("\"");
106         }
107         if (collectionTag.getStyleId()!=null) {
108             buffer.append(" id=\"");
109             buffer.append(collectionTag.getStyleId());
110             buffer.append("\"");
111         }
112         if (collectionTag.getWidth()!=null || collectionTag.getHeight()!=null) {
113             if (!BrowserUtil.isIe((HttpServletRequest JavaDoc) pageContext.getRequest())) {
114                 buffer.append(" style=\"");
115                 if (collectionTag.getWidth()!=null) {
116                     buffer.append("width:");
117                     buffer.append(collectionTag.getWidth());
118                     buffer.append("px;");
119                 }
120                 if (collectionTag.getHeight()!=null) {
121                     buffer.append("height:");
122                     buffer.append(collectionTag.getHeight());
123                     buffer.append("px;");
124                 }
125                 buffer.append("\"");
126             }
127         }
128         buffer.append(">\n");
129     }
130     
131     /**
132      * Prepare to display the headers.
133      */

134     public void doStartHeaders(StringBuffer JavaDoc out_buffer) {
135         out_buffer.append("<tr");
136         if (collectionTag.getHeight()!=null) {
137             out_buffer.append(" style=\"position:relative;top:expression(this.offsetParent.scrollTop);\"");
138         }
139         out_buffer.append(">");
140     }
141     /**
142      * Display a header.
143      */

144     public void doPrintHeader(StringBuffer JavaDoc out_buffer, String JavaDoc in_header, String JavaDoc in_width, String JavaDoc in_sortUrl) {
145         out_buffer.append("<th");
146         if (styleClass!=null) {
147             out_buffer.append(" class=\"");
148             out_buffer.append(buildHeaderStyleClass(styleClass, collectionTag.getColumn()));
149             out_buffer.append("\"");
150         }
151         if (in_width!=null) {
152             out_buffer.append(" width=\"");
153             out_buffer.append(in_width);
154             out_buffer.append("\"");
155         }
156         out_buffer.append(">");
157                     
158         out_buffer.append(doPrintSortUrl(in_header, in_sortUrl));
159             
160         
161         out_buffer.append("</th>");
162     }
163     
164     /**
165      * Display an hyperlink to sort the column.
166      */

167     protected String JavaDoc doPrintSortUrl(String JavaDoc in_header, String JavaDoc in_sortUrl) {
168         if (in_sortUrl == null) {
169             return in_header;
170         }
171         else if (collectionTag.getSortPictogram().equalsIgnoreCase("none")) {
172             // pas d'image pour faire le tri, mais un lien sur le titre
173
StringBuffer JavaDoc lc_tempBuffer = new StringBuffer JavaDoc("<table border=\"0\" width=\"100%\"><tr><td>");
174             lc_tempBuffer.append("&nbsp;");
175             lc_tempBuffer.append("</td><td>");
176             lc_tempBuffer.append("<a HREF=\"");
177             lc_tempBuffer.append(in_sortUrl);
178             lc_tempBuffer.append("\">");
179             lc_tempBuffer.append(in_header);
180             lc_tempBuffer.append("</a>");
181             lc_tempBuffer.append("</td></tr></table>");
182             return lc_tempBuffer.toString();
183         }
184         else {
185             // utilisation d'une image pour faire le tri
186
StringBuffer JavaDoc lc_tempBuffer = new StringBuffer JavaDoc("<table border=\"0\" width=\"100%\"><tr><td>");
187             lc_tempBuffer.append("<a HREF=\"");
188             lc_tempBuffer.append(in_sortUrl);
189             lc_tempBuffer.append("\"><img SRC=\"");
190             lc_tempBuffer.append(LayoutUtils.getSkin(pageContext.getSession()).getImageDirectory(pageContext.getRequest()));
191             lc_tempBuffer.append("/");
192             lc_tempBuffer.append(LayoutUtils.getSkin(pageContext.getSession()).getProperty(collectionTag.getSortPictogram()));
193             lc_tempBuffer.append("\" border=\"0\" alt=\"");
194             lc_tempBuffer.append(LayoutUtils.getSkin(pageContext.getSession()).getProperty(collectionTag.getSortLabel()));
195             lc_tempBuffer.append("\"></a>");
196     
197             lc_tempBuffer.append("</td><td>");
198             lc_tempBuffer.append(in_header);
199             lc_tempBuffer.append("</td></tr></table>");
200         
201             return lc_tempBuffer.toString();
202         }
203     }
204
205      
206     /**
207      * Finish to render the headers.
208      */

209     public void doEndHeaders(StringBuffer JavaDoc out_buffer) {
210         out_buffer.append("</tr>");
211     }
212     /**
213      * Prepare to render a line.
214      */

215     public void doStartItems(StringBuffer JavaDoc out_buffer) {
216         if (headerOpen) {
217             out_buffer.append("</thead>");
218             out_buffer.append("<tbody");
219             if (collectionTag.getHeight()!=null || collectionTag.getWidth()!=null) {
220                 if (BrowserUtil.isGecko((HttpServletRequest JavaDoc) pageContext.getRequest())) {
221                     out_buffer.append(" style=\"overflow:-moz-scrollbars-vertical;");
222                     if (collectionTag.getHeight()!=null) {
223                         out_buffer.append("height:");
224                         out_buffer.append(collectionTag.getHeight());
225                         out_buffer.append("px;");
226                     }
227                     if (collectionTag.getWidth()!=null) {
228                         out_buffer.append("width:");
229                         out_buffer.append(collectionTag.getWidth());
230                         out_buffer.append("px;");
231                     }
232                     out_buffer.append("\"");
233                 }
234             }
235             out_buffer.append(">");
236             headerOpen = false;
237         }
238         out_buffer.append("<tr");
239         boolean lc_onclick = false;
240         if (collectionTag.getOnRowClick()!=null) {
241             out_buffer.append(" onclick=\"");
242             out_buffer.append(Expression.evaluate(collectionTag.getOnRowClick(), pageContext));
243             out_buffer.append("\"");
244             lc_onclick = true;
245         }
246         if (collectionTag.getOnRowDblClick()!=null) {
247             out_buffer.append(" ondblclick=\"");
248             out_buffer.append(Expression.evaluate(collectionTag.getOnRowDblClick(), pageContext));
249             out_buffer.append("\"");
250             lc_onclick = true;
251         }
252         if (collectionTag.getOnRowMouseOver()!=null) {
253             out_buffer.append(" onmouseover=\"");
254             out_buffer.append(Expression.evaluate(collectionTag.getOnRowMouseOver(), pageContext));
255             out_buffer.append("\"");
256             lc_onclick = true;
257         }
258         if (collectionTag.getOnRowMouseOut()!=null) {
259             out_buffer.append(" onmouseout=\"");
260             out_buffer.append(Expression.evaluate(collectionTag.getOnRowMouseOut(), pageContext));
261             out_buffer.append("\"");
262         }
263         if (lc_onclick) {
264             out_buffer.append(" style=\"cursor:pointer;cursor:hand;\"");
265         }
266         out_buffer.append(">");
267     }
268     /**
269      * Render an element in the line.
270      */

271     public void doPrintItem(StringBuffer JavaDoc out_buffer, String JavaDoc in_item, String JavaDoc[] in_styleClass, String JavaDoc in_id) {
272         out_buffer.append("<td");
273         if (in_styleClass[0] != null) {
274             out_buffer.append(" class=\"");
275             out_buffer.append(in_styleClass[0]);
276             out_buffer.append("\"");
277         }
278         if (in_styleClass.length>1) {
279             out_buffer.append(" style=\"");
280             for (int i = 0; i < in_styleClass.length-1; i++) {
281                 out_buffer.append(in_styleClass[i+1]);
282             }
283             out_buffer.append("\"");
284         }
285         
286         Integer JavaDoc lc_int = (Integer JavaDoc) pageContext.getAttribute(CollectionItemTag.SPAN_KEY);
287         if (lc_int!=null && lc_int.intValue()!=1) {
288             out_buffer.append(" rowspan=\"");
289             out_buffer.append(lc_int.intValue());
290             out_buffer.append("\"");
291         }
292         
293         out_buffer.append(">");
294         
295         if (in_id!=null) {
296             out_buffer.append("<div id=\"");
297             out_buffer.append(in_id);
298             out_buffer.append("\">");
299         }
300         
301         out_buffer.append(in_item);
302         
303         if (in_id!=null) {
304             out_buffer.append("</div>");
305         }
306
307         out_buffer.append("</td>");
308     }
309     /**
310      * Finish to render a line.
311      */

312     public void doEndItems(StringBuffer JavaDoc out_buffer) {
313         out_buffer.append("</tr>");
314     }
315     public void doEndPanel(StringBuffer JavaDoc buffer) {
316         buffer.append("</tbody>");
317         buffer.append("</table>\n");
318         if (collectionTag.getWidth()!=null || collectionTag.getHeight()!=null) {
319             buffer.append("</div>");
320         }
321     }
322     
323     // ------------------------ Helper methods ----------------------
324

325     /**
326      * Can be overriden to use specific styleClass for first and last columns.
327      */

328     protected String JavaDoc buildHeaderStyleClass(String JavaDoc in_styleClass, int in_columnNumber) {
329         return in_styleClass;
330     }
331     
332     /**
333      * Can be ovveriden to use specific styleClass for footer.
334      */

335     protected String JavaDoc buildFooterStyleClass(int in_columnNumber) {
336         return null;
337     }
338     
339     /**
340      * Can be overriden to use specific styleClass for empty collection message.
341      */

342     protected String JavaDoc buildEmptyStyleClass(String JavaDoc in_styleClass) {
343         return in_styleClass;
344     }
345     
346     // ------------------- Footer renderer --------------------------
347

348     public void endFooter(StringBuffer JavaDoc in_buffer) {
349         in_buffer.append("</tr>\n");
350
351     }
352     public void printFooterElement(StringBuffer JavaDoc in_buffer, String JavaDoc in_element, int in_span) {
353         in_buffer.append("<td ");
354         if (in_span>1) {
355             in_buffer.append(" colspan=\"");
356             in_buffer.append(in_span);
357             in_buffer.append("\"");
358         }
359         String JavaDoc lc_styleClass = buildFooterStyleClass(collectionTag.getColumn());
360         if (lc_styleClass!=null) {
361             in_buffer.append(" class=\"");
362             in_buffer.append(lc_styleClass);
363             in_buffer.append("\"");
364         }
365         in_buffer.append(">");
366         in_buffer.append(in_element);
367         in_buffer.append("</td>");
368
369     }
370     public void startFooter(StringBuffer JavaDoc in_buffer) {
371         in_buffer.append("<tr>");
372     }
373     
374     // ------------------- Math data renderer -----------------------
375

376     /**
377      * Print end of math data
378      */

379     public void endMathData(StringBuffer JavaDoc in_buffer) {
380         in_buffer.append("</tr>\n");
381     }
382     
383     public void renderMathData(StringBuffer JavaDoc in_buffer, String JavaDoc in_element, int in_span, String JavaDoc in_resultId, String JavaDoc in_styleClass) {
384         in_buffer.append("<td");
385         if (in_styleClass!=null){
386             in_buffer.append(" class=\"");
387             in_buffer.append(in_styleClass);
388             in_buffer.append("\"");
389         }
390         if (in_span>1){
391             in_buffer.append(" colspan=\"");
392             in_buffer.append(in_span);
393             in_buffer.append("\"");
394         }
395         if (in_resultId!=null && in_resultId.length()!=0){
396             in_buffer.append(" id=\"" + in_resultId + "\"");
397         }
398         in_buffer.append(">");
399         in_buffer.append(in_element == null ? "" : in_element);
400         in_buffer.append("</td>\n");
401     }
402     
403     public void startMathData(StringBuffer JavaDoc in_buffer) {
404         in_buffer.append("<tr>\n");
405     }
406     
407     // ---------------------- Multi level header renderer ---------------------
408

409     public void renderMultiLevelHeader(StringBuffer JavaDoc in_buffer, String JavaDoc in_title, String JavaDoc in_sortUrl, String JavaDoc in_styleClass, int in_colspan, int in_rowspan, String JavaDoc in_width) {
410         in_buffer.append("<th");
411         if (in_colspan!=1) {
412             in_buffer.append(" colspan=\"");
413             in_buffer.append(in_colspan);
414             in_buffer.append("\"");
415         }
416         if (in_styleClass!=null) {
417             in_buffer.append(" class=\"");
418             in_buffer.append(buildHeaderStyleClass(in_styleClass, collectionTag.getColumn()));
419             in_buffer.append("\"");
420         }
421         if (in_rowspan!=1) {
422             in_buffer.append(" rowspan=\"");
423             in_buffer.append(in_rowspan);
424             in_buffer.append("\"");
425         }
426         if (in_width!=null) {
427             in_buffer.append(" width=\"");
428             in_buffer.append(in_width);
429             in_buffer.append("\"");
430         }
431         in_buffer.append(">");
432         in_buffer.append(doPrintSortUrl(in_title, in_sortUrl));
433         in_buffer.append("</th>\n");
434     }
435
436     public void endMultiLevelHeaderRow(StringBuffer JavaDoc in_buffer) {
437         in_buffer.append("</tr>\n");
438     }
439     public void startMultiLevelHeaderRow(StringBuffer JavaDoc in_buffer) {
440         if (!headerOpen) {
441             in_buffer.append("<thead>");
442             headerOpen = true;
443         }
444         in_buffer.append("<tr valign=\"top\"");
445         if (collectionTag.getHeight()!=null && BrowserUtil.isIe((HttpServletRequest JavaDoc) pageContext.getRequest())) {
446             in_buffer.append(" style=\"position:relative;top:expression(this.offsetParent.scrollTop);\"");
447         }
448         in_buffer.append(">\n");
449     }
450     
451     // ------------------------ Unused methods ---------------------------
452

453     public void doAfterBody(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
454         // No-op
455
}
456
457     public void doBeforeBody(StringBuffer JavaDoc buffer, String JavaDoc align) throws JspException JavaDoc {
458         // No-op
459
}
460
461     public void doPrintBlankLine(StringBuffer JavaDoc buffer, int cols) throws JspException JavaDoc {
462         // No-op
463
}
464 }
Popular Tags