@Override
public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException {
UISheet sheet = (UISheet) uiComponent;
Style style = new Style(facesContext, sheet);
final String sheetId = sheet.getClientId(facesContext);
TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
// Outer sheet div
writer.startElement(HtmlElements.DIV, sheet);
writer.writeIdAttribute(sheetId);
final String css = Classes.create(sheet).getStringValue() + " " + Classes.create(sheet, "outer").getStringValue();
// todo: please remove the "outer" sub in the future,
// todo: it was renamed from tobago-sheet-outer to tobago-sheet because of naming conventions.
writer.writeClassAttribute(css);
writer.writeStyleAttribute(style);
UICommand clickAction = null;
UICommand dblClickAction = null;
int columnSelectorIndex = -1;
int i = 0;
for (UIComponent child : (List<UIComponent>) sheet.getChildren()) {
if (child instanceof UIColumnEvent) {
UIColumnEvent columnEvent = (UIColumnEvent) child;
if (columnEvent.isRendered()) {
UIComponent selectionChild = (UIComponent) child.getChildren().get(0);
if (selectionChild != null && selectionChild instanceof UICommand && selectionChild.isRendered()) {
UICommand action = (UICommand) selectionChild;
if ("click".equals(columnEvent.getEvent())) {
clickAction = action;
}
if ("dblclick".equals(columnEvent.getEvent())) {
dblClickAction = action;
}
}
}
} else if (child instanceof UIColumnSelector) {
columnSelectorIndex = i;
}
i++;
}
renderSheet(facesContext, sheet, (clickAction != null || dblClickAction != null), style);
writer.endElement(HtmlElements.DIV);
// TODO check ajax id
if (!(FacesContextUtils.isAjax(facesContext)
&& sheetId.equals(FacesContextUtils.getAjaxComponentId(facesContext)))) {
Integer frequency = null;
UIComponent facetReload = sheet.getFacet(Facets.RELOAD);
if (facetReload != null && facetReload instanceof UIReload && facetReload.isRendered()) {
UIReload update = (UIReload) facetReload;
frequency = update.getFrequency();
}
final String[] cmds = {
"new Tobago.Sheet('" + sheetId
+ "', " + sheet.getFirst()
+ ", '" + sheet.getSelectable()
+ "', " + columnSelectorIndex
+ ", " + frequency
+ ", " + (clickAction != null ? HtmlRendererUtils.getJavascriptString(clickAction.getId()) : null)
+ ", " + HtmlRendererUtils.getRenderedPartiallyJavascriptArray(facesContext, clickAction)
+ ", " + (dblClickAction != null ? HtmlRendererUtils.getJavascriptString(dblClickAction.getId()) : null)