
net.sf.nervalreports.generators.PDFTableRow Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pdf-generator Show documentation
Show all versions of pdf-generator Show documentation
This is the PDF generator package of NervalReports (a lightweight report creation library),
used to generate a report directly to a .pdf file.
/** This file is part of nervalreports.
*
* nervalreports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nervalreports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with nervalreports. If not, see . */
package net.sf.nervalreports.generators;
import java.util.ArrayList;
/** Row construction information for {@link PDFReportGenerator}.
* Note: for simplicity, one row will never break in more than one page.
* @author farrer */
/* default */ class PDFTableRow {
/** Initial Y position of the row. Note that it won't reflect page breaks,
* so always continuous (absolute, not relative) to the current table initial position. */
private float initialY;
/** Current needed height (y) to contains all row cell's content. */
private int neededHeight;
/** if {@link #neededHeight} is currently defined, or to be calculated. */
private boolean definedNeededHeight;
/** The maximun correction factor of row's cells. */
private float[] correctionFactor = new float[2];
/** The {@link #correctionFactor} is defined, or to be calculated. */
private boolean definedCorrectionFactor;
/** Current row cells. */
private final ArrayList cells;
/** Default constructor.
* @param totalColumns total columns of the table.
* @param initialX X position where row draw should start.
* @param initialY Y position where row draw should start.
* @param pageWidth current width of the page.
* @param columnWidth definition of width for the container of the table (usually the page itself, or another table's cell). */
PDFTableRow(int totalColumns, float initialX, float initialY, float pageWidth, int[] columnWidth) {
this.initialY = initialY;
int averageWidth = Math.round(pageWidth / totalColumns);
float curX = initialX;
/* Create the potential cells structure. */
cells = new ArrayList();
for (int i = 0; i < totalColumns; i++) {
int cellWidth = (columnWidth != null) ? columnWidth[i] : averageWidth;
cells.add(new PDFTableCell(curX, cellWidth));
curX += cellWidth;
}
definedNeededHeight = false;
definedCorrectionFactor = false;
}
/** Reset the values (and content) of each table. */
void reset(float initialY) {
for (PDFTableCell cell : cells) {
cell.reset();
}
definedNeededHeight = false;
definedCorrectionFactor = false;
this.correctionFactor[0] = 0.0f;
this.correctionFactor[1] = 0.0f;
this.neededHeight = 0;
this.initialY = initialY;
}
/** @return the needed height to contain all row cells content. Note that the value will only be calculated once,
* so subsequential calls (without reset) even on modified cells will return the same value. */
int getNeededHeight() {
if (!definedNeededHeight) {
neededHeight = 0;
definedNeededHeight = true;
for (PDFTableCell cell : cells) {
if (cell.getNeededHeight() > neededHeight) {
neededHeight = cell.getNeededHeight();
}
}
}
return neededHeight;
}
/** @return the biggest cell's correction factor.
* @see PDFTextSentence#getYCorrectionFactor() */
float[] getYCorrectionFactor() {
if (!definedCorrectionFactor) {
for (PDFTableCell cell : cells) {
float[] cellCorrectionFactor = cell.getYCorrectionFactor();
for (int i = 0; i < 2; i++) {
if (cellCorrectionFactor[i] > correctionFactor[i]) {
correctionFactor[i] = cellCorrectionFactor[i];
}
}
}
definedCorrectionFactor = true;
}
return correctionFactor;
}
/** @return the cell of index */
PDFTableCell getCell(int index) {
if (index >= cells.size()) {
new IllegalArgumentException("Index beyond the total.");
}
return cells.get(index);
}
/** @return {@link #cells}. */
ArrayList getCells() {
return cells;
}
/** @return {@link #initialY}. */
float getInitialY() {
return initialY;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy