| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
package net.sf.statcvs.renderer; |
| 24 | |
|
| 25 | |
import java.util.Iterator; |
| 26 | |
|
| 27 | |
import net.sf.statcvs.pages.HTML; |
| 28 | |
import net.sf.statcvs.pages.MarkupSyntax; |
| 29 | |
import net.sf.statcvs.reportmodel.Column; |
| 30 | |
import net.sf.statcvs.reportmodel.Table; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public class TableRenderer { |
| 39 | |
|
| 40 | |
private final Table table; |
| 41 | 0 | private final HTMLTableCellRenderer renderer = new HTMLTableCellRenderer(); |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | public TableRenderer(final Table table, final MarkupSyntax output) { |
| 48 | 0 | this.table = table; |
| 49 | 0 | renderer.setOutput(output); |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public String getRenderedTable() { |
| 57 | 0 | final StringBuffer result = new StringBuffer(" <table ").append(renderer.getOutput().getTableFormat()); |
| 58 | 0 | result.append(" rules=\"groups\" summary=\"").append(HTML.escape(table.getSummary())); |
| 59 | 0 | result.append("\">\n"); |
| 60 | 0 | result.append(getColumnDescriptions()); |
| 61 | 0 | result.append(getTableHead()); |
| 62 | 0 | if (table.showTotals()) { |
| 63 | 0 | result.append(getTableTotals()); |
| 64 | |
} |
| 65 | 0 | result.append(getTableBody()); |
| 66 | 0 | result.append(" </table>\n\n"); |
| 67 | 0 | return result.toString(); |
| 68 | |
} |
| 69 | |
|
| 70 | |
private String getColumnDescriptions() { |
| 71 | 0 | final StringBuffer result = new StringBuffer(); |
| 72 | 0 | final Iterator it = table.getColumnIterator(); |
| 73 | 0 | boolean isFirstColumn = true; |
| 74 | 0 | while (it.hasNext()) { |
| 75 | 0 | it.next(); |
| 76 | 0 | if (table.hasKeysInFirstColumn() && isFirstColumn) { |
| 77 | 0 | result.append(" <colgroup align=\"left\"/>\n"); |
| 78 | 0 | isFirstColumn = false; |
| 79 | |
} else { |
| 80 | 0 | result.append(" <colgroup align=\"right\"/>\n"); |
| 81 | |
} |
| 82 | |
} |
| 83 | 0 | return result.toString(); |
| 84 | |
} |
| 85 | |
|
| 86 | |
private String getTableHead() { |
| 87 | 0 | final StringBuffer result = new StringBuffer(" <thead>\n <tr>\n"); |
| 88 | 0 | final Iterator it = table.getColumnIterator(); |
| 89 | 0 | while (it.hasNext()) { |
| 90 | 0 | final Column column = (Column) it.next(); |
| 91 | 0 | column.renderHead(renderer); |
| 92 | 0 | result.append(" ").append(renderer.getColumnHead()).append("\n"); |
| 93 | 0 | } |
| 94 | 0 | result.append(" </tr>\n </thead>\n"); |
| 95 | 0 | return result.toString(); |
| 96 | |
} |
| 97 | |
|
| 98 | |
private String getTableTotals() { |
| 99 | 0 | final StringBuffer result = new StringBuffer(" <tfoot>\n <tr>\n"); |
| 100 | 0 | final Iterator it = table.getColumnIterator(); |
| 101 | 0 | boolean isFirstColumn = true; |
| 102 | 0 | while (it.hasNext()) { |
| 103 | 0 | final Column column = (Column) it.next(); |
| 104 | 0 | column.renderTotal(renderer); |
| 105 | 0 | if (isFirstColumn && table.hasKeysInFirstColumn()) { |
| 106 | 0 | result.append(" ").append(renderer.getRowHead()).append("\n"); |
| 107 | 0 | isFirstColumn = false; |
| 108 | |
} else { |
| 109 | 0 | result.append(" ").append(renderer.getTableCell()).append("\n"); |
| 110 | |
} |
| 111 | 0 | } |
| 112 | 0 | result.append(" </tr>\n </tfoot>\n"); |
| 113 | 0 | return result.toString(); |
| 114 | |
} |
| 115 | |
|
| 116 | |
private String getTableBody() { |
| 117 | 0 | final StringBuffer result = new StringBuffer(" <tbody>\n"); |
| 118 | 0 | for (int i = 0; i < table.getRowCount(); i++) { |
| 119 | 0 | result.append(getTableRow(i)); |
| 120 | |
} |
| 121 | 0 | result.append(" </tbody>\n"); |
| 122 | 0 | return result.toString(); |
| 123 | |
} |
| 124 | |
|
| 125 | |
private String getTableRow(final int rowIndex) { |
| 126 | 0 | final StringBuffer result = new StringBuffer(); |
| 127 | 0 | if (rowIndex % 2 == 0) { |
| 128 | 0 | result.append(" <tr ").append(renderer.getEvenRowFormat()).append(">\n"); |
| 129 | |
} else { |
| 130 | 0 | result.append(" <tr ").append(renderer.getOddRowFormat()).append(">\n"); |
| 131 | |
} |
| 132 | 0 | final Iterator it = table.getColumnIterator(); |
| 133 | 0 | boolean isFirstColumn = true; |
| 134 | 0 | while (it.hasNext()) { |
| 135 | 0 | final Column column = (Column) it.next(); |
| 136 | 0 | column.renderCell(rowIndex, renderer); |
| 137 | 0 | if (isFirstColumn && table.hasKeysInFirstColumn()) { |
| 138 | 0 | result.append(" ").append(renderer.getRowHead()).append("\n"); |
| 139 | 0 | isFirstColumn = false; |
| 140 | |
} else { |
| 141 | 0 | result.append(" ").append(renderer.getTableCell()).append("\n"); |
| 142 | |
} |
| 143 | 0 | } |
| 144 | 0 | result.append(" </tr>\n"); |
| 145 | 0 | return result.toString(); |
| 146 | |
} |
| 147 | |
} |