KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > export > JRPrintServiceExporter


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * 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,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.export;
29
30 import java.awt.Graphics JavaDoc;
31 import java.awt.print.PageFormat JavaDoc;
32 import java.awt.print.Printable JavaDoc;
33 import java.awt.print.PrinterException JavaDoc;
34 import java.awt.print.PrinterJob JavaDoc;
35
36 import javax.print.PrintService JavaDoc;
37 import javax.print.PrintServiceLookup JavaDoc;
38 import javax.print.attribute.HashPrintRequestAttributeSet JavaDoc;
39 import javax.print.attribute.HashPrintServiceAttributeSet JavaDoc;
40 import javax.print.attribute.PrintRequestAttributeSet JavaDoc;
41 import javax.print.attribute.PrintServiceAttributeSet JavaDoc;
42 import javax.print.attribute.standard.MediaPrintableArea JavaDoc;
43 import javax.print.attribute.standard.OrientationRequested JavaDoc;
44 import javax.print.attribute.standard.PageRanges JavaDoc;
45
46 import net.sf.jasperreports.engine.JRAbstractExporter;
47 import net.sf.jasperreports.engine.JRException;
48 import net.sf.jasperreports.engine.JRExporterParameter;
49 import net.sf.jasperreports.engine.JRReport;
50 import net.sf.jasperreports.engine.JasperPrint;
51 import net.sf.jasperreports.engine.print.JRPrinterAWT;
52
53
54 /**
55  * @author Teodor Danciu (teodord@users.sourceforge.net)
56  * @version $Id: JRPrintServiceExporter.java 1469 2006-11-08 16:37:14 +0200 (Wed, 08 Nov 2006) teodord $
57  */

58 public class JRPrintServiceExporter extends JRAbstractExporter implements Printable JavaDoc
59 {
60
61
62     /**
63      *
64      */

65     protected JRGraphics2DExporter exporter = null;
66     protected boolean displayPageDialog = false;
67     protected boolean displayPrintDialog = false;
68
69     protected int reportIndex = 0;
70     
71     /**
72      *
73      */

74     public void exportReport() throws JRException
75     {
76         /* */
77         setOffset();
78
79         try
80         {
81             /* */
82             setExportContext();
83     
84             /* */
85             setInput();
86
87             /* */
88             if (!isModeBatch)
89             {
90                 setPageRange();
91             }
92     
93             PrintServiceAttributeSet JavaDoc printServiceAttributeSet =
94                 (PrintServiceAttributeSet JavaDoc)parameters.get(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET);
95             if (printServiceAttributeSet == null)
96             {
97                 printServiceAttributeSet = new HashPrintServiceAttributeSet JavaDoc();
98             }
99
100             Boolean JavaDoc pageDialog = (Boolean JavaDoc)parameters.get(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG);
101             if (pageDialog != null)
102             {
103                 displayPageDialog = pageDialog.booleanValue();
104             }
105     
106             Boolean JavaDoc printDialog = (Boolean JavaDoc)parameters.get(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG);
107             if (printDialog != null)
108             {
109                 displayPrintDialog = printDialog.booleanValue();
110             }
111     
112             PrinterJob JavaDoc printerJob = PrinterJob.getPrinterJob();
113             
114             JRPrinterAWT.initPrinterJobFields(printerJob);
115             
116             printerJob.setPrintable(this);
117             
118             // determining the print service only once
119
PrintService JavaDoc selectedService = (PrintService JavaDoc) parameters.get(JRPrintServiceExporterParameter.PRINT_SERVICE);
120             if (selectedService == null) {
121                 PrintService JavaDoc[] services = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet);
122                 if (services.length > 0)
123                     selectedService = services[0];
124             }
125             
126             if (selectedService == null)
127             {
128                 throw new JRException("No suitable print service found.");
129             }
130
131             try
132             {
133                 printerJob.setPrintService(selectedService);
134             }
135             catch (PrinterException JavaDoc e)
136             {
137                 throw new JRException(e);
138             }
139
140             // fix for bug ID artf1455 from jasperforge.org bug database
141
for(reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++)
142             {
143                 jasperPrint = (JasperPrint)jasperPrintList.get(reportIndex);
144
145                 exporter = new JRGraphics2DExporter();
146                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
147                 exporter.setParameter(JRExporterParameter.PROGRESS_MONITOR, parameters.get(JRExporterParameter.PROGRESS_MONITOR));
148                 exporter.setParameter(JRExporterParameter.OFFSET_X, parameters.get(JRExporterParameter.OFFSET_X));
149                 exporter.setParameter(JRExporterParameter.OFFSET_Y, parameters.get(JRExporterParameter.OFFSET_Y));
150                 exporter.setParameter(JRExporterParameter.CLASS_LOADER, classLoader);
151                 exporter.setParameter(JRExporterParameter.URL_HANDLER_FACTORY, urlHandlerFactory);
152                 exporter.setParameter(JRGraphics2DExporterParameter.MINIMIZE_PRINTER_JOB_SIZE, parameters.get(JRGraphics2DExporterParameter.MINIMIZE_PRINTER_JOB_SIZE));
153         
154                 PrintRequestAttributeSet JavaDoc printRequestAttributeSet = new HashPrintRequestAttributeSet JavaDoc();
155                 PrintRequestAttributeSet JavaDoc printRequestAttributeSetParam =
156                     (PrintRequestAttributeSet JavaDoc)parameters.get(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET);
157                 if (printRequestAttributeSetParam != null)
158                 {
159                     printRequestAttributeSet.addAll(printRequestAttributeSetParam);
160                 }
161         
162                 try
163                 {
164                     if (!printRequestAttributeSet.containsKey(MediaPrintableArea JavaDoc.class))
165                     {
166                         int printableWidth;
167                         int printableHeight;
168                         switch (jasperPrint.getOrientation())
169                         {
170                             case JRReport.ORIENTATION_LANDSCAPE:
171                                 printableWidth = jasperPrint.getPageHeight();
172                                 printableHeight = jasperPrint.getPageWidth();
173                                 break;
174                             default:
175                                 printableWidth = jasperPrint.getPageWidth();
176                                 printableHeight = jasperPrint.getPageHeight();
177                                 break;
178                         }
179                         
180                         printRequestAttributeSet.add(
181                             new MediaPrintableArea JavaDoc(
182                                 0f,
183                                 0f,
184                                 printableWidth / 72f,
185                                 printableHeight / 72f,
186                                 MediaPrintableArea.INCH
187                                 )
188                             );
189                     }
190
191                     if (!printRequestAttributeSet.containsKey(OrientationRequested JavaDoc.class))
192                     {
193                         OrientationRequested JavaDoc orientation;
194                         switch (jasperPrint.getOrientation())
195                         {
196                             case JRReport.ORIENTATION_LANDSCAPE:
197                                 orientation = OrientationRequested.LANDSCAPE;
198                                 break;
199                             default:
200                                 orientation = OrientationRequested.PORTRAIT;
201                                 break;
202                         }
203                         printRequestAttributeSet.add(orientation);
204                     }
205                     
206                     if (!isModeBatch)
207                     {
208                         printRequestAttributeSet.add(new PageRanges JavaDoc(startPageIndex + 1, endPageIndex + 1));
209                     }
210
211                     printerJob.setJobName("JasperReports - " + jasperPrint.getName());
212
213                     if (displayPageDialog)
214                     {
215                         printerJob.pageDialog(printRequestAttributeSet);
216                     }
217                     
218                     if (displayPrintDialog)
219                     {
220                         if (printerJob.printDialog(printRequestAttributeSet))
221                         {
222                             printerJob.print(printRequestAttributeSet);
223                         }
224                     }
225                     else
226                     {
227                         printerJob.print(printRequestAttributeSet);
228                     }
229                 }
230                 catch (PrinterException JavaDoc e)
231                 {
232                     throw new JRException(e);
233                 }
234             }
235         }
236         finally
237         {
238             resetExportContext();
239         }
240     }
241
242
243     /**
244      *
245      */

246     public int print(Graphics JavaDoc graphics, PageFormat JavaDoc pageFormat, int pageIndex) throws PrinterException JavaDoc
247     {
248         if (Thread.currentThread().isInterrupted())
249         {
250             throw new PrinterException JavaDoc("Current thread interrupted.");
251         }
252
253         if ( pageIndex < 0 || pageIndex >= jasperPrint.getPages().size() )
254         {
255             return Printable.NO_SUCH_PAGE;
256         }
257         
258         exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, graphics);
259         exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer JavaDoc(pageIndex));
260         
261         try
262         {
263             exporter.exportReport();
264         }
265         catch (JRException e)
266         {
267             throw new PrinterException JavaDoc(e.getMessage());
268         }
269
270         return Printable.PAGE_EXISTS;
271     }
272
273
274 }
275
Popular Tags