001: /*
002: Copyright 2004-2007 Paul R. Holser, Jr. All rights reserved.
003: Licensed under the Academic Free License version 3.0
004: */
005:
006: package joptsimple.util;
007:
008: import java.util.ArrayList;
009: import java.util.Collections;
010: import java.util.Iterator;
011: import java.util.List;
012:
013: /**
014: * A means to display data in a text grid.
015: *
016: * @since 2.1
017: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
018: * @version $Id: ColumnarData.java,v 1.7 2007/04/10 20:06:27 pholser Exp $
019: */
020: public class ColumnarData {
021: private static final String LINE_SEPARATOR = System
022: .getProperty("line.separator");
023: private final List columns = new ArrayList();
024: private final String[] headers;
025:
026: /**
027: * Creates a new grid with the given column headers.
028: *
029: * @param headers column headers
030: */
031: public ColumnarData(String[] headers) {
032: this .headers = (String[]) headers.clone();
033:
034: clear();
035: }
036:
037: /**
038: * Adds a row to the grid. The data will fall under the corresponding headers.
039: * There can be fewer elements in the row than headers. Any data in columns outside
040: * of the number of headers will not be added to the grid.
041: *
042: * @param rowData row data to add
043: */
044: public void addRow(Object[] rowData) {
045: int i = 0;
046:
047: for (Iterator it = columns.iterator(); it.hasNext();) {
048: Column each = (Column) it.next();
049:
050: if (i < rowData.length)
051: each.addRow(rowData[i]);
052:
053: ++i;
054: }
055: }
056:
057: /**
058: * Gives a string that represents the data formatted in columns.
059: *
060: * @return the formatted grid
061: */
062: public String format() {
063: StringBuffer buffer = new StringBuffer();
064:
065: writeHeadersOn(buffer);
066: writeSeparatorsOn(buffer);
067: writeRowsOn(buffer);
068:
069: return buffer.toString();
070: }
071:
072: /**
073: * Removes all data from the grid, but preserves the headers.
074: */
075: public final void clear() {
076: columns.clear();
077:
078: for (int i = 0; i < headers.length; i++)
079: columns.add(new Column(headers[i]));
080: }
081:
082: private void writeHeadersOn(StringBuffer buffer) {
083: for (Iterator it = columns.iterator(); it.hasNext();)
084: ((Column) it.next()).writeHeaderOn(buffer);
085:
086: buffer.append(LINE_SEPARATOR);
087: }
088:
089: private void writeSeparatorsOn(StringBuffer buffer) {
090: for (Iterator it = columns.iterator(); it.hasNext();)
091: ((Column) it.next()).writeSeparatorOn(buffer);
092:
093: buffer.append(LINE_SEPARATOR);
094: }
095:
096: private void writeRowsOn(StringBuffer buffer) {
097: int maxHeight = ((Column) Collections.max(columns,
098: Column.BY_HEIGHT)).height();
099:
100: for (int i = 0; i < maxHeight; ++i)
101: writeRowOn(buffer, i);
102: }
103:
104: private void writeRowOn(StringBuffer buffer, int rowIndex) {
105: for (Iterator it = columns.iterator(); it.hasNext();)
106: ((Column) it.next()).writeCellOn(rowIndex, buffer);
107:
108: buffer.append(LINE_SEPARATOR);
109: }
110: }
|