001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * PdfExportTask.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.pdf;
030:
031: import java.io.BufferedOutputStream;
032: import java.io.File;
033: import java.io.FileOutputStream;
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: import org.jfree.report.JFreeReport;
038: import org.jfree.report.ReportProcessingException;
039: import org.jfree.report.modules.gui.common.StatusListener;
040: import org.jfree.report.modules.gui.commonswing.ReportProgressDialog;
041: import org.jfree.report.modules.gui.commonswing.StatusType;
042: import org.jfree.report.modules.gui.commonswing.SwingGuiContext;
043: import org.jfree.report.modules.output.pageable.base.PageableReportProcessor;
044: import org.jfree.report.modules.output.pageable.pdf.PdfOutputProcessor;
045: import org.jfree.report.util.i18n.Messages;
046: import org.jfree.util.Configuration;
047: import org.jfree.util.Log;
048:
049: /**
050: * An export task implementation which writes a given report into a PDF file.
051: *
052: * @author Thomas Morgner
053: */
054: public class PdfExportTask implements Runnable {
055: /**
056: * Provides access to externalized strings
057: */
058: private Messages messages;
059:
060: private JFreeReport report;
061: private ReportProgressDialog progressListener;
062: private StatusListener statusListener;
063: private File targetFile;
064:
065: /**
066: * Creates a new PDF export task.
067: */
068: public PdfExportTask(final JFreeReport report,
069: final ReportProgressDialog progressListener,
070: final SwingGuiContext swingGuiContext)
071: throws ReportProcessingException {
072: if (report == null) {
073: throw new NullPointerException(
074: "PdfExportTask(..): Report parameter cannot be null");
075: }
076:
077: this .report = report;
078: if (swingGuiContext != null) {
079: this .statusListener = swingGuiContext.getStatusListener();
080: this .messages = new Messages(swingGuiContext.getLocale(),
081: PdfExportPlugin.BASE_RESOURCE_CLASS);
082: } else {
083: this .messages = new Messages(
084: PdfExportPlugin.BASE_RESOURCE_CLASS);
085: }
086:
087: this .progressListener = progressListener;
088: final Configuration config = report.getConfiguration();
089: final String targetFileName = config
090: .getConfigProperty("org.jfree.report.modules.gui.pdf.TargetFileName"); //$NON-NLS-1$
091: if (targetFileName == null) {
092: throw new NullPointerException(
093: "TargetFileName must be set in the configuration.");
094: }
095:
096: targetFile = new File(targetFileName);
097: if (targetFile.exists()) {
098: if (targetFile.delete() == false) {
099: throw new ReportProcessingException(
100: messages
101: .getErrorString("PdfExportTask.ERROR_0001_TARGET_EXISTS")); //$NON-NLS-1$
102: }
103: }
104: }
105:
106: /**
107: * When an object implementing interface <code>Runnable</code> is used to create a thread, starting the thread causes
108: * the object's <code>run</code> method to be called in that separately executing thread.
109: * <p/>
110: * The general contract of the method <code>run</code> is that it may take any action whatsoever.
111: *
112: * @see Thread#run()
113: */
114: public void run() {
115: PageableReportProcessor proc = null;
116: OutputStream fout = null;
117: try {
118: fout = new BufferedOutputStream(new FileOutputStream(
119: targetFile));
120: final PdfOutputProcessor outputProcessor = new PdfOutputProcessor(
121: report.getConfiguration(), fout);
122: proc = new PageableReportProcessor(report, outputProcessor);
123: if (progressListener != null) {
124: proc.addReportProgressListener(progressListener);
125: progressListener.setVisible(true);
126: }
127: proc.processReport();
128: if (statusListener != null) {
129: statusListener
130: .setStatus(
131: StatusType.INFORMATION,
132: messages
133: .getString("PdfExportTask.USER_EXPORT_COMPLETE")); //$NON-NLS-1$
134: }
135: } catch (Exception e) {
136: if (statusListener != null) {
137: statusListener.setStatus(StatusType.ERROR, messages
138: .getString("PdfExportTask.USER_EXPORT_FAILED")); //$NON-NLS-1$
139: }
140: Log.error("Failed"); //$NON-NLS-1$
141: } finally {
142: if (proc != null) {
143: if (progressListener != null) {
144: proc.removeReportProgressListener(progressListener);
145: }
146: proc.close();
147: }
148: if (fout != null) {
149: try {
150: fout.close();
151: } catch (IOException e) {
152: // We tried our best ...
153: }
154: }
155:
156: if (progressListener != null) {
157: progressListener.setVisible(false);
158: }
159:
160: }
161: }
162: }
|