Frames | No Frames |
1: /** 2: * ======================================== 3: * JFreeReport : a free Java report library 4: * ======================================== 5: * 6: * Project Info: http://reporting.pentaho.org/ 7: * 8: * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. 9: * 10: * This library is free software; you can redistribute it and/or modify it under the terms 11: * of the GNU Lesser General Public License as published by the Free Software Foundation; 12: * either version 2.1 of the License, or (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 15: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16: * See the GNU Lesser General Public License for more details. 17: * 18: * You should have received a copy of the GNU Lesser General Public License along with this 19: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20: * Boston, MA 02111-1307, USA. 21: * 22: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 23: * in the United States and other countries.] 24: * 25: * ------------ 26: * $Id: PrinterUtility.java 2725 2007-04-01 18:49:29Z taqua $ 27: * ------------ 28: * (C) Copyright 2000-2005, by Object Refinery Limited. 29: * (C) Copyright 2005-2007, by Pentaho Corporation. 30: */ 31: 32: package org.jfree.report.modules.gui.swing.printing; 33: 34: import java.awt.print.PageFormat; 35: import java.awt.print.PrinterException; 36: import java.awt.print.PrinterJob; 37: 38: import org.jfree.report.flow.ReportJob; 39: import org.jfree.util.Configuration; 40: import org.jfree.util.Log; 41: 42: /** 43: * Creation-Date: 03.12.2006, 15:01:24 44: * 45: * @author Thomas Morgner 46: */ 47: public class PrinterUtility 48: { 49: public static final String PRINTER_JOB_NAME_KEY = 50: "org.jfree.report.modules.gui.common.print.JobName"; 51: public static final String NUMBER_COPIES_KEY = 52: "org.jfree.report.modules.gui.common.print.NumberOfCopies"; 53: 54: private PrinterUtility() 55: { 56: } 57: 58: public static void printDirectly(final ReportJob report) 59: throws PrinterException 60: { 61: final Configuration reportConfiguration = report.getConfiguration(); 62: final String jobName = reportConfiguration.getConfigProperty 63: (PRINTER_JOB_NAME_KEY, "JFreeReport"); 64: 65: final PrinterJob printerJob = PrinterJob.getPrinterJob(); 66: printerJob.setJobName(jobName); 67: printerJob.setPageable(new PrintReportProcessor(report)); 68: printerJob.setCopies(getNumberOfCopies(reportConfiguration)); 69: 70: // this tries to resolve at least some of the pain .. 71: // final PageFormat pageFormat = report.getPageFormat(); 72: // if (pageFormat != null) 73: // { 74: // report.setPageFormat(printerJob.validatePage(pageFormat)); 75: // } 76: // else 77: // { 78: // report.setPageFormat(printerJob.defaultPage()); 79: // } 80: printerJob.print(); 81: } 82: 83: public static boolean print(final ReportJob report) 84: throws PrinterException 85: { 86: final Configuration reportConfiguration = report.getConfiguration(); 87: final String jobName = reportConfiguration.getConfigProperty 88: (PRINTER_JOB_NAME_KEY, "JFreeReport"); 89: 90: final PrintReportProcessor document = new PrintReportProcessor(report); 91: 92: final PrinterJob printerJob = PrinterJob.getPrinterJob(); 93: // final PageFormat pageFormat = report.getPageFormat(); 94: // if (pageFormat != null) 95: // { 96: // report.setPageFormat(printerJob.validatePage(pageFormat)); 97: // } 98: // else 99: // { 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: }