Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8223717: javafx printing: Support Specifying Print to File in the API #543

Closed
wants to merge 10 commits into from
Closed
Expand Up @@ -69,10 +69,8 @@
import java.awt.print.Pageable;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.net.MalformedURLException;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Set;
import com.sun.glass.ui.Application;
Expand Down Expand Up @@ -347,10 +345,7 @@ private void updateOutputFile() {
Destination dest =
(Destination)printReqAttrSet.get(Destination.class);
if (dest != null) {
try {
settings.setOutputFile(dest.getURI().toURL().toString());
} catch (MalformedURLException e) {
}
settings.setOutputFile(dest.getURI().getPath());
} else {
settings.setOutputFile("");
}
prrace marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -631,16 +626,10 @@ private void syncOutputFile() {
printReqAttrSet.remove(Destination.class);
String file = settings.getOutputFile();
if (file != null && !file.isEmpty()) {
try {
URL url = new URL(file);
if (!"file".equals(url.getProtocol())) {
return;
}
URI uri = url.toURI();
Destination d = new Destination(uri);
printReqAttrSet.add(d);
} catch (MalformedURLException | URISyntaxException e) {
}
// check SE, check access ?
URI uri = (new File(file)).toURI();
Destination d = new Destination(uri);
printReqAttrSet.add(d);
}
}

Expand Down Expand Up @@ -836,11 +825,8 @@ private void checkPermissions() {
if (security != null) {
security.checkPrintJobAccess();
String file = settings.getOutputFile();
if (!file.isEmpty()) {
try {
security.checkWrite((new URL(file)).getPath());
} catch (MalformedURLException e) {
}
if (file != null && !file.isEmpty()) {
security.checkWrite(file);
}
}
}
Expand Down
54 changes: 27 additions & 27 deletions modules/javafx.graphics/src/main/java/javafx/print/JobSettings.java
Expand Up @@ -464,49 +464,59 @@ public void setJobName(String name) {

/////////////////////// START OUTPUTFILE /////////////////////

private static final String DEFAULT_OUTPUTFILE = "";
private SimpleStringProperty outputFile;

/**
* A {@code StringProperty} representing the {@code java.net.URL}
* encoded name of a filesystem file, to which the platform printer
* A {@code StringProperty} representing the
* name of a filesystem file, to which the platform printer
* driver should spool the rendered print data.
* <p>
prrace marked this conversation as resolved.
Show resolved Hide resolved
* Applications can use this to request print-to-file behavior where
* the native print system is capable of spooling the output to a
* filesystem file, rather than the printer device.
* Applications can use this to programmatically request print-to-file
* behavior where the native print system is capable of spooling the
* output to a filesystem file, rather than the printer device.
* <p>
* This is often useful where the printer driver generates a format
* such as Postscript or PDF, and the application intends to distribute
* the result instead of printing it, or for some other reason the
* application does not want physical media (paper) emitted by the printer.
kevinrushforth marked this conversation as resolved.
Show resolved Hide resolved
* <p>
* If the application does not have permission to write to the specified
* file, a {@code SecurityException} may be thrown when printing.
* The default value is an empty string, which is interpreted as unset,
* equivalent to null, which means output is sent to the printer.
* So in order to reset to print to the printer, clear the value of
* this property by setting it to null or an empty string.
kevinrushforth marked this conversation as resolved.
Show resolved Hide resolved
* <p>
* Additionally if the application displays a printer dialog which allows
* the user to specify a file destination including altering an application
kevinrushforth marked this conversation as resolved.
Show resolved Hide resolved
* specified file destination, the value of this property will reflect that
* user-specified choice, including clearing it to re-set to print to
kevinrushforth marked this conversation as resolved.
Show resolved Hide resolved
* the printer, if the user does so.
* <p>
* If the print system does not support print-to-file, then this
* setting will be ignored.
* <p>
* If the URL specifies a non-existent path, or does not specify
* a writable file it may be ignored, or a SecurityException may be thrown.
* The default value is an empty string, which is interpreted as unset,
* which means output is sent to the printer.
* If the specified name specifies a non-existent path, or does not specify
* a user writable file, when printing the results are platform-dependent, including
* replacement with a default output file location, printing to the printer instead,
* or a platform printing error.
kevinrushforth marked this conversation as resolved.
Show resolved Hide resolved
* If a {@code SecurityManager} is installed and it denies access to the
* specified file a {@code SecurityException} may be thrown.
* @defaultValue an empty String
kevinrushforth marked this conversation as resolved.
Show resolved Hide resolved
*
* @return the name of a printer spool file
* @since 17
*/
public final StringProperty outputFileProperty() {
if (outputFile == null) {
outputFile =
new SimpleStringProperty(JobSettings.this, "outputFile",
DEFAULT_OUTPUTFILE) {
new SimpleStringProperty(JobSettings.this, "outputFile", "") {

@Override
public void set(String value) {
if (!isJobNew()) {
return;
}
if (value == null) {
value = DEFAULT_OUTPUTFILE;
value = "";
}
super.set(value);
}
Expand All @@ -533,23 +543,13 @@ public String toString() {
return outputFile;
}

/**
* Get the output file path name.
* @return the output file as a string encoded {@code java.net.URL}
* @since 17
*/
public String getOutputFile() {
prrace marked this conversation as resolved.
Show resolved Hide resolved
return outputFileProperty().get();
}


/**
* Set the output file.
* @param urlString the output file as a string encoded {@code java.net.URL}
* @since 17
*/
public void setOutputFile(String urlString) {
outputFileProperty().set(urlString);
public void setOutputFile(String filePath) {
outputFileProperty().set(filePath);
}
/////////////////////// END OUTPUTFILE /////////////////////

Expand Down
27 changes: 12 additions & 15 deletions tests/manual/printing/PrintToFileTest.java
Expand Up @@ -24,8 +24,6 @@
*/

import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;

import javafx.application.Platform;
import javafx.collections.FXCollections;
Expand Down Expand Up @@ -113,28 +111,27 @@ public void runTest() {
System.out.println("START OF PRINT JOB");
PrinterJob job = PrinterJob.createPrinterJob();
JobSettings settings = job.getJobSettings();
File f = new File("printtofiletest.prn");
String fileName = "printtofiletest.prn";
settings.outputFileProperty().set(fileName);
String destFileName = settings.outputFileProperty().get();
System.out.println("dest="+ destFileName);
File f = new File(destFileName);
f.delete();
try {
URL url = f.toURL();
String urlStr = url.toString();
settings.outputFileProperty().set(urlStr);
} catch (MalformedURLException e) {
System.out.println(e);
failed = true;
}

Platform.runLater(() -> {
Text t = new Text("file="+settings.getOutputFile());
root.getChildren().add(t);
});
Text printNode = new Text("\n\nTEST\nabc\ndef");
job.printPage(printNode);
job.endJob();
try {
// wait for printer spooler to create the file.
Thread.sleep(3000);
} catch (InterruptedException e) {
}
if (f.exists()) {
prrace marked this conversation as resolved.
Show resolved Hide resolved
System.out.println("created file");
System.out.println("created file " + f);
passed = true;
f.delete();
} else {
failed = true;
}
Expand All @@ -148,7 +145,7 @@ public void runTest() {
}
}
Platform.runLater(() -> displayMessage());

}).start();
}

Expand Down