Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright ${year}, Red Hat, Inc. and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This 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 software 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 software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.richfaces.renderkit;
 
 import java.util.Map;
 
 import  javax.faces.FacesException;
 import  javax.faces.application.ResourceDependencies;
 import  javax.faces.application.ResourceDependency;
 import  javax.faces.component.UIComponent;
 import  javax.faces.context.FacesContext;
 import  javax.faces.context.ResponseWriter;
 
 import  org.ajax4jsf.model.DataVisitResult;

Author(s):
Anton Belevich
 
 @JsfRenderer(type = "org.richfaces.DataGridRenderer", family = .)
 @ResourceDependencies({ @ResourceDependency(library = "javax.faces", name = "jsf.js"),
         @ResourceDependency(name = "jquery.js"),
         @ResourceDependency(name = "richfaces.js"),
         @ResourceDependency(library = "org.richfaces", name = "richfaces-queue.reslib"),
         @ResourceDependency(name = "richfaces-base-component.js"),
         @ResourceDependency(name = "richfaces-event.js"),
         @ResourceDependency(library = "org.richfaces", name = "datagrid.ecss") })
 public class DataGridRenderer extends AbstractRowsRenderer implements MetaComponentRenderer {
     private static final EncodeStrategy THEAD = new EncodeStrategy() {
         public void begin(ResponseWriter writer, FacesContext context, UIComponent componentObject[] params)
             throws IOException {
             String clientId = component.getClientId(context) + ":h";
 
             boolean partial = (Boolean) (Booleanparams[0];
             if (partial) {
                 context.getPartialViewContext().getPartialResponseWriter().startUpdate(clientId);
             }
 
             writer.startElement(HtmlConstants.THEAD_ELEMENT, component);
             writer.writeAttribute(HtmlConstants.ID_ATTRIBUTE, clientIdnull);
             writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-thead"null);
             writer.startElement(HtmlConstants.TR_ELEMENT, component);
             writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-h"null);
             writer.startElement(HtmlConstants.TH_ELEM, component);
             writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-h-c"null);
 
             int columns = (Integercomponent.getAttributes().get("columns");
             if (columns > 0) {
                 writer.writeAttribute(HtmlConstants.COLSPAN_ATTRIBUTE, columnsnull);
             }
         }
 
         public void end(ResponseWriter writer, FacesContext context, UIComponent componentObject[] paramsthrows IOException {
             writer.endElement(HtmlConstants.TH_ELEM);
             writer.endElement(HtmlConstants.TR_ELEMENT);
             writer.endElement(HtmlConstants.THEAD_ELEMENT);
 
             boolean partial = (Boolean) (Booleanparams[0];
             if (partial) {
                 context.getPartialViewContext().getPartialResponseWriter().endUpdate();
             }
         }
     };
     private static final EncodeStrategy TFOOT = new EncodeStrategy() {
         public void begin(ResponseWriter writer, FacesContext context, UIComponent componentObject[] params)
             throws IOException {
 
             String clientId = component.getClientId(context) + ":f";
 
             boolean partial = (Boolean) (Booleanparams[0];
             if (partial) {
                 context.getPartialViewContext().getPartialResponseWriter().startUpdate(clientId);
             }
 
             writer.startElement(HtmlConstants.TFOOT_ELEMENT, component);
            writer.writeAttribute(HtmlConstants.ID_ATTRIBUTE, clientIdnull);
            writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-tfoot"null);
            writer.startElement(HtmlConstants.TR_ELEMENT, component);
            writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-f"null);
            writer.startElement(HtmlConstants.TD_ELEM, component);
            writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-f-c"null);
            int columns = (Integercomponent.getAttributes().get("columns");
            if (columns > 0) {
                writer.writeAttribute(HtmlConstants.COLSPAN_ATTRIBUTE, columnsnull);
            }
        }
        public void end(ResponseWriter writer, FacesContext context, UIComponent componentObject[] paramsthrows IOException {
            writer.endElement(HtmlConstants.TD_ELEM);
            writer.endElement(HtmlConstants.TR_ELEMENT);
            writer.endElement(HtmlConstants.TFOOT_ELEMENT);
            boolean partial = (Boolean) (Booleanparams[0];
            if (partial) {
                context.getPartialViewContext().getPartialResponseWriter().endUpdate();
            }
        }
    };
    private static final EncodeStrategy CAPTION = new EncodeStrategy() {
        public void begin(ResponseWriter writer, FacesContext context, UIComponent componentObject[] params)
            throws IOException {
            writer.startElement(HtmlConstants.CAPTION_ELEMENT, component);
            writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-cap"null);
        }
        public void end(ResponseWriter writer, FacesContext context, UIComponent componentObject[] paramsthrows IOException {
            writer.endElement(HtmlConstants.CAPTION_ELEMENT);
        }
    };
    private static final EncodeStrategy NODATA = new EncodeStrategy() {
        public void begin(ResponseWriter writer, FacesContext context, UIComponent componentObject[] params)
            throws IOException {
            writer.startElement(HtmlConstants.TR_ELEMENT, component);
            writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-nd"null);
            writer.startElement(HtmlConstants.TD_ELEM, component);
            writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-nd-c"null);
            int columns = (Integercomponent.getAttributes().get("columns");
            if (columns > 0) {
                writer.writeAttribute(HtmlConstants.COLSPAN_ATTRIBUTE, columnsnull);
            }
        }
        public void end(ResponseWriter writer, FacesContext context, UIComponent componentObject[] paramsthrows IOException {
            writer.endElement(HtmlConstants.TD_ELEM);
            writer.endElement(HtmlConstants.TR_ELEMENT);
        }
    };
    @Override
    public void encodeRow(ResponseWriter writer, FacesContext facesContextRowHolderBase rowHolderthrows IOException {
        AbstractDataGrid dataGrid = (AbstractDataGridrowHolder.getRow();
        int columns = dataGrid.getColumns();
        int processCell = rowHolder.getProcessCell();
        if (columns > 0 && (processCell % columns == 0)) {
            if (processCell != 0) {
                writer.endElement(HtmlConstants.TR_ELEMENT);
                rowHolder.resetProcessCell();
            }
            writer.startElement(HtmlConstants.TR_ELEMENT, dataGrid);
            writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-r"null);
            rowHolder.nextRow();
        }
        writer.startElement(HtmlConstants.TD_ELEM, dataGrid);
        writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-c"null);
        renderChildren(facesContextdataGrid);
        writer.endElement(HtmlConstants.TD_ELEM);
    }
    public void encodeHeader(ResponseWriter writer, FacesContext facesContextAbstractDataGrid dataGridboolean partial)
        throws IOException {
        UIComponent headerFacet = dataGrid.getHeader();
        encodeFacet(writerfacesContextheaderFacetdataGridnew Object[] { partial });
    }
    public void encodeFooter(ResponseWriter writer, FacesContext facesContextAbstractDataGrid dataGridboolean partial)
        throws IOException {
        UIComponent footerFacet = dataGrid.getFooter();
        encodeFacet(writerfacesContextfooterFacetdataGridnew Object[] { partial });
    }
    public void encodeCaption(ResponseWriter writer, FacesContext facesContextAbstractDataGrid dataGridthrows IOException {
        UIComponent captionFacet = dataGrid.getCaption();
        encodeFacet(writerfacesContextcaptionFacetdataGridnull);
    }
    public void encodeNoData(ResponseWriter writer, FacesContext facesContextAbstractDataGrid dataGridthrows IOException {
        UIComponent noDataFacet = dataGrid.getNoData();
        encodeFacet(writerfacesContextnoDataFacetdataGridnull);
    }
    public void encodeFacet(ResponseWriter writer, FacesContext facesContext, UIComponent facetEncodeStrategy strategy,
        AbstractDataGrid dataGridObject[] paramsthrows IOException {
        if (facet != null && facet.isRendered()) {
            strategy.begin(writerfacesContextdataGridparams);
            facet.encodeAll(facesContext);
            strategy.end(writerfacesContextdataGridparams);
        }
    }
    public void encodeTBody(ResponseWriter writer, FacesContext facesContextAbstractDataGrid dataGridboolean partial)
        throws IOException {
        String clientId = dataGrid.getClientId(facesContext) + ":dgb";
        if (partial) {
            facesContext.getPartialViewContext().getPartialResponseWriter().startUpdate(clientId);
        }
        writer.startElement(HtmlConstants.TBODY_ELEMENT, dataGrid);
        writer.writeAttribute(HtmlConstants.ID_ATTRIBUTE, clientIdnull);
        writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-body"null);
        if (dataGrid.getRowCount() > 0) {
            processRows(writerfacesContextdataGridnull);
        } else {
            encodeNoData(writerfacesContextdataGrid);
        }
        writer.endElement(HtmlConstants.TBODY_ELEMENT);
        if (partial) {
            facesContext.getPartialViewContext().getPartialResponseWriter().endUpdate();
        }
    }
    @Override
    protected void doEncodeChildren(ResponseWriter writer, FacesContext facesContext, UIComponent componentthrows IOException {
        AbstractDataGrid dataGrid = (AbstractDataGridcomponent;
        writer.startElement(HtmlConstants.TABLE_ELEMENT, dataGrid);
        writer.writeAttribute(HtmlConstants.ID_ATTRIBUTE, dataGrid.getClientId(facesContext), null);
        Map<StringObjectattributes = dataGrid.getAttributes();
        String classes = concatClasses("rf-dg"attributes.get(HtmlConstants.STYLE_CLASS_ATTR));
        writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, classesnull);
        RenderKitUtils.renderAttribute(facesContext"style"attributes.get(HtmlConstants.STYLE_ATTRIBUTE));
        RenderKitUtils.renderAttribute(facesContext, HtmlConstants.TITLE_ATTRIBUTE,
            attributes.get(HtmlConstants.TITLE_ATTRIBUTE));
        encodeCaption(writerfacesContextdataGrid);
        encodeHeader(writerfacesContextdataGridfalse);
        encodeFooter(writerfacesContextdataGridfalse);
        encodeTBody(writerfacesContextdataGridfalse);
        writer.endElement(HtmlConstants.TABLE_ELEMENT);
    }
    @Override
    public RowHolderBase createRowHolder(FacesContext context, UIComponent componentObject[] options) {
        return new RowHolder(context, (AbstractDataGridcomponent);
    }
    @Override
    protected Class<? extends UIComponent> getComponentClass() {
        return AbstractDataGrid.class;
    }
    public void encodeMetaComponent(FacesContext context, UIComponent componentString metaComponentIdthrows IOException {
        AbstractDataGrid table = (AbstractDataGridcomponent;
        if (..equals(metaComponentId)) {
            encodeHeader(context.getResponseWriter(), contexttabletrue);
        } else if (..equals(metaComponentId)) {
            encodeFooter(context.getResponseWriter(), contexttabletrue);
        } else if (..equals(metaComponentId)) {
            encodeTBody(context.getResponseWriter(), contexttabletrue);
        } else {
            throw new IllegalArgumentException("Unsupported metaComponentIdentifier: " + metaComponentId);
        }
    }
    public void decodeMetaComponent(FacesContext context, UIComponent componentString metaComponentId) {
        throw new UnsupportedOperationException();
    }
    @Override
    protected void doCleanup(FacesContext contextRowHolderBase rowHolderthrows IOException {
        ResponseWriter writer = context.getResponseWriter();
        AbstractDataGrid dataGrid = (AbstractDataGridrowHolder.getRow();
        int cell = rowHolder.getProcessCell();
        int columns = dataGrid.getColumns();
        int rest = columns - cell;
        for (int i = 0; i < resti++) {
            writer.startElement(HtmlConstants.TD_ELEM, dataGrid);
            writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, "rf-dg-c"null);
            writer.endElement(HtmlConstants.TD_ELEM);
        }
        writer.endElement(HtmlConstants.TR_ELEMENT);
    }
    public DataVisitResult process(FacesContext facesContextObject rowKeyObject argument) {
        RowHolderBase holder = (RowHolderBaseargument;
        Row row = holder.getRow();
        row.setRowKey(facesContextrowKey);
        try {
            ResponseWriter writer = facesContext.getResponseWriter();
            encodeRow(writerfacesContextholder);
        } catch (IOException e) {
            throw new FacesException(e);
        }
        holder.nextCell();
        return DataVisitResult.CONTINUE;
    }
    @Override
    public void encodeFakeRow(FacesContext facesContextRowHolderBase rowHolderthrows IOException {
        UIComponent component = (UIComponent) rowHolder.getRow();
        ResponseWriter writer = facesContext.getResponseWriter();
        writer.startElement(HtmlConstants.TR_ELEMENT, component);
        writer.writeAttribute(HtmlConstants.STYLE_ATTRIBUTE, "display:none"null);
        writer.startElement(HtmlConstants.TD_ELEM, component);
        writer.endElement(HtmlConstants.TD_ELEM);
        writer.endElement(HtmlConstants.TR_ELEMENT);
    }
New to GrepCode? Check out our FAQ X