📄 multileveltotaltabledecorator.java
字号:
{ returnValue = StringUtils.defaultString(returnValue); returnValue += totalAllRows(); } } return returnValue; } /** * Issue a grand total row at the bottom. * @return the suitable string */ protected String totalAllRows() { if (containsTotaledColumns) { List headerCells = tableModel.getHeaderCellList(); StringBuffer output = new StringBuffer(); int currentRow = getListIndex(); output.append(TagConstants.TAG_OPEN + TagConstants.TAGNAME_ROW + " class=\"grandtotal-row\"" + TagConstants.TAG_CLOSE); boolean first = true; for (Iterator iterator = headerCells.iterator(); iterator.hasNext();) { HeaderCell headerCell = (HeaderCell) iterator.next(); if (first) { output.append(getTotalsTdOpen(headerCell, getGrandTotalLabel())); output.append(getGrandTotalDescription()); first = false; } else if (headerCell.isTotaled()) { // a total if the column should be totaled Object total = getTotalForColumn(headerCell.getColumnNumber(), 0, currentRow); output.append(getTotalsTdOpen(headerCell, getGrandTotalSum())); output.append(formatTotal(headerCell, total)); } else { // blank, if it is not a totals column output.append(getTotalsTdOpen(headerCell, getGrandTotalNoSum())); } output.append(TagConstants.TAG_OPENCLOSING + TagConstants.TAGNAME_COLUMN + TagConstants.TAG_CLOSE); } output.append("\n</tr>\n"); return output.toString(); } else { return ""; } } protected String getCellValue(int columnNumber, int rowNumber) { List fullList = tableModel.getRowListFull(); Row row = (Row) fullList.get(rowNumber); ColumnIterator columnIterator = row.getColumnIterator(tableModel.getHeaderCellList()); while (columnIterator.hasNext()) { Column column = columnIterator.nextColumn(); if (column.getHeaderCell().getColumnNumber() == columnNumber) { try { column.initialize(); return column.getChoppedAndLinkedValue(); } catch (ObjectLookupException e) { logger.error("Error: " + e.getMessage(), e); throw new RuntimeException("Error: " + e.getMessage(), e); } catch (DecoratorException e) { logger.error("Error: " + e.getMessage(), e); throw new RuntimeException("Error: " + e.getMessage(), e); } } } throw new RuntimeException("Unable to find column " + columnNumber + " in the list of columns"); } protected Object getTotalForColumn(int columnNumber, int startRow, int stopRow) { List fullList = tableModel.getRowListFull(); List window = fullList.subList(startRow, stopRow + 1); Object total = null; for (Iterator iterator = window.iterator(); iterator.hasNext();) { Row row = (Row) iterator.next(); ColumnIterator columnIterator = row.getColumnIterator(tableModel.getHeaderCellList()); while (columnIterator.hasNext()) { Column column = columnIterator.nextColumn(); if (column.getHeaderCell().getColumnNumber() == columnNumber) { Object value = null; try { value = column.getValue(false); } catch (ObjectLookupException e) { logger.error(e); } catch (DecoratorException e) { logger.error(e); } if (value != null && ! TagConstants.EMPTY_STRING.equals(value)) { total = add(column, total, value); } } } } return total; } protected Object add(Column column, Object total, Object value) { if (value == null) { return total; } else if (value instanceof Number) { Number oldTotal = new Double(0); if (total != null) { oldTotal = (Number)total; } return new Double(oldTotal.doubleValue() + ((Number) value).doubleValue()); } else { throw new UnsupportedOperationException("Cannot add a value of " + value + " in column " + column.getHeaderCell().getTitle()); } } public String getTotalsTdOpen(HeaderCell header, String totalClass) { String cssClass = ObjectUtils.toString(header.getHtmlAttributes().get("class")); StringBuffer buffer = new StringBuffer(); buffer.append(TagConstants.TAG_OPEN); buffer.append(TagConstants.TAGNAME_COLUMN); if (cssClass != null || totalClass != null) { buffer.append(" class=\""); if (cssClass != null) { buffer.append(cssClass); if (totalClass != null) { buffer.append(" "); } } if (totalClass != null) { buffer.append(totalClass); } buffer.append("\""); } buffer.append(TagConstants.TAG_CLOSE); return buffer.toString(); } public String getTotalsRowOpen() { return TagConstants.TAG_OPEN + TagConstants.TAGNAME_ROW + " class=\"subtotal\"" + TagConstants.TAG_CLOSE; } public String getTotalRowLabel(String groupingValue) { return subtotalDesc.format(new Object[]{groupingValue}); } public String formatTotal(HeaderCell header, Object total) { Object displayValue = total; if (header.getColumnDecorators().length > 0) { for (int i = 0; i < header.getColumnDecorators().length; i++) { DisplaytagColumnDecorator decorator = header.getColumnDecorators()[i]; try { displayValue = decorator.decorate(total, this.getPageContext(), tableModel.getMedia()); } catch (DecoratorException e) { logger.warn(e.getMessage(), e); // ignore, use undecorated value for totals } } } return displayValue != null ? displayValue.toString() : ""; } class GroupTotals { /** * The label class. */ protected String totalLabelClass = getSubtotalLabelClass(); /** * The row opener */ protected String totalsRowOpen = getTotalsRowOpen(); /** * The value class. */ protected String totalValueClass = getSubtotalValueClass(); private int columnNumber; private int firstRowOfCurrentSet; public GroupTotals(int headerCellColumn) { this.columnNumber = headerCellColumn; this.firstRowOfCurrentSet = 0; } public void printTotals(int currentRow, StringBuffer out) { // For each column, output: List headerCells = tableModel.getHeaderCellList(); if (firstRowOfCurrentSet < currentRow) // If there is more than one row, show a total { out.append(totalsRowOpen); for (Iterator iterator = headerCells.iterator(); iterator.hasNext();) { HeaderCell headerCell = (HeaderCell) iterator.next(); if (columnNumber == headerCell.getColumnNumber()) { // a totals label if it is the column for the current group String currentLabel = getCellValue(columnNumber, firstRowOfCurrentSet); out.append(getTotalsTdOpen(headerCell, getTotalLabelClass() + " group-" + (columnNumber + 1))); out.append(getTotalRowLabel(currentLabel)); } else if (headerCell.isTotaled()) { // a total if the column should be totaled Object total = getTotalForColumn(headerCell.getColumnNumber(), firstRowOfCurrentSet, currentRow); out.append(getTotalsTdOpen(headerCell, getTotalValueClass() + " group-" + (columnNumber + 1))); out.append(formatTotal(headerCell, total)); } else { // blank, if it is not a totals column String style = "group-" + (columnNumber + 1); if (headerCell.getColumnNumber() < innermostGroup) { style += " " + getTotalLabelClass() + " "; } out.append(getTotalsTdOpen(headerCell, style)); } out.append(TagConstants.TAG_OPENCLOSING + TagConstants.TAGNAME_COLUMN + TagConstants.TAG_CLOSE); } out.append("\n</tr>\n"); } } public void setStartRow(int i) { firstRowOfCurrentSet = i; } public String getTotalLabelClass() { return totalLabelClass; } public void setTotalsRowOpen(String totalsRowOpen) { this.totalsRowOpen = totalsRowOpen; } public void setTotalLabelClass(String totalLabelClass) { this.totalLabelClass = totalLabelClass; } public String getTotalValueClass() { return totalValueClass; } public void setTotalValueClass(String totalValueClass) { this.totalValueClass = totalValueClass; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -