jdk/src/macosx/classes/sun/lwawt/macosx/CPrinterJob.java
changeset 17675 b727bb0815d7
parent 17405 9071dd38156b
child 21227 1cae198f4f9c
equal deleted inserted replaced
17674:46246fbf98d4 17675:b727bb0815d7
    34 import java.security.PrivilegedAction;
    34 import java.security.PrivilegedAction;
    35 
    35 
    36 import javax.print.*;
    36 import javax.print.*;
    37 import javax.print.attribute.PrintRequestAttributeSet;
    37 import javax.print.attribute.PrintRequestAttributeSet;
    38 import javax.print.attribute.HashPrintRequestAttributeSet;
    38 import javax.print.attribute.HashPrintRequestAttributeSet;
       
    39 import javax.print.attribute.standard.PageRanges;
    39 
    40 
    40 import sun.java2d.*;
    41 import sun.java2d.*;
    41 import sun.print.*;
    42 import sun.print.*;
    42 
    43 
    43 public class CPrinterJob extends RasterPrinterJob {
    44 public class CPrinterJob extends RasterPrinterJob {
   171         // See if this has an NSPrintInfo in it.
   172         // See if this has an NSPrintInfo in it.
   172         NSPrintInfo nsPrintInfo = (NSPrintInfo)attributes.get(NSPrintInfo.class);
   173         NSPrintInfo nsPrintInfo = (NSPrintInfo)attributes.get(NSPrintInfo.class);
   173         if (nsPrintInfo != null) {
   174         if (nsPrintInfo != null) {
   174             fNSPrintInfo = nsPrintInfo.getValue();
   175             fNSPrintInfo = nsPrintInfo.getValue();
   175         }
   176         }
       
   177 
       
   178         PageRanges pageRangesAttr =  (PageRanges)attributes.get(PageRanges.class);
       
   179         if (isSupportedValue(pageRangesAttr, attributes)) {
       
   180             SunPageSelection rangeSelect = (SunPageSelection)attributes.get(SunPageSelection.class);
       
   181             // If rangeSelect is not null, we are using AWT's print dialog that has
       
   182             // All, Selection, and Range radio buttons
       
   183             if (rangeSelect == null || rangeSelect == SunPageSelection.RANGE) {
       
   184                 int[][] range = pageRangesAttr.getMembers();
       
   185                 // setPageRange will set firstPage and lastPage as called in getFirstPage
       
   186                 // and getLastPage
       
   187                 setPageRange(range[0][0] - 1, range[0][1] - 1);
       
   188             }
       
   189         }
   176     }
   190     }
   177 
   191 
   178     volatile boolean onEventThread;
   192     volatile boolean onEventThread;
   179 
   193 
   180     @Override
   194     @Override
   223         /* Get the range of pages we are to print. If the
   237         /* Get the range of pages we are to print. If the
   224          * last page to print is unknown, then we print to
   238          * last page to print is unknown, then we print to
   225          * the end of the document. Note that firstPage
   239          * the end of the document. Note that firstPage
   226          * and lastPage are 0 based page indices.
   240          * and lastPage are 0 based page indices.
   227          */
   241          */
   228         int numPages = mDocument.getNumberOfPages();
       
   229 
   242 
   230         int firstPage = getFirstPage();
   243         int firstPage = getFirstPage();
   231         int lastPage = getLastPage();
   244         int lastPage = getLastPage();
   232         if(lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) {
   245         if(lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) {
   233             int totalPages = mDocument.getNumberOfPages();
   246             int totalPages = mDocument.getNumberOfPages();
   240             synchronized (this) {
   253             synchronized (this) {
   241                 performingPrinting = true;
   254                 performingPrinting = true;
   242                 userCancelled = false;
   255                 userCancelled = false;
   243             }
   256             }
   244 
   257 
   245             if (EventQueue.isDispatchThread()) {
   258             //Add support for PageRange
   246                 // This is an AWT EventQueue, and this print rendering loop needs to block it.
   259             PageRanges pr = (attributes == null) ?  null
   247 
   260                                                  : (PageRanges)attributes.get(PageRanges.class);
   248                 onEventThread = true;
   261             int[][] prMembers = (pr == null) ? new int[0][0] : pr.getMembers();
   249 
   262             int loopi = 0;
   250                 printingLoop = AccessController.doPrivileged(new PrivilegedAction<SecondaryLoop>() {
   263             do {
   251                     @Override
   264                 if (EventQueue.isDispatchThread()) {
   252                     public SecondaryLoop run() {
   265                     // This is an AWT EventQueue, and this print rendering loop needs to block it.
   253                         return Toolkit.getDefaultToolkit()
   266 
   254                                 .getSystemEventQueue()
   267                     onEventThread = true;
   255                                 .createSecondaryLoop();
   268 
       
   269                     printingLoop = AccessController.doPrivileged(new PrivilegedAction<SecondaryLoop>() {
       
   270                         @Override
       
   271                         public SecondaryLoop run() {
       
   272                             return Toolkit.getDefaultToolkit()
       
   273                                     .getSystemEventQueue()
       
   274                                     .createSecondaryLoop();
       
   275                         }
       
   276                     });
       
   277 
       
   278                     try {
       
   279                         // Fire off the print rendering loop on the AppKit thread, and don't have
       
   280                         //  it wait and block this thread.
       
   281                         if (printLoop(false, firstPage, lastPage)) {
       
   282                             // Start a secondary loop on EDT until printing operation is finished or cancelled
       
   283                             printingLoop.enter();
       
   284                         }
       
   285                     } catch (Exception e) {
       
   286                         e.printStackTrace();
   256                     }
   287                     }
   257                 });
   288               } else {
   258 
   289                     // Fire off the print rendering loop on the AppKit, and block this thread
   259                 try {
   290                     //  until it is done.
   260                     // Fire off the print rendering loop on the AppKit thread, and don't have
   291                     // But don't actually block... we need to come back here!
   261                     //  it wait and block this thread.
   292                     onEventThread = false;
   262                     if (printLoop(false, firstPage, lastPage)) {
   293 
   263                         // Start a secondary loop on EDT until printing operation is finished or cancelled
   294                     try {
   264                         printingLoop.enter();
   295                         printLoop(true, firstPage, lastPage);
       
   296                     } catch (Exception e) {
       
   297                         e.printStackTrace();
   265                     }
   298                     }
   266                 } catch (Exception e) {
       
   267                     e.printStackTrace();
       
   268                 }
   299                 }
   269             } else {
   300                 if (++loopi < prMembers.length) {
   270                 // Fire off the print rendering loop on the AppKit, and block this thread
   301                      firstPage = prMembers[loopi][0]-1;
   271                 //  until it is done.
   302                      lastPage = prMembers[loopi][1] -1;
   272                 // But don't actually block... we need to come back here!
       
   273                 onEventThread = false;
       
   274 
       
   275                 try {
       
   276                     printLoop(true, firstPage, lastPage);
       
   277                 } catch (Exception e) {
       
   278                     e.printStackTrace();
       
   279                 }
   303                 }
   280             }
   304             }  while (loopi < prMembers.length);
   281         } finally {
   305         } finally {
   282             synchronized (this) {
   306             synchronized (this) {
   283                 // NOTE: Native code shouldn't allow exceptions out while
   307                 // NOTE: Native code shouldn't allow exceptions out while
   284                 // printing. They should cancel the print loop.
   308                 // printing. They should cancel the print loop.
   285                 performingPrinting = false;
   309                 performingPrinting = false;