Table.java 3.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 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.
 *
 *  CityDoctor2 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 CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.reporting.pdf;

import org.jdom2.Element;

/**
 * Construct a table for the pdf report.
 * 
 * @author Matthias Betz
 *
 */
public class Table {

	private static final String TABLE_HEADER_COLOR = "#58ACFA";

	private Element tableBlock;

	private Element tableHeader;
	private Element tableBody;
Matthias Betz's avatar
Matthias Betz committed
37
	private Element tableElement;
38
39
40
41
42
43
44
45
46

	private int columns;

	public Table(int columns) {
		this.columns = columns;
		tableBlock = new Element("block", PdfReport.FO_NS);
		tableBlock.setAttribute("padding-after", "10");
		tableBlock.setAttribute("keep-together", "always");

Matthias Betz's avatar
Matthias Betz committed
47
48
49
50
51
		tableElement = new Element("table", PdfReport.FO_NS);
		tableBlock.addContent(tableElement);
		PdfReport.applySolidBorder(tableElement);
		tableElement.setAttribute("table-layout", "fixed");
		tableElement.setAttribute("width", "100%");
52
53

		tableHeader = new Element("table-header", PdfReport.FO_NS);
Matthias Betz's avatar
Matthias Betz committed
54
		tableElement.addContent(tableHeader);
55
56
57
		tableHeader.setAttribute("background-color", TABLE_HEADER_COLOR);

		tableBody = new Element("table-body", PdfReport.FO_NS);
Matthias Betz's avatar
Matthias Betz committed
58
		tableElement.addContent(tableBody);
59
60
61
62
63
64
65
66
67
68
69
70
71
	}

	public void setTableColumnWidth(int... width) {
		int sum = 0;
		for (int w : width) {
			sum += w;
		}
		if (sum != 100 || width.length != columns) {
			throw new IllegalArgumentException("column width must result in a sum of 100");
		}
		for (int i = 1; i <= width.length; i++) {
			int w = width[i - 1];
			Element tableColumn = new Element("table-column", PdfReport.FO_NS);
Matthias Betz's avatar
Matthias Betz committed
72
			tableElement.addContent(i - 1, tableColumn);
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
			tableColumn.setAttribute("column-number", "" + i);
			tableColumn.setAttribute("column-width", w + "%");
		}
	}

	public void setTitle(String... content) {
		if (content.length != columns) {
			throw new IllegalArgumentException(
					"Column length is: " + columns + " but only " + content.length + " values given");
		}
		if (tableHeader.getContentSize() > 0) {
			tableHeader.removeContent();
		}
		tableHeader.addContent(createTableRow(content));
	}

	public void addRow(String... content) {
		if (content.length != columns) {
			throw new IllegalArgumentException(
					"Column length is: " + columns + " but only " + content.length + " values given");
		}
		tableBody.addContent(createTableRow(content));
	}

	Element getTableBlock() {
		return tableBlock;
	}

	private Element createTableRow(String... content) {
		Element tableRow = new Element("table-row", PdfReport.FO_NS);

		for (int i = 0; i < content.length; i++) {
			Element tableCell = new Element("table-cell", PdfReport.FO_NS);
			tableRow.addContent(tableCell);
			tableCell.addContent(PdfReport.createTextElement(content[i]));
			tableCell.setAttribute("padding", "3pt 3pt 3pt 3pt");
			PdfReport.applySolidBorder(tableCell);
		}
		return tableRow;
	}

}