Java tutorial
/** * Copyright (C) 2012-2013 BonitaSoft S.A. * BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2.0 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.bonitasoft.studio.migration.utils; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Locale; import org.bonitasoft.studio.common.log.BonitaStudioLog; import org.bonitasoft.studio.migration.MigrationPlugin; import org.bonitasoft.studio.migration.i18n.Messages; import org.bonitasoft.studio.migration.model.report.Change; import org.bonitasoft.studio.migration.model.report.Report; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.jface.viewers.TableViewer; import com.itextpdf.text.BadElementException; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; /** * @author Romain Bioteau * */ public class PDFMigrationReportWriter { private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); private static Font legendFont = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL); private Report report; private TableViewer viewer; public PDFMigrationReportWriter(Report report) { this.report = report; } public PDFMigrationReportWriter(Report report, TableViewer viewer) { this(report); this.viewer = viewer; } public void execute(String filePath) { try { Document document = new Document(PageSize.A4.rotate(), 5, 5, 10, 10); PdfWriter.getInstance(document, new FileOutputStream(filePath)); document.open(); addMetaData(document); addTitlePage(document); addContent(document); document.close(); } catch (Exception e) { BonitaStudioLog.error(e); } } // iText allows to add metadata to the PDF which can be viewed in your Adobe // Reader // under File -> Properties private void addMetaData(Document document) { document.addTitle(report.getName()); document.addSubject("Migration status report"); document.addKeywords("BPMN, Migration, BonitaSoft, Process"); document.addAuthor("Bonita Studio"); document.addCreator("Bonita Studio"); } private void addTitlePage(Document document) throws DocumentException { Paragraph preface = new Paragraph(); // Lets write a big header preface.add(new Paragraph(report.getName(), catFont)); //addEmptyLine(preface, 1); // Will create: Report generated by: _name, _date preface.add(new Paragraph( new SimpleDateFormat("dd MMM yyyy", new Locale(Platform.getNL())).format(new Date()), smallBold)); document.add(preface); } private void addContent(Document document) throws DocumentException, MalformedURLException, IOException { Paragraph mainPara = new Paragraph(); addEmptyLine(mainPara, 1); createLegend(mainPara); // Add a table createTable(mainPara); // Now add all this to the document document.add(mainPara); } private void createLegend(Paragraph mainPara) throws BadElementException, MalformedURLException, IOException { mainPara.add(new Chunk(" ", legendFont)); Image im = getImageForStatus(IStatus.OK); mainPara.add(new Chunk(im, 0, 0, false)); mainPara.add(new Chunk(" ", legendFont)); mainPara.add(new Chunk(Messages.noActionRequired, legendFont)); mainPara.add(new Chunk(" ", legendFont)); im = getImageForStatus(IStatus.WARNING); mainPara.add(new Chunk(im, 0, 0, false)); mainPara.add(new Chunk(" ", legendFont)); mainPara.add(new Chunk(Messages.reviewRequired, legendFont)); mainPara.add(new Chunk(" ", legendFont)); im = getImageForStatus(IStatus.ERROR); mainPara.add(new Chunk(im, 0, 0, false)); mainPara.add(new Chunk(" ", legendFont)); mainPara.add(new Chunk(Messages.actionRequired, legendFont)); } private void createTable(Paragraph paragrah) throws MalformedURLException, IOException, DocumentException { PdfPTable table = new PdfPTable(6); table.setHeaderRows(0); table.setWidthPercentage(95f); PdfPCell c1 = new PdfPCell(new Phrase(Messages.elementType)); c1.setBackgroundColor(BaseColor.LIGHT_GRAY); c1.setHorizontalAlignment(Element.ALIGN_CENTER); c1.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase(Messages.name)); c1.setBackgroundColor(BaseColor.LIGHT_GRAY); c1.setHorizontalAlignment(Element.ALIGN_CENTER); c1.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase(Messages.property)); c1.setBackgroundColor(BaseColor.LIGHT_GRAY); c1.setHorizontalAlignment(Element.ALIGN_CENTER); c1.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase(Messages.information)); c1.setBackgroundColor(BaseColor.LIGHT_GRAY); c1.setHorizontalAlignment(Element.ALIGN_CENTER); c1.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase(Messages.status)); c1.setBackgroundColor(BaseColor.LIGHT_GRAY); c1.setHorizontalAlignment(Element.ALIGN_CENTER); c1.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase(Messages.reviewed)); c1.setBackgroundColor(BaseColor.LIGHT_GRAY); c1.setHorizontalAlignment(Element.ALIGN_CENTER); c1.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(c1); table.setHeaderRows(1); List<Change> changes = report.getChanges(); if (viewer != null) { for (int i = 0; i < changes.size(); i++) { addTableRow(table, (Change) viewer.getElementAt(i)); } } else { for (Change change : changes) { addTableRow(table, change); } } table.setWidths(new int[] { 3, 3, 3, 5, 2, 2 }); table.setHorizontalAlignment(Element.ALIGN_CENTER); table.setComplete(true); paragrah.add(table); } private void addTableRow(PdfPTable table, Change change) throws BadElementException, MalformedURLException, IOException { PdfPCell cell = new PdfPCell(new Phrase(change.getElementType())); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase(change.getElementName())); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase(change.getPropertyName())); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Phrase(change.getDescription())); cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setPadding(4f); table.addCell(cell); cell = new PdfPCell(getImageForStatus(change.getStatus()), false); cell.setPadding(10f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); String reviewed = Messages.no; if (change.isReviewed()) { reviewed = Messages.yes; } cell = new PdfPCell(new Phrase(reviewed)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); } private com.itextpdf.text.Image getImageForStatus(int status) throws BadElementException, MalformedURLException, IOException { switch (status) { case IStatus.OK: Image im = Image.getInstance(FileLocator .toFileURL(MigrationPlugin.getDefault().getBundle().getResource("/icons/valid.png"))); im.setCompressionLevel(12); im.scaleToFit(12, 12); return im; case IStatus.WARNING: im = Image.getInstance(FileLocator .toFileURL(MigrationPlugin.getDefault().getBundle().getResource("/icons/warning.gif"))); im.setCompressionLevel(12); im.scaleToFit(16, 16); return im; case IStatus.ERROR: im = Image.getInstance(FileLocator .toFileURL(MigrationPlugin.getDefault().getBundle().getResource("/icons/error.png"))); im.setCompressionLevel(12); im.scaleToFit(12, 12); return im; default: break; } return null; } private void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } }