Skip to content

Commit

Permalink
* Generate table cells when table height calculation is invoked.
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuzel committed Nov 8, 2020
1 parent a80f6f7 commit 76bb236
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
```

Expand Down
30 changes: 27 additions & 3 deletions src/main/java/com/github/vkuzel/simplepdflayout/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Table>, ChildElement<Table>, ElementWithBorder, ElementWithMargin {

Expand All @@ -41,7 +42,9 @@ public final class Table implements ParentElement<Table>, ChildElement<Table>, E
private Border border = null;

private TableCellConfigurer cellConfigurer;
private List<List<String>> data;
private List<List<String>> data = emptyList();

private boolean cellsGenerated = false;

Table(ParentElement<?> parentElement) {
this.parentElement = parentElement;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -166,23 +173,30 @@ public Border getBorder() {
}

public Table setCellConfigurer(TableCellConfigurer cellConfigurer) {
ensureCellsAreNotGenerated();
this.cellConfigurer = cellConfigurer;
return this;
}

public Table setData(List<List<String>> 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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/github/vkuzel/simplepdflayout/TableTest.java
Original file line number Diff line number Diff line change
@@ -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<List<String>> 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));
}
}

0 comments on commit 76bb236

Please sign in to comment.