/*
* xulfaces : bring XUL power to Java
*
* Copyright (C) 2005 Olivier SCHMITT
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.xulfaces.renderer.grid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import org.xulfaces.annotation.faces.RENDERER;
import org.xulfaces.component.grid.ColumnComponent;
import org.xulfaces.component.grid.GridComponent;
import org.xulfaces.renderer.XULRenderer;
/**
*
* @author kito31
* @version $Id: GridRenderer.java,v 1.7 2007/04/09 13:23:43 kito31 Exp $
*/
@RENDERER(kit = "XUL_RENDERKIT", family = "xul.component.family", type = "xul.renderer.Grid")
public class GridRenderer extends XULRenderer {
@Override
public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
super.encodeBegin(facesContext, component);
ResponseWriter responseWriter = facesContext.getResponseWriter();
responseWriter.startElement("grid", component);
responseWriter.writeAttribute("id", component.getClientId(facesContext), "id");
renderAttributes(facesContext, component, (List) component
.getAttributes().get("annotatedAttributes"));
}
@Override
public void encodeEnd(FacesContext facesContext, UIComponent arg1) throws IOException {
ResponseWriter responseWriter = facesContext.getResponseWriter();
responseWriter.endElement("grid");
}
public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException {
GridComponent gridComponent = (GridComponent) component;
ResponseWriter responseWriter = facesContext.getResponseWriter();
responseWriter.startElement("columns", component);
List<UIComponent> children = gridComponent.getChildren();
for (UIComponent kid : children) {
renderChild(facesContext, kid);
}
responseWriter.endElement("columns");
Object value = gridComponent.getValue();
if (value != null) {
responseWriter.startElement("rows", component);
String rowsId = gridComponent.getClientId(facesContext)+"_rows";
responseWriter.writeAttribute("id",rowsId,"id");
if (value instanceof Collection) {
int first = gridComponent.getFirst();
int rows = gridComponent.getRows();
int rowCount = gridComponent.getRowCount();
if (rows <= 0) {
rows = rowCount - first;
}
int last = first + rows;
if (last > rowCount) last = rowCount;
for (int i = first; i < last; i++) {
gridComponent.setRowIndex(i);
if (!gridComponent.isRowAvailable()) {
return;
}
Object object = gridComponent.getRowData();
responseWriter.startElement("row", component);
encodeRow(facesContext, gridComponent, object);
responseWriter.endElement("row");
}
}
responseWriter.endElement("rows");
}
}
protected void encodeRow(FacesContext facesContext, GridComponent gridComponent, Object value)
throws IOException {
List<UIComponent> children = gridComponent.getChildren();
for (UIComponent kid : children) {
if(kid instanceof ColumnComponent) {
List<UIComponent> colChildren = kid.getChildren();
for (UIComponent colKid : colChildren) {
renderChild(facesContext, colKid);
}
}
else {
renderChild(facesContext, kid);
}
}
}
public boolean getRendersChildren() {
return true;
}
}