< prev index next >

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

Print this page
rev 53644 : 8234393: [macos] printing ignores printer tray
   1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  25 
  26 package sun.lwawt.macosx;
  27 
  28 
  29 import java.awt.*;
  30 import java.awt.geom.Rectangle2D;
  31 import java.awt.image.BufferedImage;
  32 import java.awt.print.*;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 
  36 import javax.print.*;
  37 import javax.print.attribute.PrintRequestAttributeSet;
  38 import javax.print.attribute.HashPrintRequestAttributeSet;
  39 import javax.print.attribute.standard.Copies;
  40 import javax.print.attribute.standard.Media;
  41 import javax.print.attribute.standard.MediaPrintableArea;
  42 import javax.print.attribute.standard.MediaSize;
  43 import javax.print.attribute.standard.MediaSizeName;
  44 import javax.print.attribute.standard.PageRanges;

  45 
  46 import sun.java2d.*;
  47 import sun.print.*;
  48 
  49 public final class CPrinterJob extends RasterPrinterJob {
  50     // NOTE: This uses RasterPrinterJob as a base, but it doesn't use
  51     // all of the RasterPrinterJob functions. RasterPrinterJob will
  52     // break down printing to pieces that aren't necessary under MacOSX
  53     // printing, such as controlling the # of copies and collating. These
  54     // are handled by the native printing. RasterPrinterJob is kept for
  55     // future compatibility and the state keeping that it handles.
  56 
  57     private static String sShouldNotReachHere = "Should not reach here.";
  58 
  59     private volatile SecondaryLoop printingLoop;
  60 
  61     private boolean noDefaultPrinter = false;
  62 
  63     private static Font defaultFont;
  64 


  65     // This is the NSPrintInfo for this PrinterJob. Protect multi thread
  66     //  access to it. It is used by the pageDialog, jobDialog, and printLoop.
  67     //  This way the state of these items is shared across these calls.
  68     //  PageFormat data is passed in and set on the fNSPrintInfo on a per call
  69     //  basis.
  70     private long fNSPrintInfo = -1;
  71     private Object fNSPrintInfoLock = new Object();
  72 
  73     static {
  74         // AWT has to be initialized for the native code to function correctly.
  75         Toolkit.getDefaultToolkit();
  76     }
  77 
  78     /**
  79      * Presents a dialog to the user for changing the properties of
  80      * the print job.
  81      * This method will display a native dialog if a native print
  82      * service is selected, and user choice of printers will be restricted
  83      * to these native print services.
  84      * To present the cross platform print dialog for all services,


 160      * Clones the {@code PageFormat} argument and alters the
 161      * clone to describe a default page size and orientation.
 162      * @param page the {@code PageFormat} to be cloned and altered
 163      * @return clone of {@code page}, altered to describe a default
 164      *                      {@code PageFormat}.
 165      */
 166     @Override
 167     public PageFormat defaultPage(PageFormat page) {
 168         PageFormat newPage = (PageFormat)page.clone();
 169         getDefaultPage(newPage);
 170         return newPage;
 171     }
 172 
 173     @Override
 174     protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
 175         super.setAttributes(attributes);
 176 
 177         if (attributes == null) {
 178             return;
 179         }





 180 
 181         PageRanges pageRangesAttr =  (PageRanges)attributes.get(PageRanges.class);
 182         if (isSupportedValue(pageRangesAttr, attributes)) {
 183             SunPageSelection rangeSelect = (SunPageSelection)attributes.get(SunPageSelection.class);
 184             // If rangeSelect is not null, we are using AWT's print dialog that has
 185             // All, Selection, and Range radio buttons
 186             if (rangeSelect == null || rangeSelect == SunPageSelection.RANGE) {
 187                 int[][] range = pageRangesAttr.getMembers();
 188                 // setPageRange will set firstPage and lastPage as called in getFirstPage
 189                 // and getLastPage
 190                 setPageRange(range[0][0] - 1, range[0][1] - 1);
 191             } else {
 192                 // if rangeSelect is SunPageSelection.ALL
 193                 // then setPageRange appropriately
 194                 setPageRange(-1, -1);
 195             }
 196         }
 197     }
 198 
 199     private void setPageRangeAttribute(int from, int to, boolean isRangeSet) {


 591         }
 592         return page;
 593     }
 594 
 595     private Printable getPrintable(int pageIndex) {
 596         // This is called from the native side.
 597         Printable painter;
 598         try {
 599             painter = getPageable().getPrintable(pageIndex);
 600         } catch (Exception e) {
 601             return null;
 602         }
 603         return painter;
 604     }
 605 
 606     private String getPrinterName(){
 607         // This is called from the native side.
 608         PrintService service = getPrintService();
 609         if (service == null) return null;
 610         return service.getName();




 611     }
 612 
 613     private void setPrinterServiceFromNative(String printerName) {
 614         // This is called from the native side.
 615         PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
 616 
 617         for (int i = 0; i < services.length; i++) {
 618             PrintService service = services[i];
 619 
 620             if (printerName.equals(service.getName())) {
 621                 try {
 622                     setPrintService(service);
 623                 } catch (PrinterException e) {
 624                     // ignored
 625                 }
 626                 return;
 627             }
 628         }
 629     }
 630 


   1 /*
   2  * Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  25 
  26 package sun.lwawt.macosx;
  27 
  28 
  29 import java.awt.*;
  30 import java.awt.geom.Rectangle2D;
  31 import java.awt.image.BufferedImage;
  32 import java.awt.print.*;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 
  36 import javax.print.*;
  37 import javax.print.attribute.PrintRequestAttributeSet;
  38 import javax.print.attribute.HashPrintRequestAttributeSet;
  39 import javax.print.attribute.standard.Copies;
  40 import javax.print.attribute.standard.Media;
  41 import javax.print.attribute.standard.MediaPrintableArea;
  42 import javax.print.attribute.standard.MediaSize;
  43 import javax.print.attribute.standard.MediaSizeName;
  44 import javax.print.attribute.standard.PageRanges;
  45 import javax.print.attribute.Attribute;
  46 
  47 import sun.java2d.*;
  48 import sun.print.*;
  49 
  50 public final class CPrinterJob extends RasterPrinterJob {
  51     // NOTE: This uses RasterPrinterJob as a base, but it doesn't use
  52     // all of the RasterPrinterJob functions. RasterPrinterJob will
  53     // break down printing to pieces that aren't necessary under MacOSX
  54     // printing, such as controlling the # of copies and collating. These
  55     // are handled by the native printing. RasterPrinterJob is kept for
  56     // future compatibility and the state keeping that it handles.
  57 
  58     private static String sShouldNotReachHere = "Should not reach here.";
  59 
  60     private volatile SecondaryLoop printingLoop;
  61 
  62     private boolean noDefaultPrinter = false;
  63 
  64     private static Font defaultFont;
  65 
  66     private String tray = null;
  67 
  68     // This is the NSPrintInfo for this PrinterJob. Protect multi thread
  69     //  access to it. It is used by the pageDialog, jobDialog, and printLoop.
  70     //  This way the state of these items is shared across these calls.
  71     //  PageFormat data is passed in and set on the fNSPrintInfo on a per call
  72     //  basis.
  73     private long fNSPrintInfo = -1;
  74     private Object fNSPrintInfoLock = new Object();
  75 
  76     static {
  77         // AWT has to be initialized for the native code to function correctly.
  78         Toolkit.getDefaultToolkit();
  79     }
  80 
  81     /**
  82      * Presents a dialog to the user for changing the properties of
  83      * the print job.
  84      * This method will display a native dialog if a native print
  85      * service is selected, and user choice of printers will be restricted
  86      * to these native print services.
  87      * To present the cross platform print dialog for all services,


 163      * Clones the {@code PageFormat} argument and alters the
 164      * clone to describe a default page size and orientation.
 165      * @param page the {@code PageFormat} to be cloned and altered
 166      * @return clone of {@code page}, altered to describe a default
 167      *                      {@code PageFormat}.
 168      */
 169     @Override
 170     public PageFormat defaultPage(PageFormat page) {
 171         PageFormat newPage = (PageFormat)page.clone();
 172         getDefaultPage(newPage);
 173         return newPage;
 174     }
 175 
 176     @Override
 177     protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
 178         super.setAttributes(attributes);
 179 
 180         if (attributes == null) {
 181             return;
 182         }
 183         Attribute attr = attributes.get(Media.class);
 184         if (attr instanceof CustomMediaTray) {
 185             CustomMediaTray customTray = (CustomMediaTray) attr;
 186             tray = customTray.getChoiceName();
 187         }
 188 
 189         PageRanges pageRangesAttr =  (PageRanges)attributes.get(PageRanges.class);
 190         if (isSupportedValue(pageRangesAttr, attributes)) {
 191             SunPageSelection rangeSelect = (SunPageSelection)attributes.get(SunPageSelection.class);
 192             // If rangeSelect is not null, we are using AWT's print dialog that has
 193             // All, Selection, and Range radio buttons
 194             if (rangeSelect == null || rangeSelect == SunPageSelection.RANGE) {
 195                 int[][] range = pageRangesAttr.getMembers();
 196                 // setPageRange will set firstPage and lastPage as called in getFirstPage
 197                 // and getLastPage
 198                 setPageRange(range[0][0] - 1, range[0][1] - 1);
 199             } else {
 200                 // if rangeSelect is SunPageSelection.ALL
 201                 // then setPageRange appropriately
 202                 setPageRange(-1, -1);
 203             }
 204         }
 205     }
 206 
 207     private void setPageRangeAttribute(int from, int to, boolean isRangeSet) {


 599         }
 600         return page;
 601     }
 602 
 603     private Printable getPrintable(int pageIndex) {
 604         // This is called from the native side.
 605         Printable painter;
 606         try {
 607             painter = getPageable().getPrintable(pageIndex);
 608         } catch (Exception e) {
 609             return null;
 610         }
 611         return painter;
 612     }
 613 
 614     private String getPrinterName(){
 615         // This is called from the native side.
 616         PrintService service = getPrintService();
 617         if (service == null) return null;
 618         return service.getName();
 619     }
 620 
 621     private String getPrinterTray() {
 622         return tray;
 623     }
 624 
 625     private void setPrinterServiceFromNative(String printerName) {
 626         // This is called from the native side.
 627         PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
 628 
 629         for (int i = 0; i < services.length; i++) {
 630             PrintService service = services[i];
 631 
 632             if (printerName.equals(service.getName())) {
 633                 try {
 634                     setPrintService(service);
 635                 } catch (PrinterException e) {
 636                     // ignored
 637                 }
 638                 return;
 639             }
 640         }
 641     }
 642 


< prev index next >