1
18 package net.bull.javamelody.internal.web.pdf;
19
20 import java.util.List;
21
22 import com.lowagie.text.Document;
23 import com.lowagie.text.DocumentException;
24 import com.lowagie.text.Element;
25 import com.lowagie.text.Font;
26 import com.lowagie.text.Image;
27 import com.lowagie.text.Phrase;
28 import com.lowagie.text.pdf.PdfPCell;
29 import com.lowagie.text.pdf.PdfPTable;
30
31
35 abstract class PdfAbstractTableReport extends PdfAbstractReport {
36 private final Font cellFont = PdfFonts.TABLE_CELL.getFont();
37 private PdfPTable table;
38 private boolean oddRow;
39
40 PdfAbstractTableReport(Document document) {
41 super(document);
42 }
43
44 void initTable(List<String> headers, int[] relativeWidths) throws DocumentException {
45 assert headers.size() == relativeWidths.length;
46 final PdfPTable mytable = new PdfPTable(headers.size());
47 mytable.setWidthPercentage(100);
48 mytable.setWidths(relativeWidths);
49 mytable.setHeaderRows(1);
50 final PdfPCell defaultCell = mytable.getDefaultCell();
51 defaultCell.setGrayFill(0.9f);
52 defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
53 defaultCell.setPaddingLeft(0);
54 defaultCell.setPaddingRight(0);
55 final Font tableHeaderFont = PdfFonts.TABLE_HEADER.getFont();
56 for (final String header : headers) {
57 mytable.addCell(new Phrase(header, tableHeaderFont));
58 }
59 defaultCell.setPaddingLeft(2);
60 defaultCell.setPaddingRight(2);
61 this.table = mytable;
62 }
63
64 void nextRow() {
65 if (oddRow) {
66 getDefaultCell().setGrayFill(0.97f);
67 } else {
68 getDefaultCell().setGrayFill(1);
69 }
70 oddRow = !oddRow;
71 }
72
73 PdfPCell getDefaultCell() {
74 return table.getDefaultCell();
75 }
76
77 void addCell(String string) {
78 table.addCell(new Phrase(string, cellFont));
79 }
80
81 void addCell(Phrase phrase) {
82 table.addCell(phrase);
83 }
84
85 void addCell(Image image) {
86 table.addCell(image);
87 }
88
89 void addCell(PdfPCell cell) {
90 table.addCell(cell);
91 }
92
93
101 boolean addTableToDocument() throws DocumentException {
102 return addToDocument(table);
103 }
104 }
105