final boolean interactive)
throws PrinterException {
final PrinterJob job = PrinterJob.getPrinterJob();
final Printable printable;
final PrintingStatus printingStatus;
final boolean isHeadless = GraphicsEnvironment.isHeadless();
final boolean isEventDispatchThread =
SwingUtilities.isEventDispatchThread();
final Printable textPrintable = getPrintable(headerFormat, footerFormat);
if (interactive && ! isHeadless) {
printingStatus =
PrintingStatus.createPrintingStatus(this, job);
printable =
printingStatus.createNotificationPrintable(textPrintable);
} else {
printingStatus = null;
printable = textPrintable;
}
if (service != null) {
job.setPrintService(service);
}
job.setPrintable(printable);
final PrintRequestAttributeSet attr = (attributes == null)
? new HashPrintRequestAttributeSet()
: attributes;
if (showPrintDialog && ! isHeadless && ! job.printDialog(attr)) {
return false;
}
/*
* there are three cases for printing:
* 1. print non interactively (! interactive || isHeadless)
* 2. print interactively off EDT
* 3. print interactively on EDT
*
* 1 and 2 prints on the current thread (3 prints on another thread)
* 2 and 3 deal with PrintingStatusDialog
*/
final Callable<Object> doPrint =
new Callable<Object>() {
public Object call() throws Exception {
try {
job.print(attr);
} finally {
if (printingStatus != null) {
printingStatus.dispose();
}
}
return null;
}
};
final FutureTask<Object> futurePrinting =
new FutureTask<Object>(doPrint);
final Runnable runnablePrinting =
new Runnable() {
public void run() {
//disable component
boolean wasEnabled = false;
if (isEventDispatchThread) {
if (isEnabled()) {
wasEnabled = true;
setEnabled(false);
}
} else {
try {
wasEnabled = SwingUtilities2.submit(
new Callable<Boolean>() {
public Boolean call() throws Exception {
boolean rv = isEnabled();
if (rv) {
setEnabled(false);
}
return rv;
}
}).get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof Error) {
throw (Error) cause;
}
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new AssertionError(cause);
}
}
getDocument().render(futurePrinting);
//enable component
if (wasEnabled) {
if (isEventDispatchThread) {
setEnabled(true);
} else {
try {
SwingUtilities2.submit(
new Runnable() {
public void run() {
setEnabled(true);
}
}, null).get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof Error) {
throw (Error) cause;
}
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new AssertionError(cause);
}
}
}
}
};
if (! interactive || isHeadless) {
runnablePrinting.run();
} else {
if (isEventDispatchThread) {
(new Thread(runnablePrinting)).start();
printingStatus.showModal(true);
} else {
printingStatus.showModal(false);
runnablePrinting.run();
}
}
//the printing is done successfully or otherwise.
//dialog is hidden if needed.
try {
futurePrinting.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof PrinterAbortException) {
if (printingStatus != null
&& printingStatus.isAborted()) {
return false;
} else {
throw (PrinterAbortException) cause;
}
} else if (cause instanceof PrinterException) {