Search in sources :

Example 1 with HtmlPanelGrid

use of jakarta.faces.component.html.HtmlPanelGrid in project myfaces by apache.

the class HtmlGridRendererBase method renderHeaderOrFooter.

protected void renderHeaderOrFooter(FacesContext context, ResponseWriter writer, UIComponent component, int columns, boolean header) throws IOException {
    UIComponent facet = component.getFacet(header ? "header" : "footer");
    if (facet == null) {
        return;
    }
    writer.startElement(header ? HTML.THEAD_ELEM : HTML.TFOOT_ELEM, null);
    // component);
    // component);
    writer.startElement(HTML.TR_ELEM, null);
    // component);
    writer.startElement(header ? HTML.TH_ELEM : HTML.TD_ELEM, null);
    String styleClass = (component instanceof HtmlPanelGrid) ? (header ? ((HtmlPanelGrid) component).getHeaderClass() : ((HtmlPanelGrid) component).getFooterClass()) : (header ? (String) component.getAttributes().get(ComponentAttrs.HEADER_CLASS_ATTR) : (String) component.getAttributes().get(ComponentAttrs.FOOTER_CLASS_ATTR));
    if (styleClass != null) {
        writer.writeAttribute(HTML.CLASS_ATTR, styleClass, header ? ComponentAttrs.HEADER_CLASS_ATTR : ComponentAttrs.FOOTER_CLASS_ATTR);
    }
    if (header) {
        writer.writeAttribute(HTML.SCOPE_ATTR, HTML.SCOPE_COLGROUP_VALUE, null);
    }
    writer.writeAttribute(HTML.COLSPAN_ATTR, Integer.toString(columns), null);
    // RendererUtils.renderChild(context, facet);
    facet.encodeAll(context);
    writer.endElement(header ? HTML.TH_ELEM : HTML.TD_ELEM);
    writer.endElement(HTML.TR_ELEM);
    writer.endElement(header ? HTML.THEAD_ELEM : HTML.TFOOT_ELEM);
}
Also used : UIComponent(jakarta.faces.component.UIComponent) HtmlPanelGrid(jakarta.faces.component.html.HtmlPanelGrid)

Example 2 with HtmlPanelGrid

use of jakarta.faces.component.html.HtmlPanelGrid in project myfaces by apache.

the class HtmlGridRendererBase method encodeEnd.

@Override
public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
    RendererUtils.checkParamValidity(facesContext, component, UIPanel.class);
    int columns;
    if (component instanceof HtmlPanelGrid) {
        columns = ((HtmlPanelGrid) component).getColumns();
    } else {
        Integer i = (Integer) component.getAttributes().get(ComponentAttrs.COLUMNS_ATTR);
        columns = i != null ? i : 0;
    }
    if (columns <= 0) {
        if (log.isLoggable(Level.SEVERE)) {
            log.severe("Wrong columns attribute for PanelGrid " + component.getClientId(facesContext) + ": " + columns);
        }
        columns = 1;
    }
    ResponseWriter writer = facesContext.getResponseWriter();
    Map<String, List<ClientBehavior>> behaviors = null;
    if (component instanceof ClientBehaviorHolder) {
        behaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
    }
    if (behaviors != null && !behaviors.isEmpty()) {
        ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
    }
    writer.startElement(HTML.TABLE_ELEM, component);
    if (component instanceof ClientBehaviorHolder) {
        if (!behaviors.isEmpty()) {
            HtmlRendererUtils.writeIdAndName(writer, component, facesContext);
        } else {
            HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
        }
        long commonPropertiesMarked = 0L;
        if (isCommonPropertiesOptimizationEnabled(facesContext)) {
            commonPropertiesMarked = CommonHtmlAttributesUtil.getMarkedAttributes(component);
        }
        if (behaviors.isEmpty() && isCommonPropertiesOptimizationEnabled(facesContext)) {
            CommonHtmlAttributesUtil.renderEventProperties(writer, commonPropertiesMarked, component);
        } else {
            if (isCommonEventsOptimizationEnabled(facesContext)) {
                CommonHtmlEventsUtil.renderBehaviorizedEventHandlers(facesContext, writer, commonPropertiesMarked, CommonHtmlEventsUtil.getMarkedEvents(component), component, behaviors);
            } else {
                HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, component, behaviors);
            }
        }
        if (isCommonPropertiesOptimizationEnabled(facesContext)) {
            HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.TABLE_ATTRIBUTES);
            CommonHtmlAttributesUtil.renderCommonPassthroughPropertiesWithoutEvents(writer, commonPropertiesMarked, component);
        } else {
            HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.TABLE_PASSTHROUGH_ATTRIBUTES_WITHOUT_EVENTS);
        }
    } else {
        HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
        if (isCommonPropertiesOptimizationEnabled(facesContext)) {
            HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.TABLE_ATTRIBUTES);
            CommonHtmlAttributesUtil.renderCommonPassthroughProperties(writer, CommonHtmlAttributesUtil.getMarkedAttributes(component), component);
        } else {
            HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
        }
    }
    writer.flush();
    HtmlRendererUtils.renderTableCaption(facesContext, writer, component);
    // theader and tfooter are rendered before the tbody
    // Header facet
    renderHeaderOrFooter(facesContext, writer, component, columns, true);
    // Footer facet
    renderHeaderOrFooter(facesContext, writer, component, columns, false);
    renderChildren(facesContext, writer, component, columns);
    writer.endElement(HTML.TABLE_ELEM);
}
Also used : ResponseWriter(jakarta.faces.context.ResponseWriter) List(java.util.List) HtmlPanelGrid(jakarta.faces.component.html.HtmlPanelGrid) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder)

Example 3 with HtmlPanelGrid

use of jakarta.faces.component.html.HtmlPanelGrid in project myfaces by apache.

the class HtmlGridRendererBase method renderChildren.

protected void renderChildren(FacesContext context, ResponseWriter writer, UIComponent component, int columns) throws IOException {
    String columnClasses;
    String rowClasses;
    if (component instanceof HtmlPanelGrid) {
        columnClasses = ((HtmlPanelGrid) component).getColumnClasses();
        rowClasses = ((HtmlPanelGrid) component).getRowClasses();
    } else {
        columnClasses = (String) component.getAttributes().get(ComponentAttrs.COLUMN_CLASSES_ATTR);
        rowClasses = (String) component.getAttributes().get(ComponentAttrs.ROW_CLASSES_ATTR);
    }
    String[] columnClassesArray = (columnClasses == null) ? ArrayUtils.EMPTY_STRING_ARRAY : StringUtils.trim(StringUtils.splitShortString(columnClasses, ','));
    int columnClassesCount = columnClassesArray.length;
    String[] rowClassesArray = (rowClasses == null) ? org.apache.myfaces.util.lang.ArrayUtils.EMPTY_STRING_ARRAY : StringUtils.trim(StringUtils.splitShortString(rowClasses, ','));
    int rowClassesCount = rowClassesArray.length;
    int childCount = getChildCount(component);
    if (childCount > 0) {
        // get the row indizes for which a new TBODY element should be created
        Integer[] bodyrows = null;
        String bodyrowsAttr = (String) component.getAttributes().get(ComponentAttrs.BODYROWS_ATTR);
        if (bodyrowsAttr != null && !bodyrowsAttr.isEmpty()) {
            String[] bodyrowsString = StringUtils.trim(StringUtils.splitShortString(bodyrowsAttr, ','));
            // parsing with no exception handling, because of JSF-spec:
            // "If present, this must be a comma separated list of integers."
            bodyrows = new Integer[bodyrowsString.length];
            for (int i = 0; i < bodyrowsString.length; i++) {
                bodyrows[i] = Integer.valueOf(bodyrowsString[i]);
            }
        } else {
            bodyrows = ZERO_INT_ARRAY;
        }
        int bodyrowsCount = 0;
        int rowIndex = -1;
        int columnIndex = 0;
        int rowClassIndex = 0;
        boolean rowStarted = false;
        for (int i = 0, size = component.getChildCount(); i < size; i++) {
            UIComponent child = component.getChildren().get(i);
            if (child.isRendered()) {
                if (columnIndex == 0) {
                    rowIndex++;
                    if (rowStarted) {
                        // do we have to close the last row?
                        writer.endElement(HTML.TR_ELEM);
                    }
                    // is the current row listed in the bodyrows attribute
                    if (ArrayUtils.contains(bodyrows, rowIndex)) {
                        // close any preopened TBODY element first
                        if (bodyrowsCount != 0) {
                            writer.endElement(HTML.TBODY_ELEM);
                        }
                        // component);
                        writer.startElement(HTML.TBODY_ELEM, null);
                        bodyrowsCount++;
                    }
                    // start of new/next row
                    // component);
                    writer.startElement(HTML.TR_ELEM, null);
                    String rowClass = null;
                    if (component instanceof HtmlPanelGrid) {
                        rowClass = ((HtmlPanelGrid) component).getRowClass();
                    }
                    if (rowClassIndex < rowClassesCount) {
                        if (rowClass == null) {
                            rowClass = rowClassesArray[rowClassIndex];
                        } else {
                            rowClass = rowClass + ' ' + rowClassesArray[rowClassIndex];
                        }
                    }
                    if (rowClass != null) {
                        writer.writeAttribute(HTML.CLASS_ATTR, rowClass, null);
                    }
                    rowStarted = true;
                    rowClassIndex++;
                    if (rowClassIndex == rowClassesCount) {
                        rowClassIndex = 0;
                    }
                }
                // component);
                writer.startElement(HTML.TD_ELEM, null);
                if (columnIndex < columnClassesCount) {
                    writer.writeAttribute(HTML.CLASS_ATTR, columnClassesArray[columnIndex], null);
                }
                columnIndex = childAttributes(context, writer, child, columnIndex);
                child.encodeAll(context);
                writer.endElement(HTML.TD_ELEM);
                columnIndex++;
                if (columnIndex >= columns) {
                    columnIndex = 0;
                }
            }
        }
        if (rowStarted) {
            if (columnIndex > 0) {
                Level level = context.isProjectStage(ProjectStage.Production) ? Level.FINE : Level.WARNING;
                if (log.isLoggable(level)) {
                    log.log(level, "PanelGrid " + ComponentUtils.getPathToComponent(component) + " has not enough children. Child count should be a " + "multiple of the columns attribute.");
                }
                // Render empty columns, so that table is correct
                for (; columnIndex < columns; columnIndex++) {
                    // component);
                    writer.startElement(HTML.TD_ELEM, null);
                    if (columnIndex < columnClassesCount) {
                        writer.writeAttribute(HTML.CLASS_ATTR, columnClassesArray[columnIndex], null);
                    }
                    writer.endElement(HTML.TD_ELEM);
                }
            }
            writer.endElement(HTML.TR_ELEM);
            // close any preopened TBODY element first
            if (bodyrowsCount != 0) {
                writer.endElement(HTML.TBODY_ELEM);
            }
        }
    }
}
Also used : UIComponent(jakarta.faces.component.UIComponent) Level(java.util.logging.Level) HtmlPanelGrid(jakarta.faces.component.html.HtmlPanelGrid)

Example 4 with HtmlPanelGrid

use of jakarta.faces.component.html.HtmlPanelGrid in project myfaces by apache.

the class HtmlRendererUtils method renderTableCaption.

public static void renderTableCaption(FacesContext context, ResponseWriter writer, UIComponent component) throws IOException {
    UIComponent captionFacet = component.getFacet("caption");
    if (captionFacet == null) {
        return;
    }
    String captionClass;
    String captionStyle;
    if (component instanceof HtmlPanelGrid) {
        HtmlPanelGrid panelGrid = (HtmlPanelGrid) component;
        captionClass = panelGrid.getCaptionClass();
        captionStyle = panelGrid.getCaptionStyle();
    } else if (component instanceof HtmlDataTable) {
        HtmlDataTable dataTable = (HtmlDataTable) component;
        captionClass = dataTable.getCaptionClass();
        captionStyle = dataTable.getCaptionStyle();
    } else {
        captionClass = (String) component.getAttributes().get(ComponentAttrs.CAPTION_CLASS_ATTR);
        captionStyle = (String) component.getAttributes().get(ComponentAttrs.CAPTION_STYLE_ATTR);
    }
    // component);
    writer.startElement(HTML.CAPTION_ELEM, null);
    if (captionClass != null) {
        writer.writeAttribute(HTML.CLASS_ATTR, captionClass, null);
    }
    if (captionStyle != null) {
        writer.writeAttribute(HTML.STYLE_ATTR, captionStyle, null);
    }
    // RendererUtils.renderChild(context, captionFacet);
    captionFacet.encodeAll(context);
    writer.endElement(HTML.CAPTION_ELEM);
}
Also used : HtmlDataTable(jakarta.faces.component.html.HtmlDataTable) UIComponent(jakarta.faces.component.UIComponent) HtmlPanelGrid(jakarta.faces.component.html.HtmlPanelGrid)

Example 5 with HtmlPanelGrid

use of jakarta.faces.component.html.HtmlPanelGrid in project myfaces by apache.

the class HtmlGridRendererTest method setUp.

public void setUp() throws Exception {
    super.setUp();
    panelGrid = new HtmlPanelGrid();
    colText = new HtmlOutputText();
    writer = new MockResponseWriter(new StringWriter(), null, null);
    facesContext.setResponseWriter(writer);
    facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
    facesContext.getRenderKit().addRenderer(panelGrid.getFamily(), panelGrid.getRendererType(), new HtmlGridRenderer());
    facesContext.getRenderKit().addRenderer(colText.getFamily(), colText.getRendererType(), new HtmlTextRenderer());
    facesContext.getAttributes().put("org.apache.myfaces.RENDERED_FACES_JS", Boolean.TRUE);
}
Also used : StringWriter(java.io.StringWriter) MockResponseWriter(org.apache.myfaces.test.mock.MockResponseWriter) HtmlPanelGrid(jakarta.faces.component.html.HtmlPanelGrid) HtmlOutputText(jakarta.faces.component.html.HtmlOutputText)

Aggregations

HtmlPanelGrid (jakarta.faces.component.html.HtmlPanelGrid)5 UIComponent (jakarta.faces.component.UIComponent)3 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)1 HtmlDataTable (jakarta.faces.component.html.HtmlDataTable)1 HtmlOutputText (jakarta.faces.component.html.HtmlOutputText)1 ResponseWriter (jakarta.faces.context.ResponseWriter)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1 Level (java.util.logging.Level)1 MockResponseWriter (org.apache.myfaces.test.mock.MockResponseWriter)1