Author: abelevich
Date: 2007-05-17 14:46:13 -0400 (Thu, 17 May 2007)
New Revision: 795
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer2.java
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ExtendedColumnVisitor.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/GridRendererState.java
Log:
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ExtendedColumnVisitor.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ExtendedColumnVisitor.java 2007-05-17
18:45:56 UTC (rev 794)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ExtendedColumnVisitor.java 2007-05-17
18:46:13 UTC (rev 795)
@@ -14,14 +14,26 @@
* @author Anton Belevich
*
*/
-public interface ExtendedColumnVisitor extends ColumnVisitor {
+public abstract class ExtendedColumnVisitor implements ColumnVisitor {
/* (non-Javadoc)
* @see
org.richfaces.renderkit.html.ColumnVisitor#visit(javax.faces.context.FacesContext,
org.richfaces.component.UIScrollableGridColumn, javax.faces.context.ResponseWriter,
org.richfaces.renderkit.html.GridRendererState)
*/
public int visit(FacesContext context, UIScrollableGridColumn column,
- ResponseWriter writer, GridRendererState state) throws IOException;
+ ResponseWriter writer, GridRendererState state) throws IOException {
+
+ if(state.isFrozenColumn()){
+ int i = state.getFrozenColumnCount();
+ state.setFrozenColumnCount(i-1);
+ if(state.isFrozenPart()){
+ renderContent(context, column, writer,state);
+ }
+ }else if(!state.isFrozenPart() && !state.isFrozenColumn()){
+ renderContent(context, column, writer,state);
+ }
+
+ return 1;
+ };
- public void renderContent(FacesContext context, UIScrollableGridColumn column,
- ResponseWriter writer, GridRendererState state) throws IOException;
+ abstract public void renderContent(FacesContext context, UIScrollableGridColumn
column,ResponseWriter writer, GridRendererState state) throws IOException;
}
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/GridRendererState.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/GridRendererState.java 2007-05-17
18:45:56 UTC (rev 794)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/GridRendererState.java 2007-05-17
18:46:13 UTC (rev 795)
@@ -36,6 +36,8 @@
private String _cell_id_prefix;
+ private int sumWidth = 0;
+
private boolean header = false;
private String _column_type;
@@ -44,6 +46,8 @@
private AjaxContext ajaxContext;
+ private String part;
+
private boolean _frozenPart = false;
private ResponseWriter writer;
@@ -331,5 +335,20 @@
public void setHeader(boolean header) {
this.header = header;
}
-
+
+ public String getPart() {
+ return part;
+ }
+
+ public void setPart(String part) {
+ this.part = part;
+ }
+
+ public int getSumWidth() {
+ return sumWidth;
+ }
+
+ public void setSumWidth(int sumWidth) {
+ this.sumWidth = sumWidth;
+ }
}
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer2.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer2.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer2.java 2007-05-17
18:46:13 UTC (rev 795)
@@ -0,0 +1,409 @@
+package org.richfaces.renderkit.html;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+
+import org.ajax4jsf.ajax.repeat.DataVisitor;
+import org.ajax4jsf.framework.ajax.AjaxContext;
+import org.ajax4jsf.framework.ajax.AjaxEvent;
+import org.ajax4jsf.framework.renderer.AjaxRendererUtils;
+import org.ajax4jsf.framework.renderer.ComponentVariables;
+import org.ajax4jsf.framework.renderer.ComponentsVariableResolver;
+import org.ajax4jsf.framework.renderer.RendererBase;
+import org.ajax4jsf.framework.renderer.RendererUtils.HTML;
+import org.ajax4jsf.framework.util.javascript.JSFunction;
+import org.richfaces.component.UIScrollableGrid;
+import org.richfaces.component.UIScrollableGridColumn;
+import org.richfaces.renderkit.CompositeRenderer;
+import org.richfaces.utils.TemplateLoader;
+
+
+
+/**
+ * @author Anton Belevich
+ *
+ */
+
+public abstract class ScrollableGridBaseRenderer2 extends CompositeRenderer {
+
+ private final String FOOTER_PART = "footer";
+
+ private final String HEADER_PART = "header";
+
+ private final String CELL_ID_PREFFIX ="bcc_";
+
+ private final String COLUMN_FROZEN_TYPE = "frozen";
+
+ private final String COLUMN_NORMAL_TYPE = "normal";
+
+ private RendererBase cellTemplate = null;
+
+ private RendererBase headerCellTemplate = null;
+
+ private RendererBase footerCellTemplate = null;
+
+ private final ColumnVisitor columnsWidthCounter = new ColumnVisitor(){
+
+ public int visit(FacesContext context, UIScrollableGridColumn column, ResponseWriter
writer, GridRendererState state) throws IOException {
+
+ int prevWidth = 0;
+ int width = 0;
+
+
+ String widthPx = (String)column.getAttributes().get("width");
+ prevWidth = state.getSumWidth();
+
+ if(widthPx.endsWith("px")){
+ width = prevWidth + Integer.parseInt(widthPx.substring(0,
widthPx.indexOf("px")));
+ }
+
+ state.setSumWidth(width);
+ return 1;
+ }
+ };
+
+ private final ColumnVisitor headerCellRenderer = new ExtendedColumnVisitor(){
+
+ public void renderContent(FacesContext context, UIScrollableGridColumn column,
ResponseWriter writer, GridRendererState state) throws IOException {
+
+ int index = state.getCellIndex();
+ Integer cell_index = new Integer(index);
+ String client_id = state.getClientId();
+ String cell_id = state.getColumnType()+ ":" + state.getRowIndex()+
":" + index;
+ headerCellTemplate = getHeaderCellTemplate();
+ ComponentVariables variables =
ComponentsVariableResolver.getVariables(headerCellTemplate, column);
+ variables.setVariable("cell_id",cell_id);
+ variables.setVariable("client_id", client_id);
+ variables.setVariable("cell_index", cell_index);
+
+ headerCellTemplate.encodeBegin(context, column);
+ UIComponent component = column.getFacet(HEADER_PART);
+ if(component != null){
+ renderChild(context, component);
+ }
+ headerCellTemplate.encodeEnd(context, column);
+
+ }
+ };
+
+ private final ColumnVisitor footerCellRenderer = new ExtendedColumnVisitor(){
+
+ public void renderContent(FacesContext context, UIScrollableGridColumn column,
ResponseWriter writer, GridRendererState state) throws IOException {
+
+ String cell_id = state.getColumnType()+ ":" + state.getRowIndex()+
":" + state.getCellIndex();
+ String client_id = state.getClientId();
+
+ footerCellTemplate = getFooterCellTemplate();
+ ComponentVariables variables =
ComponentsVariableResolver.getVariables(footerCellTemplate, column);
+ variables.setVariable("cell_id",cell_id);
+ variables.setVariable("client_id", client_id);
+ footerCellTemplate.encodeBegin(context, column);
+ UIComponent component = column.getFacet(FOOTER_PART);
+ if(component != null){
+ renderChild(context, component);
+ }
+ footerCellTemplate.encodeEnd(context, column);
+
+ }
+ };
+
+ private final ColumnVisitor cellRenderer = new ExtendedColumnVisitor(){
+
+ public void renderContent(FacesContext context, UIScrollableGridColumn column,
ResponseWriter writer, GridRendererState state) throws IOException {
+
+ String cell_id = state.getColumnType()+ ":" + state.getRowIndex()+
":" + state.getCellIndex();
+ String client_id = state.getClientId();
+
+ cellTemplate = getCellTemplate();
+ ComponentVariables variables = ComponentsVariableResolver.getVariables(cellTemplate,
column);
+ variables.setVariable("cell_id",cell_id);
+ variables.setVariable("client_id", client_id);
+ cellTemplate.encodeBegin(context, column);
+ renderChildren(context, column);
+ cellTemplate.encodeEnd(context, column);
+ }
+
+ };
+
+ private final DataVisitor rowsRenderer = new DataVisitor(){
+
+ public void process(FacesContext context, Object rowIndex, Object argument) throws
IOException {
+
+ GridRendererState state = (GridRendererState)argument;
+ UIScrollableGrid grid = state.getGrid();
+
+ grid.setRowKey(rowIndex);
+
+ if(grid.isRowAvailable()){
+
+ int index = ((Integer)rowIndex).intValue();
+ state.setRowIndex(index);
+ String row_id = state.getClientId() + ":" + "row_" +
state.getColumnType() + ":" + index;
+ ResponseWriter writer = context.getResponseWriter();
+ writer.startElement(HTML.TR_ELEMENT, grid);
+ state.setFrozenColumnCount(((Integer)grid.getAttributes().get("frozenColCount")).intValue());
+ getUtils().writeAttribute(writer, "id",row_id);
+ getUtils().writeAttribute(writer, "class","ClientUI_Grid_BR");
+ ColumnWalker.iterateOverColumns(context, grid, cellRenderer, writer, state);
+ writer.endElement(HTML.TR_ELEMENT);
+
+ }
+ }
+ };
+
+ private final ColumnVisitor ajaxCellRenderer = new ExtendedColumnVisitor(){
+
+ public int visit(FacesContext context, UIScrollableGridColumn column, ResponseWriter
writer, GridRendererState state) throws IOException {
+
+ if(state.isFrozenColumn()){
+ int i = state.getFrozenColumnCount();
+ state.setFrozenColumnCount(i-1);
+ state.setCellIdPrefix(CELL_ID_PREFFIX + COLUMN_FROZEN_TYPE);
+ renderContent(context, column, writer,state);
+ }else{
+ state.setCellIdPrefix(CELL_ID_PREFFIX + COLUMN_NORMAL_TYPE);
+ renderContent(context, column, writer,state);
+ }
+ return 1;
+ }
+
+ public void renderContent(FacesContext context, UIScrollableGridColumn column,
ResponseWriter writer, GridRendererState state) throws IOException {
+ writer.startElement(HTML.SPAN_ELEM, column);
+ String columnWidth = (String)column.getAttributes().get("width");
+
+ String cell_id = state.getClientId() + ":" + state.getCellIdPrefix() +
":" + state.getRowIndex()+":" + state.getCellIndex();
+ getUtils().writeAttribute(writer, "id",cell_id);
+ getUtils().writeAttribute(writer, "class",
"ClientUI_Grid_BCBody");
+ getUtils().writeAttribute(writer, "style", "width:" +
columnWidth);
+ renderChildren(context, column);
+ writer.endElement(HTML.SPAN_ELEM);
+ AjaxContext ajaxContext = state.getAjaxContext();
+ ajaxContext.addRenderedArea(cell_id);
+ }
+
+ };
+
+ private final DataVisitor ajaxRowsRenderer = new DataVisitor(){
+
+ public void process(FacesContext context, Object rowKey, Object argument) throws
IOException {
+
+ GridRendererState state = (GridRendererState)argument;
+ UIScrollableGrid grid = state.getGrid();
+ grid.setRowKey(rowKey);
+ ResponseWriter writer = context.getResponseWriter();
+ state.setFrozenColumnCount(((Integer)grid.getAttributes().get("frozenColCount")).intValue());
+ ColumnWalker.iterateOverColumns(context, grid, ajaxCellRenderer, writer, state);
+ state.nextRow();
+ }
+ };
+
+ protected String getJavaScriptVarName(FacesContext context, UIScrollableGrid grid) {
+ String id = grid.getBaseClientId(context);
+ return "Richfaces_ScrollableGrid_" + id.replaceAll("[^A-Za-z0-9_]",
"_");
+ }
+
+ protected String getScriptContributions(FacesContext context, UIScrollableGrid grid) {
+ return super.getScriptContributions(getJavaScriptVarName(context, grid), context,
grid);
+ }
+
+ public boolean getRendersChildren() {
+ return true;
+ }
+
+ public void renderGridBody(FacesContext context, UIScrollableGrid grid, boolean
isFrozen) throws IOException{
+
+ final GridRendererState state = GridRendererState.getRendererState(context);
+
+ if(isFrozen){
+ state.setColumType(COLUMN_FROZEN_TYPE);
+ }else{
+ state.setColumType(COLUMN_NORMAL_TYPE);
+ }
+
+ state.setFrozenColumnCount(((Integer)grid.getAttributes().get("frozenColCount")).intValue());
+ state.setFrozenPart(isFrozen);
+ state.setClientId(grid.getClientId(context));
+ grid.walk(context, rowsRenderer, state);
+ grid.setRowKey(null);
+ }
+
+ public void renderHeaders(FacesContext context, UIScrollableGrid grid, boolean
isFrozen)throws IOException{
+
+ ResponseWriter writer = context.getResponseWriter();
+ final GridRendererState state = GridRendererState.getRendererState(context);
+
+ if(isFrozen){
+ state.setColumType(COLUMN_FROZEN_TYPE);
+ }else{
+ state.setColumType(COLUMN_NORMAL_TYPE);
+ }
+
+ state.setClientId(grid.getClientId(context));
+ state.setPart(HEADER_PART);
+ state.setFrozenColumnCount(((Integer)grid.getAttributes().get("frozenColCount")).intValue());
+ state.setFrozenPart(isFrozen);
+ state.setClientId(grid.getClientId(context));
+ ColumnWalker.iterateOverColumns(context, grid, headerCellRenderer, writer, state);
+ }
+
+ public void renderFooters(FacesContext context, UIScrollableGrid grid, boolean isFrozen)
throws IOException{
+
+ ResponseWriter writer = context.getResponseWriter();
+ final GridRendererState state = GridRendererState.getRendererState(context);
+
+ if(isFrozen){
+ state.setColumType(COLUMN_FROZEN_TYPE);
+ }else{
+ state.setColumType(COLUMN_NORMAL_TYPE);
+ }
+
+ state.setClientId(grid.getClientId(context));
+ state.setFrozenColumnCount(((Integer)grid.getAttributes().get("frozenColCount")).intValue());
+ state.setFrozenPart(isFrozen);
+ int colsCount = ColumnWalker.iterateOverColumns(context, grid, footerCellRenderer,
writer, state);
+ int rowsCount = grid.getRowCount();
+
+ ComponentVariables variables = ComponentsVariableResolver.getVariables(this, grid);
+ variables.setVariable("rows_count", new Integer(rowsCount));
+ variables.setVariable("columns_count", new Integer(colsCount));
+
+ }
+
+ public void setUpState(FacesContext context, UIScrollableGrid grid) {
+ GridRendererState.createState(context, grid);
+ }
+
+ public void tearDownState(FacesContext context){
+ GridRendererState.restoreState(context);
+ }
+
+ public String getRowsAjaxUpdate(FacesContext context, UIScrollableGrid grid){
+
+ JSFunction function = AjaxRendererUtils.buildAjaxFunction(grid, context);
+ Map options = AjaxRendererUtils.buildEventOptions(context, grid);
+
+ function.addParameter(options);
+ String completeFunction = function.toScript()+"; return false;";
+ return completeFunction;
+
+ }
+
+ protected void doDecode(FacesContext context, UIComponent component) {
+
+ super.doDecode(context, component);
+
+ if(component instanceof UIScrollableGrid){
+
+ UIScrollableGrid grid = (UIScrollableGrid)component;
+ ExternalContext externalContext = context.getExternalContext();
+ String clientId = grid.getClientId(context) + "_state_input";
+
+ Map parameters = externalContext.getRequestParameterMap();
+ if(parameters.containsKey(clientId)){
+
+ String submitedState = (String)parameters.get(clientId);
+ String [] values = submitedState.split(",");
+ grid.setRow_count(new Integer(Integer.parseInt(values[0])));
+ grid.setDataIndex(new Integer(Integer.parseInt(values[1])));
+ grid.setStartRow(new Integer(Integer.parseInt(values[2])));
+ grid.queueEvent(new AjaxEvent(grid));
+
+ }
+
+ }
+
+ }
+
+ public void renderAjaxChildren(FacesContext context, UIComponent component)throws
IOException{
+
+ UIScrollableGrid grid = (UIScrollableGrid)component;
+
+ GridRendererState state = GridRendererState.createState(context, grid);
+ grid.setFirst(grid.getDataIndex().intValue());
+ grid.setRows(grid.getRow_count().intValue());
+ int start_row = grid.getStartRow().intValue();
+
+ AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
+ String client_id = grid.getClientId(context);
+ state.setClientId(client_id);
+ state.setAjaxContext(ajaxContext);
+ state.setRowIndex(start_row);
+
+ grid.walk(context, ajaxRowsRenderer, state);
+
+ }
+
+ public void encodeChildren(FacesContext context, UIComponent component
+ ) throws IOException {
+
+ AjaxContext ajaxContext = AjaxContext.getCurrentInstance();
+ if(ajaxContext.isAjaxRequest()){
+ renderAjaxChildren(context, component);
+ }else{
+ super.encodeChildren(context, component);
+ }
+
+ }
+
+ public void encodeBegin(FacesContext context, UIComponent component) throws IOException
{
+
+ if(component instanceof UIScrollableGrid){
+ UIScrollableGrid grid = (UIScrollableGrid)component;
+ setUpState(context, grid);
+ }
+
+ AjaxContext ajaxContext = AjaxContext.getCurrentInstance();
+ if(ajaxContext.isAjaxRequest()){
+ }else{
+ super.encodeBegin(context, component);
+ }
+
+ }
+
+ public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
+
+ AjaxContext ajaxContext = AjaxContext.getCurrentInstance();
+ if(ajaxContext.isAjaxRequest()){
+ }else{
+ super.encodeEnd(context, component);
+ }
+
+ }
+
+ public void setUpColumnsWidth(FacesContext context, UIScrollableGrid grid) throws
IOException{
+ GridRendererState state = GridRendererState.getRendererState(context);
+ state.setFrozenColumnCount(((Integer)grid.getAttributes().get("frozenColCount")).intValue());
+ ColumnWalker.iterateOverColumns(context, grid, columnsWidthCounter, null, state);
+ ComponentVariables variables = ComponentsVariableResolver.getVariables(this, grid);
+ int sumWidth = state.getSumWidth() + 20;
+ variables.setVariable("sumWidth", new Integer(sumWidth));
+ }
+
+ private RendererBase getCellTemplate() {
+ if (cellTemplate == null) {
+ cellTemplate =
TemplateLoader.loadTemplate("org.richfaces.renderkit.html.ScrollableGridCellRenderer");
+ }
+ return cellTemplate;
+ }
+
+ private RendererBase getHeaderCellTemplate() {
+
+ if (headerCellTemplate == null) {
+ headerCellTemplate =
TemplateLoader.loadTemplate("org.richfaces.renderkit.html.ScrollableGridHeaderCellRenderer");
+ }
+ return headerCellTemplate;
+ }
+
+ private RendererBase getFooterCellTemplate() {
+
+ if (footerCellTemplate == null) {
+ footerCellTemplate =
TemplateLoader.loadTemplate("org.richfaces.renderkit.html.ScrollableGridFooterCellRenderer");
+ }
+ return footerCellTemplate;
+ }
+}