KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout.util;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import javax.servlet.jsp.JspException JavaDoc;
7 import javax.servlet.jsp.PageContext JavaDoc;
8 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
9
10 import fr.improve.struts.taglib.layout.collection.CollectionItemTag;
11 import fr.improve.struts.taglib.layout.collection.CollectionTag;
12 import fr.improve.struts.taglib.layout.datagrid.Datagrid;
13 import fr.improve.struts.taglib.layout.el.Expression;
14
15 /**
16  * Simple implementation of the IDatagird renderer..
17  *
18  * @author: Jean-NoŽl Ribette
19  */

20 public class BasicDatagrid implements IDatagridRenderer {
21     protected Map JavaDoc styles = new HashMap JavaDoc();
22     protected CollectionTag collectionTag;
23     protected PageContext JavaDoc pageContext;
24     protected String JavaDoc styleClass;
25     
26     public void init(PageContext JavaDoc pg, String JavaDoc in_styleClass, TagSupport JavaDoc in_tag) throws JspException JavaDoc {
27         collectionTag = (CollectionTag) in_tag;
28         pageContext = pg;
29         styleClass = collectionTag.getStyleClass();
30         styles.clear();
31         styles.put(Datagrid.SELECTED, styleClass + "_SEL");
32         styles.put(Datagrid.REMOVED, styleClass + "_DEL");
33     }
34     /**
35      * Display a special message if the collection is empty.
36      */

37     public void doPrintEmptyCollection(StringBuffer JavaDoc out_buffer, String JavaDoc in_message) {
38         out_buffer.append("<tr><td colspan=\"");
39         out_buffer.append(collectionTag.getHeaders().size());
40         out_buffer.append("\" class=\"");
41         out_buffer.append(styleClass);
42         out_buffer.append("\">&nbsp;");
43         out_buffer.append(in_message);
44         out_buffer.append("</td></tr>");
45     }
46     /**
47      * Overrides the display ot the title.
48      * PENDING: use a nice layout.
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             //buffer.append("</td></tr><tr align=\"center\"><td colspan=\"2\">");
62
}
63     }
64     /**
65      * Override doStartPanel
66      */

67     public void doStartPanel(StringBuffer JavaDoc buffer, String JavaDoc align, String JavaDoc width) {
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         buffer.append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"");
85         if (align!=null) {
86             buffer.append(" align=\"");
87             buffer.append(align);
88             buffer.append("\"");
89         }
90         if (width!=null) {
91             buffer.append(" width=\"");
92             buffer.append(width);
93             buffer.append("\"");
94         }
95         if (styleClass!=null) {
96             buffer.append(" class=\"");
97             buffer.append(styleClass);
98             buffer.append("\"");
99         }
100         buffer.append("><tr><td valign=\"top\">");
101         buffer.append("<table cellspacing=\"1\" cellpadding=\"1\" border=\"0\" width=\"100%\"");
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(styleClass);
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             
219         out_buffer.append(" class=\"");
220         if (collectionTag.getIndex() % 2 ==0) {
221             out_buffer.append(getRowStyleClass());
222         } else {
223              out_buffer.append(getRowStyleClass2());
224         }
225         out_buffer.append("\">");
226     }
227     /**
228      * Render an element in the line.
229      */

230     public void doPrintItem(StringBuffer JavaDoc out_buffer, String JavaDoc in_item, String JavaDoc[] in_styleClass, String JavaDoc in_id) {
231         out_buffer.append("<td");
232         /*
233         if (in_styleClass[0] != null) {
234             out_buffer.append(" class=\"");
235             out_buffer.append(in_styleClass[0]);
236             out_buffer.append("\"");
237         }
238         */

239         if (in_styleClass.length>1) {
240             out_buffer.append(" style=\"");
241             for (int i = 0; i < in_styleClass.length-1; i++) {
242                 out_buffer.append(in_styleClass[i+1]);
243             }
244             out_buffer.append("\"");
245         }
246         
247         Integer JavaDoc lc_int = (Integer JavaDoc) pageContext.getAttribute(CollectionItemTag.SPAN_KEY);
248         if (lc_int!=null && lc_int.intValue()!=1) {
249             out_buffer.append(" rowspan=\"");
250             out_buffer.append(lc_int.intValue());
251             out_buffer.append("\"");
252         }
253         
254         out_buffer.append(">");
255         
256         if (in_id!=null) {
257             out_buffer.append("<div id=\"");
258             out_buffer.append(in_id);
259             out_buffer.append("\">");
260         }
261         
262         out_buffer.append(in_item);
263         
264         if (in_id!=null) {
265             out_buffer.append("</div>");
266         }
267
268         out_buffer.append("</td>");
269     }
270     /**
271      * Finish to render a line.
272      */

273     public void doEndItems(StringBuffer JavaDoc out_buffer) {
274         out_buffer.append("</tr>");
275     }
276     public void doEndPanel(StringBuffer JavaDoc buffer) {
277         buffer.append("</table></td></tr></table>\n");
278         if (collectionTag.getWidth()!=null || collectionTag.getHeight()!=null) {
279             buffer.append("</div>");
280         }
281     }
282     
283     // ------------------------- specific datagrid methods ----------------- //
284

285     /**
286      * Return the style of the specified column.
287      */

288     public String JavaDoc getColumnStyleClass(int in_columnNumber) {
289         // No specified style
290
return "";
291     }
292
293     /**
294      * Return the usual row style class.
295      */

296     public String JavaDoc getRowStyleClass() {
297         return styleClass;
298     }
299
300     /**
301      * Return the alternate style class.
302      */

303     public String JavaDoc getRowStyleClass2() {
304         return styleClass + "2";
305     }
306
307     /**
308      * Return the styles for specific states.
309      */

310     public Map JavaDoc getRowStyleClassMap() {
311         return styles;
312     }
313     
314     // ---------------- unused inherited methods --------------- //
315

316     public void doAfterBody(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
317         // Do nothing
318
}
319
320     public void doBeforeBody(StringBuffer JavaDoc buffer, String JavaDoc align) throws JspException JavaDoc {
321         // Do nothing
322
}
323
324     public void doPrintBlankLine(StringBuffer JavaDoc buffer, int cols) throws JspException JavaDoc {
325         // Do nothing
326
}
327
328 }
Popular Tags