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: * PlainTextExportTask.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.plaintext;
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.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.plaintext.PageableTextOutputProcessor;
045: import org.jfree.report.modules.output.pageable.plaintext.PlainTextPageableModule;
046: import org.jfree.report.modules.output.pageable.plaintext.driver.AbstractEpsonPrinterDriver;
047: import org.jfree.report.modules.output.pageable.plaintext.driver.Epson24PinPrinterDriver;
048: import org.jfree.report.modules.output.pageable.plaintext.driver.Epson9PinPrinterDriver;
049: import org.jfree.report.modules.output.pageable.plaintext.driver.IBMCompatiblePrinterDriver;
050: import org.jfree.report.modules.output.pageable.plaintext.driver.PrinterDriver;
051: import org.jfree.report.modules.output.pageable.plaintext.driver.TextFilePrinterDriver;
052: import org.jfree.report.util.StringUtil;
053: import org.jfree.report.util.i18n.Messages;
054: import org.jfree.util.Configuration;
055: import org.jfree.util.Log;
056:
057: /**
058: * An export task implementation that writes the report into a plain text file.
059: *
060: * @author Thomas Morgner
061: */
062: public class PlainTextExportTask implements Runnable {
063: /**
064: * Provides access to externalized strings
065: */
066: private Messages messages;
067:
068: /**
069: * The progress monitor component that visualizes the export progress.
070: */
071: private final ReportProgressDialog progressDialog;
072: /**
073: * The name of the target file.
074: */
075: private final String fileName;
076: /**
077: * The report that should be exported.
078: */
079: private final JFreeReport report;
080: /**
081: * The desired export type, one of the constants defined in the PlainTextExportDialog.
082: */
083: private final int exportType;
084: /**
085: * The chars per inch for the export.
086: */
087: private final float charPerInch;
088: /**
089: * The lines per inch for the export.
090: */
091: private final float linesPerInch;
092:
093: private String printer;
094: private StatusListener statusListener;
095:
096: /**
097: * Creates a new plain text export task.
098: *
099: * @param dialog the progress monitor dialog.
100: * @param report the report that should be exported.
101: */
102: public PlainTextExportTask(final JFreeReport report,
103: final ReportProgressDialog dialog,
104: final SwingGuiContext swingGuiContext) {
105: if (report == null) {
106: throw new NullPointerException(
107: "PlainTextExportTask(): Report parameter must not be null"); //$NON-NLS-1$
108: }
109:
110: final Configuration config = report.getConfiguration();
111: fileName = config
112: .getConfigProperty("org.jfree.report.modules.gui.plaintext.FileName"); //$NON-NLS-1$
113: final String selectedPrinterText = config
114: .getConfigProperty("org.jfree.report.modules.gui.plaintext.ExportType"); //$NON-NLS-1$
115: if ("9pin".equals(selectedPrinterText)) //$NON-NLS-1$
116: {
117: exportType = PlainTextExportDialog.TYPE_EPSON9_OUTPUT;
118: printer = config
119: .getConfigProperty(Epson9PinPrinterDriver.EPSON_9PIN_PRINTER_TYPE);
120: } else if ("24pin".equals(selectedPrinterText)) //$NON-NLS-1$
121: {
122: exportType = PlainTextExportDialog.TYPE_EPSON24_OUTPUT;
123: printer = config
124: .getConfigProperty(Epson24PinPrinterDriver.EPSON_24PIN_PRINTER_TYPE);
125: } else if ("ibm".equals(selectedPrinterText)) //$NON-NLS-1$
126: {
127: exportType = PlainTextExportDialog.TYPE_IBM_OUTPUT;
128: printer = null;
129: } else {
130: exportType = PlainTextExportDialog.TYPE_PLAIN_OUTPUT;
131: printer = null;
132: }
133:
134: this .progressDialog = dialog;
135: this .report = report;
136:
137: charPerInch = StringUtil.parseFloat(report
138: .getReportConfiguration().getConfigProperty(
139: PlainTextPageableModule.CHARS_PER_INCH), 10.0f);
140: linesPerInch = StringUtil.parseFloat(report
141: .getReportConfiguration().getConfigProperty(
142: PlainTextPageableModule.LINES_PER_INCH), 6.0f);
143: if (swingGuiContext != null) {
144: this .statusListener = swingGuiContext.getStatusListener();
145: this .messages = new Messages(swingGuiContext.getLocale(),
146: PlainTextExportPlugin.BASE_RESOURCE_CLASS);
147: }
148: }
149:
150: /**
151: * Returns the printer command set for the given report and export type.
152: *
153: * @param out the output stream.
154: * @return The printer command set.
155: */
156: protected PrinterDriver getPrinterCommandSet(final OutputStream out) {
157: switch (exportType) {
158: case PlainTextExportDialog.TYPE_PLAIN_OUTPUT: {
159: return new TextFilePrinterDriver(out, charPerInch,
160: linesPerInch);
161: }
162: case PlainTextExportDialog.TYPE_IBM_OUTPUT: {
163: return new IBMCompatiblePrinterDriver(out, charPerInch,
164: linesPerInch);
165: }
166: case PlainTextExportDialog.TYPE_EPSON9_OUTPUT: {
167: final Epson9PinPrinterDriver driver = new Epson9PinPrinterDriver(
168: out, charPerInch, linesPerInch, printer);
169: applyFallbackEncoding(driver);
170: return driver;
171: }
172: case PlainTextExportDialog.TYPE_EPSON24_OUTPUT: {
173: final Epson24PinPrinterDriver driver = new Epson24PinPrinterDriver(
174: out, charPerInch, linesPerInch, printer);
175: applyFallbackEncoding(driver);
176: return driver;
177: }
178: default:
179: throw new IllegalArgumentException();
180: }
181: }
182:
183: /**
184: * Epson targets need special care, sadly the old printers used their own code page schema, which is totally
185: * incompatible with the modern ones. All epson printers that were manufactured before 1991/1992 suffer form this
186: * weakness.
187: *
188: * @param driver the driver, which should be configured.
189: */
190: private void applyFallbackEncoding(
191: final AbstractEpsonPrinterDriver driver) {
192: final String encoding = report
193: .getReportConfiguration()
194: .getConfigProperty(
195: "org.jfree.report.modules.gui.plaintext.FallbackEncoding"); //$NON-NLS-1$
196: driver.setFallBackCharset((byte) StringUtil.parseInt(encoding,
197: 0));
198: }
199:
200: /**
201: * Exports the report into a plain text file.
202: */
203: public void run() {
204: PageableReportProcessor proc = null;
205: OutputStream out = null;
206: File file = null;
207: try {
208: file = new File(fileName).getCanonicalFile();
209: final File directory = file.getParentFile();
210: if (directory != null) {
211: if (directory.exists() == false) {
212: if (directory.mkdirs() == false) {
213: Log
214: .warn("Can't create directories. Hoping and praying now.."); //$NON-NLS-1$
215: }
216: }
217: }
218: out = new BufferedOutputStream(new FileOutputStream(file));
219:
220: final PageableTextOutputProcessor outputProcessor = new PageableTextOutputProcessor(
221: getPrinterCommandSet(out), report
222: .getConfiguration());
223: proc = new PageableReportProcessor(report, outputProcessor);
224:
225: if (progressDialog != null) {
226: progressDialog.setModal(false);
227: progressDialog.setVisible(true);
228: proc.addReportProgressListener(progressDialog);
229: }
230:
231: proc.processReport();
232:
233: if (statusListener != null) {
234: statusListener
235: .setStatus(
236: StatusType.INFORMATION,
237: messages
238: .getString("PlainTextExportTask.USER_TASK_FINISHED")); //$NON-NLS-1$
239: }
240: } catch (ReportInterruptedException re) {
241: if (statusListener != null) {
242: statusListener
243: .setStatus(
244: StatusType.WARNING,
245: messages
246: .getString("PlainTextExportTask.USER_TASK_ABORTED")); //$NON-NLS-1$
247: }
248: try {
249: out.close();
250: out = null;
251: if (file.delete() == false) {
252: Log
253: .warn(new Log.SimpleMessage(
254: "Unable to delete incomplete export:", file)); //$NON-NLS-1$
255: }
256: } catch (SecurityException se) {
257: // ignore me
258: } catch (IOException ioe) {
259: // ignore me...
260: }
261: } catch (Exception re) {
262: Log.error("PlainText export failed", re); //$NON-NLS-1$
263: if (statusListener != null) {
264: statusListener
265: .setStatus(
266: StatusType.ERROR,
267: messages
268: .getString("PlainTextExportTask.USER_TASK_FAILED")); //$NON-NLS-1$
269: }
270: } finally {
271: try {
272: if (out != null) {
273: out.close();
274: }
275: } catch (Exception e) {
276: Log.error("Unable to close the output stream.", e); //$NON-NLS-1$
277: // if there is already another error, this exception is
278: // just a minor obstactle. Something big crashed before ...
279: }
280:
281: if (progressDialog != null) {
282: proc.removeReportProgressListener(progressDialog);
283: }
284:
285: }
286: if (progressDialog != null) {
287: progressDialog.setVisible(false);
288: }
289: }
290: }
|