1 /*
2  * Copyright 2008-2019 by Emeric Vernat
3  *
4  *     This file is part of Java Melody.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 /**
32  * Parent abstrait des classes de rapport pdf avec un tableau.
33  * @author Emeric Vernat
34  */

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; // NOPMD
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     /**
94      * Adds the <CODE>table</CODE> to the <CODE>Document</CODE>.
95      *
96      * @return <CODE>true</CODE> if the element was added, <CODE>false
97      *         </CODE> if not
98      * @throws DocumentException
99      *             when a document isn't open yet, or has been closed
100      */

101     boolean addTableToDocument() throws DocumentException {
102         return addToDocument(table);
103     }
104 }
105