diff --git a/README.md b/README.md
index 5779cba..76ce873 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,7 @@ A library which makes it easier to manually create a simple PDF document with [A
}
dependencies {
- implementation("com.github.vkuzel:Simple-PDF-Layout:2.1.0")
+ implementation("com.github.vkuzel:Simple-PDF-Layout:2.1.1")
}
```
diff --git a/src/main/java/com/github/vkuzel/simplepdflayout/Table.java b/src/main/java/com/github/vkuzel/simplepdflayout/Table.java
index 76d40da..d389de5 100644
--- a/src/main/java/com/github/vkuzel/simplepdflayout/Table.java
+++ b/src/main/java/com/github/vkuzel/simplepdflayout/Table.java
@@ -18,6 +18,7 @@
import static com.github.vkuzel.simplepdflayout.calculator.PositionCalculator.Axis.Y;
import static com.github.vkuzel.simplepdflayout.property.Line.Style.SOLID;
import static java.awt.Color.GRAY;
+import static java.util.Collections.emptyList;
public final class Table implements ParentElement
, ChildElement, ElementWithBorder, ElementWithMargin {
@@ -41,7 +42,9 @@ public final class Table implements ParentElement, ChildElement, E
private Border border = null;
private TableCellConfigurer cellConfigurer;
- private List> data;
+ private List> data = emptyList();
+
+ private boolean cellsGenerated = false;
Table(ParentElement> parentElement) {
this.parentElement = parentElement;
@@ -98,11 +101,13 @@ public Table setDimensionPercent(float widthPercent, float heightPercent) {
}
public Table setWidth(float width) {
+ ensureCellsAreNotGenerated();
widthDimensionCalculator = new FixedDimensionCalculator(width);
return this;
}
public Table setWidthPercent(float widthPercent) {
+ ensureCellsAreNotGenerated();
widthDimensionCalculator = new PercentOfParentContentDimensionCalculator(parentElement, WIDTH, widthPercent);
return this;
}
@@ -113,11 +118,13 @@ public Table setWidthOfChildren() {
}
public Table setHeight(float height) {
+ ensureCellsAreNotGenerated();
heightDimensionCalculator = new FixedDimensionCalculator(height);
return this;
}
public Table setHeightPercent(float heightPercent) {
+ ensureCellsAreNotGenerated();
heightDimensionCalculator = new PercentOfParentContentDimensionCalculator(parentElement, HEIGHT, heightPercent);
return this;
}
@@ -166,23 +173,30 @@ public Border getBorder() {
}
public Table setCellConfigurer(TableCellConfigurer cellConfigurer) {
+ ensureCellsAreNotGenerated();
this.cellConfigurer = cellConfigurer;
return this;
}
public Table setData(List> rowsColumnsValues) {
+ ensureCellsAreNotGenerated();
this.data = rowsColumnsValues;
return this;
}
@Override
public void render(RenderingContext renderingContext) {
+ generateCells(renderingContext.getCalculationContext());
borderRenderer.render(renderingContext);
- createCells(renderingContext.getCalculationContext());
childrenRenderer.render(renderingContext);
}
- private void createCells(CalculationContext calculationContext) {
+ private void generateCells(CalculationContext calculationContext) {
+ if (cellsGenerated) {
+ return;
+ }
+ cellsGenerated = true;
+
int noOfRows = data.size();
int noOfColumns = data.isEmpty() || data.get(0).isEmpty() ? 0 : data.get(0).size();
@@ -249,6 +263,12 @@ private void configureCell(Table table, Text cell, int noOfRows, int noOfColumns
cell.setBorder(Border.of(top, line, line, left));
}
+ private void ensureCellsAreNotGenerated() {
+ if (cellsGenerated) {
+ throw new IllegalStateException("Table cells are generated, modification is not allowed!");
+ }
+ }
+
@Override
public ParentElement> getParent() {
return parentElement;
@@ -301,21 +321,25 @@ public float calculateContentY(CalculationContext calculationContext) {
@Override
public float calculateWidth(CalculationContext calculationContext) {
+ generateCells(calculationContext);
return widthDimensionCalculator.calculate(calculationContext);
}
@Override
public float calculateHeight(CalculationContext calculationContext) {
+ generateCells(calculationContext);
return heightDimensionCalculator.calculate(calculationContext);
}
@Override
public float calculateContentWidth(CalculationContext calculationContext) {
+ generateCells(calculationContext);
return widthContentDimensionCalculator.calculate(calculationContext);
}
@Override
public float calculateContentHeight(CalculationContext calculationContext) {
+ generateCells(calculationContext);
return heightContentDimensionCalculator.calculate(calculationContext);
}
diff --git a/src/test/java/com/github/vkuzel/simplepdflayout/TableTest.java b/src/test/java/com/github/vkuzel/simplepdflayout/TableTest.java
new file mode 100644
index 0000000..78241e8
--- /dev/null
+++ b/src/test/java/com/github/vkuzel/simplepdflayout/TableTest.java
@@ -0,0 +1,40 @@
+package com.github.vkuzel.simplepdflayout;
+
+import com.github.vkuzel.simplepdflayout.calculator.CalculationContext;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static java.util.Collections.singletonList;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class TableTest {
+
+ private static final Page ANY_PAGE = Page.a4();
+ private static final List> ANY_DATA = singletonList(singletonList("Test"));
+
+ @Test
+ void tableCellsShouldBeAlreadyGeneratedWhenHeightCalculationIsInvoked() {
+ // given
+ Table table = new Table(ANY_PAGE).setData(ANY_DATA);
+
+ // when
+ float height = table.calculateHeight(new CalculationContext());
+
+ // then
+ assertTrue(height > 0);
+ }
+
+ @Test
+ void tableCannotBeModifiedIfCellsAreGenerated() {
+ // given
+ Table table = new Table(ANY_PAGE).setData(ANY_DATA);
+
+ // when ... generates cells
+ table.calculateContentHeight(new CalculationContext());
+
+ // then
+ assertThrows(IllegalStateException.class, () -> table.setData(ANY_DATA));
+ }
+}