@Override
protected void onInitialize() {
super.onInitialize();
PivotModel pivotModel = getModelObject();
PivotTableRenderModel renderModel = PivotTableRenderModel.create(pivotModel);
// rendering header
RepeatingView column = new RepeatingView("header");
add(column);
Component tmp = null;
for (HeaderRenderRow row : renderModel.getHeaderRows()) {
// rendering row header (first columns)
WebMarkupContainer tr = new WebMarkupContainer(column.newChildId());
column.add(tr);
RepeatingView rowHeader = new RepeatingView("rowHeader");
tr.add(rowHeader);
for (HeaderRenderCell cell : row.getRowHeader()) {
if (cell.getPivotField() == null) {
// rendering an empty cell
tmp = new Label(rowHeader.newChildId(), "");
tmp.add(AttributeModifier.append("class", "empty"));
applyRowColSpan(cell, tmp);
rowHeader.add(tmp);
} else {
// rendering row field
tmp = createTitleLabel(rowHeader.newChildId(), cell.getPivotField());
applyRowColSpan(cell, tmp);
rowHeader.add(tmp);
}
}
// rendering column keys
RepeatingView value = new RepeatingView("value");
tr.add(value);
for (RenderCell cell : row.getValueCells()) {
if (cell instanceof HeaderValueRenderCell) {
HeaderValueRenderCell headerValueRenderCell = (HeaderValueRenderCell) cell;
tmp = createValueLabel(value.newChildId(), headerValueRenderCell.getRawValue(),
headerValueRenderCell.getPivotField());
applyRowColSpan(cell, tmp);
value.add(tmp);
} else {
HeaderRenderCell headerRenderCell = (HeaderRenderCell) cell;
tmp = createTitleLabel(value.newChildId(), headerRenderCell.getPivotField());
applyRowColSpan(cell, tmp);
value.add(tmp);
}
}
// rendering grand total column
RepeatingView grandTotalColumn = new RepeatingView("grandTotalColumn");
for (RenderCell cell : row.getGrandTotalColumn()) {
if (cell instanceof GrandTotalHeaderRenderCell) {
GrandTotalHeaderRenderCell grandTotalHeaderRenderCell = (GrandTotalHeaderRenderCell) cell;
if (grandTotalHeaderRenderCell.getRawValue() != null) {
tmp = new Label(grandTotalColumn.newChildId(), grandTotalHeaderRenderCell.getRawValue()
.toString());
applyRowColSpan(cell, tmp);
grandTotalColumn.add(tmp);
} else {
tmp = new WebMarkupContainer(grandTotalColumn.newChildId());
applyRowColSpan(cell, tmp);
tmp.add(AttributeModifier.append("class", "empty"));
grandTotalColumn.add(tmp);
}
} else {
HeaderRenderCell headerCell = (HeaderRenderCell) cell;
tmp = createTitleLabel(value.newChildId(), headerCell.getPivotField());
applyRowColSpan(cell, tmp);
grandTotalColumn.add(tmp);
}
}
grandTotalColumn.setVisible(row.getGrandTotalColumn().size() > 0);
tr.add(grandTotalColumn);
}
// rendering rows
RepeatingView row = new RepeatingView("row");
add(row);
for (DataRenderRow renderRow : renderModel.getValueRows()) {
WebMarkupContainer tr = new WebMarkupContainer(row.newChildId());
row.add(tr);
RepeatingView rowHeader = new RepeatingView("rowHeader");
tr.add(rowHeader);
for (DataHeaderRenderCell cell : renderRow.getRowHeader()) {
tmp = createValueLabel(rowHeader.newChildId(), cell.getRawValue(), cell.getPivotField());
applyRowColSpan(cell, tmp);
rowHeader.add(tmp);
}
RepeatingView value = new RepeatingView("value");
tr.add(value);
for (RenderCell cell : renderRow.getValue()) {
if (cell instanceof DataValueRenderCell) {
tmp = createValueLabel(value.newChildId(), cell.getRawValue(), cell.getPivotField());
applyRowColSpan(cell, tmp);
value.add(tmp);
} else {
GrandTotalValueRenderCell grandTotalCell = (GrandTotalValueRenderCell) cell;
tmp = createGrandTotalLabel(value.newChildId(), grandTotalCell.getRawValue(), grandTotalCell.isForRow());
applyRowColSpan(cell, tmp);
tmp.add(AttributeModifier.append("class", "grand-total"));
value.add(tmp);
}
}
}
WebMarkupContainer grandTotalRow = new WebMarkupContainer("grandTotalRow");
grandTotalRow.setVisible(renderModel.getGrandTotalRows().size() > 0);
add(grandTotalRow);
/*
* We currently expect exactly one GrantTotalRenderRow, therefor we dont
* need a repeating viewer
*/
for (GrandTotalRenderRow grantTotalRenderRow : renderModel.getGrandTotalRows()) {
for (GrandTotalRowHeaderRenderCell cell : grantTotalRenderRow.getRowHeader()) {
Label grandTotalRowHeader = new Label("rowHeader", "Grand Total");
applyRowColSpan(cell, grandTotalRowHeader);
grandTotalRow.add(grandTotalRowHeader);
}