KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > reporters > PDFReporter


1 /*
2 This file is part of the PolePosition database benchmark
3 http://www.polepos.org
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA. */

19
20 package org.polepos.reporters;
21
22 import java.awt.*;
23 import java.awt.Font JavaDoc;
24 import java.io.*;
25 import java.util.*;
26 import java.util.List JavaDoc;
27
28 import org.jfree.chart.*;
29 import org.polepos.framework.*;
30
31 import com.lowagie.text.*;
32 import com.lowagie.text.pdf.*;
33
34
35 public class PDFReporter extends GraphReporter {
36     private Circuit mCircuit;
37     private Document mDocument;
38     private PdfWriter mWriter;
39     
40     private com.lowagie.text.Font h1Font = FontFactory.getFont(FontFactory.HELVETICA,15,Font.BOLD);
41     private com.lowagie.text.Font h2Font = FontFactory.getFont(FontFactory.HELVETICA,12,Font.BOLD);
42     
43     private com.lowagie.text.Font bigFont = FontFactory.getFont(FontFactory.HELVETICA,10,Font.BOLD);
44     private com.lowagie.text.Font smallFont = FontFactory.getFont(FontFactory.HELVETICA,9,Font.PLAIN);
45     
46     
47     protected void report(Graph graph) {
48         
49         if(mDocument==null) {
50             setupDocument(PATH);
51             try {
52                 renderFirstPage(graph);
53             } catch (DocumentException e) {
54                 e.printStackTrace();
55             }
56         }
57         if(mDocument==null) {
58             return;
59         }
60         
61         Circuit circuit = graph.circuit();
62         if(! circuit.equals(mCircuit)){
63             mCircuit = circuit;
64         }
65         
66         JFreeChart chart = new ChartBuilder().createChart(graph);
67         
68         chart.setBackgroundPaint(null);
69         try {
70             renderTable(graph);
71             renderChart(chart);
72             mDocument.newPage();
73         } catch (Exception JavaDoc e) {
74             e.printStackTrace();
75         }
76     }
77     
78     private void renderFirstPage(Graph graph) throws DocumentException{
79         if(mDocument == null){
80             return;
81         }
82         
83         Paragraph para=new Paragraph();
84         para.add(new Chunk("PolePosition\n",h1Font));
85         para.add(new Chunk("the open source database benchmark\n",smallFont));
86         para.add(linked(new Chunk(WEBSITE + "\n\n\n", smallFont), WEBSITE));
87         para.add(new Chunk("Participating teams\n\n",h2Font));
88         
89         mDocument.add(para);
90         
91         List JavaDoc printed = new ArrayList();
92         
93         for(TeamCar teamCar :graph.teamCars()){
94             Team team = teamCar.getTeam();
95             String JavaDoc webSite = team.website();
96             if(webSite != null){
97                 if(! printed.contains(team)){
98                     printed.add(team);
99                     renderTeam(team.name(), team.description(), webSite);
100                 }
101             }else{
102                 Car car = teamCar.getCar();
103                 webSite = car.getWebsite();
104                 if(webSite != null){
105                     if(! printed.contains(car)){
106                         printed.add(car);
107                         renderTeam(car.name(), car.description(), webSite);
108                     }
109                 }
110             }
111         }
112         mDocument.newPage();
113     }
114     
115     private void renderTeam(String JavaDoc name, String JavaDoc description, String JavaDoc website) throws DocumentException{
116         Paragraph para=new Paragraph();
117         para.add(linked(new Chunk(name + "\n",bigFont), website));
118         if(description != null){
119             para.add(linked(new Chunk(description + "\n",smallFont), website));
120         }
121         if(website != null){
122             para.add(linked(new Chunk(website + "\n", smallFont), website));
123         }
124         para.add(new Chunk("\n",smallFont));
125         mDocument.add(para);
126     }
127     
128     private Element linked(Chunk chunk, String JavaDoc link){
129         if(link == null){
130             return chunk;
131         }
132         Anchor anchor = new Anchor(chunk);
133         anchor.setReference(link);
134         return anchor;
135     }
136     
137     
138
139     private void setupDocument(String JavaDoc path) {
140         String JavaDoc fileName = path + "/" + "PolePosition.pdf";
141         File file = new File(fileName);
142         file.delete();
143         mDocument = new Document();
144         try {
145             mWriter = PdfWriter.getInstance(mDocument, new FileOutputStream(file));
146             mDocument.open();
147         } catch (Exception JavaDoc exc) {
148             exc.printStackTrace();
149             mDocument=null;
150         }
151     }
152
153     @Override JavaDoc
154     public void endSeason() {
155         super.endSeason();
156         if(mDocument != null){
157             mDocument.close();
158         }
159         if(mWriter != null){
160             mWriter.close();
161         }
162     }
163
164     private void renderTable(Graph graph) throws DocumentException {
165         Paragraph para=new Paragraph();
166         // para.setAlignment(Element.ALIGN_CENTER);
167

168         Circuit circuit = graph.circuit();
169         Lap lap = graph.lap();
170         
171         para.add(new Chunk("Circuit: " + circuit.name()+ "\n",bigFont));
172         para.add(new Chunk(circuit.description() + "\n",smallFont));
173         para.add(new Chunk("Lap: " + lap.name()+ "\n\n",bigFont));
174         
175         List JavaDoc<TeamCar> teamCars=graph.teamCars();
176         List JavaDoc<TurnSetup> setups=graph.setups();
177         Table table = setupTable(graph);
178         int idx=1;
179         addTableCell(table, 0, 0,"t [time in ms]" , null,false,true);
180         for(TurnSetup setup : setups) {
181             StringBuffer JavaDoc header = new StringBuffer JavaDoc();
182             boolean first = true;
183             for(SetupProperty sp : setup.properties()){
184                 if(! first){
185                     header.append("\n");
186                 }
187                 String JavaDoc name = sp.name();
188                 if(! name.equals("commitinterval")){
189                     header.append(name);
190                     header.append(":");
191                     header.append(sp.value());
192                     first = false;
193                 }
194             }
195             addTableCell(table, idx, 0, header.toString(),null, true,true);
196             idx++;
197         }
198         table.endHeaders();
199         int vidx=1;
200         for(TeamCar teamCar : teamCars) {
201             addTableCell(table,0,vidx,teamCar.toString(),teamCar.website(),true,false);
202             int hidx=1;
203             for(TurnSetup setup : setups) {
204                 String JavaDoc text=String.valueOf(graph.timeFor(teamCar,setup));
205                 addTableCell(table,hidx,vidx,text, null,false,false);
206                 hidx++;
207             }
208             vidx++;
209         }
210         para.add(table);
211         para.add(new Chunk("\n",bigFont));
212         mDocument.add(para);
213     }
214
215     private void addTableCell(Table table, int hidx, int vidx, String JavaDoc text, String JavaDoc link, boolean bold,boolean header) throws BadElementException {
216         Chunk chunk = new Chunk(text,FontFactory.getFont(FontFactory.HELVETICA,9,(bold ? Font.BOLD : Font.PLAIN)));
217         chunk.setTextRise(3);
218         Cell cell=new Cell(linked(chunk, link));
219         cell.setHeader(header);
220         if(! header){
221             cell.setNoWrap(true);
222         }
223         cell.setVerticalAlignment(Element.ALIGN_CENTER);
224         table.addCell(cell,new Point(vidx,hidx));
225     }
226     
227     private void renderChart(JFreeChart chart) throws DocumentException, BadElementException {
228         int width = 500;
229         int height = 300;
230         PdfContentByte cb = mWriter.getDirectContent();
231         PdfTemplate tp = cb.createTemplate(width, height);
232         Graphics2D graphics = tp.createGraphics(width, height, new DefaultFontMapper());
233         java.awt.Rectangle JavaDoc area = new java.awt.Rectangle JavaDoc(0, 0, width, height);
234         chart.draw(graphics, area);
235         graphics.dispose();
236         ImgTemplate imgtmpl=new ImgTemplate(tp);
237         //imgtmpl.setAlignment(Element.ALIGN_CENTER);
238
mDocument.add(imgtmpl);
239     }
240
241     private Table setupTable(Graph graph) throws BadElementException {
242         Table table=new Table(graph.setups().size()+1);
243         table.setAutoFillEmptyCells(true);
244         table.setSpaceInsideCell(2);
245         
246         table.setBorderWidth(0);
247         table.setDefaultCellBorder(1);
248         table.setTableFitsPage(true);
249         return table;
250     }
251
252     protected void finish() {
253     }
254 }
255
Popular Tags