Add a row to the table @param text each value for corresponding column in this row
(String... text)
| 102 | * @param text each value for corresponding column in this row |
| 103 | */ |
| 104 | public void addRow(String... text) { |
| 105 | if (text.length != mWidths.length) { |
| 106 | throw new IllegalArgumentException("Expected " + mWidths.length + " columns, got " + text.length); |
| 107 | } |
| 108 | final String[][] cols = new String[text.length][]; |
| 109 | int rows = 0; |
| 110 | for (int col = 0; col < text.length; ++col) { |
| 111 | cols[col] = splitToWidthWithWrap(mWidths[col], text[col]); |
| 112 | rows = Math.max(rows, cols[col].length); |
| 113 | } |
| 114 | for (int row = 0; row < rows; row++) { |
| 115 | for (int col = 0; col < cols.length; ++col) { |
| 116 | final String s = (row < cols[col].length) ? cols[col][row] : ""; |
| 117 | if (s.length() > mWidths[col]) { |
| 118 | System.err.println("Arrays.toString(mWidths) = " + Arrays.toString(mWidths)); |
| 119 | System.err.println("Arrays.toString(text) = " + Arrays.toString(text)); |
| 120 | throw new IllegalArgumentException("text too wide, expected: " + mWidths[col] + " got: " + s.length() + " " + s); |
| 121 | } |
| 122 | final String formatString = "|" + widthString(mSpacing) + widthString(mWidths[col]) + widthString(mSpacing); |
| 123 | mTable.append(String.format(formatString, "", s, "")); |
| 124 | } |
| 125 | mTable.append("|").append(StringUtils.LS); |
| 126 | } |
| 127 | addSeparator(); |
| 128 | } |
| 129 | |
| 130 | private static String[] splitToWidthWithWrap(int width, String text) { |
| 131 | final WrappingStringBuilder sb = new WrappingStringBuilder(); |