KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspContext JavaDoc;
24 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26
27 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
28 import org.apache.beehive.netui.tags.rendering.TrTag;
29 import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
30 import org.apache.beehive.netui.tags.rendering.StringBuilderRenderAppender;
31 import org.apache.beehive.netui.tags.html.HtmlConstants;
32 import org.apache.beehive.netui.databinding.datagrid.runtime.rendering.table.TableRenderer;
33 import org.apache.beehive.netui.databinding.datagrid.runtime.util.JspUtil;
34 import org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel;
35 import org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel;
36 import org.apache.beehive.netui.util.Bundle;
37 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
38
39 /**
40  * <p>
41  * This tag is optionally used to render HTML table roe tags inside of one of the data grid tags that
42  * are used to denote data grid row rendering boundaries. When the {@link Header}, {@link Rows}, or {@link Footer}
43  * have their <code>renderRow</code> attribute set to <code>false</code>, no HTML table row element will render
44  * before starting to render the body of one of these tags. This tag should be used when <code>renderRows</code>
45  * is <code>false</code> in order to render an HTML table row. The Row tag is used this way in order to
46  * allow a page author to set JSP tag attributes that can configure each rendered table row differently.
47  * For example:
48  * <pre>
49  * &lt;netui-data:rows renderRows="false">
50  * &lt;netui-data:row styleClass="rowStyle${container.index}">
51  * &lt;netui-data:spanCell value="${container.item}"/>
52  * &lt;/netui-data:row>
53  * &lt;/netui-data:rows>
54  * </pre>
55  * and a data set containing ["foo", "bar", "baz"] will render:
56  * <pre>
57  * &lt;tr class="rowStyle0">foo&lt;/tr>
58  * &lt;tr class="rowStyle1">bar&lt;/tr>
59  * &lt;tr class="rowStyle2">baz&lt;/tr>
60  * </pre>
61  * If the &lt;netui-data:row$gt; were omitted, none of the &lt;tr&gt; elements would be rendered in the output. Note,
62  * this tag <b>should not</b> be used inside of the Header, Rows, or Footer tags unless their <code>renderRow</code>
63  * attribute is set to <code>false</code>
64  * </p>
65  * @jsptagref.tagdescription
66  * <p>
67  * This tag is optionally used to render HTML table roe tags inside of one of the data grid tags that
68  * are used to denote data grid row rendering boundaries. When the {@link Header}, {@link Rows}, or {@link Footer}
69  * have their <code>renderRow</code> attribute set to <code>false</code>, no HTML table row element will render
70  * before starting to render the body of one of these tags. This tag should be used when <code>renderRows</code>
71  * is <code>false</code> in order to render an HTML table row. The Row tag is used this way in order to
72  * allow a page author to set JSP tag attributes that can configure each rendered table row differently.
73  * For example:
74  * <pre>
75  * &lt;netui-data:rows renderRows="false">
76  * &lt;netui-data:row styleClass="rowStyle${container.index}">
77  * &lt;netui-data:spanCell value="${container.item}"/>
78  * &lt;/netui-data:row>
79  * &lt;/netui-data:rows>
80  * </pre>
81  * and a data set containing ["foo", "bar", "baz"] will render:
82  * <pre>
83  * &lt;tr class="rowStyle0">foo&lt;/tr>
84  * &lt;tr class="rowStyle1">bar&lt;/tr>
85  * &lt;tr class="rowStyle2">baz&lt;/tr>
86  * </pre>
87  * If the &lt;netui-data:row$gt; were omitted, none of the &lt;tr&gt; elements would be rendered in the output. Note,
88  * this tag <b>should not</b> be used inside of the Header, Rows, or Footer tags unless their <code>renderRow</code>
89  * attribute is set to <code>false</code>
90  * </p>
91  * @netui:tag name="row" body-content="scriptless"
92  * description="Tag optionally used inside of a Header, Rows, or Footer tag to render HTML table row elements."
93  */

94 public class Row
95     extends AbstractDataGridHtmlTag {
96
97     private TrTag.State _trState = new TrTag.State();
98
99     /**
100      * The name of this tag; this value is used for error reporting.
101      * @return the String name of this tag
102      */

103     public final String JavaDoc getTagName() {
104         return "Row";
105     }
106
107     /**
108      * Sets the onClick JavaScript for the HTML tr tag.
109      *
110      * @param onClick the onClick event.
111      * @jsptagref.attributedescription The onClick JavaScript for the HTML tr tag.
112      * @jsptagref.attributesyntaxvalue <i>string_onClick</i>
113      * @netui:attribute required="false" rtexprvalue="true" description="The onClick JavaScript for the HTML tr tag."
114      */

115     public void setOnClick(String JavaDoc onClick) {
116         _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONCLICK, onClick);
117     }
118
119     /**
120      * Sets the onDblClick JavaScript for the HTML tr tag.
121      *
122      * @param onDblClick the onDblClick event.
123      * @jsptagref.attributedescription The onDblClick JavaScript for the HTML tr tag.
124      * @jsptagref.attributesyntaxvalue <i>string_onDblClick</i>
125      * @netui:attribute required="false" rtexprvalue="true" description="The onDblClick JavaScript for the HTML tr tag."
126      */

127     public void setOnDblClick(String JavaDoc onDblClick) {
128         _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONDBLCLICK, onDblClick);
129     }
130
131     /**
132      * Sets the onKeyDown JavaScript for the HTML tr tag.
133      *
134      * @param onKeyDown the onKeyDown event.
135      * @jsptagref.attributedescription The onKeyDown JavaScript for the HTML tr tag.
136      * @jsptagref.attributesyntaxvalue <i>string_onKeyDown</i>
137      * @netui:attribute required="false" rtexprvalue="true" description="The onKeyDown JavaScript for the HTML tr tag."
138      */

139     public void setOnKeyDown(String JavaDoc onKeyDown) {
140         _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONKEYDOWN, onKeyDown);
141     }
142
143     /**
144      * Sets the onKeyUp JavaScript for the HTML tr tag.
145      *
146      * @param onKeyUp the onKeyUp event.
147      * @jsptagref.attributedescription The onKeyUp JavaScript for the HTML tr tag.
148      * @jsptagref.attributesyntaxvalue <i>string_onKeyUp</i>
149      * @netui:attribute required="false" rtexprvalue="true" description="The onKeyUp JavaScript for the HTML tr tag."
150      */

151     public void setOnKeyUp(String JavaDoc onKeyUp) {
152         _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONKEYUP, onKeyUp);
153     }
154
155     /**
156      * Sets the onKeyPress JavaScript for the HTML tr tag.
157      *
158      * @param onKeyPress the onKeyPress event.
159      * @jsptagref.attributedescription The onKeyPress JavaScript for the HTML tr tag.
160      * @jsptagref.attributesyntaxvalue <i>string_onKeyPress</i>
161      * @netui:attribute required="false" rtexprvalue="true" description="The onKeyPress JavaScript for the HTML tr tag."
162      */

163     public void setOnKeyPress(String JavaDoc onKeyPress) {
164         _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONKEYPRESS, onKeyPress);
165     }
166
167     /**
168      * Sets the onMouseDown JavaScript for the HTML tr tag.
169      *
170      * @param onMouseDown the onMouseDown event.
171      * @jsptagref.attributedescription The onMouseDown JavaScript for the HTML tr tag.
172      * @jsptagref.attributesyntaxvalue <i>string_onMouseDown</i>
173      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseDown JavaScript for the HTML tr tag."
174      */

175     public void setOnMouseDown(String JavaDoc onMouseDown) {
176         _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEDOWN, onMouseDown);
177     }
178
179     /**
180      * Sets the onMouseUp JavaScript for the HTML tr tag.
181      *
182      * @param onMouseUp the onMouseUp event.
183      * @jsptagref.attributedescription The onMouseUp JavaScript for the HTML tr tag.
184      * @jsptagref.attributesyntaxvalue <i>string_onMouseUp</i>
185      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseUp JavaScript for the HTML tr tag."
186      */

187     public void setOnMouseUp(String JavaDoc onMouseUp) {
188         _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEUP, onMouseUp);
189     }
190
191     /**
192      * Sets the onMouseMove JavaScript for the HTML tr tag.
193      *
194      * @param onMouseMove the onMouseMove event.
195      * @jsptagref.attributedescription The onMouseMove JavaScript for the HTML tr tag.
196      * @jsptagref.attributesyntaxvalue <i>string_onMouseMove</i>
197      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseMove JavaScript for the HTML tr tag."
198      */

199     public void setOnMouseMove(String JavaDoc onMouseMove) {
200         _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEMOVE, onMouseMove);
201     }
202
203     /**
204      * Sets the onMouseOut JavaScript for the HTML tr tag.
205      *
206      * @param onMouseOut the onMouseOut event.
207      * @jsptagref.attributedescription The onMouseOut JavaScript for the HTML tr tag.
208      * @jsptagref.attributesyntaxvalue <i>string_onMouseOut</i>
209      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseOut JavaScript for the HTML tr tag."
210      */

211     public void setOnMouseOut(String JavaDoc onMouseOut) {
212         _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEOUT, onMouseOut);
213     }
214
215     /**
216      * Sets the onMouseOver JavaScript for the HTML tr tag.
217      *
218      * @param onMouseOver the onMouseOver event.
219      * @jsptagref.attributedescription The onMouseOver JavaScript for the HTML tr tag.
220      * @jsptagref.attributesyntaxvalue <i>string_onMouseOver</i>
221      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseOver JavaScript for the HTML tr tag."
222      */

223     public void setOnMouseOver(String JavaDoc onMouseOver) {
224         _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEOVER, onMouseOver);
225     }
226
227     /**
228      * Sets the style attribute for the HTML tr tag.
229      *
230      * @param style the style
231      * @jsptagref.attributedescription The style attribute for the HTML tr tag.
232      * @jsptagref.attributesyntaxvalue <i>string_style</i>
233      * @netui:attribute required="false" rtexprvalue="true"
234      * description="The style attribute for the HTML tr tag."
235      */

236     public void setStyle(String JavaDoc style) {
237         if("".equals(style)) return;
238
239         _trState.style = style;
240     }
241
242     /**
243      * Sets the style class for the HTML tr tag.
244      *
245      * @param styleClass the style class.
246      * @jsptagref.attributedescription The style class for the HTML tr tag.
247      * @jsptagref.attributesyntaxvalue <i>string_style_class</i>
248      * @netui:attribute required="false" rtexprvalue="true" description="The style class for the HTML tr tag."
249      */

250     public void setStyleClass(String JavaDoc styleClass) {
251         if("".equals(styleClass)) return;
252
253         _trState.styleClass = styleClass;
254     }
255
256     /**
257      * Sets the title attribute for the HTML tr tag.
258      *
259      * @param title the title
260      * @jsptagref.attributedescription The title for the HTML tr tag.
261      * @jsptagref.attributesyntaxvalue <i>string_title</i>
262      * @netui:attribute required="false" rtexprvalue="true" description="The title for the HTML tr tag."
263      */

264     public void setTitle(String JavaDoc title) {
265         _trState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.TITLE, title);
266     }
267
268     /**
269      * Sets the lang attribute for the HTML tr tag.
270      * @param lang the lang
271      * @jsptagref.attributedescription The lang for the HTML tr tag.
272      * @jsptagref.attributesyntaxvalue <i>string_lang</i>
273      * @netui:attribute required="false" rtexprvalue="true"
274      * description="The lang for the HTML tr tag."
275      */

276     public void setLang(String JavaDoc lang)
277     {
278         _trState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.LANG, lang);
279     }
280
281     /**
282      * Sets the lang attribute for the HTML element tr tag.
283      * @param dir the dir
284      * @jsptagref.attributedescription The dir for the HTML tr tag.
285      * @jsptagref.attributesyntaxvalue <i>string_dir</i>
286      * @netui:attribute required="false" rtexprvalue="true"
287      * description="The dir for the HTML tr tag.
288      */

289     public void setDir(String JavaDoc dir)
290     {
291         _trState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.DIR, dir);
292     }
293
294     /**
295      * Set the name of the tagId for the HTML tr tag.
296      *
297      * @param tagId the the name of the tagId for the table row.
298      * @jsptagref.attributedescription The tagId for the HTML tr tat.
299      * @jsptagref.attributesyntaxvalue <i>string_tagId</i>
300      * @netui:attribute required="false" rtexprvalue="true"
301      * description="String value. Sets the id (or name) attribute of the HTML tr tag."
302      */

303     public void setTagId(String JavaDoc tagId)
304         throws JspException JavaDoc {
305         JspContext JavaDoc jspContext = getJspContext();
306         DataGridTagModel dataGridModel = DataGridUtil.getDataGridTagModel(jspContext);
307         if(dataGridModel == null)
308             throw new JspException JavaDoc(Bundle.getErrorString("DataGridTags_MissingDataGridModel"));
309
310         int renderState = dataGridModel.getRenderState();
311
312         if(renderState == DataGridTagModel.RENDER_STATE_GRID)
313             applyIndexedTagId(_trState, tagId);
314         else applyTagId(_trState, tagId);
315     }
316
317     /**
318      * <p>
319      * Render this tag. This tag renders during the data grid's {@link DataGridTagModel#RENDER_STATE_HEADER},
320      * {@link DataGridTagModel#RENDER_STATE_GRID}, and the {@link DataGridTagModel#RENDER_STATE_FOOTER} render
321      * states. This tag will always render the an HTML table row tag and its body. The result is added
322      * to the output stream.
323      * </p>
324      * <p>
325      * Unless the {@link #setStyleClass(String)} attribute has been set and is non-null, the following style
326      * classes are used during the various supported rendering states:
327      * <table>
328      * <tr><td>Render State</td><td>Style Class</td></tr>
329      * <tr><td>{@link DataGridTagModel#RENDER_STATE_HEADER}</td>
330      * <td>{@link org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel#getHeaderRowClass()}</td></tr>
331      * <tr><td>{@link DataGridTagModel#RENDER_STATE_GRID}</td>
332      * <td>{@link org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel#getRowClass()} for
333      * an even row {@link org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel#getAltRowClass()}
334      * for odd rows.</td></tr>
335      * <tr><td>{@link DataGridTagModel#RENDER_STATE_FOOTER}</td>
336      * <td>{@link org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel#getFooterRowClass()}</td></tr>
337      * </table>
338      * </p>
339      * @throws JspException when the {@link DataGridTagModel} can not be found in the {@link JspContext}
340      * @throws IOException
341      */

342     public void doTag()
343         throws JspException JavaDoc, IOException JavaDoc {
344
345         JspContext JavaDoc jspContext = getJspContext();
346         DataGridTagModel dataGridModel = DataGridUtil.getDataGridTagModel(jspContext);
347         if(dataGridModel == null)
348             throw new JspException JavaDoc(Bundle.getErrorString("DataGridTags_MissingDataGridModel"));
349
350         int renderState = dataGridModel.getRenderState();
351
352         if(!(renderState == DataGridTagModel.RENDER_STATE_HEADER ||
353            renderState != DataGridTagModel.RENDER_STATE_GRID ||
354            renderState != DataGridTagModel.RENDER_STATE_FOOTER))
355             return;
356
357         JspFragment JavaDoc fragment = getJspBody();
358
359         StyleModel styleModel = dataGridModel.getStyleModel();
360         assert styleModel != null;
361
362         TableRenderer tableRenderer = dataGridModel.getTableRenderer();
363         assert tableRenderer != null;
364
365         HttpServletRequest JavaDoc request = JspUtil.getRequest(getJspContext());
366         InternalStringBuilder content = new InternalStringBuilder();
367         AbstractRenderAppender appender = new StringBuilderRenderAppender(content);
368
369         if(_trState.styleClass == null) {
370             if(renderState == DataGridTagModel.RENDER_STATE_GRID) {
371                 int index = dataGridModel.getCurrentIndex();
372                 if(index % 2 == 0)
373                     _trState.styleClass = styleModel.getRowClass();
374                 else _trState.styleClass = styleModel.getAltRowClass();
375             }
376             else if(renderState == DataGridTagModel.RENDER_STATE_HEADER)
377                 _trState.styleClass = styleModel.getHeaderRowClass();
378             else if(renderState == DataGridTagModel.RENDER_STATE_FOOTER)
379                 _trState.styleClass = styleModel.getFooterRowClass();
380             else assert false : "Attempting to apply style information during an invalid render state";
381         }
382
383         String JavaDoc trScript = null;
384         if(_trState.id != null)
385             trScript = renderNameAndId(request, _trState, null);
386
387         tableRenderer.openTableRow(_trState, appender);
388
389         StringWriter JavaDoc sw = new StringWriter JavaDoc();
390         if(fragment != null)
391             fragment.invoke(sw);
392         appender.append(sw.toString());
393
394         tableRenderer.closeTableRow(appender);
395
396         if(trScript != null)
397             appender.append(trScript);
398
399         jspContext.getOut().write(content.toString());
400
401         return;
402     }
403 }
404
Popular Tags