1 19 20 package org.polepos.reporters; 21 22 import java.awt.*; 23 import java.awt.Font ; 24 import java.io.*; 25 import java.util.*; 26 import java.util.List ; 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 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 printed = new ArrayList(); 92 93 for(TeamCar teamCar :graph.teamCars()){ 94 Team team = teamCar.getTeam(); 95 String 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 name, String description, String 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 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 path) { 140 String 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 exc) { 148 exc.printStackTrace(); 149 mDocument=null; 150 } 151 } 152 153 @Override  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 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 <TeamCar> teamCars=graph.teamCars(); 176 List <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 header = new StringBuffer (); 182 boolean first = true; 183 for(SetupProperty sp : setup.properties()){ 184 if(! first){ 185 header.append("\n"); 186 } 187 String 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 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 text, String 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 area = new java.awt.Rectangle (0, 0, width, height); 234 chart.draw(graphics, area); 235 graphics.dispose(); 236 ImgTemplate imgtmpl=new ImgTemplate(tp); 237 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 |