KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.JspContext JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 import org.apache.beehive.netui.tags.IHtmlI18n;
30 import org.apache.beehive.netui.tags.IHtmlEvents;
31 import org.apache.beehive.netui.tags.html.HtmlConstants;
32 import org.apache.beehive.netui.tags.rendering.THeadTag;
33 import org.apache.beehive.netui.tags.rendering.TrTag;
34 import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
35 import org.apache.beehive.netui.tags.rendering.StringBuilderRenderAppender;
36 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
37 import org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel;
38 import org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel;
39 import org.apache.beehive.netui.databinding.datagrid.runtime.rendering.table.TableRenderer;
40 import org.apache.beehive.netui.databinding.datagrid.runtime.util.JspUtil;
41 import org.apache.beehive.netui.util.Bundle;
42
43 /**
44  * <p>
45  * The Header tag is used to render a table row(s) before the data grid has rendered data rows as demarcated
46  * by the {@link Rows} tag. The header can also optionally render a &lt;thead&gt; HTML tag if the data grid
47  * is rendering HTML table row groups. To enable this functionality, set the
48  * {@link DataGrid#setRenderRowGroups(boolean)} attribute. The location of this tag inside of a
49  * data grid does not affect when its content renders. Table cells containing header describing columns
50  * of data along with sort and filter state / UI is commonly placed inside of a data grid's header.
51  * </p>
52  * <p>
53  * The attribute setters for the footer tag are used to add HTML attributes to the &lg;thead&gt; HTML
54  * tag. When row group rendering is disabled, attributes set here do not render.
55  * </p>
56  * <p>
57  * Because this tag renders inside of an HTML table, it by default renders an HTML
58  * &lt;tr&gt; tag to represent a table row. Table row tag rendering can be disabled using the {@link #setRenderRow(boolean)}
59  * attribute. When this is disabled, the page author is responsible for maintaining the integrity of the
60  * HTML table by writing &lt;tr&gt; tags manually or by using the {@link Row} tag. When this tag is rendering
61  * it does not produce table cells; the contents of the table row in the footer is entirely left to the
62  * page author. With row rendering disabled, it is also possible to add multiple table rows to the
63  * end of a data grid.
64  * </p>
65  * <p>
66  * The set of JSP implicit objects available to the body include:
67  * <ul>
68  * <li><code>dataGridModel</code> -- the {@link org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel}
69  * for the cell's containing data grid.</li>
70  * </ul>
71  * </p>
72  *
73  * @jsptagref.tagdescription
74  * <p>
75  * The Header tag is used to render a table row(s) before the data grid has rendered data rows as demarcated
76  * by the {@link Rows} tag. The header can also optionally render a &lt;thead&gt; HTML tag if the data grid
77  * is rendering HTML table row groups. To enable this functionality, set the
78  * {@link DataGrid#setRenderRowGroups(boolean)} attribute. The location of this tag inside of a
79  * data grid does not affect when its content renders. Table cells containing header describing columns
80  * of data along with sort and filter state / UI is commonly placed inside of a data grid's header.
81  * </p>
82  * <p>
83  * The attribute setters for the footer tag are used to add HTML attributes to the &lg;thead&gt; HTML
84  * tag. When row group rendering is disabled, attributes set here do not render.
85  * </p>
86  * <p>
87  * Because this tag renders inside of an HTML table, it by default renders an HTML
88  * &lt;tr&gt; tag to represent a table row. Table row tag rendering can be disabled using the {@link #setRenderRow(boolean)}
89  * attribute. When this is disabled, the page author is responsible for maintaining the integrity of the
90  * HTML table by writing &lt;tr&gt; tags manually or by using the {@link Row} tag. When this tag is rendering
91  * it does not produce table cells; the contents of the table row in the footer is entirely left to the
92  * page author. With row rendering disabled, it is also possible to add multiple table rows to the
93  * end of a data grid.
94  * </p>
95  * <p>
96  * The set of JSP implicit objects available to the body include:
97  * <ul>
98  * <li><code>dataGridModel</code> -- the {@link org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel}
99  * for the cell's containing data grid.</li>
100  * </ul>
101  * </p>
102  * @netui:tag name="header" description="Renders the header into a NetUI data grid" body-content="scriptless"
103  */

104 public class Header
105     extends AbstractDataGridHtmlTag
106     implements IHtmlEvents, IHtmlI18n {
107
108     private boolean _renderRow = true;
109     private THeadTag.State _theadTag = new THeadTag.State();
110
111     /**
112      * The name of this tag; this value is used for error reporting.
113      * @return the String name of this tag
114      */

115     public String JavaDoc getTagName() {
116         return "Header";
117     }
118
119     /**
120      * Sets the onClick JavaScript event for the HTML thead tag.
121      *
122      * @param onClick the onClick event.
123      * @jsptagref.attributedescription The onClick JavaScript event for the HTML thead tag.
124      * @jsptagref.attributesyntaxvalue <i>string_onClick</i>
125      * @netui:attribute required="false" rtexprvalue="true" description="The onClick JavaScript event for the HTML thead tag."
126      */

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

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

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

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

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

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

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

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

223     public void setOnMouseOut(String JavaDoc onMouseOut) {
224         _theadTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEOUT, onMouseOut);
225     }
226
227     /**
228      * Sets the onMouseOver JavaScript event for the HTML thead tag.
229      *
230      * @param onMouseOver the onMouseOver event.
231      * @jsptagref.attributedescription The onMouseOver JavaScript event for the HTML thead tag.
232      * @jsptagref.attributesyntaxvalue <i>string_onMouseOver</i>
233      * @netui:attribute required="false" rtexprvalue="true" description="The onMouseOver JavaScript event for the HTML thead tag."
234      */

235     public void setOnMouseOver(String JavaDoc onMouseOver) {
236         _theadTag.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, HtmlConstants.ONMOUSEOVER, onMouseOver);
237     }
238
239     /**
240      * Sets the style rendered by the HTML thead tag.
241      *
242      * @param style the style
243      * @jsptagref.attributedescription The style rendered by the HTML thead tag.
244      * @jsptagref.attributesyntaxvalue <i>string_style</i>
245      * @netui:attribute required="false" rtexprvalue="true" description="The style rendered by the HTML thead tag"
246      */

247     public void setStyle(String JavaDoc style) {
248         if("".equals(style)) return;
249
250         _theadTag.style = style;
251     }
252
253     /**
254      * Sets the style class of the rendered by the HTML thead tag.
255      *
256      * @param styleClass the style class
257      * @jsptagref.attributedescription The style class rendered by the HTML thead tag
258      * @jsptagref.attributesyntaxvalue <i>string_style_class</i>
259      * @netui:attribute required="false" rtexprvalue="true" description="The style class rendered by the HTML thead tag"
260      */

261     public void setStyleClass(String JavaDoc styleClass) {
262         if("".equals(styleClass)) return;
263
264         _theadTag.styleClass = styleClass;
265     }
266
267     /**
268      * Sets the value of the title attribute rendered by the HTML thead tag.
269      *
270      * @param title the title
271      * @jsptagref.attributedescription The title rendered by the HTML thead tag
272      * @jsptagref.attributesyntaxvalue <i>string_title</i>
273      * @netui:attribute required="false" rtexprvalue="true" description="The title rendered by the HTML thead tag."
274      */

275     public void setTitle(String JavaDoc title) {
276         _theadTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.TITLE, title);
277     }
278
279     /**
280      * Sets the value of the horizontal align attribute rendered by the HTML thead tag.
281      *
282      * @param align the alignment
283      * @jsptagref.attributedescription The horizontal alignment rendered by the HTML thead tag.
284      * @jsptagref.attributesyntaxvalue <i>string_align</i>
285      * @netui:attribute required="false" rtexprvalue="true" description="The horizontal alignment attribute rendered by the HTML thead tag."
286      */

287     public void setAlign(String JavaDoc align) {
288         /* todo: should this enforce left|center|right|justify|char as in the spec */
289         _theadTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.ALIGN, align);
290     }
291
292     /**
293      * Sets the value of the horizontal alignment character attribute rendered by the HTML thead tag.
294      *
295      * @param alignChar the alignment character
296      * @jsptagref.attributedescription The horizontal alignment character rendered by the HTML thead tag.
297      * @jsptagref.attributesyntaxvalue <i>string_alignChar</i>
298      * @netui:attribute required="false" rtexprvalue="true" description="The horizontal alignment character rendered by the HTML thead tag."
299      */

300     public void setChar(String JavaDoc alignChar) {
301         _theadTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.CHAR, alignChar);
302     }
303
304     /**
305      * Sets the value of the horizontal alignment character offset attribute rendered by the HTML thead tag.
306      *
307      * @param alignCharOff the alignment character offset
308      * @jsptagref.attributedescription The horizontal alignment character offset rendered by the HTML thead tag.
309      * @jsptagref.attributesyntaxvalue <i>string_alignCharOff</i>
310      * @netui:attribute required="false" rtexprvalue="true" description="The horizontal alignment character offset rendered by the HTML thead tag."
311      */

312     public void setCharoff(String JavaDoc alignCharOff) {
313         _theadTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.CHAROFF, alignCharOff);
314     }
315
316     /**
317      * Sets the value of the vertical alignment attribute rendered by the HTML thead tag.
318      *
319      * @param valign the vertical alignment
320      * @jsptagref.attributedescription The vertical alignment rendered by the HTML thead tag
321      * @jsptagref.attributesyntaxvalue <i>string_align</i>
322      * @netui:attribute required="false" rtexprvalue="true" description="The vertical alignment rendered by the HTML thead tag.
323      */

324     public void setValign(String JavaDoc valign) {
325         /* todo: should this enforce top|middle|bottom|baseline as in the spec */
326         _theadTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.VALIGN, valign);
327     }
328
329     /**
330      * Sets the value of the language attribute rendered by the HTML thead tag
331      *
332      * @param lang the language
333      * @jsptagref.attributedescription The language attribute rendered by the HTML thead tag.
334      * @jsptagref.attributesyntaxvalue <i>string_lang</i>
335      * @netui:attribute required="false" rtexprvalue="true" description="The language attribute rendered by the HTML thead tag."
336      */

337     public void setLang(String JavaDoc lang) {
338         _theadTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.LANG, lang);
339     }
340
341     /**
342      * Sets the value of the text direction attribute rendered by the HTML thead tag.
343      *
344      * @param dir the dir attribute
345      * @jsptagref.attributedescription The text direction attribute rendered by the HTML thead tag.
346      * @jsptagref.attributesyntaxvalue <i>string_dir</i>
347      * @netui:attribute required="false" rtexprvalue="true" description="The text direction rendered by the HTML thead tag."
348      */

349     public void setDir(String JavaDoc dir) {
350         _theadTag.registerAttribute(AbstractHtmlState.ATTR_GENERAL, HtmlConstants.DIR, dir);
351     }
352
353     /**
354      * Set a boolean that enables / disables rendering HTML table rows by this tag. If the
355      * value is enabled, an HTML &lt;tr&gt; will be rendered when this tag renders its body. If
356      * the value is disabled, no &lt;tr&gt; tags will be rendered and the page author is responsible
357      * for maintaining the integrity of the HTML table.
358      * @jsptagref.attributedescription
359      * Set a boolean that enables / disables rendering HTML table rows by this tag. If the
360      * value is enabled, an HTML &lt;tr&gt; will be rendered when this tag renders its body. If
361      * the value is disabled, no &lt;tr&gt; tags will be rendered and the page author is responsible
362      * for maintaining the integrity of the HTML table.
363      * @jsptagref.attributesyntaxvalue <i>boolean_renderRow</i>
364      * @netui:attribute required="false" rtexprvalue="true"
365      * description="Boolean to enable / disable rendering HTML table row tags"
366      */

367     public void setRenderRow(boolean renderRow) {
368         _renderRow = renderRow;
369     }
370
371     /**
372      * Set the name of the tagId for the HTML thead tag.
373      *
374      * @param tagId the the name of the tagId for the thead tag.
375      * @jsptagref.attributedescription The tagId.
376      * @jsptagref.attributesyntaxvalue <i>string_tagId</i>
377      * @netui:attribute required="false" rtexprvalue="true"
378      * description="String value. Sets the id (or name) attribute of the rendered HTML tag. "
379      */

380     public void setTagId(String JavaDoc tagId)
381         throws JspException JavaDoc {
382         applyTagId(_theadTag, tagId);
383     }
384
385     /**
386      * Render this tag. This method renders during the data grid's {@link DataGridTagModel#RENDER_STATE_HEADER}
387      * state in order to add table rows to the beginning of a data grid's HTML table. If the data grid is rendering
388      * HTML row groups, this tag will output an HTML &lt;thead&gt; tag. Then, if this tag is rendering
389      * a table row, it will produce an HTML &lt;tr&gt; tag. Then the content of the body will be rendered. If
390      * table row rendering is disabled, the page author is responsible for rendering the appropriate HTML
391      * table row tags as this tag renders inside of the HTML table opened by the data grid.
392      * @throws IOException
393      * @throws JspException when the {@link DataGridTagModel} can not be found in the {@link JspContext}
394      */

395     public void doTag()
396             throws JspException JavaDoc, IOException JavaDoc {
397
398         JspContext JavaDoc jspContext = getJspContext();
399         DataGridTagModel dgm = DataGridUtil.getDataGridTagModel(jspContext);
400         if(dgm == null)
401             throw new JspException JavaDoc(Bundle.getErrorString("DataGridTags_MissingDataGridModel"));
402
403         int gridRenderState = dgm.getRenderState();
404         if(gridRenderState == DataGridTagModel.RENDER_STATE_HEADER) {
405
406             InternalStringBuilder content = new InternalStringBuilder();
407             AbstractRenderAppender appender = new StringBuilderRenderAppender(content);
408
409             StyleModel styleModel = dgm.getStyleModel();
410             assert styleModel != null;
411
412             TableRenderer tableRenderer = dgm.getTableRenderer();
413             assert tableRenderer != null;
414
415             if(dgm.isRenderRowGroups()) {
416                 _theadTag.styleClass = (_theadTag.styleClass != null ? _theadTag.styleClass : styleModel.getTableHeadClass());
417                 tableRenderer.openTableHead(_theadTag, appender);
418             }
419
420             TrTag.State trState = null;
421             if(_renderRow) {
422                 trState = new TrTag.State();
423                 trState.styleClass = styleModel.getHeaderRowClass();
424                 tableRenderer.openHeaderRow(trState, appender);
425             }
426
427             JspFragment JavaDoc fragment = getJspBody();
428             if(fragment != null) {
429                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
430                 fragment.invoke(sw);
431                 appender.append(sw.toString());
432             }
433
434             if(_renderRow)
435                 tableRenderer.closeHeaderRow(appender);
436
437             if(dgm.isRenderRowGroups()) {
438                 tableRenderer.closeTableHead(appender);
439                 String JavaDoc tfootScript = null;
440                 if(_theadTag.id != null) {
441                     HttpServletRequest JavaDoc request = JspUtil.getRequest(getJspContext());
442                     tfootScript = renderNameAndId(request, _theadTag, null);
443                 }
444
445                 if(tfootScript != null)
446                     appender.append(tfootScript);
447             }
448
449             jspContext.getOut().write(content.toString());
450         }
451     }
452 }
453
Popular Tags