|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
24 | 24 | /*
|
25 | 25 | * @test
|
26 | 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 |
| 27 | + * @key printer |
| 28 | + * @summary StreamPrintService ignores the PrintReqAttrSet when printing through 2D Printing |
| 29 | + * @run main StreamPrintingOrientation |
29 | 30 | */
|
30 | 31 |
|
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 | 32 | import java.io.File;
|
38 |
| -import java.util.Locale; |
| 33 | +import java.io.FileOutputStream; |
| 34 | +import java.nio.file.Files; |
| 35 | + |
| 36 | +import java.awt.Color; |
| 37 | +import java.awt.Graphics; |
| 38 | +import java.awt.Graphics2D; |
| 39 | +import java.awt.print.PageFormat; |
| 40 | +import java.awt.print.Printable; |
| 41 | +import java.awt.print.PrinterException; |
| 42 | +import java.awt.print.PrinterJob; |
| 43 | +import javax.print.attribute.Attribute; |
| 44 | +import javax.print.PrintService; |
| 45 | +import javax.print.StreamPrintServiceFactory; |
| 46 | +import javax.print.attribute.standard.Copies; |
| 47 | +import javax.print.attribute.standard.JobName; |
| 48 | +import javax.print.attribute.standard.OrientationRequested; |
| 49 | +import javax.print.attribute.HashPrintRequestAttributeSet; |
| 50 | + |
| 51 | +public class StreamPrintingOrientation implements Printable { |
| 52 | + |
| 53 | + public static void main(String[] args) throws Exception { |
| 54 | + StreamPrintingOrientation pd = new StreamPrintingOrientation(); |
| 55 | + PrinterJob pj = PrinterJob.getPrinterJob(); |
| 56 | + HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet(); |
| 57 | + PrintService service = null; |
| 58 | + |
| 59 | + FileOutputStream fos = null; |
| 60 | + String mType = "application/postscript"; |
39 | 61 |
|
40 |
| -class StreamPrintingOrientation implements Printable { |
41 |
| - /** |
42 |
| - * Constructor |
43 |
| - */ |
44 |
| - public StreamPrintingOrientation() { |
45 |
| - super(); |
| 62 | + File fl = new File("stream_landscape.ps"); |
| 63 | + fl.deleteOnExit(); |
| 64 | + fos = new FileOutputStream(fl); |
| 65 | + StreamPrintServiceFactory[] factories = PrinterJob.lookupStreamPrintServices(mType); |
| 66 | + if (factories.length > 0) { |
| 67 | + service = factories[0].getPrintService(fos); |
46 | 68 | }
|
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 | 69 |
|
56 |
| - FileOutputStream fos = null; |
57 |
| - File f = null, f1 = null; |
58 |
| - String mType = "application/postscript"; |
| 70 | + if (service != null) { |
| 71 | + System.out.println("Stream Print Service " + service); |
| 72 | + pj.setPrintService(service); |
| 73 | + } else { |
| 74 | + throw new RuntimeException("No stream Print Service available."); |
| 75 | + } |
| 76 | + |
| 77 | + pj.setPrintable(pd); |
| 78 | + prSet.add(OrientationRequested.LANDSCAPE); |
| 79 | + prSet.add(new Copies(1)); |
| 80 | + prSet.add(new JobName("orientation test", null)); |
| 81 | + System.out.println("open PrintDialog.."); |
59 | 82 |
|
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); |
| 83 | + System.out.println("\nValues in attr set passed to print method"); |
| 84 | + Attribute attr[] = prSet.toArray(); |
| 85 | + for (int x = 0; x < attr.length; x++) { |
| 86 | + System.out.println("Name " + attr[x].getName() + " " + attr[x]); |
| 87 | + } |
| 88 | + System.out.println("About to print the data ..."); |
| 89 | + if (service != null) { |
| 90 | + System.out.println("TEST: calling Print"); |
| 91 | + pj.print(prSet); |
| 92 | + System.out.println("TEST: Printed"); |
| 93 | + } |
66 | 94 |
|
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 |
| - } |
| 95 | + File fp = new File("stream_portrait.ps"); |
| 96 | + fp.deleteOnExit(); |
| 97 | + fos = new FileOutputStream(fp); |
| 98 | + if (factories.length > 0) { |
| 99 | + service = factories[0].getPrintService(fos); |
| 100 | + } |
76 | 101 |
|
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 |
| - } |
| 102 | + pj.setPrintService(service); |
| 103 | + pj.setPrintable(pd); |
| 104 | + prSet.add(OrientationRequested.PORTRAIT); |
| 105 | + prSet.add(new Copies(1)); |
| 106 | + prSet.add(new JobName("orientation test", null)); |
| 107 | + if (service != null) { |
| 108 | + pj.print(prSet); |
| 109 | + } |
100 | 110 |
|
| 111 | + if (Files.mismatch(fl.toPath(), fp.toPath()) == -1) { |
| 112 | + throw new RuntimeException("Printing stream orientation is same " + |
| 113 | + "for both PORTRAIT and LANDSCAPE"); |
101 | 114 | }
|
| 115 | + } |
102 | 116 |
|
103 |
| - //printable interface |
104 |
| - public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { |
| 117 | + //printable interface |
| 118 | + public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { |
105 | 119 |
|
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; |
| 120 | + if (pi > 0) { |
| 121 | + return Printable.NO_SUCH_PAGE; |
117 | 122 | }
|
| 123 | + // Simply draw two rectangles |
| 124 | + Graphics2D g2 = (Graphics2D) g; |
| 125 | + g2.setColor(Color.black); |
| 126 | + g2.translate(pf.getImageableX(), pf.getImageableY()); |
| 127 | + System.out.println("StreamPrinting Test Width " + pf.getWidth() + " Height " + pf.getHeight()); |
| 128 | + g2.drawRect(1, 1, 200, 300); |
| 129 | + g2.drawRect(1, 1, 25, 25); |
| 130 | + return Printable.PAGE_EXISTS; |
| 131 | + } |
118 | 132 | }
|
0 commit comments