1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4904236
  27  * @summary You would see a cross-platform print dialog being popped up. Check whether orientation is shown as LANDSCAPE. Click 'OK'. 'streamexample.ps' will be created in the same dir where this application was executed. Pass if the orientation in the ps file is landscape.
  28  * @run main/manual StreamPrintingOrientation
  29  */
  30 
  31 import java.awt.*;
  32 import java.awt.print.*;
  33 import javax.print.*;
  34 import javax.print.attribute.standard.*;
  35 import javax.print.attribute.*;
  36 import java.io.FileOutputStream;
  37 import java.io.File;
  38 import java.util.Locale;
  39 
  40 class StreamPrintingOrientation implements Printable {
  41         /**
  42          * Constructor
  43          */
  44          public StreamPrintingOrientation() {
  45                 super();
  46         }
  47         /**
  48          * Starts the application.
  49          */
  50         public static void main(java.lang.String[] args) {
  51                 StreamPrintingOrientation pd = new StreamPrintingOrientation();
  52                 PrinterJob pj = PrinterJob.getPrinterJob();
  53                 HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet();
  54                 PrintService service = null;
  55 
  56                 FileOutputStream fos = null;
  57                 File f = null, f1 = null;
  58                 String mType = "application/postscript";
  59 
  60                 try {
  61                         f = new File("streamexample.ps");
  62                         fos = new FileOutputStream(f);
  63                         StreamPrintServiceFactory[] factories = PrinterJob.lookupStreamPrintServices(mType);
  64                         if (factories.length > 0)
  65                                 service = factories[0].getPrintService(fos);
  66 
  67                         if (service != null) {
  68                                 System.out.println("Stream Print Service "+service);
  69                                 pj.setPrintService(service);
  70                         } else {
  71                                 throw new RuntimeException("No stream Print Service available.");
  72                         }
  73                 } catch (Exception e) {
  74                         e.printStackTrace();
  75                 }
  76 
  77                 pj.setPrintable(pd);
  78                 prSet.add(OrientationRequested.LANDSCAPE);
  79                 prSet.add(new Copies(3));
  80                 prSet.add(new JobName("orientation test", null));
  81                 System.out.println("open PrintDialog..");
  82                 if (pj.printDialog(prSet)) {
  83                         try {
  84                                 System.out.println("\nValues in attr set passed to print method");
  85                                 Attribute attr[] = prSet.toArray();
  86                                 for (int x = 0; x < attr.length; x ++) {
  87                                         System.out.println("Name "+attr[x].getName()+"  "+attr[x]);
  88                                 }
  89                                 System.out.println("About to print the data ...");
  90                                 if (service != null) {
  91                                         System.out.println("TEST: calling Print");
  92                                         pj.print(prSet);
  93                                         System.out.println("TEST: Printed");
  94                                 }
  95                         }
  96                         catch (PrinterException pe) {
  97                                 pe.printStackTrace();
  98                         }
  99                 }
 100 
 101         }
 102 
 103         //printable interface
 104         public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
 105 
 106                 if (pi > 0) {
 107                         return Printable.NO_SUCH_PAGE;
 108                 }
 109                 // Simply draw two rectangles
 110                 Graphics2D g2 = (Graphics2D)g;
 111                 g2.setColor(Color.black);
 112                 g2.translate(pf.getImageableX(), pf.getImageableY());
 113                 System.out.println("StreamPrinting Test Width "+pf.getWidth()+" Height "+pf.getHeight());
 114                 g2.drawRect(1,1,200,300);
 115                 g2.drawRect(1,1,25,25);
 116                 return Printable.PAGE_EXISTS;
 117         }
 118 }