001: package org.dbbrowser.ui.helper.exporthelper;
002:
003: import com.lowagie.text.*;
004: import com.lowagie.text.pdf.PdfWriter;
005: import com.lowagie.text.pdf.PdfPTable;
006: import infrastructure.logging.Log;
007: import javax.swing.event.ChangeEvent;
008: import javax.swing.event.ChangeListener;
009: import javax.swing.table.AbstractTableModel;
010: import java.io.*;
011: import java.util.List;
012: import org.dbbrowser.ui.helper.exporthelper.wizard.WizardState;
013:
014: /**
015: * Class used to export a table to PDF file
016: */
017: public class ExporterForPDFFile implements ExportHelper {
018: private boolean stop = false;
019:
020: /**
021: * Export the data in the table to the specified file as a PDF file
022: * @param abstractTableModel
023: * @param changeListener
024: * @throws ExportHelperException
025: */
026: public void export(AbstractTableModel abstractTableModel,
027: ChangeListener changeListener, File fileToExportTo)
028: throws ExportHelperException {
029: this .stop = false;
030:
031: Log.getInstance().infoMessage("Starting PDF Export...",
032: ExporterForPDFFile.class.getName());
033: Document pdfDocument = setupPDFDocument(abstractTableModel,
034: fileToExportTo);
035: List listOfColumnsToInclude = (List) WizardState.getInstance()
036: .getState("List of columns to include");
037:
038: //Open the pdf document
039: pdfDocument.open();
040:
041: int rowCount = abstractTableModel.getRowCount();
042: int columnCount = abstractTableModel.getColumnCount();
043:
044: //Setup the table
045: PdfPTable table = null;
046: table = new PdfPTable(listOfColumnsToInclude.size());
047: table.setHeaderRows(1);
048:
049: table.setWidthPercentage(100.0f);
050: table.getDefaultCell().setGrayFill(0.8f);
051:
052: //Set to 0 if you want to include row number
053: int startPosition = 0;
054:
055: //Processing column names - start from 1 as the first column is the row number
056: for (int columnIndex = startPosition; columnIndex < columnCount; columnIndex++) {
057: String columnName = abstractTableModel
058: .getColumnName(columnIndex);
059: if (listOfColumnsToInclude.contains(columnName)) {
060: Phrase tableCell = new Phrase(columnName);
061: table.addCell(tableCell);
062: }
063: }
064:
065: table.getDefaultCell().setGrayFill(0.0f);
066:
067: //Process each row
068: for (int rowIndex = startPosition; rowIndex < rowCount; rowIndex++) {
069: //If stopped, exit and export incomplete table
070: if (stop == true) {
071: break;
072: }
073:
074: Log.getInstance().infoMessage(
075: "Exporting row " + (rowIndex + 1) + " of "
076: + rowCount + " as PDF file...",
077: ExporterForPDFFile.class.getName());
078:
079: //Process each column in the row - start from 1 as the first column is the row number
080: for (int columnIndex = startPosition; columnIndex < columnCount; columnIndex++) {
081: String columnName = abstractTableModel
082: .getColumnName(columnIndex);
083: if (listOfColumnsToInclude.contains(columnName)) {
084: Object cell = abstractTableModel.getValueAt(
085: rowIndex, columnIndex);
086: if (cell != null) {
087: String value = cell.toString();
088: Phrase tableCell = new Phrase(value);
089: table.addCell(tableCell);
090: } else {
091: Phrase tableCell = new Phrase("");
092: table.addCell(tableCell);
093: }
094: }
095: }
096:
097: //Inform the change listener
098: changeListener.stateChanged(new ChangeEvent(new Integer(
099: rowIndex)));
100: }
101:
102: try {
103: pdfDocument.add(table);
104: } catch (DocumentException exc) {
105: throw new ExportHelperException(exc.getMessage());
106: }
107:
108: //Close and flush the document
109: pdfDocument.close();
110:
111: //Write the document
112: Log.getInstance().infoMessage("finished PDF Export",
113: ExporterForPDFFile.class.getName());
114: }
115:
116: /**
117: * Call this to stop the export. This is required as it may be a long process and it runs in a seperate thread
118: */
119: public void stop() {
120: this .stop = true;
121: }
122:
123: private static Document setupPDFDocument(
124: AbstractTableModel abstractTableModel, File fileToExportTo)
125: throws ExportHelperException {
126: //Build a document
127: Document pdfDocument = null;
128: String orientation = (String) WizardState.getInstance()
129: .getState("Paper Orientation");
130: if ("PORTRAIT".equals(orientation)) {
131: pdfDocument = new Document(PageSize.A4);
132: } else {
133: pdfDocument = new Document(PageSize.A4.rotate());
134: }
135:
136: try {
137: PdfWriter.getInstance(pdfDocument, new FileOutputStream(
138: fileToExportTo));
139: } catch (DocumentException exc) {
140: throw new ExportHelperException(exc.getMessage());
141: } catch (FileNotFoundException exc) {
142: throw new ExportHelperException(exc.getMessage());
143: }
144:
145: //Add document metadata
146: pdfDocument.addTitle("Results");
147: pdfDocument.addAuthor("DBBrowser");
148: pdfDocument.addSubject("Data results");
149: pdfDocument.addCreator("DBBrowser");
150:
151: //Add header
152: String headerString = (String) WizardState.getInstance()
153: .getState("Header");
154: Phrase headerPhrase = new Phrase(headerString);
155: HeaderFooter header = new HeaderFooter(headerPhrase, false);
156: header.setAlignment(HeaderFooter.ALIGN_CENTER);
157: pdfDocument.setHeader(header);
158:
159: //Set the footer
160: Object footerString = WizardState.getInstance().getState(
161: "Footer");
162: Phrase footerPhrase = null;
163:
164: if (footerString != null) {
165: footerPhrase = new Phrase((String) footerString
166: + " - Page - ");
167: } else {
168: footerPhrase = new Phrase("Page - ");
169: }
170:
171: HeaderFooter footer = new HeaderFooter(footerPhrase, true);
172: footer.setAlignment(HeaderFooter.ALIGN_CENTER);
173: pdfDocument.setFooter(footer);
174:
175: return pdfDocument;
176: }
177: }
|