Java Code Examples for javax.print.attribute.PrintRequestAttributeSet#get()

The following examples show how to use javax.print.attribute.PrintRequestAttributeSet#get() . 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: CPrinterJob.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
    super.setAttributes(attributes);

    if (attributes == null) {
        return;
    }

    // See if this has an NSPrintInfo in it.
    NSPrintInfo nsPrintInfo = (NSPrintInfo)attributes.get(NSPrintInfo.class);
    if (nsPrintInfo != null) {
        fNSPrintInfo = nsPrintInfo.getValue();
    }

    PageRanges pageRangesAttr =  (PageRanges)attributes.get(PageRanges.class);
    if (isSupportedValue(pageRangesAttr, attributes)) {
        SunPageSelection rangeSelect = (SunPageSelection)attributes.get(SunPageSelection.class);
        // If rangeSelect is not null, we are using AWT's print dialog that has
        // All, Selection, and Range radio buttons
        if (rangeSelect == null || rangeSelect == SunPageSelection.RANGE) {
            int[][] range = pageRangesAttr.getMembers();
            // setPageRange will set firstPage and lastPage as called in getFirstPage
            // and getLastPage
            setPageRange(range[0][0] - 1, range[0][1] - 1);
        }
    }
}
 
Example 2
Source File: CPrinterJob.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
    super.setAttributes(attributes);

    if (attributes == null) {
        return;
    }

    PageRanges pageRangesAttr =  (PageRanges)attributes.get(PageRanges.class);
    if (isSupportedValue(pageRangesAttr, attributes)) {
        SunPageSelection rangeSelect = (SunPageSelection)attributes.get(SunPageSelection.class);
        // If rangeSelect is not null, we are using AWT's print dialog that has
        // All, Selection, and Range radio buttons
        if (rangeSelect == null || rangeSelect == SunPageSelection.RANGE) {
            int[][] range = pageRangesAttr.getMembers();
            // setPageRange will set firstPage and lastPage as called in getFirstPage
            // and getLastPage
            setPageRange(range[0][0] - 1, range[0][1] - 1);
        }
    }
}
 
Example 3
Source File: PSPrinterJob.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void setAttributes(PrintRequestAttributeSet attributes)
                             throws PrinterException {
    super.setAttributes(attributes);
    if (attributes == null) {
        return; // now always use attributes, so this shouldn't happen.
    }
    Attribute attr = attributes.get(Media.class);
    if (attr instanceof CustomMediaTray) {
        CustomMediaTray customTray = (CustomMediaTray)attr;
        String choice = customTray.getChoiceName();
        if (choice != null) {
            mOptions = " InputSlot="+ choice;
        }
    }
}
 
Example 4
Source File: CPrinterJob.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
    super.setAttributes(attributes);

    if (attributes == null) {
        return;
    }

    PageRanges pageRangesAttr =  (PageRanges)attributes.get(PageRanges.class);
    if (isSupportedValue(pageRangesAttr, attributes)) {
        SunPageSelection rangeSelect = (SunPageSelection)attributes.get(SunPageSelection.class);
        // If rangeSelect is not null, we are using AWT's print dialog that has
        // All, Selection, and Range radio buttons
        if (rangeSelect == null || rangeSelect == SunPageSelection.RANGE) {
            int[][] range = pageRangesAttr.getMembers();
            // setPageRange will set firstPage and lastPage as called in getFirstPage
            // and getLastPage
            setPageRange(range[0][0] - 1, range[0][1] - 1);
        }
    }
}
 
Example 5
Source File: PSPrinterJob.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
protected void setAttributes(PrintRequestAttributeSet attributes)
                             throws PrinterException {
    super.setAttributes(attributes);
    if (attributes == null) {
        return; // now always use attributes, so this shouldn't happen.
    }
    Attribute attr = attributes.get(Media.class);
    if (attr instanceof CustomMediaTray) {
        CustomMediaTray customTray = (CustomMediaTray)attr;
        String choice = customTray.getChoiceName();
        if (choice != null) {
            mOptions = " InputSlot="+ choice;
        }
    }
}
 
Example 6
Source File: CPrinterJob.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
    super.setAttributes(attributes);

    if (attributes == null) {
        return;
    }

    // See if this has an NSPrintInfo in it.
    NSPrintInfo nsPrintInfo = (NSPrintInfo)attributes.get(NSPrintInfo.class);
    if (nsPrintInfo != null) {
        fNSPrintInfo = nsPrintInfo.getValue();
    }

    PageRanges pageRangesAttr =  (PageRanges)attributes.get(PageRanges.class);
    if (isSupportedValue(pageRangesAttr, attributes)) {
        SunPageSelection rangeSelect = (SunPageSelection)attributes.get(SunPageSelection.class);
        // If rangeSelect is not null, we are using AWT's print dialog that has
        // All, Selection, and Range radio buttons
        if (rangeSelect == null || rangeSelect == SunPageSelection.RANGE) {
            int[][] range = pageRangesAttr.getMembers();
            // setPageRange will set firstPage and lastPage as called in getFirstPage
            // and getLastPage
            setPageRange(range[0][0] - 1, range[0][1] - 1);
        }
    }
}
 
Example 7
Source File: PrinterIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void printsWithLandscapeOrientation() throws Exception {
    PrinterEndpoint endpoint = new PrinterEndpoint();
    PrinterConfiguration configuration = new PrinterConfiguration();
    configuration.setHostname("localhost");
    configuration.setPort(631);
    configuration.setPrintername("DefaultPrinter");
    configuration.setMediaSizeName(MediaSizeName.ISO_A4);
    configuration.setInternalSides(Sides.ONE_SIDED);
    configuration.setInternalOrientation(OrientationRequested.REVERSE_LANDSCAPE);
    configuration.setMediaTray("middle");
    configuration.setSendToPrinter(false);

    PrinterProducer producer = new PrinterProducer(endpoint, configuration);
    producer.start();
    PrinterOperations printerOperations = producer.getPrinterOperations();
    PrintRequestAttributeSet attributeSet = printerOperations.getPrintRequestAttributeSet();

    Attribute attribute = attributeSet.get(OrientationRequested.class);
    Assert.assertNotNull(attribute);
    Assert.assertEquals("reverse-landscape", attribute.toString());
}
 
Example 8
Source File: RasterPrinterJob.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void setParentWindowID(PrintRequestAttributeSet attrs) {
    parentWindowID = 0L;
    onTop = (DialogOnTop)attrs.get(DialogOnTop.class);
    if (onTop != null) {
        parentWindowID = onTop.getID();
    }
}
 
Example 9
Source File: RasterPrinterJob.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void setParentWindowID(PrintRequestAttributeSet attrs) {
    parentWindowID = 0L;
    onTop = (DialogOnTop)attrs.get(DialogOnTop.class);
    if (onTop != null) {
        parentWindowID = onTop.getID();
    }
}
 
Example 10
Source File: DummyPrintTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void print(Doc doc,
                  PrintRequestAttributeSet printRequestAttributeSet)
      throws PrintException {
    System.out.println("job name: " + printRequestAttributeSet.get(JobName.class));
    System.out.println("copies: " + printRequestAttributeSet.get(Copies.class));
    if(printRequestAttributeSet.get(JobName.class) == null ||
        printRequestAttributeSet.get(Copies.class) == null) {
        throw new RuntimeException("Copies and JobName is not passed correctly");
    }
}
 
Example 11
Source File: CPrinterJob.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
    super.setAttributes(attributes);

    if (attributes == null) {
        return;
    }

    // See if this has an NSPrintInfo in it.
    NSPrintInfo nsPrintInfo = (NSPrintInfo)attributes.get(NSPrintInfo.class);
    if (nsPrintInfo != null) {
        fNSPrintInfo = nsPrintInfo.getValue();
    }

    PageRanges pageRangesAttr =  (PageRanges)attributes.get(PageRanges.class);
    if (isSupportedValue(pageRangesAttr, attributes)) {
        SunPageSelection rangeSelect = (SunPageSelection)attributes.get(SunPageSelection.class);
        // If rangeSelect is not null, we are using AWT's print dialog that has
        // All, Selection, and Range radio buttons
        if (rangeSelect == null || rangeSelect == SunPageSelection.RANGE) {
            int[][] range = pageRangesAttr.getMembers();
            // setPageRange will set firstPage and lastPage as called in getFirstPage
            // and getLastPage
            setPageRange(range[0][0] - 1, range[0][1] - 1);
        }
    }
}
 
Example 12
Source File: PrinterJob.java    From openjdk-8-source 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 13
Source File: WPrinterJob.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * copy the attributes to the native print job
 * Note that this method, and hence the re-initialisation
 * of the GDI values is done on each entry to the print dialog since
 * an app could redisplay the print dialog for the same job and
 * 1) the application may have changed attribute settings
 * 2) the application may have changed the printer.
 * In the event that the user changes the printer using the
  dialog, then it is up to GDI to report back all changed values.
 */
@Override
protected void setAttributes(PrintRequestAttributeSet attributes)
    throws PrinterException {

    // initialize attribute values
    initAttributeMembers();
    super.setAttributes(attributes);

    mAttCopies = getCopiesInt();
    mDestination = destinationAttr;

    if (attributes == null) {
        return; // now always use attributes, so this shouldn't happen.
    }
    Attribute[] attrs = attributes.toArray();
    for (int i=0; i< attrs.length; i++) {
        Attribute attr = attrs[i];
        try {
             if (attr.getCategory() == Sides.class) {
                setSidesAttrib(attr);
            }
            else if (attr.getCategory() == Chromaticity.class) {
                setColorAttrib(attr);
            }
            else if (attr.getCategory() == PrinterResolution.class) {
                setResolutionAttrib(attr);
            }
            else if (attr.getCategory() == PrintQuality.class) {
                setQualityAttrib(attr);
            }
            else if (attr.getCategory() == SheetCollate.class) {
                setCollateAttrib(attr);
            }  else if (attr.getCategory() == Media.class ||
                        attr.getCategory() == SunAlternateMedia.class) {
                /* SunAlternateMedia is used if its a tray, and
                 * any Media that is specified is not a tray.
                 */
                if (attr.getCategory() == SunAlternateMedia.class) {
                    Media media = (Media)attributes.get(Media.class);
                    if (media == null ||
                        !(media instanceof MediaTray)) {
                        attr = ((SunAlternateMedia)attr).getMedia();
                    }
                }
                if (attr instanceof MediaSizeName) {
                    setWin32MediaAttrib(attr);
                }
                if (attr instanceof MediaTray) {
                    setMediaTrayAttrib(attr);
                }
            }

        } catch (ClassCastException e) {
        }
    }
}
 
Example 14
Source File: PrinterJob.java    From jdk8u60 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 15
Source File: WPrinterJob.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * copy the attributes to the native print job
 * Note that this method, and hence the re-initialisation
 * of the GDI values is done on each entry to the print dialog since
 * an app could redisplay the print dialog for the same job and
 * 1) the application may have changed attribute settings
 * 2) the application may have changed the printer.
 * In the event that the user changes the printer using the
  dialog, then it is up to GDI to report back all changed values.
 */
protected void setAttributes(PrintRequestAttributeSet attributes)
    throws PrinterException {

    // initialize attribute values
    initAttributeMembers();
    super.setAttributes(attributes);

    mAttCopies = getCopiesInt();
    mDestination = destinationAttr;

    if (attributes == null) {
        return; // now always use attributes, so this shouldn't happen.
    }
    Attribute[] attrs = attributes.toArray();
    for (int i=0; i< attrs.length; i++) {
        Attribute attr = attrs[i];
        try {
             if (attr.getCategory() == Sides.class) {
                setSidesAttrib(attr);
            }
            else if (attr.getCategory() == Chromaticity.class) {
                setColorAttrib(attr);
            }
            else if (attr.getCategory() == PrinterResolution.class) {
                setResolutionAttrib(attr);
            }
            else if (attr.getCategory() == PrintQuality.class) {
                setQualityAttrib(attr);
            }
            else if (attr.getCategory() == SheetCollate.class) {
                setCollateAttrib(attr);
            }  else if (attr.getCategory() == Media.class ||
                        attr.getCategory() == SunAlternateMedia.class) {
                /* SunAlternateMedia is used if its a tray, and
                 * any Media that is specified is not a tray.
                 */
                if (attr.getCategory() == SunAlternateMedia.class) {
                    Media media = (Media)attributes.get(Media.class);
                    if (media == null ||
                        !(media instanceof MediaTray)) {
                        attr = ((SunAlternateMedia)attr).getMedia();
                    }
                }
                if (attr instanceof MediaSizeName) {
                    setWin32MediaAttrib(attr);
                }
                if (attr instanceof MediaTray) {
                    setMediaTrayAttrib(attr);
                }
            }

        } catch (ClassCastException e) {
        }
    }
}
 
Example 16
Source File: PrinterJob.java    From JDKSourceCode1.8 with MIT License 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: PrinterJob.java    From jdk8u-jdk 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 18
Source File: PrinterJob.java    From openjdk-jdk8u-backup 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 19
Source File: WPrinterJob.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * copy the attributes to the native print job
 * Note that this method, and hence the re-initialisation
 * of the GDI values is done on each entry to the print dialog since
 * an app could redisplay the print dialog for the same job and
 * 1) the application may have changed attribute settings
 * 2) the application may have changed the printer.
 * In the event that the user changes the printer using the
  dialog, then it is up to GDI to report back all changed values.
 */
@Override
protected void setAttributes(PrintRequestAttributeSet attributes)
    throws PrinterException {

    // initialize attribute values
    initAttributeMembers();
    super.setAttributes(attributes);

    mAttCopies = getCopiesInt();
    mDestination = destinationAttr;

    if (attributes == null) {
        return; // now always use attributes, so this shouldn't happen.
    }
    Attribute[] attrs = attributes.toArray();
    for (int i=0; i< attrs.length; i++) {
        Attribute attr = attrs[i];
        try {
             if (attr.getCategory() == Sides.class) {
                setSidesAttrib(attr);
            }
            else if (attr.getCategory() == Chromaticity.class) {
                setColorAttrib(attr);
            }
            else if (attr.getCategory() == PrinterResolution.class) {
                setResolutionAttrib(attr);
            }
            else if (attr.getCategory() == PrintQuality.class) {
                setQualityAttrib(attr);
            }
            else if (attr.getCategory() == SheetCollate.class) {
                setCollateAttrib(attr);
            }  else if (attr.getCategory() == Media.class ||
                        attr.getCategory() == SunAlternateMedia.class) {
                /* SunAlternateMedia is used if its a tray, and
                 * any Media that is specified is not a tray.
                 */
                if (attr.getCategory() == SunAlternateMedia.class) {
                    Media media = (Media)attributes.get(Media.class);
                    if (media == null ||
                        !(media instanceof MediaTray)) {
                        attr = ((SunAlternateMedia)attr).getMedia();
                    }
                }
                if (attr instanceof MediaSizeName) {
                    setWin32MediaAttrib(attr);
                }
                if (attr instanceof MediaTray) {
                    setMediaTrayAttrib(attr);
                }
            }

        } catch (ClassCastException e) {
        }
    }
}
 
Example 20
Source File: PrinterJob.java    From jdk1.8-source-analysis 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;
}