Source Code Cross Referenced for ExporterForPDFFile.java in  » Database-Client » DBBrowser » org » dbbrowser » ui » helper » exporthelper » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » DBBrowser » org.dbbrowser.ui.helper.exporthelper 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.