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.List;
 import java.util.Map;
 
 import  javax.faces.application.ResourceDependencies;
 import  javax.faces.application.ResourceDependency;
 import  javax.faces.component.ContextCallback;
 import  javax.faces.component.UIColumn;
 import  javax.faces.component.UIComponent;
 import  javax.faces.component.behavior.ClientBehavior;
 import  javax.faces.component.behavior.ClientBehaviorHolder;
 import  javax.faces.context.FacesContext;
 import  javax.faces.context.ResponseWriter;
 
 import  org.ajax4jsf.javascript.JSFunction;
 import  org.richfaces.component.Row;

Author(s):
Anton Belevich
 
 @JsfRenderer(type = "org.richfaces.DataTableRenderer", family = .)
 @ResourceDependencies({ @ResourceDependency(library = "org.richfaces", name = "base-component.reslib"),
         @ResourceDependency(library = "org.richfaces", name = "datatable.js"),
         @ResourceDependency(library = "org.richfaces", name = "datatable.ecss") })
 public class DataTableRenderer extends AbstractTableRenderer {
     private static final String BEHAVIOR_EVENT_NAME = "javax.faces.behavior.event";
     private static final String ROW = "row";
 
     private class DataTableHiddenEncodeStrategy implements EncodeStrategy {
         public void begin(ResponseWriter writer, FacesContext context, UIComponent componentObject[] params)
             throws IOException {
             AbstractDataTable dataTable = (AbstractDataTablecomponent;
 
             writer.startElement(HtmlConstants.TBODY_ELEMENT, dataTable);
             writer.writeAttribute(HtmlConstants.ID_ATTRIBUTE, dataTable.getContainerClientId(context) + HIDDEN_CONTAINER_ID,
                 null);
             writer.startElement(HtmlConstants.TR_ELEMENT, dataTable);
             writer.startElement(HtmlConstants.TD_ELEM, dataTable);
             writer.writeAttribute(HtmlConstants.STYLE_ATTRIBUTE, "display: none"null);
         }
 
         public void end(ResponseWriter writer, FacesContext context, UIComponent componentObject[] paramsthrows IOException {
             writer.endElement(HtmlConstants.TD_ELEM);
             writer.endElement(HtmlConstants.TR_ELEMENT);
             writer.endElement(HtmlConstants.TBODY_ELEMENT);
         }
     }
 
     ;
 
     private class RichHeaderEncodeStrategy implements EncodeStrategy {
         public void begin(ResponseWriter writer, FacesContext context, UIComponent componentObject[] params)
             throws IOException {
             org.richfaces.component.AbstractColumn column = (org.richfaces.component.AbstractColumncomponent;
             writer.writeAttribute(HtmlConstants.ID_ATTRIBUTE, column.getClientId(context), null);
         }
 
         public void end(ResponseWriter writer, FacesContext context, UIComponent componentObject[] paramsthrows IOException {
         }
     }
 
     protected void doDecode(FacesContext contextfinal UIComponent component) {
         super.doDecode(contextcomponent);
 
         if ((null == context) || (null == component)) {
             throw new NullPointerException();
         }
        if (component instanceof ClientBehaviorHolder) {
            final Map<StringList<ClientBehavior>> behaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
            if (behaviors == null || behaviors.isEmpty()) {
                return;
            }
            Map<StringStringparametersMap = context.getExternalContext().getRequestParameterMap();
            final String behaviorEvent = parametersMap.get();
            if (behaviorEvent == null || !behaviorEvent.startsWith()) {
                return;
            }
            String behaviorSourceId = RenderKitUtils.getBehaviorSourceId(context);
            ((UIDataAdaptorcomponent).invokeOnRow(contextbehaviorSourceIdnew ContextCallback() {
                public void invokeContextCallback(FacesContext context, UIComponent target) {
                    if (target.equals(component)) {
                        List<ClientBehavior> behaviorsForEvent = behaviors.get(behaviorEvent);
                        if (behaviorsForEvent != null && !behaviorsForEvent.isEmpty()) {
                            for (ClientBehavior behavior : behaviorsForEvent) {
                                behavior.decode(contextcomponent);
                            }
                        }
                    }
                }
            });
        }
    }
    public void encodeTableStructure(ResponseWriter writer, FacesContext contextUIDataTableBase dataTablethrows IOException {
        if (dataTable instanceof AbstractDataTable) {
            encodeStyle(writercontextdataTablenull);
            encodeCaption(writercontext, (AbstractDataTabledataTable);
            // TODO nick - do we need this element if "columnsWidth" is absent?
            writer.startElement(HtmlConstants.COLGROUP_ELEMENT, dataTable);
            int columns = getColumnsCount(dataTable.columns());
            writer.writeAttribute(HtmlConstants.SPAN_ELEM, String.valueOf(columns), null);
            String columnsWidth = (StringdataTable.getAttributes().get("columnsWidth");
            if (columnsWidth != null) {
                String[] widths = columnsWidth.split(",");
                for (int i = 0; i < widths.lengthi++) {
                    writer.startElement(HtmlConstants.COL_ELEMENT, dataTable);
                    writer.writeAttribute(HtmlConstants.WIDTH_ATTRIBUTE, widths[i], null);
                    writer.endElement(HtmlConstants.COL_ELEMENT);
                }
            }
            writer.endElement(HtmlConstants.COLGROUP_ELEMENT);
        }
    }
    @Override
    public void encodeBeforeRows(ResponseWriter writer, FacesContext facesContextUIDataTableBase dataTableBase,
        boolean encodeParentTBodyboolean partialUpdatethrows IOException {
        if (encodeParentTBody) {
            if (partialUpdate) {
                partialStart(facesContextdataTableBase.getContainerClientId(facesContext) + ":tb");
            }
            encodeTableBodyStart(writerfacesContextdataTableBase);
        }
    }
    @Override
    public void encodeAfterRows(ResponseWriter writer, FacesContext facesContextUIDataTableBase dataTableBase,
        boolean encodeParentTBodyboolean partialUpdatethrows IOException {
        if (encodeParentTBody) {
            encodeTableBodyEnd(writer);
            if (partialUpdate) {
                partialEnd(facesContext);
            }
        }
    }
    public void encodeRow(ResponseWriter writer, FacesContext facesContextRowHolderBase holderthrows IOException {
        RowHolder rowHolder = (RowHolderholder;
        Row row = rowHolder.getRow();
        AbstractDataTable dataTable = (AbstractDataTablerow;
        putRowStylesIntoContext(facesContextrowHolder);
        boolean partialUpdate = rowHolder.isUpdatePartial();
        boolean parentTbodyStart = rowHolder.isEncodeParentTBody();
        boolean tbodyStart = parentTbodyStart;
        rowHolder.setRowStart(true);
        Iterator<UIComponent> components = row.columns();
        int columnNumber = 0;
        boolean rowBegun = false;
        while (components.hasNext()) {
            UIComponent child = components.next();
            if (child.isRendered()) {
                rowBegun = true;
                if (child instanceof Row) {
                    boolean isSubtable = (child instanceof AbstractCollapsibleSubTable);
                    // new row -> close </tr>
                    if (rowHolder.getProcessCell() != 0) {
                        encodeRowEnd(writer);
                        if (isSubtable) {
                            encodeTableBodyEnd(writer);
                            tbodyStart = false;
                            if (partialUpdate) {
                                partialEnd(facesContext);
                            }
                        }
                    }
                    rowHolder.nextCell();
                    if (isSubtable && partialUpdate) {
                        String id = dataTable.getRelativeClientId(facesContext) + ":" + child.getId() + ":c";
                        partialStart(facesContextid);
                    }
                    if (!isSubtable && !parentTbodyStart && !tbodyStart) {
                        encodeTableBodyStart(writerfacesContextdataTable);
                        rowHolder.setRowStart(true);
                        tbodyStart = true;
                    }
                    child.encodeAll(facesContext);
                    if (!isSubtable) {
                        encodeRowEnd(writer);
                        if (!components.hasNext()) {
                            if (!parentTbodyStart && tbodyStart) {
                                encodeTableBodyEnd(writer);
                                tbodyStart = false;
                            }
                        }
                        rowHolder.setRowStart(true);
                        rowHolder.resetProcessCell();
                    }
                    if (isSubtable && partialUpdate) {
                        partialEnd(facesContext);
                    }
                } else if (child instanceof UIColumn) {
                    if (!parentTbodyStart && !tbodyStart) {
                        if (partialUpdate) {
                            partialStart(facesContextdataTable.getRelativeClientId(facesContext) + ":tb");
                        }
                        encodeTableBodyStart(writerfacesContextdataTable);
                        rowHolder.setRowStart(true);
                        tbodyStart = true;
                    }
                    child.getAttributes().put(COLUMN_CLASS, getColumnClass(rowHoldercolumnNumber));
                    encodeColumn(facesContextwriter, (UIColumn) childrowHolder);
                    columnNumber++;
                    if (!components.hasNext()) {
                        encodeRowEnd(writer);
                        if (!parentTbodyStart && tbodyStart) {
                            encodeTableBodyEnd(writer);
                            tbodyStart = false;
                            if (partialUpdate) {
                                partialEnd(facesContext);
                            }
                        }
                    }
                }
            } else if (!components.hasNext() && rowBegun) {
                encodeRowEnd(writer);
                if (!parentTbodyStart && tbodyStart) {
                    encodeTableBodyEnd(writer);
                    tbodyStart = false;
                    if (partialUpdate) {
                        partialEnd(facesContext);
                    }
                }
            }
        }
    }
    public boolean encodeParentTBody(UIDataTableBase dataTableBase) {
        Iterator<UIComponent> iterator = dataTableBase.columns();
        while (iterator.hasNext()) {
            if (iterator.next() instanceof Row) {
                return false;
            }
        }
        return true;
    }
    protected void doEncodeBegin(ResponseWriter writer, FacesContext context, UIComponent componentthrows IOException {
        UIDataTableBase dataTable = (UIDataTableBasecomponent;
        encodeTableStart(writercontextdataTable);
        encodeTableFacets(writercontextdataTable);
    }
    protected void doEncodeEnd(ResponseWriter writer, FacesContext context, UIComponent componentthrows IOException {
        encodeTableEnd(writer);
    }
    protected Class<? extends UIComponent> getComponentClass() {
        return AbstractDataTable.class;
    }
    public void encodeCaption(ResponseWriter writer, FacesContext contextAbstractDataTable dataTablethrows IOException {
        UIComponent caption = dataTable.getCaption();
        if (caption == null) {
            return;
        }
        if (!caption.isRendered()) {
            return;
        }
        writer.startElement(HtmlConstants.CAPTION_ELEMENT, dataTable);
        String captionClass = (StringdataTable.getAttributes().get("captionClass");
        String captionSkinClass = getCaptionSkinClass();
        captionClass = HtmlUtil.concatClasses(captionClasscaptionSkinClass);
        writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE, captionClass"captionClass");
        String captionStyle = (StringdataTable.getAttributes().get("captionStyle");
        if (captionStyle != null && captionStyle.trim().length() != 0) {
            writer.writeAttribute(HtmlConstants.STYLE_ATTRIBUTE, captionStyle"captionStyle");
        }
        caption.encodeAll(context);
        writer.endElement(HtmlConstants.CAPTION_ELEMENT);
    }
    public EncodeStrategy getHeaderEncodeStrategy(UIComponent columnString facetName) {
        return (column instanceof org.richfaces.component.AbstractColumn && ..equals(facetName)) ? new RichHeaderEncodeStrategy()
            : new SimpleHeaderEncodeStrategy();
    }
    public boolean containsThead() {
        return true;
    }
    public void encodeClientScript(ResponseWriter writer, FacesContext facesContextUIDataTableBase dataTableBase)
        throws IOException {
        AbstractDataTable dataTable = (AbstractDataTabledataTableBase;
        writer.startElement(HtmlConstants.SCRIPT_ELEM, dataTable);
        writer.writeAttribute(HtmlConstants.TYPE_ATTR, HtmlConstants.JAVASCRIPT_TYPE, null);
        JSFunction function = new JSFunction("new RichFaces.ui.DataTable");
        function.addParameter(dataTable.getClientId(facesContext));
        AjaxOptions ajaxOptions = AjaxRendererUtils.buildEventOptions(facesContextdataTable);
        Map<StringObjectoptions = new HashMap<StringObject>();
        options.put("ajaxEventOptions"ajaxOptions.getParameters());
        function.addParameter(options);
        writer.writeText(function.toScript(), null);
        writer.endElement(HtmlConstants.SCRIPT_ELEM);
    }
    @Override
    public void encodeHiddenInput(ResponseWriter writer, FacesContext contextUIDataTableBase componentthrows IOException {
    }
    public String getTableSkinClass() {
        return "rf-dt";
    }
    public String getCaptionSkinClass() {
        return "rf-dt-cap";
    }
    public String getRowSkinClass() {
        return "rf-dt-r";
    }
    public String getFirstRowSkinClass() {
        return "rf-dt-fst-r";
    }
    public String getCellSkinClass() {
        return "rf-dt-c";
    }
    public String getHeaderSkinClass() {
        return "rf-dt-hdr";
    }
    public String getHeaderFirstSkinClass() {
        return "rf-dt-hdr-fst";
    }
    public String getHeaderCellSkinClass() {
        return "rf-dt-hdr-c";
    }
    public String getColumnHeaderSkinClass() {
        return "rf-dt-shdr";
    }
        return "rf-dt-shdr-fst";
    }
        return "rf-dt-shdr-c";
    }
    public String getColumnFooterSkinClass() {
        return "rf-dt-sftr";
    }
        return "rf-dt-sftr-fst";
    }
        return "rf-dt-sftr-c";
    }
    public String getFooterSkinClass() {
        return "rf-dt-ftr";
    }
    public String getFooterFirstSkinClass() {
        return "rf-dt-ftr-fst";
    }
    public String getFooterCellSkinClass() {
        return "rf-dt-ftr-c";
    }
    public String getNoDataClass() {
        return "rf-dt-nd";
    }
    @Override
    public String getNoDataCellClass() {
        return "rf-dt-nd-c";
    }
    @Override
    public String getTableBodySkinClass() {
        return "rf-dt-b";
    }
        return new DataTableHiddenEncodeStrategy();
    }
New to GrepCode? Check out our FAQ X