001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * 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,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.export;
029:
030: import java.awt.Graphics;
031: import java.awt.print.PageFormat;
032: import java.awt.print.Printable;
033: import java.awt.print.PrinterException;
034: import java.awt.print.PrinterJob;
035:
036: import javax.print.PrintService;
037: import javax.print.PrintServiceLookup;
038: import javax.print.attribute.Attribute;
039: import javax.print.attribute.HashPrintRequestAttributeSet;
040: import javax.print.attribute.HashPrintServiceAttributeSet;
041: import javax.print.attribute.PrintRequestAttributeSet;
042: import javax.print.attribute.PrintServiceAttributeSet;
043: import javax.print.attribute.standard.MediaPrintableArea;
044: import javax.print.attribute.standard.OrientationRequested;
045: import javax.print.attribute.standard.PageRanges;
046: import javax.print.attribute.standard.PrinterIsAcceptingJobs;
047:
048: import net.sf.jasperreports.engine.JRAbstractExporter;
049: import net.sf.jasperreports.engine.JRException;
050: import net.sf.jasperreports.engine.JRExporterParameter;
051: import net.sf.jasperreports.engine.JRReport;
052: import net.sf.jasperreports.engine.JasperPrint;
053: import net.sf.jasperreports.engine.print.JRPrinterAWT;
054:
055: /**
056: * @author Teodor Danciu (teodord@users.sourceforge.net)
057: * @version $Id: JRPrintServiceExporter.java 1732 2007-05-31 14:57:03Z teodord $
058: */
059: public class JRPrintServiceExporter extends JRAbstractExporter
060: implements Printable {
061:
062: /**
063: *
064: */
065: protected JRGraphics2DExporter exporter = null;
066: protected boolean displayPageDialog = false;
067: protected boolean displayPageDialogOnlyOnce = false;
068: protected boolean displayPrintDialog = false;
069: protected boolean displayPrintDialogOnlyOnce = false;
070:
071: protected int reportIndex = 0;
072:
073: private PrintService printService = null;
074:
075: /**
076: *
077: */
078: public void exportReport() throws JRException {
079: /* */
080: setOffset();
081:
082: try {
083: /* */
084: setExportContext();
085:
086: /* */
087: setInput();
088:
089: /* */
090: if (!isModeBatch) {
091: setPageRange();
092: }
093:
094: PrintServiceAttributeSet printServiceAttributeSet = (PrintServiceAttributeSet) parameters
095: .get(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET);
096: if (printServiceAttributeSet == null) {
097: printServiceAttributeSet = new HashPrintServiceAttributeSet();
098: }
099:
100: Boolean pageDialog = (Boolean) parameters
101: .get(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG);
102: if (pageDialog != null) {
103: displayPageDialog = pageDialog.booleanValue();
104: }
105:
106: Boolean pageDialogOnlyOnce = (Boolean) parameters
107: .get(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG_ONLY_ONCE);
108: if (displayPageDialog && pageDialogOnlyOnce != null) {
109: // it can be (eventually) set to true only if displayPageDialog is true
110: displayPageDialogOnlyOnce = pageDialogOnlyOnce
111: .booleanValue();
112: }
113:
114: Boolean printDialog = (Boolean) parameters
115: .get(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG);
116: if (printDialog != null) {
117: displayPrintDialog = printDialog.booleanValue();
118: }
119:
120: Boolean printDialogOnlyOnce = (Boolean) parameters
121: .get(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG_ONLY_ONCE);
122: if (displayPrintDialog && printDialogOnlyOnce != null) {
123: // it can be (eventually) set to true only if displayPrintDialog is true
124: displayPrintDialogOnlyOnce = printDialogOnlyOnce
125: .booleanValue();
126: }
127: PrinterJob printerJob = PrinterJob.getPrinterJob();
128:
129: JRPrinterAWT.initPrinterJobFields(printerJob);
130:
131: printerJob.setPrintable(this );
132:
133: // determining the print service only once
134: printService = (PrintService) parameters
135: .get(JRPrintServiceExporterParameter.PRINT_SERVICE);
136: if (printService == null) {
137: PrintService[] services = PrintServiceLookup
138: .lookupPrintServices(null,
139: printServiceAttributeSet);
140: if (services.length > 0)
141: printService = services[0];
142: }
143:
144: if (printService == null) {
145: throw new JRException(
146: "No suitable print service found.");
147: }
148:
149: try {
150: printerJob.setPrintService(printService);
151: } catch (PrinterException e) {
152: throw new JRException(e);
153: }
154:
155: PrintRequestAttributeSet printRequestAttributeSet = null;
156: if (displayPrintDialogOnlyOnce || displayPageDialogOnlyOnce) {
157: printRequestAttributeSet = new HashPrintRequestAttributeSet();
158: setDefaultPrintRequestAttributeSet(printRequestAttributeSet);
159: setOrientation((JasperPrint) jasperPrintList.get(0),
160: printRequestAttributeSet);
161: if (displayPageDialogOnlyOnce) {
162: if (printerJob.pageDialog(printRequestAttributeSet) == null)
163: return;
164: else
165: displayPageDialog = false;
166: }
167: if (displayPrintDialogOnlyOnce) {
168: if (!printerJob
169: .printDialog(printRequestAttributeSet))
170: return;
171: else
172: displayPrintDialog = false;
173: }
174: }
175:
176: // fix for bug ID artf1455 from jasperforge.org bug database
177: for (reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++) {
178: jasperPrint = (JasperPrint) jasperPrintList
179: .get(reportIndex);
180:
181: exporter = new JRGraphics2DExporter();
182: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
183: jasperPrint);
184: exporter
185: .setParameter(
186: JRExporterParameter.PROGRESS_MONITOR,
187: parameters
188: .get(JRExporterParameter.PROGRESS_MONITOR));
189: exporter.setParameter(JRExporterParameter.OFFSET_X,
190: parameters.get(JRExporterParameter.OFFSET_X));
191: exporter.setParameter(JRExporterParameter.OFFSET_Y,
192: parameters.get(JRExporterParameter.OFFSET_Y));
193: exporter.setParameter(JRExporterParameter.CLASS_LOADER,
194: classLoader);
195: exporter.setParameter(
196: JRExporterParameter.URL_HANDLER_FACTORY,
197: urlHandlerFactory);
198: exporter
199: .setParameter(
200: JRGraphics2DExporterParameter.MINIMIZE_PRINTER_JOB_SIZE,
201: parameters
202: .get(JRGraphics2DExporterParameter.MINIMIZE_PRINTER_JOB_SIZE));
203:
204: if (displayPrintDialog
205: || displayPageDialog
206: || (!displayPrintDialogOnlyOnce && !displayPageDialogOnlyOnce)) {
207: printRequestAttributeSet = new HashPrintRequestAttributeSet();
208: setDefaultPrintRequestAttributeSet(printRequestAttributeSet);
209: setOrientation(jasperPrint,
210: printRequestAttributeSet);
211: }
212:
213: try {
214:
215: if (!isModeBatch) {
216: printRequestAttributeSet.add(new PageRanges(
217: startPageIndex + 1, endPageIndex + 1));
218: }
219:
220: printerJob.setJobName("JasperReports - "
221: + jasperPrint.getName());
222:
223: if (displayPageDialog) {
224: printerJob.pageDialog(printRequestAttributeSet);
225: }
226: if (displayPrintDialog) {
227: if (printerJob
228: .printDialog(printRequestAttributeSet)) {
229: printerJob.print(printRequestAttributeSet);
230: }
231: } else {
232: printerJob.print(printRequestAttributeSet);
233: }
234: } catch (PrinterException e) {
235: throw new JRException(e);
236: }
237: }
238: } finally {
239: resetExportContext();
240: }
241: }
242:
243: /**
244: *
245: */
246: public int print(Graphics graphics, PageFormat pageFormat,
247: int pageIndex) throws PrinterException {
248: if (Thread.currentThread().isInterrupted()) {
249: throw new PrinterException("Current thread interrupted.");
250: }
251:
252: if (pageIndex < 0 || pageIndex >= jasperPrint.getPages().size()) {
253: return Printable.NO_SUCH_PAGE;
254: }
255:
256: exporter.setParameter(
257: JRGraphics2DExporterParameter.GRAPHICS_2D, graphics);
258: exporter.setParameter(JRExporterParameter.PAGE_INDEX,
259: new Integer(pageIndex));
260:
261: try {
262: exporter.exportReport();
263: } catch (JRException e) {
264: throw new PrinterException(e.getMessage());
265: }
266:
267: return Printable.PAGE_EXISTS;
268: }
269:
270: private void setOrientation(JasperPrint jPrint,
271: PrintRequestAttributeSet printRequestAttributeSet) {
272: if (!printRequestAttributeSet
273: .containsKey(MediaPrintableArea.class)) {
274: int printableWidth;
275: int printableHeight;
276: switch (jPrint.getOrientation()) {
277: case JRReport.ORIENTATION_LANDSCAPE:
278: printableWidth = jPrint.getPageHeight();
279: printableHeight = jPrint.getPageWidth();
280: break;
281: default:
282: printableWidth = jPrint.getPageWidth();
283: printableHeight = jPrint.getPageHeight();
284: break;
285: }
286:
287: printRequestAttributeSet.add(new MediaPrintableArea(0f, 0f,
288: printableWidth / 72f, printableHeight / 72f,
289: MediaPrintableArea.INCH));
290: }
291:
292: if (!printRequestAttributeSet
293: .containsKey(OrientationRequested.class)) {
294: OrientationRequested orientation;
295: switch (jPrint.getOrientation()) {
296: case JRReport.ORIENTATION_LANDSCAPE:
297: orientation = OrientationRequested.LANDSCAPE;
298: break;
299: default:
300: orientation = OrientationRequested.PORTRAIT;
301: break;
302: }
303: printRequestAttributeSet.add(orientation);
304: }
305:
306: }
307:
308: private void setDefaultPrintRequestAttributeSet(
309: PrintRequestAttributeSet printRequestAttributeSet) {
310: PrintRequestAttributeSet printRequestAttributeSetParam = (PrintRequestAttributeSet) parameters
311: .get(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET);
312: if (printRequestAttributeSetParam != null) {
313: printRequestAttributeSet
314: .addAll(printRequestAttributeSetParam);
315: }
316: }
317:
318: // artf1936
319: public static boolean checkAvailablePrinters() {
320: PrintService[] ss = java.awt.print.PrinterJob
321: .lookupPrintServices();
322: for (int i = 0; i < ss.length; i++) {
323: Attribute[] att = ss[i].getAttributes().toArray();
324: for (int j = 0; j < att.length; j++) {
325: if (att[j]
326: .equals(PrinterIsAcceptingJobs.ACCEPTING_JOBS)) {
327: return true;
328: }
329: }
330: }
331: return false;
332: }
333:
334: /**
335: * Returns the {@link PrintService} instance used by the exporter last time the exportReport() method was run.
336: */
337: public PrintService getPrintService() {
338: return printService;
339: }
340:
341: }
|