KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > util > BasicCollection


1 package fr.improve.struts.taglib.layout.util;
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
11 /**
12  * Simple implementation of the CollectionInterface.
13  *
14  * @author: Jean-Noël Ribette, F. BELLINGARD, Damien Viel
15  */

16 public class BasicCollection extends BasicPanel implements CollectionInterface, IMathCollectionRenderer, IFooterRenderer, IMultiLevelHeaderRenderer {
17     protected CollectionTag collectionTag;
18     protected PageContext JavaDoc pageContext;
19     
20     public void init(PageContext JavaDoc pg, String JavaDoc in_styleClass, TagSupport JavaDoc in_tag) throws JspException JavaDoc {
21         super.init(pg, in_styleClass, in_tag);
22         collectionTag = (CollectionTag) in_tag;
23         pageContext = pg;
24     }
25     
26     /**
27      * Display a special message if the collection is empty.
28      */

29     public void doPrintEmptyCollection(StringBuffer JavaDoc out_buffer, String JavaDoc in_message) {
30         out_buffer.append("<tr><td colspan=\"");
31         out_buffer.append(collectionTag.getHeaders().size());
32         out_buffer.append("\" class=\"");
33         out_buffer.append(styleClass);
34         out_buffer.append("\">&nbsp;");
35         out_buffer.append(in_message);
36         out_buffer.append("</td></tr>");
37     }
38     /**
39      * Overrides the display ot the title.
40      * PENDING: use a nice layout.
41      */

42     public void doPrintTitle(StringBuffer JavaDoc buffer, String JavaDoc title) {
43         if (title!=null) {
44             if (styleClass!=null) {
45                 buffer.append("<p class=\"");
46                 buffer.append(styleClass);
47                 buffer.append("\">");
48             }
49             buffer.append(title);
50             if (styleClass!=null) {
51                 buffer.append("</p>");
52             }
53             //buffer.append("</td></tr><tr align=\"center\"><td colspan=\"2\">");
54
}
55     }
56     /**
57      * Override doStartPanel
58      */

59     public void doStartPanel(StringBuffer JavaDoc buffer, String JavaDoc align, String JavaDoc width) {
60         if (collectionTag.getWidth()!=null || collectionTag.getHeight()!=null) {
61             buffer.append("<div style=\"");
62             if (collectionTag.getWidth()!=null) {
63                 buffer.append("width:");
64                 buffer.append(collectionTag.getWidth());
65                 buffer.append(";");
66                 buffer.append("overflow-x:auto;");
67             }
68             if (collectionTag.getHeight()!=null) {
69                 buffer.append("height:");
70                 buffer.append(collectionTag.getHeight());
71                 buffer.append(";");
72                 buffer.append("overflow-y:auto;");
73             }
74             buffer.append("\">");
75         }
76         buffer.append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"");
77         if (align!=null) {
78             buffer.append(" align=\"");
79             buffer.append(align);
80             buffer.append("\"");
81         }
82         if (width!=null) {
83             buffer.append(" width=\"");
84             buffer.append(width);
85             buffer.append("\"");
86         }
87         if (styleClass!=null) {
88             buffer.append(" class=\"");
89             buffer.append(styleClass);
90             buffer.append("\"");
91         }
92         buffer.append("><tr><td valign=\"top\">");
93         buffer.append("<table cellspacing=\"1\" cellpadding=\"1\" border=\"0\" width=\"100%\"");
94         if (collectionTag.getStyleId()!=null) {
95             buffer.append(" id=\"");
96             buffer.append(collectionTag.getStyleId());
97             buffer.append("\"");
98         }
99         buffer.append(">\n");
100     }
101     /**
102      * Prepare to display the headers.
103      */

104     public void doStartHeaders(StringBuffer JavaDoc out_buffer) {
105         out_buffer.append("<tr>");
106     }
107     /**
108      * Display a header.
109      */

110     public void doPrintHeader(StringBuffer JavaDoc out_buffer, String JavaDoc in_header, String JavaDoc in_width, String JavaDoc in_sortUrl) {
111         out_buffer.append("<th");
112         if (styleClass!=null) {
113             out_buffer.append(" class=\"");
114             out_buffer.append(styleClass);
115             out_buffer.append("\"");
116         }
117         if (in_width!=null) {
118             out_buffer.append(" width=\"");
119             out_buffer.append(in_width);
120             out_buffer.append("\"");
121         }
122         out_buffer.append(">");
123                     
124         out_buffer.append(doPrintSortUrl(in_header, in_sortUrl));
125             
126         
127         out_buffer.append("</th>");
128     }
129     
130     /**
131      * Display an hyperlink to sort the column.
132      */

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

175     public void doEndHeaders(StringBuffer JavaDoc out_buffer) {
176         out_buffer.append("</tr>");
177     }
178     /**
179      * Prepare to render a line.
180      */

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

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

256     public void doEndItems(StringBuffer JavaDoc out_buffer) {
257         out_buffer.append("</tr>");
258     }
259     public void doEndPanel(StringBuffer JavaDoc buffer) {
260         buffer.append("</table></td></tr></table>\n");
261         if (collectionTag.getWidth()!=null || collectionTag.getHeight()!=null) {
262             buffer.append("</div>");
263         }
264     }
265     public void endFooter(StringBuffer JavaDoc in_buffer) {
266         in_buffer.append("</tr>\n");
267
268     }
269     public void printFooterElement(StringBuffer JavaDoc in_buffer, String JavaDoc in_element, int in_span) {
270         in_buffer.append("<td ");
271         if (in_span>1) {
272             in_buffer.append(" colspan=\"");
273             in_buffer.append(in_span);
274             in_buffer.append("\"");
275         }
276         in_buffer.append(">");
277         in_buffer.append(in_element);
278         in_buffer.append("</td>");
279
280     }
281     public void startFooter(StringBuffer JavaDoc in_buffer) {
282         in_buffer.append("<tr>");
283     }
284     
285     // ------------------- Math data renderer -----------------------
286

287     /**
288      * Print end of math data
289      */

290     public void endMathData(StringBuffer JavaDoc in_buffer) {
291         in_buffer.append("</tr>\n");
292     }
293     
294     public void renderMathData(StringBuffer JavaDoc in_buffer, String JavaDoc in_element, int in_span, String JavaDoc in_resultId, String JavaDoc in_styleClass) {
295         in_buffer.append("<td");
296         if (in_styleClass!=null){
297             in_buffer.append(" class=\"");
298             in_buffer.append(in_styleClass);
299             in_buffer.append("\"");
300         }
301         if (in_span>1){
302             in_buffer.append(" colspan=\"");
303             in_buffer.append(in_span);
304             in_buffer.append("\"");
305         }
306         if (in_resultId!=null && in_resultId.length()!=0){
307             in_buffer.append(" id=\"" + in_resultId + "\"");
308         }
309         in_buffer.append(">");
310         in_buffer.append(in_element == null ? "" : in_element);
311         in_buffer.append("</td>\n");
312     }
313     
314     public void startMathData(StringBuffer JavaDoc in_buffer) {
315         in_buffer.append("<tr>\n");
316     }
317     
318     // ---------------------- Multi level header renderer ---------------------
319

320     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) {
321         in_buffer.append("<th");
322         if (in_colspan!=1) {
323             in_buffer.append(" colspan=\"");
324             in_buffer.append(in_colspan);
325             in_buffer.append("\"");
326         }
327         if (in_styleClass!=null) {
328             in_buffer.append(" class=\"");
329             in_buffer.append(in_styleClass);
330             in_buffer.append("\"");
331         }
332         if (in_rowspan!=1) {
333             in_buffer.append(" rowspan=\"");
334             in_buffer.append(in_rowspan);
335             in_buffer.append("\"");
336         }
337         if (in_width!=null) {
338             in_buffer.append(" width=\"");
339             in_buffer.append(in_width);
340             in_buffer.append("\"");
341         }
342         in_buffer.append(">");
343         in_buffer.append(doPrintSortUrl(in_title, in_sortUrl));
344         in_buffer.append("</th>");
345     }
346
347     public void endMultiLevelHeaderRow(StringBuffer JavaDoc in_buffer) {
348         in_buffer.append("</tr>\n");
349     }
350     public void startMultiLevelHeaderRow(StringBuffer JavaDoc in_buffer) {
351         in_buffer.append("<tr valign=\"top\">");
352     }
353 }
Popular Tags