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

Print this page




 192 
 193 
 194         // this code uses javax.print APIs
 195         // this will make it print directly to the printer
 196         // this will not work if the user clicks on the "Preview" button
 197         // However if the printer is a StreamPrintService, its the right path.
 198         PrintService psvc = getPrintService();
 199         if (psvc instanceof StreamPrintService) {
 200             spoolToService(psvc, attributes);
 201             return;
 202         }
 203 
 204 
 205         setAttributes(attributes);
 206 
 207         /* Get the range of pages we are to print. If the
 208          * last page to print is unknown, then we print to
 209          * the end of the document. Note that firstPage
 210          * and lastPage are 0 based page indices.
 211          */
 212         int numPages = mDocument.getNumberOfPages();
 213 
 214         int firstPage = getFirstPage();
 215         int lastPage = getLastPage();
 216         if(lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) {
 217             int totalPages = mDocument.getNumberOfPages();
 218             if (totalPages != Pageable.UNKNOWN_NUMBER_OF_PAGES) {
 219                 lastPage = mDocument.getNumberOfPages() - 1;
 220             }
 221         }
 222 
 223         try {
 224             synchronized (this) {
 225                 performingPrinting = true;
 226                 userCancelled = false;
 227             }
 228 






 229             if (EventQueue.isDispatchThread()) {
 230                 // This is an AWT EventQueue, and this print rendering loop needs to block it.
 231 
 232                 onEventThread = true;
 233 
 234                 try {
 235                     // Fire off the print rendering loop on the AppKit thread, and don't have
 236                     //  it wait and block this thread.
 237                     if (printLoop(false, firstPage, lastPage)) {
 238                         // Fire off the EventConditional that will what until the condition is met,
 239                         //  but will still process AWTEvent's as they occur.
 240                         new EventDispatchAccess() {
 241                             public boolean evaluate() {
 242                                 return performingPrinting;
 243                             }
 244                         }.pumpEventsAndWait();
 245                     }
 246                 } catch (Exception e) {
 247                     e.printStackTrace();
 248                 }
 249             } else {
 250                 // Fire off the print rendering loop on the AppKit, and block this thread
 251                 //  until it is done.
 252                 // But don't actually block... we need to come back here!
 253                 onEventThread = false;
 254 
 255                 try {
 256                     printLoop(true, firstPage, lastPage);
 257                 } catch (Exception e) {
 258                     e.printStackTrace();
 259                 }
 260             }





 261         } finally {
 262             synchronized (this) {
 263                 // NOTE: Native code shouldn't allow exceptions out while
 264                 // printing. They should cancel the print loop.
 265                 performingPrinting = false;
 266                 notify();
 267             }
 268         }
 269 
 270         // Normalize the collated, # copies, numPages, first/last pages. Need to
 271         //  make note of pageRangesAttr.
 272 
 273         // Set up NSPrintInfo with the java settings (PageFormat & Paper).
 274 
 275         // Create an NSView for printing. Have knowsPageRange return YES, and give the correct
 276         //  range, or MAX? if unknown. Have rectForPage do a peekGraphics check before returning
 277         //  the rectangle. Have drawRect do the real render of the page. Have printJobTitle do
 278         //  the right thing.
 279 
 280         // Call NSPrintOperation, it will call NSView.drawRect: for each page.




 192 
 193 
 194         // this code uses javax.print APIs
 195         // this will make it print directly to the printer
 196         // this will not work if the user clicks on the "Preview" button
 197         // However if the printer is a StreamPrintService, its the right path.
 198         PrintService psvc = getPrintService();
 199         if (psvc instanceof StreamPrintService) {
 200             spoolToService(psvc, attributes);
 201             return;
 202         }
 203 
 204 
 205         setAttributes(attributes);
 206 
 207         /* Get the range of pages we are to print. If the
 208          * last page to print is unknown, then we print to
 209          * the end of the document. Note that firstPage
 210          * and lastPage are 0 based page indices.
 211          */

 212 
 213         int firstPage = getFirstPage();
 214         int lastPage = getLastPage();
 215         if(lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) {
 216             int totalPages = mDocument.getNumberOfPages();
 217             if (totalPages != Pageable.UNKNOWN_NUMBER_OF_PAGES) {
 218                 lastPage = mDocument.getNumberOfPages() - 1;
 219             }
 220         }
 221 
 222         try {
 223             synchronized (this) {
 224                 performingPrinting = true;
 225                 userCancelled = false;
 226             }
 227 
 228             //Add support for PageRange
 229             PageRanges pr = (attributes == null) ?  null 
 230                                                  : (PageRanges)attributes.get(PageRanges.class);
 231             int[][] prMembers = (pr == null) ? new int[0][0] : pr.getMembers();
 232             int loopi = 0;
 233             do {
 234                 if (EventQueue.isDispatchThread()) {
 235                     // This is an AWT EventQueue, and this print rendering loop needs to block it.
 236 
 237                     onEventThread = true;
 238 
 239                     try {
 240                         // Fire off the print rendering loop on the AppKit thread, and don't have
 241                         //  it wait and block this thread.
 242                         if (printLoop(false, firstPage, lastPage)) {
 243                             // Fire off the EventConditional that will what until the condition is met,
 244                             //  but will still process AWTEvent's as they occur.
 245                             new EventDispatchAccess() {
 246                                 public boolean evaluate() {
 247                                     return performingPrinting;
 248                                 }
 249                             }.pumpEventsAndWait();
 250                         }
 251                     } catch (Exception e) {
 252                         e.printStackTrace();
 253                     }
 254                 } else {
 255                     // Fire off the print rendering loop on the AppKit, and block this thread
 256                     //  until it is done.
 257                     // But don't actually block... we need to come back here!
 258                     onEventThread = false;
 259 
 260                     try {
 261                         printLoop(true, firstPage, lastPage);
 262                     } catch (Exception e) {
 263                         e.printStackTrace();
 264                     }
 265                 }
 266                 if (++loopi < prMembers.length) {
 267                     firstPage = prMembers[loopi][0]-1;
 268                     lastPage = prMembers[loopi][1] -1;
 269                 }
 270               }  while (loopi < prMembers.length);
 271         } finally {
 272             synchronized (this) {
 273                 // NOTE: Native code shouldn't allow exceptions out while
 274                 // printing. They should cancel the print loop.
 275                 performingPrinting = false;
 276                 notify();
 277             }
 278         }
 279 
 280         // Normalize the collated, # copies, numPages, first/last pages. Need to
 281         //  make note of pageRangesAttr.
 282 
 283         // Set up NSPrintInfo with the java settings (PageFormat & Paper).
 284 
 285         // Create an NSView for printing. Have knowsPageRange return YES, and give the correct
 286         //  range, or MAX? if unknown. Have rectForPage do a peekGraphics check before returning
 287         //  the rectangle. Have drawRect do the real render of the page. Have printJobTitle do
 288         //  the right thing.
 289 
 290         // Call NSPrintOperation, it will call NSView.drawRect: for each page.