A table decorator which adds rows with totals (for column with the "total" attribute set) and subtotals (grouping by
the column with a group="1" attribute).
| Method from org.displaytag.decorator.TotalTableDecorator Detail: |
protected String createTotalRow(boolean grandTotal) {
StringBuffer buffer = new StringBuffer(1000);
buffer.append("\n< tr class=\"total\" >"); //$NON-NLS-1$
List headerCells = tableModel.getHeaderCellList();
for (Iterator it = headerCells.iterator(); it.hasNext();)
{
HeaderCell cell = (HeaderCell) it.next();
String cssClass = ObjectUtils.toString(cell.getHtmlAttributes().get("class"));
buffer.append("< td"); //$NON-NLS-1$
if (StringUtils.isNotEmpty(cssClass))
{
buffer.append(" class=\""); //$NON-NLS-1$
buffer.append(cssClass);
buffer.append("\""); //$NON-NLS-1$
}
buffer.append(" >"); //$NON-NLS-1$
if (cell.isTotaled())
{
String totalPropertyName = cell.getBeanPropertyName();
Object total = grandTotal ? grandTotals.get(totalPropertyName) : subTotals.get(totalPropertyName);
DisplaytagColumnDecorator[] decorators = cell.getColumnDecorators();
for (int j = 0; j < decorators.length; j++)
{
try
{
total = decorators[j].decorate(total, this.getPageContext(), tableModel.getMedia());
}
catch (DecoratorException e)
{
log.warn(e.getMessage(), e);
// ignore, use undecorated value for totals
}
}
buffer.append(total);
}
else if (groupPropertyName != null && groupPropertyName.equals(cell.getBeanPropertyName()))
{
buffer.append(grandTotal ? totalLabel : MessageFormat.format(subtotalLabel, new Object[]{previousValues
.get(groupPropertyName)}));
}
buffer.append("< /td >"); //$NON-NLS-1$
}
buffer.append("< /tr >"); //$NON-NLS-1$
// reset subtotal
this.subTotals.clear();
return buffer.toString();
}
|
public final String finishRow() {
StringBuffer buffer = new StringBuffer(1000);
// Grand totals...
if (getViewIndex() == ((List) getDecoratedObject()).size() - 1)
{
if (groupPropertyName != null)
{
buffer.append(createTotalRow(false));
}
buffer.append(createTotalRow(true));
}
return buffer.toString();
}
After every row completes we evaluate to see if we should be drawing a new total line and summing the results
from the previous group. |
public void init(PageContext context,
Object decorated,
TableModel tableModel) {
super.init(context, decorated, tableModel);
// reset
groupPropertyName = null;
grandTotals.clear();
subTotals.clear();
previousValues.clear();
for (Iterator it = tableModel.getHeaderCellList().iterator(); it.hasNext();)
{
HeaderCell cell = (HeaderCell) it.next();
if (cell.getGroup() == 1)
{
groupPropertyName = cell.getBeanPropertyName();
}
}
}
|
public void setSubtotalLabel(String subtotalLabel) {
this.subtotalLabel = subtotalLabel;
}
Setter for subtotalLabel. |
public void setTotalLabel(String totalLabel) {
this.totalLabel = totalLabel;
}
|
public String startRow() {
String subtotalRow = null;
if (groupPropertyName != null)
{
Object groupedPropertyValue = evaluate(groupPropertyName);
Object previousGroupedPropertyValue = previousValues.get(groupPropertyName);
// subtotals
if (previousGroupedPropertyValue != null
&& !ObjectUtils.equals(previousGroupedPropertyValue, groupedPropertyValue))
{
subtotalRow = createTotalRow(false);
}
previousValues.put(groupPropertyName, groupedPropertyValue);
}
for (Iterator it = tableModel.getHeaderCellList().iterator(); it.hasNext();)
{
HeaderCell cell = (HeaderCell) it.next();
if (cell.isTotaled())
{
String totalPropertyName = cell.getBeanPropertyName();
Number amount = (Number) evaluate(totalPropertyName);
Number previousSubTotal = (Number) subTotals.get(totalPropertyName);
Number previousGrandTotals = (Number) grandTotals.get(totalPropertyName);
subTotals.put(totalPropertyName, new Double((previousSubTotal != null
? previousSubTotal.doubleValue()
: 0)
+ (amount != null ? amount.doubleValue() : 0)));
grandTotals.put(totalPropertyName, new Double((previousGrandTotals != null ? previousGrandTotals
.doubleValue() : 0)
+ (amount != null ? amount.doubleValue() : 0)));
}
}
return subtotalRow;
}
|