001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.databinding.datagrid;
020:
021: import java.io.IOException;
022: import java.io.StringWriter;
023: import javax.servlet.jsp.JspException;
024: import javax.servlet.jsp.JspContext;
025: import javax.servlet.jsp.tagext.JspFragment;
026: import javax.servlet.http.HttpServletRequest;
027:
028: import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
029: import org.apache.beehive.netui.tags.rendering.TrTag;
030: import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
031: import org.apache.beehive.netui.tags.rendering.StringBuilderRenderAppender;
032: import org.apache.beehive.netui.tags.html.HtmlConstants;
033: import org.apache.beehive.netui.databinding.datagrid.runtime.rendering.table.TableRenderer;
034: import org.apache.beehive.netui.databinding.datagrid.runtime.util.JspUtil;
035: import org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel;
036: import org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel;
037: import org.apache.beehive.netui.util.Bundle;
038: import org.apache.beehive.netui.util.internal.InternalStringBuilder;
039:
040: /**
041: * <p>
042: * This tag is optionally used to render HTML table roe tags inside of one of the data grid tags that
043: * are used to denote data grid row rendering boundaries. When the {@link Header}, {@link Rows}, or {@link Footer}
044: * have their <code>renderRow</code> attribute set to <code>false</code>, no HTML table row element will render
045: * before starting to render the body of one of these tags. This tag should be used when <code>renderRows</code>
046: * is <code>false</code> in order to render an HTML table row. The Row tag is used this way in order to
047: * allow a page author to set JSP tag attributes that can configure each rendered table row differently.
048: * For example:
049: * <pre>
050: * <netui-data:rows renderRows="false">
051: * <netui-data:row styleClass="rowStyle${container.index}">
052: * <netui-data:spanCell value="${container.item}"/>
053: * </netui-data:row>
054: * </netui-data:rows>
055: * </pre>
056: * and a data set containing ["foo", "bar", "baz"] will render:
057: * <pre>
058: * <tr class="rowStyle0">foo</tr>
059: * <tr class="rowStyle1">bar</tr>
060: * <tr class="rowStyle2">baz</tr>
061: * </pre>
062: * If the <netui-data:row$gt; were omitted, none of the <tr> elements would be rendered in the output. Note,
063: * this tag <b>should not</b> be used inside of the Header, Rows, or Footer tags unless their <code>renderRow</code>
064: * attribute is set to <code>false</code>
065: * </p>
066: * @jsptagref.tagdescription
067: * <p>
068: * This tag is optionally used to render HTML table roe tags inside of one of the data grid tags that
069: * are used to denote data grid row rendering boundaries. When the {@link Header}, {@link Rows}, or {@link Footer}
070: * have their <code>renderRow</code> attribute set to <code>false</code>, no HTML table row element will render
071: * before starting to render the body of one of these tags. This tag should be used when <code>renderRows</code>
072: * is <code>false</code> in order to render an HTML table row. The Row tag is used this way in order to
073: * allow a page author to set JSP tag attributes that can configure each rendered table row differently.
074: * For example:
075: * <pre>
076: * <netui-data:rows renderRows="false">
077: * <netui-data:row styleClass="rowStyle${container.index}">
078: * <netui-data:spanCell value="${container.item}"/>
079: * </netui-data:row>
080: * </netui-data:rows>
081: * </pre>
082: * and a data set containing ["foo", "bar", "baz"] will render:
083: * <pre>
084: * <tr class="rowStyle0">foo</tr>
085: * <tr class="rowStyle1">bar</tr>
086: * <tr class="rowStyle2">baz</tr>
087: * </pre>
088: * If the <netui-data:row$gt; were omitted, none of the <tr> elements would be rendered in the output. Note,
089: * this tag <b>should not</b> be used inside of the Header, Rows, or Footer tags unless their <code>renderRow</code>
090: * attribute is set to <code>false</code>
091: * </p>
092: * @netui:tag name="row" body-content="scriptless"
093: * description="Tag optionally used inside of a Header, Rows, or Footer tag to render HTML table row elements."
094: */
095: public class Row extends AbstractDataGridHtmlTag {
096:
097: private TrTag.State _trState = new TrTag.State();
098:
099: /**
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 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 onClick) {
116: _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT,
117: HtmlConstants.ONCLICK, onClick);
118: }
119:
120: /**
121: * Sets the onDblClick JavaScript for the HTML tr tag.
122: *
123: * @param onDblClick the onDblClick event.
124: * @jsptagref.attributedescription The onDblClick JavaScript for the HTML tr tag.
125: * @jsptagref.attributesyntaxvalue <i>string_onDblClick</i>
126: * @netui:attribute required="false" rtexprvalue="true" description="The onDblClick JavaScript for the HTML tr tag."
127: */
128: public void setOnDblClick(String onDblClick) {
129: _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT,
130: HtmlConstants.ONDBLCLICK, onDblClick);
131: }
132:
133: /**
134: * Sets the onKeyDown JavaScript for the HTML tr tag.
135: *
136: * @param onKeyDown the onKeyDown event.
137: * @jsptagref.attributedescription The onKeyDown JavaScript for the HTML tr tag.
138: * @jsptagref.attributesyntaxvalue <i>string_onKeyDown</i>
139: * @netui:attribute required="false" rtexprvalue="true" description="The onKeyDown JavaScript for the HTML tr tag."
140: */
141: public void setOnKeyDown(String onKeyDown) {
142: _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT,
143: HtmlConstants.ONKEYDOWN, onKeyDown);
144: }
145:
146: /**
147: * Sets the onKeyUp JavaScript for the HTML tr tag.
148: *
149: * @param onKeyUp the onKeyUp event.
150: * @jsptagref.attributedescription The onKeyUp JavaScript for the HTML tr tag.
151: * @jsptagref.attributesyntaxvalue <i>string_onKeyUp</i>
152: * @netui:attribute required="false" rtexprvalue="true" description="The onKeyUp JavaScript for the HTML tr tag."
153: */
154: public void setOnKeyUp(String onKeyUp) {
155: _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT,
156: HtmlConstants.ONKEYUP, onKeyUp);
157: }
158:
159: /**
160: * Sets the onKeyPress JavaScript for the HTML tr tag.
161: *
162: * @param onKeyPress the onKeyPress event.
163: * @jsptagref.attributedescription The onKeyPress JavaScript for the HTML tr tag.
164: * @jsptagref.attributesyntaxvalue <i>string_onKeyPress</i>
165: * @netui:attribute required="false" rtexprvalue="true" description="The onKeyPress JavaScript for the HTML tr tag."
166: */
167: public void setOnKeyPress(String onKeyPress) {
168: _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT,
169: HtmlConstants.ONKEYPRESS, onKeyPress);
170: }
171:
172: /**
173: * Sets the onMouseDown JavaScript for the HTML tr tag.
174: *
175: * @param onMouseDown the onMouseDown event.
176: * @jsptagref.attributedescription The onMouseDown JavaScript for the HTML tr tag.
177: * @jsptagref.attributesyntaxvalue <i>string_onMouseDown</i>
178: * @netui:attribute required="false" rtexprvalue="true" description="The onMouseDown JavaScript for the HTML tr tag."
179: */
180: public void setOnMouseDown(String onMouseDown) {
181: _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT,
182: HtmlConstants.ONMOUSEDOWN, onMouseDown);
183: }
184:
185: /**
186: * Sets the onMouseUp JavaScript for the HTML tr tag.
187: *
188: * @param onMouseUp the onMouseUp event.
189: * @jsptagref.attributedescription The onMouseUp JavaScript for the HTML tr tag.
190: * @jsptagref.attributesyntaxvalue <i>string_onMouseUp</i>
191: * @netui:attribute required="false" rtexprvalue="true" description="The onMouseUp JavaScript for the HTML tr tag."
192: */
193: public void setOnMouseUp(String onMouseUp) {
194: _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT,
195: HtmlConstants.ONMOUSEUP, onMouseUp);
196: }
197:
198: /**
199: * Sets the onMouseMove JavaScript for the HTML tr tag.
200: *
201: * @param onMouseMove the onMouseMove event.
202: * @jsptagref.attributedescription The onMouseMove JavaScript for the HTML tr tag.
203: * @jsptagref.attributesyntaxvalue <i>string_onMouseMove</i>
204: * @netui:attribute required="false" rtexprvalue="true" description="The onMouseMove JavaScript for the HTML tr tag."
205: */
206: public void setOnMouseMove(String onMouseMove) {
207: _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT,
208: HtmlConstants.ONMOUSEMOVE, onMouseMove);
209: }
210:
211: /**
212: * Sets the onMouseOut JavaScript for the HTML tr tag.
213: *
214: * @param onMouseOut the onMouseOut event.
215: * @jsptagref.attributedescription The onMouseOut JavaScript for the HTML tr tag.
216: * @jsptagref.attributesyntaxvalue <i>string_onMouseOut</i>
217: * @netui:attribute required="false" rtexprvalue="true" description="The onMouseOut JavaScript for the HTML tr tag."
218: */
219: public void setOnMouseOut(String onMouseOut) {
220: _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT,
221: HtmlConstants.ONMOUSEOUT, onMouseOut);
222: }
223:
224: /**
225: * Sets the onMouseOver JavaScript for the HTML tr tag.
226: *
227: * @param onMouseOver the onMouseOver event.
228: * @jsptagref.attributedescription The onMouseOver JavaScript for the HTML tr tag.
229: * @jsptagref.attributesyntaxvalue <i>string_onMouseOver</i>
230: * @netui:attribute required="false" rtexprvalue="true" description="The onMouseOver JavaScript for the HTML tr tag."
231: */
232: public void setOnMouseOver(String onMouseOver) {
233: _trState.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT,
234: HtmlConstants.ONMOUSEOVER, onMouseOver);
235: }
236:
237: /**
238: * Sets the style attribute for the HTML tr tag.
239: *
240: * @param style the style
241: * @jsptagref.attributedescription The style attribute for the HTML tr tag.
242: * @jsptagref.attributesyntaxvalue <i>string_style</i>
243: * @netui:attribute required="false" rtexprvalue="true"
244: * description="The style attribute for the HTML tr tag."
245: */
246: public void setStyle(String style) {
247: if ("".equals(style))
248: return;
249:
250: _trState.style = style;
251: }
252:
253: /**
254: * Sets the style class for the HTML tr tag.
255: *
256: * @param styleClass the style class.
257: * @jsptagref.attributedescription The style class for the HTML tr tag.
258: * @jsptagref.attributesyntaxvalue <i>string_style_class</i>
259: * @netui:attribute required="false" rtexprvalue="true" description="The style class for the HTML tr tag."
260: */
261: public void setStyleClass(String styleClass) {
262: if ("".equals(styleClass))
263: return;
264:
265: _trState.styleClass = styleClass;
266: }
267:
268: /**
269: * Sets the title attribute for the HTML tr tag.
270: *
271: * @param title the title
272: * @jsptagref.attributedescription The title for the HTML tr tag.
273: * @jsptagref.attributesyntaxvalue <i>string_title</i>
274: * @netui:attribute required="false" rtexprvalue="true" description="The title for the HTML tr tag."
275: */
276: public void setTitle(String title) {
277: _trState.registerAttribute(AbstractHtmlState.ATTR_GENERAL,
278: HtmlConstants.TITLE, title);
279: }
280:
281: /**
282: * Sets the lang attribute for the HTML tr tag.
283: * @param lang the lang
284: * @jsptagref.attributedescription The lang for the HTML tr tag.
285: * @jsptagref.attributesyntaxvalue <i>string_lang</i>
286: * @netui:attribute required="false" rtexprvalue="true"
287: * description="The lang for the HTML tr tag."
288: */
289: public void setLang(String lang) {
290: _trState.registerAttribute(AbstractHtmlState.ATTR_GENERAL,
291: HtmlConstants.LANG, lang);
292: }
293:
294: /**
295: * Sets the lang attribute for the HTML element tr tag.
296: * @param dir the dir
297: * @jsptagref.attributedescription The dir for the HTML tr tag.
298: * @jsptagref.attributesyntaxvalue <i>string_dir</i>
299: * @netui:attribute required="false" rtexprvalue="true"
300: * description="The dir for the HTML tr tag.
301: */
302: public void setDir(String dir) {
303: _trState.registerAttribute(AbstractHtmlState.ATTR_GENERAL,
304: HtmlConstants.DIR, dir);
305: }
306:
307: /**
308: * Set the name of the tagId for the HTML tr tag.
309: *
310: * @param tagId the the name of the tagId for the table row.
311: * @jsptagref.attributedescription The tagId for the HTML tr tat.
312: * @jsptagref.attributesyntaxvalue <i>string_tagId</i>
313: * @netui:attribute required="false" rtexprvalue="true"
314: * description="String value. Sets the id (or name) attribute of the HTML tr tag."
315: */
316: public void setTagId(String tagId) throws JspException {
317: JspContext jspContext = getJspContext();
318: DataGridTagModel dataGridModel = DataGridUtil
319: .getDataGridTagModel(jspContext);
320: if (dataGridModel == null)
321: throw new JspException(
322: Bundle
323: .getErrorString("DataGridTags_MissingDataGridModel"));
324:
325: int renderState = dataGridModel.getRenderState();
326:
327: if (renderState == DataGridTagModel.RENDER_STATE_GRID)
328: applyIndexedTagId(_trState, tagId);
329: else
330: applyTagId(_trState, tagId);
331: }
332:
333: /**
334: * <p>
335: * Render this tag. This tag renders during the data grid's {@link DataGridTagModel#RENDER_STATE_HEADER},
336: * {@link DataGridTagModel#RENDER_STATE_GRID}, and the {@link DataGridTagModel#RENDER_STATE_FOOTER} render
337: * states. This tag will always render the an HTML table row tag and its body. The result is added
338: * to the output stream.
339: * </p>
340: * <p>
341: * Unless the {@link #setStyleClass(String)} attribute has been set and is non-null, the following style
342: * classes are used during the various supported rendering states:
343: * <table>
344: * <tr><td>Render State</td><td>Style Class</td></tr>
345: * <tr><td>{@link DataGridTagModel#RENDER_STATE_HEADER}</td>
346: * <td>{@link org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel#getHeaderRowClass()}</td></tr>
347: * <tr><td>{@link DataGridTagModel#RENDER_STATE_GRID}</td>
348: * <td>{@link org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel#getRowClass()} for
349: * an even row {@link org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel#getAltRowClass()}
350: * for odd rows.</td></tr>
351: * <tr><td>{@link DataGridTagModel#RENDER_STATE_FOOTER}</td>
352: * <td>{@link org.apache.beehive.netui.databinding.datagrid.api.rendering.StyleModel#getFooterRowClass()}</td></tr>
353: * </table>
354: * </p>
355: * @throws JspException when the {@link DataGridTagModel} can not be found in the {@link JspContext}
356: * @throws IOException
357: */
358: public void doTag() throws JspException, IOException {
359:
360: JspContext jspContext = getJspContext();
361: DataGridTagModel dataGridModel = DataGridUtil
362: .getDataGridTagModel(jspContext);
363: if (dataGridModel == null)
364: throw new JspException(
365: Bundle
366: .getErrorString("DataGridTags_MissingDataGridModel"));
367:
368: int renderState = dataGridModel.getRenderState();
369:
370: if (!(renderState == DataGridTagModel.RENDER_STATE_HEADER
371: || renderState != DataGridTagModel.RENDER_STATE_GRID || renderState != DataGridTagModel.RENDER_STATE_FOOTER))
372: return;
373:
374: JspFragment fragment = getJspBody();
375:
376: StyleModel styleModel = dataGridModel.getStyleModel();
377: assert styleModel != null;
378:
379: TableRenderer tableRenderer = dataGridModel.getTableRenderer();
380: assert tableRenderer != null;
381:
382: HttpServletRequest request = JspUtil
383: .getRequest(getJspContext());
384: InternalStringBuilder content = new InternalStringBuilder();
385: AbstractRenderAppender appender = new StringBuilderRenderAppender(
386: content);
387:
388: if (_trState.styleClass == null) {
389: if (renderState == DataGridTagModel.RENDER_STATE_GRID) {
390: int index = dataGridModel.getCurrentIndex();
391: if (index % 2 == 0)
392: _trState.styleClass = styleModel.getRowClass();
393: else
394: _trState.styleClass = styleModel.getAltRowClass();
395: } else if (renderState == DataGridTagModel.RENDER_STATE_HEADER)
396: _trState.styleClass = styleModel.getHeaderRowClass();
397: else if (renderState == DataGridTagModel.RENDER_STATE_FOOTER)
398: _trState.styleClass = styleModel.getFooterRowClass();
399: else
400: assert false : "Attempting to apply style information during an invalid render state";
401: }
402:
403: String trScript = null;
404: if (_trState.id != null)
405: trScript = renderNameAndId(request, _trState, null);
406:
407: tableRenderer.openTableRow(_trState, appender);
408:
409: StringWriter sw = new StringWriter();
410: if (fragment != null)
411: fragment.invoke(sw);
412: appender.append(sw.toString());
413:
414: tableRenderer.closeTableRow(appender);
415:
416: if (trScript != null)
417: appender.append(trScript);
418:
419: jspContext.getOut().write(content.toString());
420:
421: return;
422: }
423: }
|