001: /*
002: * This is free software, licensed under the Gnu Public License (GPL)
003: * get a copy from <http://www.gnu.org/licenses/gpl.html>
004: */
005: package henplus.view;
006:
007: import henplus.OutputDevice;
008:
009: /**
010: * <p>Title: ExtendedTableRenderer</p>
011: * <p>Description:<br>
012: * Created on: 25.07.2003</p>
013: * @version $Id: ExtendedTableRenderer.java,v 1.5 2004/03/05 23:34:38 hzeller Exp $
014: * @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
015: */
016: public class ExtendedTableRenderer extends TableRenderer {
017:
018: public ExtendedTableRenderer(ColumnMetaData[] meta,
019: OutputDevice out, String separator, boolean showHeader,
020: boolean showFooter) {
021: super (meta, out, separator, showHeader, showFooter);
022: }
023:
024: public ExtendedTableRenderer(ColumnMetaData[] meta, OutputDevice out) {
025: this (meta, out, "|", true, true);
026: }
027:
028: /**
029: * Checks for each element in the array its type, so can handle <code>ExtendedColumn</code> correctly.
030: * @param row
031: */
032: protected void updateColumnWidths(Column[] row) {
033: int metaIndex = 0;
034: for (int i = 0; i < row.length; ++i) {
035: if (row[i] instanceof ExtendedColumn
036: && ((ExtendedColumn) row[i]).getColspan() > 1) {
037: ExtendedColumn col = (ExtendedColumn) row[i];
038: metaIndex = updateMetaWidth(metaIndex, col);
039:
040: } else {
041: meta[i].updateWidth(row[i].getWidth());
042: metaIndex++;
043: }
044: }
045: }
046:
047: private int updateMetaWidth(int metaIndex, ExtendedColumn col) {
048: int span = col.getColspan();
049: // calculate the summarized width of concerned tables
050: int sumWidth = 0;
051: int participating = 0; // count the DISPLAYED columns
052: for (int j = metaIndex; j < (metaIndex + span); j++) {
053: if (!meta[j].doDisplay())
054: continue;
055: sumWidth += meta[j].getWidth();
056: participating++;
057: }
058: // test if the spanning column's width is greater than the sum
059: if (col.getWidth() > sumWidth) {
060: // to each meta col add the same amount
061: int diff = (col.getWidth() - sumWidth) / participating;
062: for (int j = metaIndex; j < (metaIndex + span); j++) {
063: if (!meta[j].doDisplay())
064: continue;
065: meta[j].updateWidth(meta[j].getWidth() + diff);
066: }
067: }
068: // set the metaIndex
069: metaIndex += span;
070: return metaIndex;
071: }
072:
073: /**
074: * Overwrites the <code>TableRenderer</code>s implementation for special
075: * handling of <code>ExtendedColumn</code>s.
076: */
077: protected boolean printColumns(Column[] currentRow,
078: boolean hasMoreLines) {
079: int metaIndex = 0;
080: // iterate over the elements of the given row
081: for (int i = 0; i < currentRow.length; ++i) {
082: if (currentRow[i] instanceof ExtendedColumn) {
083: ExtendedColumn col = (ExtendedColumn) currentRow[i];
084: hasMoreLines = printColumn(col, hasMoreLines, metaIndex);
085: metaIndex += col.getColspan();
086: } else {
087: if (!meta[metaIndex].doDisplay())
088: continue;
089: hasMoreLines = printColumn(currentRow[i], hasMoreLines,
090: metaIndex);
091: metaIndex++;
092: }
093: }
094: return hasMoreLines;
095: }
096:
097: protected boolean printColumn(ExtendedColumn col,
098: boolean hasMoreLines, int metaIndex) {
099: String txt;
100: out.print(" ");
101: // get summarized width of meta cols
102: int span = col.getColspan();
103: int width = 0;
104: if (span > 1) {
105: for (int i = metaIndex; i < (metaIndex + span); i++) {
106: if (!meta[i].doDisplay())
107: continue;
108: width += meta[i].getWidth();
109: }
110: // for each column after the first one add spaces for the surrounding spaces and the separator
111: width += (span - 1) * 3;
112: } else {
113: width = meta[metaIndex].getWidth();
114: }
115:
116: txt = formatString(col.getNextLine(), ' ', width, col
117: .getAlignment());
118: hasMoreLines |= col.hasNextLine();
119:
120: if (col.isBoldRequested())
121: out.attributeBold();
122: else if (col.isNull())
123: out.attributeGrey();
124:
125: out.print(txt);
126:
127: if (col.isNull() || col.isBoldRequested())
128: out.attributeReset();
129:
130: out.print(colSeparator);
131: return hasMoreLines;
132: }
133: }
|