001: package org.sakaiproject.tool.section.jsf;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005: import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
006: import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
007: import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
008: import org.apache.myfaces.renderkit.html.ext.HtmlTableRenderer;
009:
010: import javax.faces.component.UIComponent;
011: import javax.faces.component.UIData;
012: import javax.faces.context.FacesContext;
013: import javax.faces.context.ResponseWriter;
014: import java.util.List;
015: import java.io.IOException;
016:
017: public class RowGroupDataTableRenderer extends HtmlTableRenderer {
018: private static final Log log = LogFactory
019: .getLog(RowGroupDataTableRenderer.class);
020:
021: public static final String SECTION_STYLE_CLASS = "groupRow";
022: public static final String CATEGORY_HEADER_STYLE_CLASS = "categoryHeader";
023: public static final String FIRST_CATEGORY_HEADER_STYLE_CLASS = "firstCategoryHeader";
024:
025: public void encodeInnerHtml(FacesContext facesContext,
026: UIComponent component) throws IOException {
027:
028: UIData uiData = (UIData) component;
029: ResponseWriter writer = facesContext.getResponseWriter();
030:
031: Styles styles = getStyles(uiData);
032:
033: int first = uiData.getFirst();
034: int rows = uiData.getRows();
035: int rowCount = uiData.getRowCount();
036: if (rows <= 0) {
037: rows = rowCount - first;
038: }
039: int last = first + rows;
040: if (last > rowCount)
041: last = rowCount;
042:
043: for (int i = first; i < last; i++) {
044: uiData.setRowIndex(i);
045: if (!uiData.isRowAvailable()) {
046: log.warn("Row is not available. Rowindex = " + i);
047: return;
048: }
049:
050: int columns = component.getChildCount();
051: renderCategoryRow(i, columns, uiData, writer, i == first);
052:
053: beforeRow(facesContext, uiData);
054: HtmlRendererUtils.writePrettyLineSeparator(facesContext);
055: renderRowStart(facesContext, writer, uiData, styles, i);
056:
057: List children = component.getChildren();
058: for (int j = 0, size = component.getChildCount(); j < size; j++) {
059: UIComponent child = (UIComponent) children.get(j);
060: if (child.isRendered()) {
061: encodeColumnChild(facesContext, writer, uiData,
062: child, styles, j);
063: }
064: }
065: renderRowEnd(facesContext, writer, uiData);
066:
067: afterRow(facesContext, uiData);
068: }
069: }
070:
071: private void renderCategoryRow(int rowNumber, int columns,
072: UIData uiData, ResponseWriter writer, boolean firstCategory)
073: throws IOException {
074: FacesContext facesContext = FacesContext.getCurrentInstance();
075:
076: // Cast the uiData into our custom component
077: RowGroupDataTable rowGroupDataTable;
078: try {
079: rowGroupDataTable = (RowGroupDataTable) uiData;
080: } catch (ClassCastException cce) {
081: log.warn(cce);
082: return;
083: }
084:
085: // Get the current section
086: RowGroupable rowGroupable;
087: List list = (List) uiData.getValue();
088: try {
089: rowGroupable = (RowGroupable) list.get(rowNumber);
090: } catch (IndexOutOfBoundsException ioobe) {
091: log.warn(ioobe);
092: return;
093: }
094:
095: // For Daisy's CM Home tool
096: if (rowNumber == 0) {
097: // reset rowGroupDataTable.category
098: rowGroupDataTable.category = null;
099: }
100:
101: // Is this section different from the previous RowGroup?
102: if (!rowGroupable.getRowGroupId().equals(
103: rowGroupDataTable.category)) {
104: // Update the SectionTable's current RowGroup
105: rowGroupDataTable.category = rowGroupable.getRowGroupId();
106:
107: // Render a table row for the RowGroup header
108: beforeRow(facesContext, uiData);
109: HtmlRendererUtils.writePrettyLineSeparator(facesContext);
110:
111: writer.startElement(HTML.TR_ELEM, uiData);
112: if (firstCategory) {
113: writer.writeAttribute(HTML.CLASS_ATTR,
114: FIRST_CATEGORY_HEADER_STYLE_CLASS, null);
115: } else {
116: writer.writeAttribute(HTML.CLASS_ATTR,
117: CATEGORY_HEADER_STYLE_CLASS, null);
118: }
119:
120: Object rowId = uiData.getAttributes().get(JSFAttr.ROW_ID);
121:
122: if (rowId != null) {
123: writer.writeAttribute(HTML.ID_ATTR, rowId.toString(),
124: null);
125: }
126:
127: // Render a single colspanned cell displaying the current RowGroup
128: writer.startElement(HTML.TD_ELEM, uiData);
129: writer.writeAttribute(HTML.COLSPAN_ATTR, columns, null);
130: writer.write(JsfUtil.getLocalizedMessage(
131: "section_table_category_header",
132: new String[] { rowGroupable.getRowGroupTitle() }));
133: writer.endElement(HTML.TD_ELEM);
134:
135: renderRowEnd(facesContext, writer, uiData);
136: afterRow(facesContext, uiData);
137: }
138: }
139:
140: }
|