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