Path: blob/master/test/jdk/javax/print/StreamPrintingOrientation.java
41144 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 490423626* @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.27* @run main/manual StreamPrintingOrientation28*/2930import java.awt.*;31import java.awt.print.*;32import javax.print.*;33import javax.print.attribute.standard.*;34import javax.print.attribute.*;35import java.io.FileOutputStream;36import java.io.File;37import java.util.Locale;3839class StreamPrintingOrientation implements Printable {40/**41* Constructor42*/43public StreamPrintingOrientation() {44super();45}46/**47* Starts the application.48*/49public static void main(java.lang.String[] args) {50StreamPrintingOrientation pd = new StreamPrintingOrientation();51PrinterJob pj = PrinterJob.getPrinterJob();52HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet();53PrintService service = null;5455FileOutputStream fos = null;56File f = null, f1 = null;57String mType = "application/postscript";5859try {60f = new File("streamexample.ps");61fos = new FileOutputStream(f);62StreamPrintServiceFactory[] factories = PrinterJob.lookupStreamPrintServices(mType);63if (factories.length > 0)64service = factories[0].getPrintService(fos);6566if (service != null) {67System.out.println("Stream Print Service "+service);68pj.setPrintService(service);69} else {70throw new RuntimeException("No stream Print Service available.");71}72} catch (Exception e) {73e.printStackTrace();74}7576pj.setPrintable(pd);77prSet.add(OrientationRequested.LANDSCAPE);78prSet.add(new Copies(3));79prSet.add(new JobName("orientation test", null));80System.out.println("open PrintDialog..");81if (pj.printDialog(prSet)) {82try {83System.out.println("\nValues in attr set passed to print method");84Attribute attr[] = prSet.toArray();85for (int x = 0; x < attr.length; x ++) {86System.out.println("Name "+attr[x].getName()+" "+attr[x]);87}88System.out.println("About to print the data ...");89if (service != null) {90System.out.println("TEST: calling Print");91pj.print(prSet);92System.out.println("TEST: Printed");93}94}95catch (PrinterException pe) {96pe.printStackTrace();97}98}99100}101102//printable interface103public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {104105if (pi > 0) {106return Printable.NO_SUCH_PAGE;107}108// Simply draw two rectangles109Graphics2D g2 = (Graphics2D)g;110g2.setColor(Color.black);111g2.translate(pf.getImageableX(), pf.getImageableY());112System.out.println("StreamPrinting Test Width "+pf.getWidth()+" Height "+pf.getHeight());113g2.drawRect(1,1,200,300);114g2.drawRect(1,1,25,25);115return Printable.PAGE_EXISTS;116}117}118119120