From 271f249c0ff24b593bcc83c86edfeb236a551be5 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Mon, 6 Oct 2014 11:48:51 +0200 Subject: Colum span handling corrected. #38 --- .../at/knowcenter/wag/egov/egiz/table/Table.java | 78 ++++++++++++++++++++-- 1 file changed, 72 insertions(+), 6 deletions(-) (limited to 'pdf-as-lib/src/main/java/at/knowcenter') diff --git a/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/table/Table.java b/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/table/Table.java index 6db7ff3a..12d0e2ee 100644 --- a/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/table/Table.java +++ b/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/table/Table.java @@ -49,9 +49,12 @@ package at.knowcenter.wag.egov.egiz.table; import java.io.Serializable; -import java.util.Map; -import java.util.HashMap; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsException; /** * This class implements an abstract table definition. The table contains table @@ -216,12 +219,75 @@ public class Table implements Serializable public void addRow(String rowNumber, ArrayList row) { rows_.put(rowNumber, row); - if (row.size() > maxCols_) - { - maxCols_ = row.size(); - } + calculateMaxCols(row); } + private void calculateMaxCols(ArrayList newrow) { + int colCount = calculateRowSize(newrow); + if (colCount > maxCols_) + { + maxCols_ = colCount; + } + } + + private int calculateRowSize(ArrayList newrow) { + int colCount = 0; + for(int i = 0; i < newrow.size(); i++) { + colCount += newrow.get(i).getColSpan(); + } + return colCount; + } + + private void recalculateMaxCol() { + Iterator> rowIt = getRows().iterator(); + while(rowIt.hasNext()) { + ArrayList row = rowIt.next(); + calculateMaxCols(row); + } + } + + private boolean recursiveNorm = false; + + /** + * Expands the last cell of each column to fill the table + * @throws PdfAsSettingsException + */ + public void normalize() throws PdfAsSettingsException { + Iterator> rowIt = getRows().iterator(); + while(rowIt.hasNext()) { + ArrayList row = rowIt.next(); + + // This row fits just fine + if(row.size() == maxCols_) { + continue; + } + + int rowSize = calculateRowSize(row); + + // This row fits just fine including row spans + if(rowSize == maxCols_) { + continue; + } + + int missingColumns = maxCols_ - rowSize; + + if(missingColumns < 0) { + // wrong max Col value! + // recalculate the maximum Columns and normalize again! + if(recursiveNorm) { + throw new PdfAsSettingsException("Failed to build virtual model of signatur block table."); + } + recalculateMaxCol(); + recursiveNorm = true; + normalize(); + recursiveNorm = false; + break; + } else { + row.get(row.size() - 1).setColSpan(row.get(row.size() - 1).getColSpan() + missingColumns); + } + } + } + /** * The toString method, used for tests or debugging. */ -- cgit v1.2.3