in impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlGridRendererBase.java [255:420]
protected void renderChildren(FacesContext context,
ResponseWriter writer,
UIComponent component,
int columns)
throws IOException
{
String columnClasses;
String rowClasses;
if (component instanceof HtmlPanelGrid grid)
{
columnClasses = grid.getColumnClasses();
rowClasses = grid.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 Faces-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);
}
writer.startElement(HTML.TBODY_ELEM, null); // component);
bodyrowsCount++;
}
//start of new/next row
writer.startElement(HTML.TR_ELEM, null); // component);
String rowClass = null;
if (component instanceof HtmlPanelGrid grid)
{
rowClass = grid.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;
}
}
writer.startElement(HTML.TD_ELEM, null); // component);
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++)
{
writer.startElement(HTML.TD_ELEM, null); // component);
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);
}
}
}
}