Skip to content

Commit ea7c47c

Browse files
committed
7179006: [macosx] Print-to-file doesn't work: printing to the default printer instead.
Reviewed-by: serb
1 parent b66fa8f commit ea7c47c

File tree

4 files changed

+189
-69
lines changed

4 files changed

+189
-69
lines changed

src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java

+17
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
importjava.awt.geom.Rectangle2D;
3131
importjava.awt.image.BufferedImage;
3232
importjava.awt.print.*;
33+
importjava.net.URI;
3334
importjava.security.AccessController;
3435
importjava.security.PrivilegedAction;
3536

3637
importjavax.print.*;
3738
importjavax.print.attribute.PrintRequestAttributeSet;
3839
importjavax.print.attribute.HashPrintRequestAttributeSet;
3940
importjavax.print.attribute.standard.Copies;
41+
importjavax.print.attribute.standard.Destination;
4042
importjavax.print.attribute.standard.Media;
4143
importjavax.print.attribute.standard.MediaPrintableArea;
4244
importjavax.print.attribute.standard.MediaSize;
@@ -251,6 +253,21 @@ private void setPrintToFile(boolean printToFile) {
251253
isPrintToFile = printToFile;
252254
}
253255

256+
privatevoidsetDestinationFile(Stringdest) {
257+
if (attributes != null && dest != null) {
258+
try {
259+
URIdestURI = newURI(dest);
260+
attributes.add(newDestination(destURI));
261+
destinationAttr = "" + destURI.getSchemeSpecificPart();
262+
} catch (Exceptione) {
263+
}
264+
}
265+
}
266+
267+
privateStringgetDestinationFile() {
268+
returndestinationAttr;
269+
}
270+
254271
@Override
255272
publicvoidprint(PrintRequestAttributeSetattributes) throwsPrinterException {
256273
// NOTE: Some of this code is copied from RasterPrinterJob.

src/java.desktop/macosx/native/libawt_lwawt/awt/CPrinterJob.m

+23-7
Original file line numberDiff line numberDiff line change
@@ -316,21 +316,25 @@ static void nsPrintInfoToJavaPrinterJob(JNIEnv* env, NSPrintInfo* src, jobject d
316316
staticJNF_MEMBER_CACHE(jm_setCollated, sjc_CPrinterJob, "setCollated", "(Z)V");
317317
staticJNF_MEMBER_CACHE(jm_setPageRangeAttribute, sjc_CPrinterJob, "setPageRangeAttribute", "(IIZ)V");
318318
staticJNF_MEMBER_CACHE(jm_setPrintToFile, sjc_CPrinterJob, "setPrintToFile", "(Z)V");
319-
320-
if (src.jobDisposition == NSPrintSaveJob) {
321-
JNFCallVoidMethod(env, dstPrinterJob, jm_setPrintToFile, true);
322-
} else {
323-
JNFCallVoidMethod(env, dstPrinterJob, jm_setPrintToFile, false);
324-
}
319+
staticJNF_MEMBER_CACHE(jm_setDestinationFile, sjc_CPrinterJob, "setDestinationFile", "(Ljava/lang/String;)V");
325320

326321
// get the selected printer's name, and set the appropriate PrintService on the Java side
327322
NSString *name = [[src printer] name];
328323
jstring printerName = JNFNSToJavaString(env, name);
329324
JNFCallVoidMethod(env, dstPrinterJob, jm_setService, printerName);
330325

331-
332326
NSMutableDictionary* printingDictionary = [src dictionary];
333327

328+
if (src.jobDisposition == NSPrintSaveJob) {
329+
JNFCallVoidMethod(env, dstPrinterJob, jm_setPrintToFile, true);
330+
NSURL *url = [printingDictionary objectForKey:NSPrintJobSavingURL];
331+
NSString *nsStr = [url absoluteString];
332+
jstring str = JNFNSToJavaString(env, nsStr);
333+
JNFCallVoidMethod(env, dstPrinterJob, jm_setDestinationFile, str);
334+
} else {
335+
JNFCallVoidMethod(env, dstPrinterJob, jm_setPrintToFile, false);
336+
}
337+
334338
NSNumber* nsCopies = [printingDictionary objectForKey:NSPrintCopies];
335339
if ([nsCopies respondsToSelector:@selector(integerValue)])
336340
{
@@ -384,6 +388,8 @@ static void javaPrinterJobToNSPrintInfo(JNIEnv* env, jobject srcPrinterJob, jobj
384388
staticJNF_MEMBER_CACHE(jm_getSelectAttrib, sjc_CPrinterJob, "getSelectAttrib", "()I");
385389
staticJNF_MEMBER_CACHE(jm_getNumberOfPages, jc_Pageable, "getNumberOfPages", "()I");
386390
staticJNF_MEMBER_CACHE(jm_getPageFormat, sjc_CPrinterJob, "getPageFormatFromAttributes", "()Ljava/awt/print/PageFormat;");
391+
staticJNF_MEMBER_CACHE(jm_getDestinationFile, sjc_CPrinterJob,
392+
"getDestinationFile", "()Ljava/lang/String;");
387393

388394
NSMutableDictionary* printingDictionary = [dst dictionary];
389395

@@ -423,6 +429,16 @@ static void javaPrinterJobToNSPrintInfo(JNIEnv* env, jobject srcPrinterJob, jobj
423429
if (page != NULL) {
424430
javaPageFormatToNSPrintInfo(env, NULL, page, dst);
425431
}
432+
433+
jstring dest = JNFCallObjectMethod(env, srcPrinterJob, jm_getDestinationFile);
434+
if (dest != NULL) {
435+
[dst setJobDisposition:NSPrintSaveJob];
436+
NSString *nsDestStr = JNFJavaToNSString(env, dest);
437+
NSURL *nsURL = [NSURLfileURLWithPath:nsDestStr isDirectory:NO];
438+
[printingDictionary setObject:nsURL forKey:NSPrintJobSavingURL];
439+
} else {
440+
[dst setJobDisposition:NSPrintSpoolJob];
441+
}
426442
}
427443

428444
/*
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,76 +23,86 @@
2323

2424
/*
2525
@test
26-
@bug 4865976 7158366
27-
@summary Pass if it program exits.
26+
@key printer
27+
@bug 4865976 7158366 7179006
28+
@summary Pass if program exits.
2829
@run main/manual PrintDlgApp
2930
*/
30-
importjava.awt.*;
31-
importjava.awt.print.*;
32-
importjavax.print.attribute.*;
31+
32+
importjava.io.File;
33+
importjava.net.URI;
34+
importjava.awt.Color;
35+
importjava.awt.Graphics;
36+
importjava.awt.Graphics2D;
37+
importjava.awt.print.PageFormat;
38+
importjava.awt.print.Printable;
39+
importjava.awt.print.PrinterJob;
40+
importjava.awt.print.PrinterException;
41+
importjavax.print.attribute.HashPrintRequestAttributeSet;
42+
importjavax.print.attribute.PrintRequestAttributeSet;
3343
importjavax.print.attribute.standard.Copies;
3444
importjavax.print.attribute.standard.Destination;
35-
importjava.util.Locale;
3645

37-
importjavax.print.*;
46+
publicclassPrintDlgAppimplementsPrintable {
3847

39-
classPrintDlgAppimplementsPrintable {
40-
/**
41-
* Constructor
42-
*/
43-
publicPrintDlgApp() {
44-
super();
45-
}
46-
/**
47-
* Starts the application.
48-
*/
49-
publicstaticvoidmain(java.lang.String[] args) {
50-
PrintDlgApppd = newPrintDlgApp();
51-
PrinterJobpj = PrinterJob.getPrinterJob();
52-
System.out.println(pj);
53-
PrintRequestAttributeSetpSet = newHashPrintRequestAttributeSet();
54-
pSet.add(newCopies(1));
55-
//PageFormat pf = pj.pageDialog(pSet);
56-
PageFormatpf = newPageFormat();
57-
System.out.println("Setting Printable...pf = "+pf);
58-
if (pf == null) {
59-
return;
60-
}
61-
pj.setPrintable(pd,pf);
48+
publicPrintDlgApp() {}
6249

63-
//try { pj.setPrintService(services[0]); } catch(Exception e) { e.printStackTrace(); }
64-
pSet.add(newDestination(newjava.io.File("./out.prn").toURI()));
65-
System.out.println("open PrintDialog..");
66-
for (inti=0; i<2; i++) {
67-
if (pj.printDialog(pSet)) {
68-
try {
69-
System.out.println("About to print the data ...");
70-
pj.print(pSet);
71-
System.out.println("Printed");
72-
}
73-
catch (PrinterExceptionpe) {
74-
pe.printStackTrace();
75-
}
76-
}
77-
}
50+
publicstaticvoidmain(String[] args) {
51+
PrinterJobpj = PrinterJob.getPrinterJob();
52+
if (pj.getPrintService() == null) {
53+
System.out.println("No printers installed. Skipping test");
54+
return;
55+
}
7856

79-
}
57+
PrintDlgApppd = newPrintDlgApp();
58+
PageFormatpf = newPageFormat();
59+
pj.setPrintable(pd, pf);
60+
61+
PrintRequestAttributeSetpSet = newHashPrintRequestAttributeSet();
62+
pSet.add(newCopies(1));
63+
64+
Destinationdest = null;
65+
for (inti=0; i<2; i++) {
66+
Filefile = newFile("./out"+i+".prn");
67+
dest = newDestination(file.toURI());
68+
pSet.add(dest);
69+
System.out.println("open PrintDialog.");
70+
if (pj.printDialog(pSet)) {
71+
// In case tester changes the destination :
72+
dest = (Destination)pSet.get(Destination.class);
73+
System.out.println("DEST="+dest);
74+
if (dest != null) {
75+
URIuri = dest.getURI();
76+
file = newFile(uri.getSchemeSpecificPart());
77+
System.out.println("will be checking for file " + file);
78+
}
79+
try {
80+
System.out.println("About to print the data ...");
81+
pj.print(pSet);
82+
System.out.println("Printed.");
83+
}
84+
catch (PrinterExceptionpe) {
85+
pe.printStackTrace();
86+
}
87+
}
88+
if (dest != null && !file.exists()) {
89+
thrownewRuntimeException("No file created");
90+
}
91+
}
92+
}
8093

81-
//printable interface
82-
publicintprint(Graphicsg, PageFormatpf, intpi) throws
83-
PrinterException {
94+
publicintprint(Graphicsg, PageFormatpf, intpi) throwsPrinterException {
8495

85-
if (pi > 0) {
86-
System.out.println("pi is greater than 0");
87-
returnPrintable.NO_SUCH_PAGE;
88-
}
89-
// Simply draw two rectangles
90-
Graphics2Dg2 = (Graphics2D)g;
91-
g2.setColor(Color.black);
92-
g2.translate(pf.getImageableX(), pf.getImageableY());
93-
g2.drawRect(1,1,200,300);
94-
g2.drawRect(1,1,25,25);
95-
System.out.println("print method called "+pi);
96-
returnPrintable.PAGE_EXISTS;
96+
if (pi > 0) {
97+
returnPrintable.NO_SUCH_PAGE;
9798
}
99+
// Simply draw two rectangles
100+
Graphics2Dg2 = (Graphics2D)g;
101+
g2.setColor(Color.black);
102+
g2.translate(pf.getImageableX(), pf.getImageableY());
103+
g2.drawRect(1,1,200,300);
104+
g2.drawRect(1,1,25,25);
105+
System.out.println("print method called "+pi);
106+
returnPrintable.PAGE_EXISTS;
107+
}
98108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2020, 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+
* @key printer
27+
* @bug 7179006
28+
* @summary Confirm printing to file works.
29+
*/
30+
31+
importjava.io.File;
32+
importjava.net.URI;
33+
importjava.awt.Color;
34+
importjava.awt.Graphics;
35+
importjava.awt.Graphics2D;
36+
importjava.awt.print.PageFormat;
37+
importjava.awt.print.Printable;
38+
importjava.awt.print.PrinterJob;
39+
importjava.awt.print.PrinterException;
40+
importjavax.print.attribute.HashPrintRequestAttributeSet;
41+
importjavax.print.attribute.PrintRequestAttributeSet;
42+
importjavax.print.attribute.standard.Destination;
43+
44+
publicclassPrintToFileTestimplementsPrintable {
45+
46+
publicPrintToFileTest() {}
47+
48+
publicstaticvoidmain(String[] args) throwsException {
49+
PrinterJobpj = PrinterJob.getPrinterJob();
50+
if (pj.getPrintService() == null) {
51+
System.out.println("No printers installed. Skipping test.");
52+
return;
53+
}
54+
pj.setPrintable(newPrintToFileTest(), newPageFormat());
55+
PrintRequestAttributeSetpSet = newHashPrintRequestAttributeSet();
56+
Filefile = newFile("./out.prn");
57+
pSet.add(newDestination(file.toURI()));
58+
pj.print(pSet);
59+
if (!file.exists()) {
60+
thrownewRuntimeException("No file created");
61+
}
62+
}
63+
64+
publicintprint(Graphicsg, PageFormatpf, intpi) throws
65+
PrinterException {
66+
67+
if (pi > 0) {
68+
returnPrintable.NO_SUCH_PAGE;
69+
}
70+
Graphics2Dg2 = (Graphics2D)g;
71+
g2.setColor(Color.black);
72+
g2.translate(pf.getImageableX(), pf.getImageableY());
73+
g2.drawRect(1,1,200,300);
74+
g2.drawRect(1,1,25,25);
75+
returnPrintable.PAGE_EXISTS;
76+
}
77+
}

0 commit comments

Comments
 (0)
close