Search in sources :

Example 11 with Media

use of javax.print.attribute.standard.Media in project jdk8u_jdk by JetBrains.

the class RasterPrinterJob method setAttributes.

/* subclasses may need to pull extra information out of the attribute set
     * They can override this method & call super.setAttributes()
     */
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
    /*  reset all values to defaults */
    setCollated(false);
    sidesAttr = null;
    pageRangesAttr = null;
    copiesAttr = 0;
    jobNameAttr = null;
    userNameAttr = null;
    destinationAttr = null;
    collateAttReq = false;
    PrintService service = getPrintService();
    if (attributes == null || service == null) {
        return;
    }
    boolean fidelity = false;
    Fidelity attrFidelity = (Fidelity) attributes.get(Fidelity.class);
    if (attrFidelity != null && attrFidelity == Fidelity.FIDELITY_TRUE) {
        fidelity = true;
    }
    if (fidelity == true) {
        AttributeSet unsupported = service.getUnsupportedAttributes(DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes);
        if (unsupported != null) {
            throw new PrinterException("Fidelity cannot be satisfied");
        }
    }
    /*
         * Since we have verified supported values if fidelity is true,
         * we can either ignore unsupported values, or substitute a
         * reasonable alternative
         */
    SheetCollate collateAttr = (SheetCollate) attributes.get(SheetCollate.class);
    if (isSupportedValue(collateAttr, attributes)) {
        setCollated(collateAttr == SheetCollate.COLLATED);
    }
    sidesAttr = (Sides) attributes.get(Sides.class);
    if (!isSupportedValue(sidesAttr, attributes)) {
        sidesAttr = Sides.ONE_SIDED;
    }
    pageRangesAttr = (PageRanges) attributes.get(PageRanges.class);
    if (!isSupportedValue(pageRangesAttr, attributes)) {
        pageRangesAttr = null;
    } else {
        if ((SunPageSelection) attributes.get(SunPageSelection.class) == SunPageSelection.RANGE) {
            // get to, from, min, max page ranges
            int[][] range = pageRangesAttr.getMembers();
            // setPageRanges uses 0-based indexing so we subtract 1
            setPageRange(range[0][0] - 1, range[0][1] - 1);
        } else {
            setPageRange(-1, -1);
        }
    }
    Copies copies = (Copies) attributes.get(Copies.class);
    if (isSupportedValue(copies, attributes) || (!fidelity && copies != null)) {
        copiesAttr = copies.getValue();
        setCopies(copiesAttr);
    } else {
        copiesAttr = getCopies();
    }
    Destination destination = (Destination) attributes.get(Destination.class);
    if (isSupportedValue(destination, attributes)) {
        try {
            // Old code (new File(destination.getURI())).getPath()
            // would generate a "URI is not hierarchical" IAE
            // for "file:out.prn" so we use getSchemeSpecificPart instead
            destinationAttr = "" + new File(destination.getURI().getSchemeSpecificPart());
        } catch (Exception e) {
            // paranoid exception
            Destination defaultDest = (Destination) service.getDefaultAttributeValue(Destination.class);
            if (defaultDest != null) {
                destinationAttr = "" + new File(defaultDest.getURI().getSchemeSpecificPart());
            }
        }
    }
    JobSheets jobSheets = (JobSheets) attributes.get(JobSheets.class);
    if (jobSheets != null) {
        noJobSheet = jobSheets == JobSheets.NONE;
    }
    JobName jobName = (JobName) attributes.get(JobName.class);
    if (isSupportedValue(jobName, attributes) || (!fidelity && jobName != null)) {
        jobNameAttr = jobName.getValue();
        setJobName(jobNameAttr);
    } else {
        jobNameAttr = getJobName();
    }
    RequestingUserName userName = (RequestingUserName) attributes.get(RequestingUserName.class);
    if (isSupportedValue(userName, attributes) || (!fidelity && userName != null)) {
        userNameAttr = userName.getValue();
    } else {
        try {
            userNameAttr = getUserName();
        } catch (SecurityException e) {
            userNameAttr = "";
        }
    }
    /* OpenBook is used internally only when app uses Printable.
         * This is the case when we use the values from the attribute set.
         */
    Media media = (Media) attributes.get(Media.class);
    OrientationRequested orientReq = (OrientationRequested) attributes.get(OrientationRequested.class);
    MediaPrintableArea mpa = (MediaPrintableArea) attributes.get(MediaPrintableArea.class);
    if ((orientReq != null || media != null || mpa != null) && getPageable() instanceof OpenBook) {
        /* We could almost(!) use PrinterJob.getPageFormat() except
             * here we need to start with the PageFormat from the OpenBook :
             */
        Pageable pageable = getPageable();
        Printable printable = pageable.getPrintable(0);
        PageFormat pf = (PageFormat) pageable.getPageFormat(0).clone();
        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 (isSupportedValue(orientReq, attributes) || (!fidelity && orientReq != null)) {
            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);
        }
        if (isSupportedValue(media, attributes) || (!fidelity && media != null)) {
            if (media instanceof MediaSizeName) {
                MediaSizeName msn = (MediaSizeName) media;
                MediaSize msz = MediaSize.getMediaSizeForName(msn);
                if (msz != null) {
                    float paperWid = msz.getX(MediaSize.INCH) * 72.0f;
                    float paperHgt = msz.getY(MediaSize.INCH) * 72.0f;
                    paper.setSize(paperWid, paperHgt);
                    if (mpa == null) {
                        paper.setImageableArea(72.0, 72.0, paperWid - 144.0, paperHgt - 144.0);
                    }
                }
            }
        }
        if (isSupportedValue(mpa, attributes) || (!fidelity && mpa != null)) {
            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]);
        }
        pf.setPaper(paper);
        pf = validatePage(pf);
        setPrintable(printable, pf);
    } else {
        // for AWT where pageable is not an instance of OpenBook,
        // we need to save paper info
        this.attributes = attributes;
    }
}
Also used : Fidelity(javax.print.attribute.standard.Fidelity) Destination(javax.print.attribute.standard.Destination) JobName(javax.print.attribute.standard.JobName) PrinterException(java.awt.print.PrinterException) OrientationRequested(javax.print.attribute.standard.OrientationRequested) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) PageFormat(java.awt.print.PageFormat) Pageable(java.awt.print.Pageable) Copies(javax.print.attribute.standard.Copies) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) JobSheets(javax.print.attribute.standard.JobSheets) PrinterAbortException(java.awt.print.PrinterAbortException) HeadlessException(java.awt.HeadlessException) PrintException(javax.print.PrintException) PrinterException(java.awt.print.PrinterException) IOException(java.io.IOException) AttributeSet(javax.print.attribute.AttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) SheetCollate(javax.print.attribute.standard.SheetCollate) RequestingUserName(javax.print.attribute.standard.RequestingUserName) Paper(java.awt.print.Paper) Printable(java.awt.print.Printable) File(java.io.File)

Example 12 with Media

use of javax.print.attribute.standard.Media in project jdk8u_jdk by JetBrains.

the class PrintJob2D method printDialog.

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;
}
Also used : PageFormat(java.awt.print.PageFormat) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) JobName(javax.print.attribute.standard.JobName) Media(javax.print.attribute.standard.Media) Paper(java.awt.print.Paper) OrientationRequested(javax.print.attribute.standard.OrientationRequested) PrintService(javax.print.PrintService)

Example 13 with Media

use of javax.print.attribute.standard.Media in project jdk8u_jdk by JetBrains.

the class WPrinterJob method getWin32MediaAttrib.

/** 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;
}
Also used : MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) SunAlternateMedia(sun.print.SunAlternateMedia)

Example 14 with Media

use of javax.print.attribute.standard.Media in project jdk8u_jdk by JetBrains.

the class WPrinterJob method getDevModeValues.

private void getDevModeValues(PrintRequestAttributeSet aset, DevModeValues info) {
    Copies c = (Copies) aset.get(Copies.class);
    if (c != null) {
        info.dmFields |= DM_COPIES;
        info.copies = (short) c.getValue();
    }
    SheetCollate sc = (SheetCollate) aset.get(SheetCollate.class);
    if (sc != null) {
        info.dmFields |= DM_COLLATE;
        info.collate = (sc == SheetCollate.COLLATED) ? DMCOLLATE_TRUE : DMCOLLATE_FALSE;
    }
    Chromaticity ch = (Chromaticity) aset.get(Chromaticity.class);
    if (ch != null) {
        info.dmFields |= DM_COLOR;
        if (ch == Chromaticity.COLOR) {
            info.color = DMCOLOR_COLOR;
        } else {
            info.color = DMCOLOR_MONOCHROME;
        }
    }
    Sides s = (Sides) aset.get(Sides.class);
    if (s != null) {
        info.dmFields |= DM_DUPLEX;
        if (s == Sides.TWO_SIDED_LONG_EDGE) {
            info.duplex = DMDUP_VERTICAL;
        } else if (s == Sides.TWO_SIDED_SHORT_EDGE) {
            info.duplex = DMDUP_HORIZONTAL;
        } else {
            // Sides.ONE_SIDED
            info.duplex = DMDUP_SIMPLEX;
        }
    }
    OrientationRequested or = (OrientationRequested) aset.get(OrientationRequested.class);
    if (or != null) {
        info.dmFields |= DM_ORIENTATION;
        info.orient = (or == OrientationRequested.LANDSCAPE) ? DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT;
    }
    Media m = (Media) aset.get(Media.class);
    if (m instanceof MediaSizeName) {
        info.dmFields |= DM_PAPERSIZE;
        MediaSizeName msn = (MediaSizeName) m;
        info.paper = (short) ((Win32PrintService) myService).findPaperID(msn);
    }
    MediaTray mt = null;
    if (m instanceof MediaTray) {
        mt = (MediaTray) m;
    }
    if (mt == null) {
        SunAlternateMedia sam = (SunAlternateMedia) aset.get(SunAlternateMedia.class);
        if (sam != null && (sam.getMedia() instanceof MediaTray)) {
            mt = (MediaTray) sam.getMedia();
        }
    }
    if (mt != null) {
        info.dmFields |= DM_DEFAULTSOURCE;
        info.bin = (short) (((Win32PrintService) myService).findTrayID(mt));
    }
    PrintQuality q = (PrintQuality) aset.get(PrintQuality.class);
    if (q != null) {
        info.dmFields |= DM_PRINTQUALITY;
        if (q == PrintQuality.DRAFT) {
            info.xres_quality = DMRES_DRAFT;
        } else if (q == PrintQuality.HIGH) {
            info.xres_quality = DMRES_HIGH;
        } else {
            info.xres_quality = DMRES_MEDIUM;
        }
    }
    PrinterResolution r = (PrinterResolution) aset.get(PrinterResolution.class);
    if (r != null) {
        info.dmFields |= DM_PRINTQUALITY | DM_YRESOLUTION;
        info.xres_quality = (short) r.getCrossFeedResolution(PrinterResolution.DPI);
        info.yres = (short) r.getFeedResolution(PrinterResolution.DPI);
    }
}
Also used : Win32PrintService(sun.print.Win32PrintService) PrinterResolution(javax.print.attribute.standard.PrinterResolution) SheetCollate(javax.print.attribute.standard.SheetCollate) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Copies(javax.print.attribute.standard.Copies) PrintQuality(javax.print.attribute.standard.PrintQuality) Media(javax.print.attribute.standard.Media) SunAlternateMedia(sun.print.SunAlternateMedia) Chromaticity(javax.print.attribute.standard.Chromaticity) SunAlternateMedia(sun.print.SunAlternateMedia) Win32MediaTray(sun.print.Win32MediaTray) MediaTray(javax.print.attribute.standard.MediaTray) Sides(javax.print.attribute.standard.Sides) OrientationRequested(javax.print.attribute.standard.OrientationRequested)

Example 15 with Media

use of javax.print.attribute.standard.Media in project jdk8u_jdk by JetBrains.

the class WPrinterJob method setAttributes.

/**
     * 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) {
        // now always use attributes, so this shouldn't happen.
        return;
    }
    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) {
        }
    }
}
Also used : Attribute(javax.print.attribute.Attribute) MediaSizeName(javax.print.attribute.standard.MediaSizeName) PrintQuality(javax.print.attribute.standard.PrintQuality) Media(javax.print.attribute.standard.Media) SunAlternateMedia(sun.print.SunAlternateMedia) Chromaticity(javax.print.attribute.standard.Chromaticity) SunAlternateMedia(sun.print.SunAlternateMedia) Win32MediaTray(sun.print.Win32MediaTray) MediaTray(javax.print.attribute.standard.MediaTray)

Aggregations

Media (javax.print.attribute.standard.Media)17 MediaSizeName (javax.print.attribute.standard.MediaSizeName)14 OrientationRequested (javax.print.attribute.standard.OrientationRequested)12 MediaPrintableArea (javax.print.attribute.standard.MediaPrintableArea)10 MediaSize (javax.print.attribute.standard.MediaSize)10 Chromaticity (javax.print.attribute.standard.Chromaticity)7 SheetCollate (javax.print.attribute.standard.SheetCollate)7 PageFormat (java.awt.print.PageFormat)5 Paper (java.awt.print.Paper)5 PrintService (javax.print.PrintService)5 Destination (javax.print.attribute.standard.Destination)5 JobName (javax.print.attribute.standard.JobName)5 PageRanges (javax.print.attribute.standard.PageRanges)5 Sides (javax.print.attribute.standard.Sides)5 Attribute (javax.print.attribute.Attribute)4 Fidelity (javax.print.attribute.standard.Fidelity)4 MediaTray (javax.print.attribute.standard.MediaTray)4 PrintQuality (javax.print.attribute.standard.PrintQuality)4 RequestingUserName (javax.print.attribute.standard.RequestingUserName)4 File (java.io.File)3