KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > databinding > datagrid > Rows


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.databinding.datagrid;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import java.io.IOException JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspContext JavaDoc;
26 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 import org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel;
30 import org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel;
31 import org.apache.beehive.netui.databinding.datagrid.runtime.rendering.table.TableRenderer;
32 import org.apache.beehive.netui.databinding.datagrid.runtime.util.JspUtil;
33 import org.apache.beehive.netui.tags.IHtmlEvents;
34 import org.apache.beehive.netui.tags.IHtmlI18n;
35 import org.apache.beehive.netui.tags.html.HtmlConstants;
36 import org.apache.beehive.netui.tags.rendering.StringBuilderRenderAppender;
37 import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
38 import org.apache.beehive.netui.tags.rendering.TrTag;
39 import org.apache.beehive.netui.tags.rendering.TBodyTag;
40 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
41 import org.apache.beehive.netui.util.Bundle;
42
43 /**
44  * <p>
45  * The Rows tag is used to render a table row(s) that contain the data from a data set displayed
46  * in a data grid. The rows rendered here render after the header reders with the {@link Header} and before
47  * the footer renders in the {@link Footer}. The body of this tag usually contains the data grid's "cell" tags
48  * which are used to render HTML table cells inside of the rows rendered by this tag. Such tags include:
49  * <ul>
50  * <li>{@link AnchorCell} which is used to render anchors</li>
51  * <li>{@link ImageAnchorCell} which is used to render clickable images</li>
52  * <li>{@link ImageCell} which is used to render an image</li>
53  * <li>{@link SpanCell} which is used to render an HTML span with data bound content</li>
54  * <li>{@link TemplateCell} which can contain arbitrary content for {@link javax.servlet.jsp.tagext.SimpleTag}</li>
55  * </ul>
56  * </p>
57  * <p>
58  * The attribute setters in this class are used to add attributes to the &lt;tbody&gt; tag which will be rendered
59  * when row groups are enabled. When row group rendering is disabled, attributes set here do not render.
60  * </p>
61  * <p>
62  * Because this tag renders inside of an HTML table, it by default renders an HTML
63  * &lt;tr&gt; tag to represent a table row. Table row tag rendering can be disabled using the {@link #setRenderRow(boolean)}
64  * attribute. When this is disabled, the page author is responsible for maintaining the integrity of the
65  * HTML table by writing &lt;tr&gt; tags manually or by using the {@link Row} tag. When this tag is rendering
66  * it does not produce table cells; the contents of the table row in the footer is entirely left to the
67  * page author. With row rendering disabled, it is also possible to add multiple table rows to the
68  * end of a data grid.
69  * </p>
70  * <p>
71  * The set of JSP implicit objects available to the body include:
72  * <ul>
73  * <li><code>dataGridModel</code> -- the {@link org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel}
74  * for the cell's containing data grid.</li>
75  * </ul>
76  * </p>
77  * @jsptagref.tagdescription
78  * <p>
79  * The Rows tag is used to render a table row(s) that contain the data from a data set displayed
80  * in a data grid. The rows rendered here render after the header reders with the {@link Header} and before
81  * the footer renders in the {@link Footer}. The body of this tag usually contains the data grid's "cell" tags
82  * which are used to render HTML table cells inside of the rows rendered by this tag. Such tags include:
83  * <ul>
84  * <li>{@link AnchorCell} which is used to render anchors</li>
85  * <li>{@link ImageAnchorCell} which is used to render clickable images</li>
86  * <li>{@link ImageCell} which is used to render an image</li>
87  * <li>{@link SpanCell} which is used to render an HTML span with data bound content</li>
88  * <li>{@link TemplateCell} which can contain arbitrary content for {@link javax.servlet.jsp.tagext.SimpleTag}</li>
89  * </ul>
90  * </p>
91  * <p>
92  * The attribute setters in this class are used to add attributes to the &lt;tbody&gt; tag which will be rendered
93  * when row groups are enabled. When row group rendering is disabled, attributes set here do not render.
94  * </p>
95  * <p>
96  * Because this tag renders inside of an HTML table, it by default renders an HTML
97  * &lt;tr&gt; tag to represent a table row. Table row tag rendering can be disabled using the {@link #setRenderRow(boolean)}
98  * attribute. When this is disabled, the page author is responsible for maintaining the integrity of the
99  * HTML table by writing &lt;tr&gt; tags manually or by using the {@link Row} tag. When this tag is rendering
100  * it does not produce table cells; the contents of the table row in the footer is entirely left to the
101  * page author. With row rendering disabled, it is also possible to add multiple table rows to the
102  * end of a data grid.
103  * </p>
104  * <p>
105  * The set of JSP implicit objects available to the body include:
106  * <ul>
107  * <li><code>dataGridModel</code> -- the {@link org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel}
108  * for the cell's containing data grid.</li>
109  * </ul>
110  * </p>
111  *
112  * @netui:tag name="rows" body-content="scriptless"
113  * description="Renders the data rows into a NetUI data grid"
114  */

115 public class Rows
116     extends AbstractDataGridHtmlTag
117     implements IHtmlEvents, IHtmlI18n {
118
119     private boolean _renderRow = true;
120     private TBodyTag.State _tbodyTag = new TBodyTag.State();
121
122     /**
123      * The name of this tag; this value is used for error reporting.
124      * @return the String name of this tag
125      */

126     public String JavaDoc getTagName() {
127         return "Rows";
128     }
129
130     /**
131      * Sets the onClick JavaScript event for the HTML tbody tag.
132      *
133      * @param onClick the onClick event.
134      * @jsptagref.attributedescription The onClick JavaScript event for the HTML tbody tag.
135      * @jsptagref.attributesyntaxvalue <i>string_onClick</i>
136      * @netui:attribute required="false" rtexprvalue="true" description="The onClick JavaScript event for the HTML tbody tag."
137      */

138     public void setOnClick(String JavaDoc onClick) {
139         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONCLICK, onClick);
140     }
141
142     /**
143      * Sets the onDblClick JavaScript event for the HTML tbody tag.
144      *
145      * @param onDblClick the onDblClick event.
146      * @jsptagref.attributedescription The onDblClick JavaScript event for the HTML tbody tag.
147      * @jsptagref.attributesyntaxvalue <i>string_onDblClick</i>
148      * @netui:attribute required="false" rtexprvalue="true" description="The onDblClick JavaScript event for the HTML tbody tag."
149      */

150     public void setOnDblClick(String JavaDoc onDblClick) {
151         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONDBLCLICK, onDblClick);
152     }
153
154     /**
155      * Sets the onKeyDown JavaScript event for the HTML tbody tag.
156      *
157      * @param onKeyDown the onKeyDown event.
158      * @jsptagref.attributedescription The onKeyDown JavaScript event for the HTML tbody tag.
159      * @jsptagref.attributesyntaxvalue <i>string_onKeyDown</i>
160      * @netui:attribute required="false" rtexprvalue="true" description="The onKeyDown JavaScript event for the HTML tbody tag."
161      */

162     public void setOnKeyDown(String JavaDoc onKeyDown) {
163         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONKEYDOWN, onKeyDown);
164     }
165
166     /**
167      * Sets the onKeyUp JavaScript event for the HTML tbody tag.
168      *
169      * @param onKeyUp the onKeyUp event.
170      * @jsptagref.attributedescription The onKeyUp JavaScript event for the HTML tbody tag.
171      * @jsptagref.attributesyntaxvalue <i>string_onKeyUp</i>
172      * @netui:attribute required="false" rtexprvalue="true" description="The onKeyUp JavaScript event for the HTML tbody tag."
173      */

174     public void setOnKeyUp(String JavaDoc onKeyUp) {
175         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONKEYUP, onKeyUp);
176     }
177
178     /**
179      * Sets the onKeyPress JavaScript event for the HTML tbody tag.
180      *
181      * @param onKeyPress the onKeyPress event.
182      * @jsptagref.attributedescription The onKeyPress JavaScript event for the HTML tbody tag.
183      * @jsptagref.attributesyntaxvalue <i>string_onKeyPress</i>
184      * @netui:attribute required="false" rtexprvalue="true" description="The onKeyPress JavaScript event for the HTML tbody tag."
185      */

186     public void setOnKeyPress(String JavaDoc onKeyPress) {
187         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONKEYPRESS, onKeyPress);
188     }
189
190     /**
191      * Sets the onMouseDown JavaScript event for the HTML tbody tag.
192      *
193      * @param onMouseDown the onMouseDown event.
194      * @jsptagref.attributedescription The onMouseDown JavaScript event for the HTML tbody tag.
195      * @jsptagref.attributesyntaxvalue <i>string_onMouseDown</i>
196      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseDown JavaScript event for the HTML tbody tag."
197      */

198     public void setOnMouseDown(String JavaDoc onMouseDown) {
199         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEDOWN, onMouseDown);
200     }
201
202     /**
203      * Sets the onMouseUp JavaScript event for the HTML tbody tag.
204      *
205      * @param onMouseUp the onMouseUp event.
206      * @jsptagref.attributedescription The onMouseUp JavaScript event for the HTML tbody tag.
207      * @jsptagref.attributesyntaxvalue <i>string_onMouseUp</i>
208      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseUp JavaScript event for the HTML tbody tag."
209      */

210     public void setOnMouseUp(String JavaDoc onMouseUp) {
211         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEUP, onMouseUp);
212     }
213
214     /**
215      * Sets the onMouseMove JavaScript event for the HTML tbody tag.
216      *
217      * @param onMouseMove the onMouseMove event.
218      * @jsptagref.attributedescription The onMouseMove JavaScript event for the HTML tbody tag.
219      * @jsptagref.attributesyntaxvalue <i>string_onMouseMove</i>
220      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseMove JavaScript event for the HTML tbody tag."
221      */

222     public void setOnMouseMove(String JavaDoc onMouseMove) {
223         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEMOVE, onMouseMove);
224     }
225
226     /**
227      * Sets the onMouseOut JavaScript event for the HTML tbody tag.
228      *
229      * @param onMouseOut the onMouseOut event.
230      * @jsptagref.attributedescription The onMouseOut JavaScript event for the HTML tbody tag.
231      * @jsptagref.attributesyntaxvalue <i>string_onMouseOut</i>
232      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseOut JavaScript event for the HTML tbody tag."
233      */

234     public void setOnMouseOut(String JavaDoc onMouseOut) {
235         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEOUT, onMouseOut);
236     }
237
238     /**
239      * Sets the onMouseOver JavaScript event for the HTML tbody tag.
240      *
241      * @param onMouseOver the onMouseOver event.
242      * @jsptagref.attributedescription The onMouseOver JavaScript event for the HTML tbody tag.
243      * @jsptagref.attributesyntaxvalue <i>string_onMouseOver</i>
244      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseOver JavaScript event for the HTML tbody tag."
245      */

246     public void setOnMouseOver(String JavaDoc onMouseOver) {
247         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEOVER, onMouseOver);
248     }
249
250     /**
251      * Sets the style of the HTML tbody tag.
252      *
253      * @param style the style
254      * @jsptagref.attributedescription The style of the HTML tbody tag.
255      * @jsptagref.attributesyntaxvalue <i>string_style</i>
256      * @netui:attribute required="false" rtexprvalue="true" description="The style of the HTML tbody tag."
257      */

258     public void setStyle(String JavaDoc style) {
259         if("".equals(style)) return;
260
261         _tbodyTag.style = style;
262     }
263
264     /**
265      * Sets the style class of the HTML tbody tag.
266      *
267      * @param styleClass the style class
268      * @jsptagref.attributedescription The style class of the HTML tbody tag.
269      * @jsptagref.attributesyntaxvalue <i>string_style_class</i>
270      * @netui:attribute required="false" rtexprvalue="true" description="The style class of the HTML tbody tag."
271      */

272     public void setStyleClass(String JavaDoc styleClass) {
273         if("".equals(styleClass)) return;
274
275         _tbodyTag.styleClass = styleClass;
276     }
277
278     /**
279      * Sets the value of the title attribute for the HTML tbody tag.
280      *
281      * @param title the title
282      * @jsptagref.attributedescription The title for the HTML tbody tag
283      * @jsptagref.attributesyntaxvalue <i>string_title</i>
284      * @netui:attribute required="false" rtexprvalue="true" description="The title for the HTML tbody tag"
285      */

286     public void setTitle(String JavaDoc title) {
287         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.TITLE, title);
288     }
289
290     /**
291      * Sets the value of the horizontal alignment attribute of the HTML tbody tag.
292
293      * @param align the horizontal alignment
294      * @jsptagref.attributedescription The horizontal alignment of the HTML tbody tag.
295      * @jsptagref.attributesyntaxvalue <i>string_align</i>
296      * @netui:attribute required="false" rtexprvalue="true" description="The cell's horizontal alignment of the HTML tbody tag."
297      */

298     public void setAlign(String JavaDoc align) {
299         /* todo: should this enforce left|center|right|justify|char as in the spec */
300         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.ALIGN, align);
301     }
302
303     /**
304      * Sets the value of the horizontal alignment character attribute of the HTML tbody tag.
305      *
306      * @param alignChar the horizontal alignment character
307      * @jsptagref.attributedescription The horizontal alignment character of the HTML tbody tag.
308      * @jsptagref.attributesyntaxvalue <i>string_alignChar</i>
309      * @netui:attribute required="false" rtexprvalue="true" description="The cell's horizontal alignment character of the HTML tbody tag."
310      */

311     public void setChar(String JavaDoc alignChar) {
312         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.CHAR, alignChar);
313     }
314
315     /**
316      * Sets the value of the horizontal alignment character offset attribute.
317      *
318      * @param alignCharOff
319      * @jsptagref.attributedescription The horizontal alignment character offset
320      * @jsptagref.attributesyntaxvalue <i>string_alignCharOff</i>
321      * @netui:attribute required="false" rtexprvalue="true" description="The cell's horizontal alignment character offset"
322      */

323     public void setCharoff(String JavaDoc alignCharOff) {
324         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.CHAROFF, alignCharOff);
325     }
326
327     /**
328      * Sets the value of the vertical alignment attribute of the HTML tbody tag.
329      *
330      * @param align the alignment
331      * @jsptagref.attributedescription The vertical alignment.
332      * @jsptagref.attributesyntaxvalue <i>string_align</i>
333      * @netui:attribute required="false" rtexprvalue="true" description="The vertical alignment of the HTML tbody tag"
334      */

335     public void setValign(String JavaDoc align) {
336         /* todo: should this enforce top|middle|bottom|baseline as in the spec */
337         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.VALIGN, align);
338     }
339
340     /**
341      * Sets the value of the language attribute of the HTML tbody tag.
342      *
343      * @param lang the language
344      * @jsptagref.attributedescription The language of the HTML tbody tag.
345      * @jsptagref.attributesyntaxvalue <i>string_lang</i>
346      * @netui:attribute required="false" rtexprvalue="true" description="The language of the HTML tbody tag."
347      */

348     public void setLang(String JavaDoc lang) {
349         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.LANG, lang);
350     }
351
352     /**
353      * Sets the value of the text direction attribute of the HTML tbody tag.
354      *
355      * @param dir the dir
356      * @jsptagref.attributedescription The text direction attribute of the HTML tbody tag.
357      * @jsptagref.attributesyntaxvalue <i>string_dir</i>
358      * @netui:attribute required="false" rtexprvalue="true" description="The text direction of the HTML tbody tag.
359      */

360     public void setDir(String JavaDoc dir) {
361         _tbodyTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.DIR, dir);
362     }
363
364     /**
365      * Set the name of the tagId for the HTML tbody tag.
366      *
367      * @param tagId - the the name of the tagId for the tbody tag.
368      * @jsptagref.attributedescription The tagId of the HTML tbody tag.
369      * @jsptagref.attributesyntaxvalue <i>string_tagId</i>
370      * @netui:attribute required="false" rtexprvalue="true"
371      * description="String value. Sets the id (or name) attribute of the HTML tbody tag."
372      */

373     public void setTagId(String JavaDoc tagId)
374         throws JspException JavaDoc {
375         applyTagId(_tbodyTag, tagId);
376     }
377
378     /**
379      * Set a boolean that enables / disables rendering HTML table rows by this tag. If the
380      * value is enabled, an HTML &lt;tr&gt; will be rendered when this tag renders its body. If
381      * the value is disabled, no &lt;tr&gt; tags will be rendered and the page author is responsible
382      * for maintaining the integrity of the HTML table.
383      * @jsptagref.attributedescription
384      * Set a boolean that enables / disables rendering HTML table rows by this tag. If the
385      * value is enabled, an HTML &lt;tr&gt; will be rendered when this tag renders its body. If
386      * the value is disabled, no &lt;tr&gt; tags will be rendered and the page author is responsible
387      * for maintaining the integrity of the HTML table.
388      * @jsptagref.attributesyntaxvalue <i>boolean_renderRow</i>
389      * @netui:attribute required="false" rtexprvalue="true"
390      * description="Boolean to enable / disable rendering HTML table row tags"
391      */

392     public void setRenderRow(boolean renderRow) {
393         _renderRow = renderRow;
394     }
395
396     /**
397      * Render this tag. This method renders during the data grid's {@link DataGridTagModel#RENDER_STATE_GRID}
398      * state in order to add table rows to the beginning of a data grid's HTML table. If the data grid is rendering
399      * HTML row groups, this tag will output an HTML &lt;tbody&gt; tag. Then, if this tag is rendering
400      * a table row, it will produce an HTML &lt;tr&gt; tag. Then the content of the body will be rendered. If
401      * table row rendering is disabled, the page author is responsible for rendering the appropriate HTML
402      * table row tags as this tag renders inside of the HTML table opened by the data grid.
403      * @throws IOException
404      * @throws JspException when the {@link DataGridTagModel} can not be found in the {@link JspContext}
405      */

406     public void doTag()
407         throws IOException JavaDoc, JspException JavaDoc {
408
409         JspContext JavaDoc jspContext = getJspContext();
410         DataGridTagModel dataGridModel = DataGridUtil.getDataGridTagModel(jspContext);
411         if(dataGridModel == null)
412             throw new JspException JavaDoc(Bundle.getErrorString("DataGridTags_MissingDataGridModel"));
413
414         if(dataGridModel.getRenderState() == DataGridTagModel.RENDER_STATE_GRID) {
415
416             StyleModel styleModel = dataGridModel.getStyleModel();
417             assert styleModel != null;
418
419             TableRenderer tableRenderer = dataGridModel.getTableRenderer();
420             assert tableRenderer != null;
421
422             InternalStringBuilder content = new InternalStringBuilder();
423             AbstractRenderAppender appender = new StringBuilderRenderAppender(content);
424             JspFragment JavaDoc fragment = getJspBody();
425
426             if(dataGridModel.isRenderRowGroups())
427                 tableRenderer.openTableBody(_tbodyTag, appender);
428
429             HttpServletRequest JavaDoc request = JspUtil.getRequest(getJspContext());
430             while(dataGridModel.hasNextDataItem()) {
431
432                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
433
434                 /* first things first -- advance to the next data item */
435                 dataGridModel.nextDataItem();
436
437                 fragment.invoke(sw);
438
439                 String JavaDoc trScript = null;
440                 if(_renderRow) {
441                     TrTag.State trState = new TrTag.State();
442                     int index = dataGridModel.getCurrentIndex();
443                     if(index % 2 == 0)
444                         trState.styleClass = styleModel.getRowClass();
445                     else trState.styleClass = styleModel.getAltRowClass();
446
447                     if(trState.id != null)
448                         trScript = renderNameAndId(request, trState, null);
449
450                     tableRenderer.openTableRow(trState, appender);
451                 }
452
453                 content.append(sw.toString());
454
455                 if(_renderRow) {
456                     tableRenderer.closeTableRow(appender);
457
458                     if(trScript != null)
459                         appender.append(trScript);
460                 }
461             }
462
463             if(dataGridModel.isRenderRowGroups()) {
464                 tableRenderer.closeTableBody(appender);
465
466                 String JavaDoc tbodyScript = null;
467                 if(_tbodyTag.id != null) {
468                     tbodyScript = renderNameAndId(request, _tbodyTag, null);
469                 }
470
471                 if(tbodyScript != null)
472                     appender.append(tbodyScript);
473             }
474
475             jspContext.getOut().write(content.toString());
476         }
477     }
478 }
479
Popular Tags