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: * RTFExportTask.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.rtf;
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.ReportInterruptedException;
039: import org.jfree.report.ReportProcessingException;
040: import org.jfree.report.modules.gui.common.StatusListener;
041: import org.jfree.report.modules.gui.commonswing.ReportProgressDialog;
042: import org.jfree.report.modules.gui.commonswing.StatusType;
043: import org.jfree.report.modules.gui.commonswing.SwingGuiContext;
044: import org.jfree.report.modules.output.table.base.StreamReportProcessor;
045: import org.jfree.report.modules.output.table.rtf.StreamRTFOutputProcessor;
046: import org.jfree.report.util.i18n.Messages;
047: import org.jfree.util.Log;
048:
049: /**
050: * An export task implementation, which writes a given report into an Excel file.
051: *
052: * @author Thomas Morgner
053: */
054: public class RTFExportTask implements Runnable {
055: private Messages messages;
056:
057: /**
058: * The progress dialog that will be used to visualize the report progress.
059: */
060: private final ReportProgressDialog progressDialog;
061: /**
062: * The file name of the output file.
063: */
064: private final String fileName;
065: /**
066: * The report which should be exported.
067: */
068: private final JFreeReport report;
069: private StatusListener statusListener;
070:
071: /**
072: * Creates a new export task.
073: *
074: * @param dialog the progress dialog that will monitor the report progress.
075: * @param report the report that should be exported.
076: */
077: public RTFExportTask(final JFreeReport report,
078: final ReportProgressDialog dialog,
079: final SwingGuiContext swingGuiContext)
080: throws ReportProcessingException {
081: if (report == null) {
082: throw new ReportProcessingException(
083: "RtfExportTask(..): Report-Parameter cannot be null"); //$NON-NLS-1$
084: }
085:
086: if (swingGuiContext != null) {
087: this .statusListener = swingGuiContext.getStatusListener();
088: this .messages = new Messages(swingGuiContext.getLocale(),
089: RTFExportPlugin.BASE_RESOURCE_CLASS);
090: } else {
091: this .messages = new Messages(
092: RTFExportPlugin.BASE_RESOURCE_CLASS);
093: }
094:
095: final String filename = report.getConfiguration()
096: .getConfigProperty(
097: "org.jfree.report.modules.gui.rtf.FileName"); //$NON-NLS-1$
098: if (filename == null) {
099: throw new ReportProcessingException(
100: messages
101: .getErrorString("RTFExportTask.RTFExportTask.ERROR_0001_NULL_FILENAME")); //$NON-NLS-1$
102: }
103: this .fileName = filename;
104: this .progressDialog = dialog;
105: this .report = report;
106: }
107:
108: /**
109: * Exports the report into an Excel file.
110: */
111: public void run() {
112: OutputStream out = null;
113: File file = null;
114: try {
115: file = new File(fileName).getCanonicalFile();
116: final File directory = file.getParentFile();
117: if (directory != null) {
118: if (directory.exists() == false) {
119: if (directory.mkdirs() == false) {
120: Log
121: .warn("Can't create directories. Hoping and praying now.."); //$NON-NLS-1$
122: }
123: }
124: }
125: out = new BufferedOutputStream(new FileOutputStream(file));
126: final StreamRTFOutputProcessor target = new StreamRTFOutputProcessor(
127: report.getConfiguration(), out);
128: final StreamReportProcessor proc = new StreamReportProcessor(
129: report, target);
130: if (progressDialog != null) {
131: progressDialog.setModal(false);
132: progressDialog.setVisible(true);
133: proc.addReportProgressListener(progressDialog);
134: }
135: proc.processReport();
136: out.close();
137: if (progressDialog != null) {
138: proc.removeReportProgressListener(progressDialog);
139: }
140:
141: if (statusListener != null) {
142: statusListener
143: .setStatus(
144: StatusType.INFORMATION,
145: messages
146: .getString("RTFExportTask.USER_TASK_FINISHED")); //$NON-NLS-1$
147: }
148:
149: } catch (ReportInterruptedException re) {
150: if (statusListener != null) {
151: statusListener.setStatus(StatusType.WARNING, messages
152: .getString("RTFExportTask.USER_TASK_ABORTED")); //$NON-NLS-1$
153: }
154: try {
155: out.close();
156: out = null;
157: if (file.delete() == false) {
158: Log
159: .warn(new Log.SimpleMessage(
160: "Unable to delete incomplete export:", file)); //$NON-NLS-1$
161: }
162: } catch (SecurityException se) {
163: // ignore me
164: } catch (IOException ioe) {
165: // ignore me...
166: }
167: } catch (Exception re) {
168: Log.error("RTF export failed", re); //$NON-NLS-1$
169: if (statusListener != null) {
170: statusListener.setStatus(StatusType.WARNING, messages
171: .getString("RTFExportTask.USER_TASK_FAILED")); //$NON-NLS-1$
172: }
173:
174: } finally {
175: try {
176: if (out != null) {
177: out.close();
178: }
179: } catch (Exception e) {
180: Log.error("Unable to close the output stream.", e); //$NON-NLS-1$
181: if (statusListener != null) {
182: statusListener
183: .setStatus(
184: StatusType.WARNING,
185: messages
186: .getString("RTFExportTask.USER_TASK_FAILED")); //$NON-NLS-1$
187: }
188:
189: // if there is already another error, this exception is
190: // just a minor obstactle. Something big crashed before ...
191: }
192: }
193: if (progressDialog != null) {
194: progressDialog.setVisible(false);
195: }
196: }
197: }
|