KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout.renderer;
2
3 import javax.servlet.jsp.JspException JavaDoc;
4 import javax.servlet.jsp.PageContext JavaDoc;
5 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
6
7 import fr.improve.struts.taglib.layout.collection.CollectionItemTag;
8 import fr.improve.struts.taglib.layout.collection.CollectionTag;
9 import fr.improve.struts.taglib.layout.el.Expression;
10 import fr.improve.struts.taglib.layout.util.CollectionInterface;
11 import fr.improve.struts.taglib.layout.util.IFooterRenderer;
12 import fr.improve.struts.taglib.layout.util.IMathCollectionRenderer;
13 import fr.improve.struts.taglib.layout.util.IMultiLevelHeaderRenderer;
14 import fr.improve.struts.taglib.layout.util.LayoutUtils;
15
16 /**
17  * New implementation of the CollectionInterface.
18  *
19  * @author: Jean-Noël Ribette, F. BELLINGARD, Damien Viel
20  */

21 public class SingleTableCollection implements CollectionInterface, IMathCollectionRenderer, IFooterRenderer, IMultiLevelHeaderRenderer {
22     protected CollectionTag collectionTag;
23     protected PageContext JavaDoc pageContext;
24     protected String JavaDoc styleClass;
25     
26     /**
27      * Initialize the renderer.
28      */

29     public void init(PageContext JavaDoc pg, String JavaDoc in_styleClass, TagSupport JavaDoc in_tag) throws JspException JavaDoc {
30         collectionTag = (CollectionTag) in_tag;
31         pageContext = pg;
32         styleClass = collectionTag.getStyleClass();
33     }
34     
35     /**
36      * Display a special message if the collection is empty.
37      */

38     public void doPrintEmptyCollection(StringBuffer JavaDoc out_buffer, String JavaDoc in_message) {
39         out_buffer.append("<tr><td colspan=\"");
40         out_buffer.append(collectionTag.getHeaders().size());
41         out_buffer.append("\" class=\"");
42         out_buffer.append(buildEmptyStyleClass(styleClass));
43         out_buffer.append("\">&nbsp;");
44         out_buffer.append(in_message);
45         out_buffer.append("</td></tr>");
46     }
47     /**
48      * Print the collection title.
49      */

50     public void doPrintTitle(StringBuffer JavaDoc buffer, String JavaDoc title) {
51         if (title!=null) {
52             if (styleClass!=null) {
53                 buffer.append("<p class=\"");
54                 buffer.append(styleClass);
55                 buffer.append("\">");
56             }
57             buffer.append(title);
58             if (styleClass!=null) {
59                 buffer.append("</p>");
60             }
61         }
62     }
63     /**
64      * Start the collection.
65      */

66     public void doStartPanel(StringBuffer JavaDoc buffer, String JavaDoc align, String JavaDoc width) {
67         // If height of width is set, generate a scrollar.
68
if (collectionTag.getWidth()!=null || collectionTag.getHeight()!=null) {
69             buffer.append("<div style=\"");
70             if (collectionTag.getWidth()!=null) {
71                 buffer.append("width:");
72                 buffer.append(collectionTag.getWidth());
73                 buffer.append(";");
74                 buffer.append("overflow-x:auto;");
75             }
76             if (collectionTag.getHeight()!=null) {
77                 buffer.append("height:");
78                 buffer.append(collectionTag.getHeight());
79                 buffer.append(";");
80                 buffer.append("overflow-y:auto;");
81             }
82             buffer.append("\">");
83         }
84         
85         // Start the table
86
buffer.append("<table");
87         if (align!=null) {
88             buffer.append(" align=\"");
89             buffer.append(align);
90             buffer.append("\"");
91         }
92         if (width!=null) {
93             buffer.append(" width=\"");
94             buffer.append(width);
95             buffer.append("\"");
96         }
97         if (styleClass!=null) {
98             buffer.append(" class=\"");
99             buffer.append(styleClass);
100             buffer.append("\"");
101         }
102         if (collectionTag.getStyleId()!=null) {
103             buffer.append(" id=\"");
104             buffer.append(collectionTag.getStyleId());
105             buffer.append("\"");
106         }
107         buffer.append(">\n");
108     }
109     /**
110      * Prepare to display the headers.
111      */

112     public void doStartHeaders(StringBuffer JavaDoc out_buffer) {
113         out_buffer.append("<tr>");
114     }
115     /**
116      * Display a header.
117      */

118     public void doPrintHeader(StringBuffer JavaDoc out_buffer, String JavaDoc in_header, String JavaDoc in_width, String JavaDoc in_sortUrl) {
119         out_buffer.append("<th");
120         if (styleClass!=null) {
121             out_buffer.append(" class=\"");
122             out_buffer.append(buildHeaderStyleClass(styleClass, collectionTag.getColumn()));
123             out_buffer.append("\"");
124         }
125         if (in_width!=null) {
126             out_buffer.append(" width=\"");
127             out_buffer.append(in_width);
128             out_buffer.append("\"");
129         }
130         out_buffer.append(">");
131                     
132         out_buffer.append(doPrintSortUrl(in_header, in_sortUrl));
133             
134         
135         out_buffer.append("</th>");
136     }
137     
138     /**
139      * Display an hyperlink to sort the column.
140      */

141     protected String JavaDoc doPrintSortUrl(String JavaDoc in_header, String JavaDoc in_sortUrl) {
142         if (in_sortUrl == null) {
143             return in_header;
144         }
145         else if (collectionTag.getSortPictogram().equalsIgnoreCase("none")) {
146             // pas d'image pour faire le tri, mais un lien sur le titre
147
StringBuffer JavaDoc lc_tempBuffer = new StringBuffer JavaDoc("<table border=\"0\" width=\"100%\"><tr><td>");
148             lc_tempBuffer.append("&nbsp;");
149             lc_tempBuffer.append("</td><td>");
150             lc_tempBuffer.append("<a HREF=\"");
151             lc_tempBuffer.append(in_sortUrl);
152             lc_tempBuffer.append("\">");
153             lc_tempBuffer.append(in_header);
154             lc_tempBuffer.append("</a>");
155             lc_tempBuffer.append("</td></tr></table>");
156             return lc_tempBuffer.toString();
157         }
158         else {
159             // utilisation d'une image pour faire le tri
160
StringBuffer JavaDoc lc_tempBuffer = new StringBuffer JavaDoc("<table border=\"0\" width=\"100%\"><tr><td>");
161             lc_tempBuffer.append("<a HREF=\"");
162             lc_tempBuffer.append(in_sortUrl);
163             lc_tempBuffer.append("\"><img SRC=\"");
164             lc_tempBuffer.append(LayoutUtils.getSkin(pageContext.getSession()).getImageDirectory(pageContext.getRequest()));
165             lc_tempBuffer.append("/");
166             lc_tempBuffer.append(LayoutUtils.getSkin(pageContext.getSession()).getProperty(collectionTag.getSortPictogram()));
167             lc_tempBuffer.append("\" border=\"0\" alt=\"");
168             lc_tempBuffer.append(LayoutUtils.getSkin(pageContext.getSession()).getProperty(collectionTag.getSortLabel()));
169             lc_tempBuffer.append("\"></a>");
170     
171             lc_tempBuffer.append("</td><td>");
172             lc_tempBuffer.append(in_header);
173             lc_tempBuffer.append("</td></tr></table>");
174         
175             return lc_tempBuffer.toString();
176         }
177     }
178
179      
180     /**
181      * Finish to render the headers.
182      */

183     public void doEndHeaders(StringBuffer JavaDoc out_buffer) {
184         out_buffer.append("</tr>");
185     }
186     /**
187      * Prepare to render a line.
188      */

189     public void doStartItems(StringBuffer JavaDoc out_buffer) {
190         out_buffer.append("<tr");
191         boolean lc_onclick = false;
192         if (collectionTag.getOnRowClick()!=null) {
193             out_buffer.append(" onclick=\"");
194             out_buffer.append(Expression.evaluate(collectionTag.getOnRowClick(), pageContext));
195             out_buffer.append("\"");
196             lc_onclick = true;
197         }
198         if (collectionTag.getOnRowDblClick()!=null) {
199             out_buffer.append(" ondblclick=\"");
200             out_buffer.append(Expression.evaluate(collectionTag.getOnRowDblClick(), pageContext));
201             out_buffer.append("\"");
202             lc_onclick = true;
203         }
204         if (collectionTag.getOnRowMouseOver()!=null) {
205             out_buffer.append(" onmouseover=\"");
206             out_buffer.append(Expression.evaluate(collectionTag.getOnRowMouseOver(), pageContext));
207             out_buffer.append("\"");
208             lc_onclick = true;
209         }
210         if (collectionTag.getOnRowMouseOut()!=null) {
211             out_buffer.append(" onmouseout=\"");
212             out_buffer.append(Expression.evaluate(collectionTag.getOnRowMouseOut(), pageContext));
213             out_buffer.append("\"");
214         }
215         if (lc_onclick) {
216             out_buffer.append(" style=\"cursor:pointer;cursor:hand;\"");
217         }
218         out_buffer.append(">");
219     }
220     /**
221      * Render an element in the line.
222      */

223     public void doPrintItem(StringBuffer JavaDoc out_buffer, String JavaDoc in_item, String JavaDoc[] in_styleClass, String JavaDoc in_id) {
224         out_buffer.append("<td");
225         if (in_styleClass[0] != null) {
226             out_buffer.append(" class=\"");
227             out_buffer.append(in_styleClass[0]);
228             out_buffer.append("\"");
229         }
230         if (in_styleClass.length>1) {
231             out_buffer.append(" style=\"");
232             for (int i = 0; i < in_styleClass.length-1; i++) {
233                 out_buffer.append(in_styleClass[i+1]);
234             }
235             out_buffer.append("\"");
236         }
237         
238         Integer JavaDoc lc_int = (Integer JavaDoc) pageContext.getAttribute(CollectionItemTag.SPAN_KEY);
239         if (lc_int!=null && lc_int.intValue()!=1) {
240             out_buffer.append(" rowspan=\"");
241             out_buffer.append(lc_int.intValue());
242             out_buffer.append("\"");
243         }
244         
245         out_buffer.append(">");
246         
247         if (in_id!=null) {
248             out_buffer.append("<div id=\"");
249             out_buffer.append(in_id);
250             out_buffer.append("\">");
251         }
252         
253         out_buffer.append(in_item);
254         
255         if (in_id!=null) {
256             out_buffer.append("</div>");
257         }
258
259         out_buffer.append("</td>");
260     }
261     /**
262      * Finish to render a line.
263      */

264     public void doEndItems(StringBuffer JavaDoc out_buffer) {
265         out_buffer.append("</tr>");
266     }
267     public void doEndPanel(StringBuffer JavaDoc buffer) {
268         buffer.append("</table>\n");
269         if (collectionTag.getWidth()!=null || collectionTag.getHeight()!=null) {
270             buffer.append("</div>");
271         }
272     }
273     
274     // ------------------------ Helper methods ----------------------
275

276     /**
277      * Can be overriden to use specific styleClass for first and last columns.
278      */

279     protected String JavaDoc buildHeaderStyleClass(String JavaDoc in_styleClass, int in_columnNumber) {
280         return in_styleClass;
281     }
282     
283     /**
284      * Can be ovveriden to use specific styleClass for footer.
285      */

286     protected String JavaDoc buildFooterStyleClass(int in_columnNumber) {
287         return null;
288     }
289     
290     /**
291      * Can be overriden to use specific styleClass for empty collection message.
292      */

293     protected String JavaDoc buildEmptyStyleClass(String JavaDoc in_styleClass) {
294         return in_styleClass;
295     }
296     
297     // ------------------- Footer renderer --------------------------
298

299     public void endFooter(StringBuffer JavaDoc in_buffer) {
300         in_buffer.append("</tr>\n");
301
302     }
303     public void printFooterElement(StringBuffer JavaDoc in_buffer, String JavaDoc in_element, int in_span) {
304         in_buffer.append("<td ");
305         if (in_span>1) {
306             in_buffer.append(" colspan=\"");
307             in_buffer.append(in_span);
308             in_buffer.append("\"");
309         }
310         String JavaDoc lc_styleClass = buildFooterStyleClass(collectionTag.getColumn());
311         if (lc_styleClass!=null) {
312             in_buffer.append(" class=\"");
313             in_buffer.append(lc_styleClass);
314             in_buffer.append("\"");
315         }
316         in_buffer.append(">");
317         in_buffer.append(in_element);
318         in_buffer.append("</td>");
319
320     }
321     public void startFooter(StringBuffer JavaDoc in_buffer) {
322         in_buffer.append("<tr>");
323     }
324     
325     // ------------------- Math data renderer -----------------------
326

327     /**
328      * Print end of math data
329      */

330     public void endMathData(StringBuffer JavaDoc in_buffer) {
331         in_buffer.append("</tr>\n");
332     }
333     
334     public void renderMathData(StringBuffer JavaDoc in_buffer, String JavaDoc in_element, int in_span, String JavaDoc in_resultId, String JavaDoc in_styleClass) {
335         in_buffer.append("<td");
336         if (in_styleClass!=null){
337             in_buffer.append(" class=\"");
338             in_buffer.append(in_styleClass);
339             in_buffer.append("\"");
340         }
341         if (in_span>1){
342             in_buffer.append(" colspan=\"");
343             in_buffer.append(in_span);
344             in_buffer.append("\"");
345         }
346         if (in_resultId!=null && in_resultId.length()!=0){
347             in_buffer.append(" id=\"" + in_resultId + "\"");
348         }
349         in_buffer.append(">");
350         in_buffer.append(in_element == null ? "" : in_element);
351         in_buffer.append("</td>\n");
352     }
353     
354     public void startMathData(StringBuffer JavaDoc in_buffer) {
355         in_buffer.append("<tr>\n");
356     }
357     
358     // ---------------------- Multi level header renderer ---------------------
359

360     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) {
361         in_buffer.append("<th");
362         if (in_colspan!=1) {
363             in_buffer.append(" colspan=\"");
364             in_buffer.append(in_colspan);
365             in_buffer.append("\"");
366         }
367         if (in_styleClass!=null) {
368             in_buffer.append(" class=\"");
369             in_buffer.append(buildHeaderStyleClass(in_styleClass, collectionTag.getColumn()));
370             in_buffer.append("\"");
371         }
372         if (in_rowspan!=1) {
373             in_buffer.append(" rowspan=\"");
374             in_buffer.append(in_rowspan);
375             in_buffer.append("\"");
376         }
377         if (in_width!=null) {
378             in_buffer.append(" width=\"");
379             in_buffer.append(in_width);
380             in_buffer.append("\"");
381         }
382         in_buffer.append(">");
383         in_buffer.append(doPrintSortUrl(in_title, in_sortUrl));
384         in_buffer.append("</th>");
385     }
386
387     public void endMultiLevelHeaderRow(StringBuffer JavaDoc in_buffer) {
388         in_buffer.append("</tr>\n");
389     }
390     public void startMultiLevelHeaderRow(StringBuffer JavaDoc in_buffer) {
391         in_buffer.append("<tr valign=\"top\">");
392     }
393     
394     // ------------------------ Unused methods ---------------------------
395

396     public void doAfterBody(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
397         // No-op
398
}
399
400     public void doBeforeBody(StringBuffer JavaDoc buffer, String JavaDoc align) throws JspException JavaDoc {
401         // No-op
402
}
403
404     public void doPrintBlankLine(StringBuffer JavaDoc buffer, int cols) throws JspException JavaDoc {
405         // No-op
406
}
407 }
Popular Tags