1 /* JavaPrinterJob.java -- AWT printing implemented on javax.print.
2    Copyright (C) 2006  Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package gnu.java.awt.print;
40 
41 import java.awt.HeadlessException;
42 import java.awt.print.PageFormat;
43 import java.awt.print.Pageable;
44 import java.awt.print.Printable;
45 import java.awt.print.PrinterException;
46 import java.awt.print.PrinterJob;
47 import java.util.Locale;
48 
49 import javax.print.CancelablePrintJob;
50 import javax.print.DocFlavor;
51 import javax.print.DocPrintJob;
52 import javax.print.PrintException;
53 import javax.print.PrintService;
54 import javax.print.PrintServiceLookup;
55 import javax.print.ServiceUI;
56 import javax.print.attribute.HashPrintRequestAttributeSet;
57 import javax.print.attribute.IntegerSyntax;
58 import javax.print.attribute.PrintRequestAttributeSet;
59 import javax.print.attribute.TextSyntax;
60 import javax.print.attribute.standard.Copies;
61 import javax.print.attribute.standard.JobName;
62 import javax.print.attribute.standard.OrientationRequested;
63 import javax.print.attribute.standard.RequestingUserName;
64 
65 /**
66  * This is the default implementation of PrinterJob
67  *
68  * @author Sven de Marothy
69  */
70 public class JavaPrinterJob extends PrinterJob
71 {
72   /**
73    * The print service associated with this job
74    */
75   private PrintService printer = null;
76 
77   /**
78    * Printing options;
79    */
80   private PrintRequestAttributeSet attributes;
81 
82   /**
83    * Available print services
84    */
85   private static PrintService[] services;
86 
87   /**
88    * The actual print job.
89    */
90   private DocPrintJob printJob;
91 
92   /**
93    * The Printable object to print.
94    */
95   private Printable printable;
96 
97   /**
98    * Page format.
99    */
100   private PageFormat pageFormat;
101 
102   /**
103    * A pageable, or null
104    */
105   private Pageable pageable = null;
106 
107   /**
108    * Cancelled or not
109    */
110   private boolean cancelled = false;
111 
112   static
113   {
114     // lookup all services without any constraints
115     services = PrintServiceLookup.lookupPrintServices
116       (DocFlavor.INPUT_STREAM.POSTSCRIPT, null);
117   }
118 
119   private static final Class copyClass = (new Copies(1)).getClass();
120   private static final Class jobNameClass = (new JobName("", null)).getClass();
121   private static final Class userNameClass = (new RequestingUserName("", null)).getClass();
122 
123   /**
124    * Initializes a new instance of <code>PrinterJob</code>.
125    */
JavaPrinterJob()126   public JavaPrinterJob()
127   {
128     attributes = new HashPrintRequestAttributeSet();
129     setCopies(1);
130     setJobName("Java Printing");
131     pageFormat = new PageFormat(); // default page format.
132   }
133 
getPageAttributes()134   private void getPageAttributes()
135   {
136     OrientationRequested orientation = (OrientationRequested)
137       attributes.get( OrientationRequested.LANDSCAPE.getCategory() );
138     if( orientation == null)
139       return;
140 
141     if( orientation.equals(OrientationRequested.PORTRAIT) )
142       pageFormat.setOrientation(PageFormat.PORTRAIT);
143     else if( orientation.equals(OrientationRequested.LANDSCAPE) )
144       pageFormat.setOrientation(PageFormat.LANDSCAPE);
145     else if( orientation.equals(OrientationRequested.REVERSE_LANDSCAPE) )
146         pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
147   }
148 
149   /**
150    * Returns the number of copies to be printed.
151    *
152    * @return The number of copies to be printed.
153    */
getCopies()154   public int getCopies()
155   {
156     return ((IntegerSyntax)attributes.get( jobNameClass )).getValue();
157   }
158 
159   /**
160    * Sets the number of copies to be printed.
161    *
162    * @param copies The number of copies to be printed.
163    */
setCopies(int copies)164   public void setCopies(int copies)
165   {
166     attributes.add( new Copies( copies ) );
167   }
168 
169   /**
170    * Returns the name of the print job.
171    *
172    * @return The name of the print job.
173    */
getJobName()174   public String getJobName()
175   {
176     return ((TextSyntax)attributes.get( jobNameClass )).getValue();
177   }
178 
179   /**
180    * Sets the name of the print job.
181    *
182    * @param job_name The name of the print job.
183    */
setJobName(String job_name)184   public void setJobName(String job_name)
185   {
186     attributes.add( new JobName(job_name, Locale.getDefault()) );
187   }
188 
189   /**
190    * Returns the printing user name.
191    *
192    * @return The printing username.
193    */
getUserName()194   public String getUserName()
195   {
196     return ((TextSyntax)attributes.get( userNameClass )).getValue();
197   }
198 
199   /**
200    * Cancels an in progress print job.
201    */
cancel()202   public void cancel()
203   {
204     try
205       {
206         if(printJob != null && (printJob instanceof CancelablePrintJob))
207           {
208             ((CancelablePrintJob)printJob).cancel();
209             cancelled = true;
210           }
211       }
212     catch(PrintException pe)
213       {
214       }
215   }
216 
217   /**
218    * Tests whether or not this job has been cancelled.
219    *
220    * @return <code>true</code> if this job has been cancelled, <code>false</code>
221    * otherwise.
222    */
isCancelled()223   public boolean isCancelled()
224   {
225     return cancelled;
226   }
227 
228   /**
229    * Clones the specified <code>PageFormat</code> object then alters the
230    * clone so that it represents the default page format.
231    *
232    * @param page_format The <code>PageFormat</code> to clone.
233    *
234    * @return A new default page format.
235    */
defaultPage(PageFormat page_format)236   public PageFormat defaultPage(PageFormat page_format)
237   {
238     return new PageFormat();
239   }
240 
241   /**
242    * Displays a dialog box to the user which allows the page format
243    * attributes to be modified.
244    *
245    * @param page_format The <code>PageFormat</code> object to modify.
246    *
247    * @return The modified <code>PageFormat</code>.
248    */
pageDialog(PageFormat page_format)249   public PageFormat pageDialog(PageFormat page_format)
250     throws HeadlessException
251   {
252     return defaultPage(null);
253   }
254 
255   /**
256    * Prints the pages.
257    */
print()258   public void print() throws PrinterException
259   {
260     if( printable == null && pageable == null ) // nothing to print?
261       return;
262 
263     PostScriptGraphics2D pg = new PostScriptGraphics2D( this );
264     SpooledDocument doc = pg.spoolPostScript( printable, pageFormat,
265                                               pageable );
266 
267     cancelled = false;
268     printJob = printer.createPrintJob();
269     try
270       {
271         printJob.print(doc, attributes);
272       }
273     catch (PrintException pe)
274       {
275         PrinterException p = new PrinterException();
276         p.initCause(pe);
277         throw p;
278       }
279     // no printjob active.
280     printJob = null;
281   }
282 
283   /**
284    * Prints the page with given attributes.
285    */
print(PrintRequestAttributeSet attributes)286   public void print (PrintRequestAttributeSet attributes)
287     throws PrinterException
288   {
289     this.attributes = attributes;
290     print();
291   }
292 
293   /**
294    * Displays a dialog box to the user which allows the print job
295    * attributes to be modified.
296    *
297    * @return <code>false</code> if the user cancels the dialog box,
298    * <code>true</code> otherwise.
299    */
printDialog()300   public boolean printDialog() throws HeadlessException
301   {
302     return printDialog( attributes );
303   }
304 
305   /**
306    * Displays a dialog box to the user which allows the print job
307    * attributes to be modified.
308    *
309    * @return <code>false</code> if the user cancels the dialog box,
310    * <code>true</code> otherwise.
311    */
printDialog(PrintRequestAttributeSet attributes)312   public boolean printDialog(PrintRequestAttributeSet attributes)
313     throws HeadlessException
314   {
315     PrintService chosenPrinter = ServiceUI.printDialog
316       (null, 50, 50, services, null,
317        DocFlavor.INPUT_STREAM.POSTSCRIPT, attributes);
318 
319     getPageAttributes();
320 
321     if( chosenPrinter != null )
322       {
323         try
324           {
325             setPrintService( chosenPrinter );
326           }
327         catch(PrinterException pe)
328           {
329             // Should not happen.
330           }
331         return true;
332       }
333     return false;
334   }
335 
336   /**
337    * This sets the pages that are to be printed.
338    *
339    * @param pageable The pages to be printed, which may not be <code>null</code>.
340    */
setPageable(Pageable pageable)341   public void setPageable(Pageable pageable)
342   {
343     if( pageable == null )
344       throw new NullPointerException("Pageable cannot be null.");
345     this.pageable = pageable;
346   }
347 
348   /**
349    * Sets this specified <code>Printable</code> as the one to use for
350    * rendering the pages on the print device.
351    *
352    * @param printable The <code>Printable</code> for the print job.
353    */
setPrintable(Printable printable)354   public void setPrintable(Printable printable)
355   {
356     this.printable = printable;
357   }
358 
359   /**
360    * Sets the <code>Printable</code> and the page format for the pages
361    * to be printed.
362    *
363    * @param printable The <code>Printable</code> for the print job.
364    * @param page_format The <code>PageFormat</code> for the print job.
365    */
setPrintable(Printable printable, PageFormat page_format)366   public void setPrintable(Printable printable, PageFormat page_format)
367   {
368     this.printable = printable;
369     this.pageFormat = page_format;
370   }
371 
372   /**
373    * Makes any alterations to the specified <code>PageFormat</code>
374    * necessary to make it work with the current printer.  The alterations
375    * are made to a clone of the input object, which is then returned.
376    *
377    * @param page_format The <code>PageFormat</code> to validate.
378    *
379    * @return The validated <code>PageFormat</code>.
380    */
validatePage(PageFormat page_format)381   public PageFormat validatePage(PageFormat page_format)
382   {
383     // FIXME
384     return page_format;
385   }
386 
387   /**
388    * Change the printer for this print job to service.  Subclasses that
389    * support setting the print service override this method.  Throws
390    * PrinterException when the class doesn't support setting the printer,
391    * the service doesn't support Pageable or Printable interfaces for 2D
392    * print output.
393    * @param service The new printer to use.
394    * @throws PrinterException if service is not valid.
395    */
setPrintService(PrintService service)396   public void setPrintService(PrintService service)
397     throws PrinterException
398   {
399     if(!service.isDocFlavorSupported(DocFlavor.INPUT_STREAM.POSTSCRIPT))
400       throw new PrinterException("This printer service is not supported.");
401     printer = service;
402   }
403 }
404