Java Code Examples for javax.print.attribute.standard.MediaSize#getMediaSizeForName()

The following examples show how to use javax.print.attribute.standard.MediaSize#getMediaSizeForName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WrongPaperPrintingTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf));
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 2
Source File: WPrinterJob.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/** MediaSizeName / dmPaper */
private final int[] getWin32MediaAttrib() {
    int wid_ht[] = {0, 0};
    if (attributes != null) {
        Media media = (Media)attributes.get(Media.class);
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize ms = MediaSize.getMediaSizeForName(msn);
            if (ms != null) {
                wid_ht[0] = (int)(ms.getX(MediaSize.INCH) * 72.0);
                wid_ht[1] = (int)(ms.getY(MediaSize.INCH) * 72.0);
            }
        }
    }
    return wid_ht;
}
 
Example 3
Source File: UnixPrintService.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private MediaPrintableArea[] getAllPrintableAreas() {

        if (mpas == null) {
            Media[] media = (Media[])getSupportedAttributeValues(Media.class,
                                                                 null, null);
            mpas = new MediaPrintableArea[media.length];
            for (int i=0; i< mpas.length; i++) {
                if (media[i] instanceof MediaSizeName) {
                    MediaSizeName msn = (MediaSizeName)media[i];
                    MediaSize mediaSize = MediaSize.getMediaSizeForName(msn);
                    if (mediaSize == null) {
                        mpas[i] = (MediaPrintableArea)
                            getDefaultAttributeValue(MediaPrintableArea.class);
                    } else {
                        mpas[i] = new MediaPrintableArea(0.25f, 0.25f,
                                        mediaSize.getX(MediaSize.INCH)-0.5f,
                                        mediaSize.getY(MediaSize.INCH)-0.5f,
                                        MediaSize.INCH);
                    }
                }
            }
        }
        MediaPrintableArea[] mpasCopy = new MediaPrintableArea[mpas.length];
        System.arraycopy(mpas, 0, mpasCopy, 0, mpas.length);
        return mpasCopy;
    }
 
Example 4
Source File: PrintJob2D.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public static MediaSizeName mapMedia(MediaType mType) {
    MediaSizeName media = null;

    // JAVAXSIZES.length and SIZES.length must be equal!
    // Attempt to recover by getting the smaller size.
    int length = Math.min(SIZES.length, JAVAXSIZES.length);

    for (int i=0; i < length; i++) {
        if (SIZES[i] == mType) {
            if ((JAVAXSIZES[i] != null) &&
                MediaSize.getMediaSizeForName(JAVAXSIZES[i]) != null) {
                media = JAVAXSIZES[i];
                break;
            } else {
                /* create Custom Media */
                media = new CustomMediaSizeName(SIZES[i].toString());

                float w = (float)Math.rint(WIDTHS[i]  / 72.0);
                float h = (float)Math.rint(LENGTHS[i] / 72.0);
                if (w > 0.0 && h > 0.0) {
                    // add new created MediaSize to our static map
                    // so it will be found when we call findMedia
                    new MediaSize(w, h, Size2DSyntax.INCH, media);
                }

                break;
            }
        }
    }
    return media;
}
 
Example 5
Source File: Java14PrintUtil.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static MediaSize lookupMediaSize( final Media media ) {

    if ( media instanceof MediaSizeName ) {
      return MediaSize.getMediaSizeForName( (MediaSizeName) media );
    } else if ( media instanceof MediaName ) {
      if ( media.equals( MediaName.ISO_A4_TRANSPARENT ) || media.equals( MediaName.ISO_A4_WHITE ) ) {
        return MediaSize.getMediaSizeForName( MediaSizeName.ISO_A4 );
      } else if ( media.equals( MediaName.NA_LETTER_TRANSPARENT ) || media.equals( MediaName.NA_LETTER_WHITE ) ) {
        return MediaSize.getMediaSizeForName( MediaSizeName.NA_LETTER );
      }
    }
    return null;
  }
 
Example 6
Source File: CPrinterJob.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
    protected MediaSize getMediaSize(Media media, PrintService service,
            PageFormat page) {
        if (media == null || !(media instanceof MediaSizeName)) {
            return getDefaultMediaSize(page);
}
        MediaSize size = MediaSize.getMediaSizeForName((MediaSizeName) media);
        return size != null ? size : getDefaultMediaSize(page);
    }
 
Example 7
Source File: MediaMappingsTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) {
    MediaSize sizeA = MediaSize.getMediaSizeForName(MediaSizeName.A);
    new MediaSize(1.0f, 2.0f, MediaSize.MM, MediaSizeName.A);
    if (!sizeA.equals(MediaSize.getMediaSizeForName(MediaSizeName.A))) {
         throw new RuntimeException("mapping changed");
    }
    MediaSize sizeB = MediaSize.getMediaSizeForName(MediaSizeName.B);
    new MediaSize(1, 2, MediaSize.MM, MediaSizeName.B);
    if (!sizeB.equals(MediaSize.getMediaSizeForName(MediaSizeName.B))) {
         throw new RuntimeException("mapping changed");
    }
}
 
Example 8
Source File: CustomMediaSizeName.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public CustomMediaSizeName(String name, String choice,
                           float width, float length) {
    super(nextValue(name));
    choiceName = choice;
    customEnumTable.add(this);
    mediaName = null;
    try {
        mediaName = MediaSize.findMedia(width, length,
                                        MediaSize.INCH);
    } catch (IllegalArgumentException iae) {
    }
    // The public API method finds a closest match even if it not
    // all that close. Here we want to be sure its *really* close.
    if (mediaName != null) {
        MediaSize sz = MediaSize.getMediaSizeForName(mediaName);
        if (sz == null) {
            mediaName = null;
        } else {
            float w = sz.getX(MediaSize.INCH);
            float h = sz.getY(MediaSize.INCH);
            float dw = Math.abs(w - width);
            float dh = Math.abs(h - length);
            if (dw > 0.1 || dh > 0.1) {
                mediaName = null;
            }
        }
    }
}
 
Example 9
Source File: MediaMappingsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) {
    MediaSize sizeA = MediaSize.getMediaSizeForName(MediaSizeName.A);
    new MediaSize(1.0f, 2.0f, MediaSize.MM, MediaSizeName.A);
    if (!sizeA.equals(MediaSize.getMediaSizeForName(MediaSizeName.A))) {
         throw new RuntimeException("mapping changed");
    }
    MediaSize sizeB = MediaSize.getMediaSizeForName(MediaSizeName.B);
    new MediaSize(1, 2, MediaSize.MM, MediaSizeName.B);
    if (!sizeB.equals(MediaSize.getMediaSizeForName(MediaSizeName.B))) {
         throw new RuntimeException("mapping changed");
    }
}
 
Example 10
Source File: PSStreamPrintJob.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private void getAttributeValues(DocFlavor flavor) throws PrintException {

        Attribute attr;
        Class category;

        if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
            fidelity = true;
        } else {
            fidelity = false;
        }

        Attribute []attrs = reqAttrSet.toArray();
        for (int i=0; i<attrs.length; i++) {
            attr = attrs[i];
            category = attr.getCategory();
            if (fidelity == true) {
                if (!service.isAttributeCategorySupported(category)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported category: " + category, category, null);
                } else if
                    (!service.isAttributeValueSupported(attr, flavor, null)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported attribute: " + attr, null, attr);
                }
            }
            if (category == JobName.class) {
                jobName = ((JobName)attr).getValue();
            } else if (category == Copies.class) {
                copies = ((Copies)attr).getValue();
            } else if (category == Media.class) {
                if (attr instanceof MediaSizeName &&
                    service.isAttributeValueSupported(attr, null, null)) {
                    mediaSize =
                        MediaSize.getMediaSizeForName((MediaSizeName)attr);
                }
            } else if (category == OrientationRequested.class) {
                orient = (OrientationRequested)attr;
            }
        }
    }
 
Example 11
Source File: PrinterJob.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calculates a {@code PageFormat} with values consistent with those
 * supported by the current {@code PrintService} for this job
 * (ie the value returned by {@code getPrintService()}) and media,
 * printable area and orientation contained in {@code attributes}.
 * <p>
 * Calling this method does not update the job.
 * It is useful for clients that have a set of attributes obtained from
 * {@code printDialog(PrintRequestAttributeSet attributes)}
 * and need a PageFormat to print a Pageable object.
 * @param attributes a set of printing attributes, for example obtained
 * from calling printDialog. If {@code attributes} is null a default
 * PageFormat is returned.
 * @return a {@code PageFormat} whose settings conform with
 * those of the current service and the specified attributes.
 * @since 1.6
 */
public PageFormat getPageFormat(PrintRequestAttributeSet attributes) {

    PrintService service = getPrintService();
    PageFormat pf = defaultPage();

    if (service == null || attributes == null) {
        return pf;
    }

    Media media = (Media)attributes.get(Media.class);
    MediaPrintableArea mpa =
        (MediaPrintableArea)attributes.get(MediaPrintableArea.class);
    OrientationRequested orientReq =
       (OrientationRequested)attributes.get(OrientationRequested.class);

    if (media == null && mpa == null && orientReq == null) {
       return pf;
    }
    Paper paper = pf.getPaper();

    /* If there's a media but no media printable area, we can try
     * to retrieve the default value for mpa and use that.
     */
    if (mpa == null && media != null &&
        service.isAttributeCategorySupported(MediaPrintableArea.class)) {
        Object mpaVals =
            service.getSupportedAttributeValues(MediaPrintableArea.class,
                                                null, attributes);
        if (mpaVals instanceof MediaPrintableArea[] &&
            ((MediaPrintableArea[])mpaVals).length > 0) {
            mpa = ((MediaPrintableArea[])mpaVals)[0];
        }
    }

    if (media != null &&
        service.isAttributeValueSupported(media, null, attributes)) {
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize msz = MediaSize.getMediaSizeForName(msn);
            if (msz != null) {
                double inch = 72.0;
                double paperWid = msz.getX(MediaSize.INCH) * inch;
                double paperHgt = msz.getY(MediaSize.INCH) * inch;
                paper.setSize(paperWid, paperHgt);
                if (mpa == null) {
                    paper.setImageableArea(inch, inch,
                                           paperWid-2*inch,
                                           paperHgt-2*inch);
                }
            }
        }
    }

    if (mpa != null &&
        service.isAttributeValueSupported(mpa, null, attributes)) {
        float [] printableArea =
            mpa.getPrintableArea(MediaPrintableArea.INCH);
        for (int i=0; i < printableArea.length; i++) {
            printableArea[i] = printableArea[i]*72.0f;
        }
        paper.setImageableArea(printableArea[0], printableArea[1],
                               printableArea[2], printableArea[3]);
    }

    if (orientReq != null &&
        service.isAttributeValueSupported(orientReq, null, attributes)) {
        int orient;
        if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
            orient = PageFormat.REVERSE_LANDSCAPE;
        } else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
            orient = PageFormat.LANDSCAPE;
        } else {
            orient = PageFormat.PORTRAIT;
        }
        pf.setOrientation(orient);
    }

    pf.setPaper(paper);
    pf = validatePage(pf);
    return pf;
}
 
Example 12
Source File: PrinterJob.java    From jdk-1.7-annotated with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates a <code>PageFormat</code> with values consistent with those
 * supported by the current <code>PrintService</code> for this job
 * (ie the value returned by <code>getPrintService()</code>) and media,
 * printable area and orientation contained in <code>attributes</code>.
 * <p>
 * Calling this method does not update the job.
 * It is useful for clients that have a set of attributes obtained from
 * <code>printDialog(PrintRequestAttributeSet attributes)</code>
 * and need a PageFormat to print a Pageable object.
 * @param attributes a set of printing attributes, for example obtained
 * from calling printDialog. If <code>attributes</code> is null a default
 * PageFormat is returned.
 * @return a <code>PageFormat</code> whose settings conform with
 * those of the current service and the specified attributes.
 * @since 1.6
 */
public PageFormat getPageFormat(PrintRequestAttributeSet attributes) {

    PrintService service = getPrintService();
    PageFormat pf = defaultPage();

    if (service == null || attributes == null) {
        return pf;
    }

    Media media = (Media)attributes.get(Media.class);
    MediaPrintableArea mpa =
        (MediaPrintableArea)attributes.get(MediaPrintableArea.class);
    OrientationRequested orientReq =
       (OrientationRequested)attributes.get(OrientationRequested.class);

    if (media == null && mpa == null && orientReq == null) {
       return pf;
    }
    Paper paper = pf.getPaper();

    /* If there's a media but no media printable area, we can try
     * to retrieve the default value for mpa and use that.
     */
    if (mpa == null && media != null &&
        service.isAttributeCategorySupported(MediaPrintableArea.class)) {
        Object mpaVals =
            service.getSupportedAttributeValues(MediaPrintableArea.class,
                                                null, attributes);
        if (mpaVals instanceof MediaPrintableArea[] &&
            ((MediaPrintableArea[])mpaVals).length > 0) {
            mpa = ((MediaPrintableArea[])mpaVals)[0];
        }
    }

    if (media != null &&
        service.isAttributeValueSupported(media, null, attributes)) {
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize msz = MediaSize.getMediaSizeForName(msn);
            if (msz != null) {
                double inch = 72.0;
                double paperWid = msz.getX(MediaSize.INCH) * inch;
                double paperHgt = msz.getY(MediaSize.INCH) * inch;
                paper.setSize(paperWid, paperHgt);
                if (mpa == null) {
                    paper.setImageableArea(inch, inch,
                                           paperWid-2*inch,
                                           paperHgt-2*inch);
                }
            }
        }
    }

    if (mpa != null &&
        service.isAttributeValueSupported(mpa, null, attributes)) {
        float [] printableArea =
            mpa.getPrintableArea(MediaPrintableArea.INCH);
        for (int i=0; i < printableArea.length; i++) {
            printableArea[i] = printableArea[i]*72.0f;
        }
        paper.setImageableArea(printableArea[0], printableArea[1],
                               printableArea[2], printableArea[3]);
    }

    if (orientReq != null &&
        service.isAttributeValueSupported(orientReq, null, attributes)) {
        int orient;
        if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
            orient = PageFormat.REVERSE_LANDSCAPE;
        } else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
            orient = PageFormat.LANDSCAPE;
        } else {
            orient = PageFormat.PORTRAIT;
        }
        pf.setOrientation(orient);
    }

    pf.setPaper(paper);
    pf = validatePage(pf);
    return pf;
}
 
Example 13
Source File: PSStreamPrintJob.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void getAttributeValues(DocFlavor flavor) throws PrintException {

        Attribute attr;
        Class category;

        if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
            fidelity = true;
        } else {
            fidelity = false;
        }

        Attribute []attrs = reqAttrSet.toArray();
        for (int i=0; i<attrs.length; i++) {
            attr = attrs[i];
            category = attr.getCategory();
            if (fidelity == true) {
                if (!service.isAttributeCategorySupported(category)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported category: " + category, category, null);
                } else if
                    (!service.isAttributeValueSupported(attr, flavor, null)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported attribute: " + attr, null, attr);
                }
            }
            if (category == JobName.class) {
                jobName = ((JobName)attr).getValue();
            } else if (category == Copies.class) {
                copies = ((Copies)attr).getValue();
            } else if (category == Media.class) {
                if (attr instanceof MediaSizeName &&
                    service.isAttributeValueSupported(attr, null, null)) {
                    mediaSize =
                        MediaSize.getMediaSizeForName((MediaSizeName)attr);
                }
            } else if (category == OrientationRequested.class) {
                orient = (OrientationRequested)attr;
            }
        }
    }
 
Example 14
Source File: Win32PrintJob.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void getAttributeValues(DocFlavor flavor) throws PrintException {

        if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
            fidelity = true;
        } else {
            fidelity = false;
        }

        Class category;
        Attribute [] attrs = reqAttrSet.toArray();
        for (int i=0; i<attrs.length; i++) {
            Attribute attr = attrs[i];
            category = attr.getCategory();
            if (fidelity == true) {
                if (!service.isAttributeCategorySupported(category)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported category: " + category, category, null);
                } else if
                    (!service.isAttributeValueSupported(attr, flavor, null)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported attribute: " + attr, null, attr);
                }
            }
            if (category == Destination.class) {
              URI uri = ((Destination)attr).getURI();
              if (!"file".equals(uri.getScheme())) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("Not a file: URI");
              } else {
                try {
                  mDestination = (new File(uri)).getPath();
                } catch (Exception e) {
                  throw new PrintException(e);
                }
                // check write access
                SecurityManager security = System.getSecurityManager();
                if (security != null) {
                  try {
                    security.checkWrite(mDestination);
                  } catch (SecurityException se) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException(se);
                  }
                }
              }
            } else if (category == JobName.class) {
                jobName = ((JobName)attr).getValue();
            } else if (category == Copies.class) {
                copies = ((Copies)attr).getValue();
            } else if (category == Media.class) {
              if (attr instanceof MediaSizeName) {
                    mediaName = (MediaSizeName)attr;
                    // If requested MediaSizeName is not supported,
                    // get the corresponding media size - this will
                    // be used to create a new PageFormat.
                    if (!service.isAttributeValueSupported(attr, null, null)) {
                        mediaSize = MediaSize.getMediaSizeForName(mediaName);
                    }
                }
            } else if (category == OrientationRequested.class) {
                orient = (OrientationRequested)attr;
            }
        }
    }
 
Example 15
Source File: PrintJob2D.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public boolean printDialog() {

        boolean proceedWithPrint = false;

        printerJob = PrinterJob.getPrinterJob();
        if (printerJob == null) {
            return false;
        }
        DialogType d = this.jobAttributes.getDialog();
        PrintService pServ = printerJob.getPrintService();
        if ((pServ == null) &&  (d == DialogType.NONE)){
            return false;
        }
        copyAttributes(pServ);

        DefaultSelectionType select =
            this.jobAttributes.getDefaultSelection();
        if (select == DefaultSelectionType.RANGE) {
            attributes.add(SunPageSelection.RANGE);
        } else if (select == DefaultSelectionType.SELECTION) {
            attributes.add(SunPageSelection.SELECTION);
        } else {
            attributes.add(SunPageSelection.ALL);
        }

        if (frame != null) {
             attributes.add(new DialogOwner(frame));
         }

        if ( d == DialogType.NONE) {
            proceedWithPrint = true;
        } else {
            if (d == DialogType.NATIVE) {
                attributes.add(DialogTypeSelection.NATIVE);
            }  else { //  (d == DialogType.COMMON)
                attributes.add(DialogTypeSelection.COMMON);
            }
            if (proceedWithPrint = printerJob.printDialog(attributes)) {
                if (pServ == null) {
                    // Windows gives an option to install a service
                    // when it detects there are no printers so
                    // we make sure we get the updated print service.
                    pServ = printerJob.getPrintService();
                    if (pServ == null) {
                        return false;
                    }
                }
                updateAttributes();
                translateOutputProps();
            }
        }

        if (proceedWithPrint) {

            JobName jname = (JobName)attributes.get(JobName.class);
            if (jname != null) {
                printerJob.setJobName(jname.toString());
            }

            pageFormat = new PageFormat();

            Media media = (Media)attributes.get(Media.class);
            MediaSize mediaSize =  null;
            if (media != null  && media instanceof MediaSizeName) {
                mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
            }

            Paper p = pageFormat.getPaper();
            if (mediaSize != null) {
                p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
                          mediaSize.getY(MediaSize.INCH)*72.0);
            }

            if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
                // AWT uses 1/4" borders by default
                p.setImageableArea(18.0, 18.0,
                                   p.getWidth()-36.0,
                                   p.getHeight()-36.0);
            } else {
                p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
            }

            pageFormat.setPaper(p);

            OrientationRequested orient =
               (OrientationRequested)attributes.get(OrientationRequested.class);
            if (orient!= null &&
                orient == OrientationRequested.REVERSE_LANDSCAPE) {
                pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
            } else if (orient == OrientationRequested.LANDSCAPE) {
                pageFormat.setOrientation(PageFormat.LANDSCAPE);
            } else {
                pageFormat.setOrientation(PageFormat.PORTRAIT);
                }

            printerJob.setPrintable(this, pageFormat);

        }

        return proceedWithPrint;
    }
 
Example 16
Source File: PrinterJob.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calculates a <code>PageFormat</code> with values consistent with those
 * supported by the current <code>PrintService</code> for this job
 * (ie the value returned by <code>getPrintService()</code>) and media,
 * printable area and orientation contained in <code>attributes</code>.
 * <p>
 * Calling this method does not update the job.
 * It is useful for clients that have a set of attributes obtained from
 * <code>printDialog(PrintRequestAttributeSet attributes)</code>
 * and need a PageFormat to print a Pageable object.
 * @param attributes a set of printing attributes, for example obtained
 * from calling printDialog. If <code>attributes</code> is null a default
 * PageFormat is returned.
 * @return a <code>PageFormat</code> whose settings conform with
 * those of the current service and the specified attributes.
 * @since 1.6
 */
public PageFormat getPageFormat(PrintRequestAttributeSet attributes) {

    PrintService service = getPrintService();
    PageFormat pf = defaultPage();

    if (service == null || attributes == null) {
        return pf;
    }

    Media media = (Media)attributes.get(Media.class);
    MediaPrintableArea mpa =
        (MediaPrintableArea)attributes.get(MediaPrintableArea.class);
    OrientationRequested orientReq =
       (OrientationRequested)attributes.get(OrientationRequested.class);

    if (media == null && mpa == null && orientReq == null) {
       return pf;
    }
    Paper paper = pf.getPaper();

    /* If there's a media but no media printable area, we can try
     * to retrieve the default value for mpa and use that.
     */
    if (mpa == null && media != null &&
        service.isAttributeCategorySupported(MediaPrintableArea.class)) {
        Object mpaVals =
            service.getSupportedAttributeValues(MediaPrintableArea.class,
                                                null, attributes);
        if (mpaVals instanceof MediaPrintableArea[] &&
            ((MediaPrintableArea[])mpaVals).length > 0) {
            mpa = ((MediaPrintableArea[])mpaVals)[0];
        }
    }

    if (media != null &&
        service.isAttributeValueSupported(media, null, attributes)) {
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize msz = MediaSize.getMediaSizeForName(msn);
            if (msz != null) {
                double inch = 72.0;
                double paperWid = msz.getX(MediaSize.INCH) * inch;
                double paperHgt = msz.getY(MediaSize.INCH) * inch;
                paper.setSize(paperWid, paperHgt);
                if (mpa == null) {
                    paper.setImageableArea(inch, inch,
                                           paperWid-2*inch,
                                           paperHgt-2*inch);
                }
            }
        }
    }

    if (mpa != null &&
        service.isAttributeValueSupported(mpa, null, attributes)) {
        float [] printableArea =
            mpa.getPrintableArea(MediaPrintableArea.INCH);
        for (int i=0; i < printableArea.length; i++) {
            printableArea[i] = printableArea[i]*72.0f;
        }
        paper.setImageableArea(printableArea[0], printableArea[1],
                               printableArea[2], printableArea[3]);
    }

    if (orientReq != null &&
        service.isAttributeValueSupported(orientReq, null, attributes)) {
        int orient;
        if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
            orient = PageFormat.REVERSE_LANDSCAPE;
        } else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
            orient = PageFormat.LANDSCAPE;
        } else {
            orient = PageFormat.PORTRAIT;
        }
        pf.setOrientation(orient);
    }

    pf.setPaper(paper);
    pf = validatePage(pf);
    return pf;
}
 
Example 17
Source File: UnixPrintJob.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private void getAttributeValues(DocFlavor flavor) throws PrintException {
    Attribute attr;
    Class category;

    if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
        fidelity = true;
    } else {
        fidelity = false;
    }

    Attribute []attrs = reqAttrSet.toArray();
    for (int i=0; i<attrs.length; i++) {
        attr = attrs[i];
        category = attr.getCategory();
        if (fidelity == true) {
            if (!service.isAttributeCategorySupported(category)) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintJobAttributeException(
                    "unsupported category: " + category, category, null);
            } else if
                (!service.isAttributeValueSupported(attr, flavor, null)) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintJobAttributeException(
                    "unsupported attribute: " + attr, null, attr);
            }
        }
        if (category == Destination.class) {
            URI uri = ((Destination)attr).getURI();
            if (!"file".equals(uri.getScheme())) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("Not a file: URI");
            } else {
                try {
                    mDestType = DESTFILE;
                    mDestination = (new File(uri)).getPath();
                } catch (Exception e) {
                    throw new PrintException(e);
                }
                // check write access
                SecurityManager security = System.getSecurityManager();
                if (security != null) {
                  try {
                    security.checkWrite(mDestination);
                  } catch (SecurityException se) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException(se);
                  }
                }
            }
        } else if (category == JobSheets.class) {
            if ((JobSheets)attr == JobSheets.NONE) {
               mNoJobSheet = true;
            }
        } else if (category == JobName.class) {
            jobName = ((JobName)attr).getValue();
        } else if (category == Copies.class) {
            copies = ((Copies)attr).getValue();
        } else if (category == Media.class) {
            if (attr instanceof MediaSizeName) {
                mediaName = (MediaSizeName)attr;
                IPPPrintService.debug_println(debugPrefix+
                                              "mediaName "+mediaName);
            if (!service.isAttributeValueSupported(attr, null, null)) {
                mediaSize = MediaSize.getMediaSizeForName(mediaName);
            }
          } else if (attr instanceof CustomMediaTray) {
              customTray = (CustomMediaTray)attr;
          }
        } else if (category == OrientationRequested.class) {
            orient = (OrientationRequested)attr;
        } else if (category == NumberUp.class) {
            nUp = (NumberUp)attr;
        } else if (category == Sides.class) {
            sides = (Sides)attr;
        }
    }
}
 
Example 18
Source File: PSStreamPrintJob.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void getAttributeValues(DocFlavor flavor) throws PrintException {

        Attribute attr;
        Class category;

        if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
            fidelity = true;
        } else {
            fidelity = false;
        }

        Attribute []attrs = reqAttrSet.toArray();
        for (int i=0; i<attrs.length; i++) {
            attr = attrs[i];
            category = attr.getCategory();
            if (fidelity == true) {
                if (!service.isAttributeCategorySupported(category)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported category: " + category, category, null);
                } else if
                    (!service.isAttributeValueSupported(attr, flavor, null)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported attribute: " + attr, null, attr);
                }
            }
            if (category == JobName.class) {
                jobName = ((JobName)attr).getValue();
            } else if (category == Copies.class) {
                copies = ((Copies)attr).getValue();
            } else if (category == Media.class) {
                if (attr instanceof MediaSizeName &&
                    service.isAttributeValueSupported(attr, null, null)) {
                    mediaSize =
                        MediaSize.getMediaSizeForName((MediaSizeName)attr);
                }
            } else if (category == OrientationRequested.class) {
                orient = (OrientationRequested)attr;
            }
        }
    }
 
Example 19
Source File: PrintJob2D.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public boolean printDialog() {

        boolean proceedWithPrint = false;

        printerJob = PrinterJob.getPrinterJob();
        if (printerJob == null) {
            return false;
        }
        DialogType d = this.jobAttributes.getDialog();
        PrintService pServ = printerJob.getPrintService();
        if ((pServ == null) &&  (d == DialogType.NONE)){
            return false;
        }
        copyAttributes(pServ);

        DefaultSelectionType select =
            this.jobAttributes.getDefaultSelection();
        if (select == DefaultSelectionType.RANGE) {
            attributes.add(SunPageSelection.RANGE);
        } else if (select == DefaultSelectionType.SELECTION) {
            attributes.add(SunPageSelection.SELECTION);
        } else {
            attributes.add(SunPageSelection.ALL);
        }

        if (frame != null) {
             attributes.add(new DialogOwner(frame));
         }

        if ( d == DialogType.NONE) {
            proceedWithPrint = true;
        } else {
            if (d == DialogType.NATIVE) {
                attributes.add(DialogTypeSelection.NATIVE);
            }  else { //  (d == DialogType.COMMON)
                attributes.add(DialogTypeSelection.COMMON);
            }
            if (proceedWithPrint = printerJob.printDialog(attributes)) {
                if (pServ == null) {
                    // Windows gives an option to install a service
                    // when it detects there are no printers so
                    // we make sure we get the updated print service.
                    pServ = printerJob.getPrintService();
                    if (pServ == null) {
                        return false;
                    }
                }
                updateAttributes();
                translateOutputProps();
            }
        }

        if (proceedWithPrint) {

            JobName jname = (JobName)attributes.get(JobName.class);
            if (jname != null) {
                printerJob.setJobName(jname.toString());
            }

            pageFormat = new PageFormat();

            Media media = (Media)attributes.get(Media.class);
            MediaSize mediaSize =  null;
            if (media != null  && media instanceof MediaSizeName) {
                mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
            }

            Paper p = pageFormat.getPaper();
            if (mediaSize != null) {
                p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
                          mediaSize.getY(MediaSize.INCH)*72.0);
            }

            if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
                // AWT uses 1/4" borders by default
                p.setImageableArea(18.0, 18.0,
                                   p.getWidth()-36.0,
                                   p.getHeight()-36.0);
            } else {
                p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
            }

            pageFormat.setPaper(p);

            OrientationRequested orient =
               (OrientationRequested)attributes.get(OrientationRequested.class);
            if (orient!= null &&
                orient == OrientationRequested.REVERSE_LANDSCAPE) {
                pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
            } else if (orient == OrientationRequested.LANDSCAPE) {
                pageFormat.setOrientation(PageFormat.LANDSCAPE);
            } else {
                pageFormat.setOrientation(PageFormat.PORTRAIT);
                }

            printerJob.setPrintable(this, pageFormat);

        }

        return proceedWithPrint;
    }
 
Example 20
Source File: PSStreamPrintJob.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private void getAttributeValues(DocFlavor flavor) throws PrintException {

        Attribute attr;
        Class category;

        if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
            fidelity = true;
        } else {
            fidelity = false;
        }

        Attribute []attrs = reqAttrSet.toArray();
        for (int i=0; i<attrs.length; i++) {
            attr = attrs[i];
            category = attr.getCategory();
            if (fidelity == true) {
                if (!service.isAttributeCategorySupported(category)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported category: " + category, category, null);
                } else if
                    (!service.isAttributeValueSupported(attr, flavor, null)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported attribute: " + attr, null, attr);
                }
            }
            if (category == JobName.class) {
                jobName = ((JobName)attr).getValue();
            } else if (category == Copies.class) {
                copies = ((Copies)attr).getValue();
            } else if (category == Media.class) {
                if (attr instanceof MediaSizeName &&
                    service.isAttributeValueSupported(attr, null, null)) {
                    mediaSize =
                        MediaSize.getMediaSizeForName((MediaSizeName)attr);
                }
            } else if (category == OrientationRequested.class) {
                orient = (OrientationRequested)attr;
            }
        }
    }