001 /** 002 * ======================================== 003 * JFreeReport : a free Java report library 004 * ======================================== 005 * 006 * Project Info: http://reporting.pentaho.org/ 007 * 008 * (C) Copyright 2000-2007, by Object Refinery Limited, 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 * $Id: PrinterUtility.java 2725 2007-04-01 18:49:29Z taqua $ 027 * ------------ 028 * (C) Copyright 2000-2005, by Object Refinery Limited. 029 * (C) Copyright 2005-2007, by Pentaho Corporation. 030 */ 031 032 package org.jfree.report.modules.gui.swing.printing; 033 034 import java.awt.print.PageFormat; 035 import java.awt.print.PrinterException; 036 import java.awt.print.PrinterJob; 037 038 import org.jfree.report.flow.ReportJob; 039 import org.jfree.util.Configuration; 040 import org.jfree.util.Log; 041 042 /** 043 * Creation-Date: 03.12.2006, 15:01:24 044 * 045 * @author Thomas Morgner 046 */ 047 public class PrinterUtility 048 { 049 public static final String PRINTER_JOB_NAME_KEY = 050 "org.jfree.report.modules.gui.common.print.JobName"; 051 public static final String NUMBER_COPIES_KEY = 052 "org.jfree.report.modules.gui.common.print.NumberOfCopies"; 053 054 private PrinterUtility() 055 { 056 } 057 058 public static void printDirectly(final ReportJob report) 059 throws PrinterException 060 { 061 final Configuration reportConfiguration = report.getConfiguration(); 062 final String jobName = reportConfiguration.getConfigProperty 063 (PRINTER_JOB_NAME_KEY, "JFreeReport"); 064 065 final PrinterJob printerJob = PrinterJob.getPrinterJob(); 066 printerJob.setJobName(jobName); 067 printerJob.setPageable(new PrintReportProcessor(report)); 068 printerJob.setCopies(getNumberOfCopies(reportConfiguration)); 069 070 // this tries to resolve at least some of the pain .. 071 // final PageFormat pageFormat = report.getPageFormat(); 072 // if (pageFormat != null) 073 // { 074 // report.setPageFormat(printerJob.validatePage(pageFormat)); 075 // } 076 // else 077 // { 078 // report.setPageFormat(printerJob.defaultPage()); 079 // } 080 printerJob.print(); 081 } 082 083 public static boolean print(final ReportJob report) 084 throws PrinterException 085 { 086 final Configuration reportConfiguration = report.getConfiguration(); 087 final String jobName = reportConfiguration.getConfigProperty 088 (PRINTER_JOB_NAME_KEY, "JFreeReport"); 089 090 final PrintReportProcessor document = new PrintReportProcessor(report); 091 092 final PrinterJob printerJob = PrinterJob.getPrinterJob(); 093 // final PageFormat pageFormat = report.getPageFormat(); 094 // if (pageFormat != null) 095 // { 096 // report.setPageFormat(printerJob.validatePage(pageFormat)); 097 // } 098 // else 099 // { 100 // report.setPageFormat(printerJob.defaultPage()); 101 // } 102 103 printerJob.setJobName(jobName); 104 printerJob.setPageable(document); 105 printerJob.setCopies(getNumberOfCopies(reportConfiguration)); 106 if (printerJob.printDialog()) 107 { 108 printerJob.print(); 109 return true; 110 } 111 return false; 112 } 113 114 public static int getNumberOfCopies(final Configuration configuration) 115 { 116 try 117 { 118 return Math.max(1, Integer.parseInt 119 (configuration.getConfigProperty(NUMBER_COPIES_KEY, "1"))); 120 } 121 catch (Exception e) 122 { 123 Log.warn("PrintUtil: Number of copies declared for the report is invalid"); 124 return 1; 125 } 126 } 127 128 }