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: * PrintUtil.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.print;
030:
031: import java.awt.print.PrinterException;
032: import java.awt.print.PrinterJob;
033:
034: import org.jfree.base.config.ModifiableConfiguration;
035: import org.jfree.report.JFreeReport;
036: import org.jfree.report.ReportProcessingException;
037: import org.jfree.report.event.ReportProgressListener;
038: import org.jfree.report.modules.output.pageable.graphics.PrintReportProcessor;
039: import org.jfree.util.Configuration;
040: import org.jfree.util.Log;
041:
042: /**
043: * Creation-Date: 05.09.2005, 18:36:03
044: *
045: * @author Thomas Morgner
046: */
047: public class PrintUtil {
048: public static final String PRINTER_JOB_NAME_KEY = "org.jfree.report.modules.output.pageable.graphics.print.JobName"; //$NON-NLS-1$
049: public static final String NUMBER_COPIES_KEY = "org.jfree.report.modules.output.pageable.graphics.print.NumberOfCopies"; //$NON-NLS-1$
050:
051: private PrintUtil() {
052: }
053:
054: public static void printDirectly(final JFreeReport report)
055: throws PrinterException, ReportProcessingException {
056: printDirectly(report, null);
057: }
058:
059: public static void printDirectly(final JFreeReport report,
060: final ReportProgressListener progressListener)
061: throws PrinterException, ReportProcessingException {
062: final ModifiableConfiguration reportConfiguration = report
063: .getReportConfiguration();
064: final String jobName = reportConfiguration.getConfigProperty(
065: PRINTER_JOB_NAME_KEY, report.getName());
066:
067: final PrinterJob printerJob = PrinterJob.getPrinterJob();
068: printerJob.setJobName(jobName);
069:
070: final PrintReportProcessor reportPane = new PrintReportProcessor(
071: report);
072: if (progressListener != null) {
073: reportPane.addReportProgressListener(progressListener);
074: }
075: printerJob.setPageable(reportPane);
076: try {
077: printerJob
078: .setCopies(getNumberOfCopies(reportConfiguration));
079: printerJob.print();
080: } finally {
081: reportPane.close();
082: if (progressListener != null) {
083: reportPane
084: .removeReportProgressListener(progressListener);
085: }
086: }
087: }
088:
089: public static boolean print(final JFreeReport report)
090: throws PrinterException, ReportProcessingException {
091: return print(report, null);
092: }
093:
094: public static boolean print(final JFreeReport report,
095: final ReportProgressListener progressListener)
096: throws PrinterException, ReportProcessingException {
097: final ModifiableConfiguration reportConfiguration = report
098: .getReportConfiguration();
099: final String jobName = reportConfiguration.getConfigProperty(
100: PRINTER_JOB_NAME_KEY, report.getName());
101:
102: final PrinterJob printerJob = PrinterJob.getPrinterJob();
103: printerJob.setJobName(jobName);
104:
105: final PrintReportProcessor reportPane = new PrintReportProcessor(
106: report);
107: if (progressListener != null) {
108: reportPane.addReportProgressListener(progressListener);
109: }
110:
111: try {
112: printerJob.setPageable(reportPane);
113: printerJob
114: .setCopies(getNumberOfCopies(reportConfiguration));
115: if (printerJob.printDialog()) {
116: printerJob.print();
117: return true;
118: }
119: return false;
120: } finally {
121: reportPane.close();
122: if (progressListener != null) {
123: reportPane
124: .removeReportProgressListener(progressListener);
125: }
126: }
127: }
128:
129: public static int getNumberOfCopies(
130: final Configuration configuration) {
131: try {
132: return Math.max(1, Integer.parseInt(configuration
133: .getConfigProperty(NUMBER_COPIES_KEY, "1"))); //$NON-NLS-1$
134: } catch (Exception e) {
135: Log
136: .warn("PrintUtil: Number of copies declared for the report is invalid"); //$NON-NLS-1$
137: return 1;
138: }
139: }
140: }
|