use of javax.print in project jdk8u_jdk by JetBrains.
the class CPrinterJob method print.
@Override
public void print(PrintRequestAttributeSet attributes) throws PrinterException {
// NOTE: Some of this code is copied from RasterPrinterJob.
// this code uses javax.print APIs
// this will make it print directly to the printer
// this will not work if the user clicks on the "Preview" button
// However if the printer is a StreamPrintService, its the right path.
PrintService psvc = getPrintService();
if (psvc == null) {
throw new PrinterException("No print service found.");
}
if (psvc instanceof StreamPrintService) {
spoolToService(psvc, attributes);
return;
}
setAttributes(attributes);
// throw exception for invalid destination
if (destinationAttr != null) {
validateDestination(destinationAttr);
}
/* Get the range of pages we are to print. If the
* last page to print is unknown, then we print to
* the end of the document. Note that firstPage
* and lastPage are 0 based page indices.
*/
int firstPage = getFirstPage();
int lastPage = getLastPage();
if (lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) {
int totalPages = mDocument.getNumberOfPages();
if (totalPages != Pageable.UNKNOWN_NUMBER_OF_PAGES) {
lastPage = mDocument.getNumberOfPages() - 1;
}
}
try {
synchronized (this) {
performingPrinting = true;
userCancelled = false;
}
//Add support for PageRange
PageRanges pr = (attributes == null) ? null : (PageRanges) attributes.get(PageRanges.class);
int[][] prMembers = (pr == null) ? new int[0][0] : pr.getMembers();
int loopi = 0;
do {
if (EventQueue.isDispatchThread()) {
// This is an AWT EventQueue, and this print rendering loop needs to block it.
onEventThread = true;
printingLoop = AccessController.doPrivileged(new PrivilegedAction<SecondaryLoop>() {
@Override
public SecondaryLoop run() {
return Toolkit.getDefaultToolkit().getSystemEventQueue().createSecondaryLoop();
}
});
try {
// it wait and block this thread.
if (printLoop(false, firstPage, lastPage)) {
// Start a secondary loop on EDT until printing operation is finished or cancelled
printingLoop.enter();
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
// Fire off the print rendering loop on the AppKit, and block this thread
// until it is done.
// But don't actually block... we need to come back here!
onEventThread = false;
try {
printLoop(true, firstPage, lastPage);
} catch (Exception e) {
e.printStackTrace();
}
}
if (++loopi < prMembers.length) {
firstPage = prMembers[loopi][0] - 1;
lastPage = prMembers[loopi][1] - 1;
}
} while (loopi < prMembers.length);
} finally {
synchronized (this) {
// NOTE: Native code shouldn't allow exceptions out while
// printing. They should cancel the print loop.
performingPrinting = false;
notify();
}
if (printingLoop != null) {
printingLoop.exit();
}
}
// Normalize the collated, # copies, numPages, first/last pages. Need to
// make note of pageRangesAttr.
// Set up NSPrintInfo with the java settings (PageFormat & Paper).
// Create an NSView for printing. Have knowsPageRange return YES, and give the correct
// range, or MAX? if unknown. Have rectForPage do a peekGraphics check before returning
// the rectangle. Have drawRect do the real render of the page. Have printJobTitle do
// the right thing.
// Call NSPrintOperation, it will call NSView.drawRect: for each page.
// NSView.drawRect: will create a CPrinterGraphics with the current CGContextRef, and then
// pass this Graphics onto the Printable with the appropriate PageFormat and index.
// Need to be able to cancel the NSPrintOperation (using code from RasterPrinterJob, be
// sure to initialize userCancelled and performingPrinting member variables).
// Extensions available from AppKit: Print to PDF or EPS file!
}
Aggregations