Search in sources :

Example 1 with Destination

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

the class PrintCrashTest method main.

public static void main(String[] args) throws Exception {
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    printerJob.setPrintable((graphics, pageFormat, pageIndex) -> {
        if (pageIndex != 0) {
            return Printable.NO_SUCH_PAGE;
        } else {
            Shape shape = new Rectangle(110, 110, 10, 10);
            Rectangle rect = shape.getBounds();
            BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(rect.width, rect.height, Transparency.BITMASK);
            graphics.drawImage(image, rect.x, rect.y, rect.width, rect.height, null);
            return Printable.PAGE_EXISTS;
        }
    });
    File file = null;
    try {
        HashPrintRequestAttributeSet hashPrintRequestAttributeSet = new HashPrintRequestAttributeSet();
        file = File.createTempFile("out", "ps");
        file.deleteOnExit();
        Destination destination = new Destination(file.toURI());
        hashPrintRequestAttributeSet.add(destination);
        printerJob.print(hashPrintRequestAttributeSet);
    } finally {
        if (file != null) {
            file.delete();
        }
    }
}
Also used : Destination(javax.print.attribute.standard.Destination) Shape(java.awt.Shape) Rectangle(java.awt.Rectangle) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob)

Example 2 with Destination

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

the class PSPrinterJob method printDialog.

/* Instance Methods */
/**
     * Presents the user a dialog for changing properties of the
     * print job interactively.
     * @returns false if the user cancels the dialog and
     *          true otherwise.
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
     * returns true.
     * @see java.awt.GraphicsEnvironment#isHeadless
     */
public boolean printDialog() throws HeadlessException {
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }
    if (attributes == null) {
        attributes = new HashPrintRequestAttributeSet();
    }
    attributes.add(new Copies(getCopies()));
    attributes.add(new JobName(getJobName(), null));
    boolean doPrint = false;
    DialogTypeSelection dts = (DialogTypeSelection) attributes.get(DialogTypeSelection.class);
    if (dts == DialogTypeSelection.NATIVE) {
        // Remove DialogTypeSelection.NATIVE to prevent infinite loop in
        // RasterPrinterJob.
        attributes.remove(DialogTypeSelection.class);
        doPrint = printDialog(attributes);
        // restore attribute
        attributes.add(DialogTypeSelection.NATIVE);
    } else {
        doPrint = printDialog(attributes);
    }
    if (doPrint) {
        JobName jobName = (JobName) attributes.get(JobName.class);
        if (jobName != null) {
            setJobName(jobName.getValue());
        }
        Copies copies = (Copies) attributes.get(Copies.class);
        if (copies != null) {
            setCopies(copies.getValue());
        }
        Destination dest = (Destination) attributes.get(Destination.class);
        if (dest != null) {
            try {
                mDestType = RasterPrinterJob.FILE;
                mDestination = (new File(dest.getURI())).getPath();
            } catch (Exception e) {
                mDestination = "out.ps";
            }
        } else {
            mDestType = RasterPrinterJob.PRINTER;
            PrintService pServ = getPrintService();
            if (pServ != null) {
                mDestination = pServ.getName();
                if (isMac) {
                    PrintServiceAttributeSet psaSet = pServ.getAttributes();
                    if (psaSet != null) {
                        mDestination = psaSet.get(PrinterName.class).toString();
                    }
                }
            }
        }
    }
    return doPrint;
}
Also used : Destination(javax.print.attribute.standard.Destination) HeadlessException(java.awt.HeadlessException) Copies(javax.print.attribute.standard.Copies) JobName(javax.print.attribute.standard.JobName) File(java.io.File) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) HeadlessException(java.awt.HeadlessException) CharConversionException(java.io.CharConversionException) PrinterException(java.awt.print.PrinterException) PrinterIOException(java.awt.print.PrinterIOException) IOException(java.io.IOException) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) DialogTypeSelection(javax.print.attribute.standard.DialogTypeSelection) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService)

Example 3 with Destination

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

the class PrintJob2D method updateAttributes.

private void updateAttributes() {
    Copies c = (Copies) attributes.get(Copies.class);
    jobAttributes.setCopies(c.getValue());
    SunPageSelection sel = (SunPageSelection) attributes.get(SunPageSelection.class);
    if (sel == SunPageSelection.RANGE) {
        jobAttributes.setDefaultSelection(DefaultSelectionType.RANGE);
    } else if (sel == SunPageSelection.SELECTION) {
        jobAttributes.setDefaultSelection(DefaultSelectionType.SELECTION);
    } else {
        jobAttributes.setDefaultSelection(DefaultSelectionType.ALL);
    }
    Destination dest = (Destination) attributes.get(Destination.class);
    if (dest != null) {
        jobAttributes.setDestination(DestinationType.FILE);
        jobAttributes.setFileName(dest.getURI().getPath());
    } else {
        jobAttributes.setDestination(DestinationType.PRINTER);
    }
    PrintService serv = printerJob.getPrintService();
    if (serv != null) {
        jobAttributes.setPrinter(serv.getName());
    }
    PageRanges range = (PageRanges) attributes.get(PageRanges.class);
    int[][] members = range.getMembers();
    jobAttributes.setPageRanges(members);
    SheetCollate collation = (SheetCollate) attributes.get(SheetCollate.class);
    if (collation == SheetCollate.COLLATED) {
        jobAttributes.setMultipleDocumentHandling(MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES);
    } else {
        jobAttributes.setMultipleDocumentHandling(MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES);
    }
    Sides sides = (Sides) attributes.get(Sides.class);
    if (sides == Sides.TWO_SIDED_LONG_EDGE) {
        jobAttributes.setSides(SidesType.TWO_SIDED_LONG_EDGE);
    } else if (sides == Sides.TWO_SIDED_SHORT_EDGE) {
        jobAttributes.setSides(SidesType.TWO_SIDED_SHORT_EDGE);
    } else {
        jobAttributes.setSides(SidesType.ONE_SIDED);
    }
    // PageAttributes
    Chromaticity color = (Chromaticity) attributes.get(Chromaticity.class);
    if (color == Chromaticity.COLOR) {
        pageAttributes.setColor(ColorType.COLOR);
    } else {
        pageAttributes.setColor(ColorType.MONOCHROME);
    }
    OrientationRequested orient = (OrientationRequested) attributes.get(OrientationRequested.class);
    if (orient == OrientationRequested.LANDSCAPE) {
        pageAttributes.setOrientationRequested(OrientationRequestedType.LANDSCAPE);
    } else {
        pageAttributes.setOrientationRequested(OrientationRequestedType.PORTRAIT);
    }
    PrintQuality qual = (PrintQuality) attributes.get(PrintQuality.class);
    if (qual == PrintQuality.DRAFT) {
        pageAttributes.setPrintQuality(PrintQualityType.DRAFT);
    } else if (qual == PrintQuality.HIGH) {
        pageAttributes.setPrintQuality(PrintQualityType.HIGH);
    } else {
        // NORMAL
        pageAttributes.setPrintQuality(PrintQualityType.NORMAL);
    }
    Media msn = (Media) attributes.get(Media.class);
    if (msn != null && msn instanceof MediaSizeName) {
        MediaType mType = unMapMedia((MediaSizeName) msn);
        if (mType != null) {
            pageAttributes.setMedia(mType);
        }
    }
    debugPrintAttributes(false, false);
}
Also used : Destination(javax.print.attribute.standard.Destination) PageRanges(javax.print.attribute.standard.PageRanges) MediaSizeName(javax.print.attribute.standard.MediaSizeName) PrintQuality(javax.print.attribute.standard.PrintQuality) Media(javax.print.attribute.standard.Media) OrientationRequested(javax.print.attribute.standard.OrientationRequested) PrintService(javax.print.PrintService) SheetCollate(javax.print.attribute.standard.SheetCollate) Copies(javax.print.attribute.standard.Copies) SunPageSelection(sun.print.SunPageSelection) Chromaticity(javax.print.attribute.standard.Chromaticity) Sides(javax.print.attribute.standard.Sides)

Example 4 with Destination

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

the class Win32PrintJob method getAttributeValues.

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;
                // 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;
        }
    }
}
Also used : Fidelity(javax.print.attribute.standard.Fidelity) Destination(javax.print.attribute.standard.Destination) PrintJobAttribute(javax.print.attribute.PrintJobAttribute) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) Attribute(javax.print.attribute.Attribute) MediaSizeName(javax.print.attribute.standard.MediaSizeName) JobName(javax.print.attribute.standard.JobName) Media(javax.print.attribute.standard.Media) URI(java.net.URI) java.awt.print(java.awt.print) IOException(java.io.IOException) PrintException(javax.print.PrintException) FileNotFoundException(java.io.FileNotFoundException) PrintException(javax.print.PrintException) File(java.io.File)

Example 5 with Destination

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

the class Win32MediaSize method getDefaultAttributeValue.

public Object getDefaultAttributeValue(Class<? extends Attribute> category) {
    if (category == null) {
        throw new NullPointerException("null category");
    }
    if (!Attribute.class.isAssignableFrom(category)) {
        throw new IllegalArgumentException(category + " is not an Attribute");
    }
    if (!isAttributeCategorySupported(category)) {
        return null;
    }
    int[] defaults = getDefaultPrinterSettings();
    // indices must match those in WPrinterJob.cpp
    int defPaper = defaults[0];
    int defYRes = defaults[2];
    int defQuality = defaults[3];
    int defCopies = defaults[4];
    int defOrient = defaults[5];
    int defSides = defaults[6];
    int defCollate = defaults[7];
    int defColor = defaults[8];
    if (category == Copies.class) {
        if (defCopies > 0) {
            return new Copies(defCopies);
        } else {
            return new Copies(1);
        }
    } else if (category == Chromaticity.class) {
        if (defColor == DMCOLOR_COLOR) {
            return Chromaticity.COLOR;
        } else {
            return Chromaticity.MONOCHROME;
        }
    } else if (category == JobName.class) {
        return new JobName("Java Printing", null);
    } else if (category == OrientationRequested.class) {
        if (defOrient == DMORIENT_LANDSCAPE) {
            return OrientationRequested.LANDSCAPE;
        } else {
            return OrientationRequested.PORTRAIT;
        }
    } else if (category == PageRanges.class) {
        return new PageRanges(1, Integer.MAX_VALUE);
    } else if (category == Media.class) {
        MediaSizeName msn = findWin32Media(defPaper);
        if (msn != null) {
            if (!isSupportedMedia(msn) && mediaSizeNames != null) {
                msn = mediaSizeNames[0];
                defPaper = findPaperID(msn);
            }
            return msn;
        } else {
            initMedia();
            if ((mediaSizeNames != null) && (mediaSizeNames.length > 0)) {
                // cannot be null but to be safe, add a check
                if ((idList != null) && (mediaSizes != null) && (idList.size() == mediaSizes.length)) {
                    Integer defIdObj = Integer.valueOf(defPaper);
                    int index = idList.indexOf(defIdObj);
                    if (index >= 0 && index < mediaSizes.length) {
                        return mediaSizes[index].getMediaSizeName();
                    }
                }
                return mediaSizeNames[0];
            }
        }
    } else if (category == MediaPrintableArea.class) {
        /* Verify defPaper */
        MediaSizeName msn = findWin32Media(defPaper);
        if (msn != null && !isSupportedMedia(msn) && mediaSizeNames != null) {
            defPaper = findPaperID(mediaSizeNames[0]);
        }
        float[] prnArea = getMediaPrintableArea(printer, defPaper);
        if (prnArea != null) {
            MediaPrintableArea printableArea = null;
            try {
                printableArea = new MediaPrintableArea(prnArea[0], prnArea[1], prnArea[2], prnArea[3], MediaPrintableArea.INCH);
            } catch (IllegalArgumentException e) {
            }
            return printableArea;
        }
        return null;
    } else if (category == SunAlternateMedia.class) {
        return null;
    } else if (category == Destination.class) {
        try {
            return new Destination((new File("out.prn")).toURI());
        } catch (SecurityException se) {
            try {
                return new Destination(new URI("file:out.prn"));
            } catch (URISyntaxException e) {
                return null;
            }
        }
    } else if (category == Sides.class) {
        switch(defSides) {
            case DMDUP_VERTICAL:
                return Sides.TWO_SIDED_LONG_EDGE;
            case DMDUP_HORIZONTAL:
                return Sides.TWO_SIDED_SHORT_EDGE;
            default:
                return Sides.ONE_SIDED;
        }
    } else if (category == PrinterResolution.class) {
        int yRes = defYRes;
        int xRes = defQuality;
        if ((xRes < 0) || (yRes < 0)) {
            int res = (yRes > xRes) ? yRes : xRes;
            if (res > 0) {
                return new PrinterResolution(res, res, PrinterResolution.DPI);
            }
        } else {
            return new PrinterResolution(xRes, yRes, PrinterResolution.DPI);
        }
    } else if (category == ColorSupported.class) {
        int caps = getPrinterCapabilities();
        if ((caps & DEVCAP_COLOR) != 0) {
            return ColorSupported.SUPPORTED;
        } else {
            return ColorSupported.NOT_SUPPORTED;
        }
    } else if (category == PrintQuality.class) {
        if ((defQuality < 0) && (defQuality >= DMRES_HIGH)) {
            switch(defQuality) {
                case DMRES_HIGH:
                    return PrintQuality.HIGH;
                case DMRES_MEDIUM:
                    return PrintQuality.NORMAL;
                default:
                    return PrintQuality.DRAFT;
            }
        }
    } else if (category == RequestingUserName.class) {
        String userName = "";
        try {
            userName = System.getProperty("user.name", "");
        } catch (SecurityException se) {
        }
        return new RequestingUserName(userName, null);
    } else if (category == SheetCollate.class) {
        if (defCollate == DMCOLLATE_TRUE) {
            return SheetCollate.COLLATED;
        } else {
            return SheetCollate.UNCOLLATED;
        }
    } else if (category == Fidelity.class) {
        return Fidelity.FIDELITY_FALSE;
    }
    return null;
}
Also used : Destination(javax.print.attribute.standard.Destination) Fidelity(javax.print.attribute.standard.Fidelity) ColorSupported(javax.print.attribute.standard.ColorSupported) PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) Attribute(javax.print.attribute.Attribute) JobName(javax.print.attribute.standard.JobName) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) OrientationRequested(javax.print.attribute.standard.OrientationRequested) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) PrinterResolution(javax.print.attribute.standard.PrinterResolution) Copies(javax.print.attribute.standard.Copies) Sides(javax.print.attribute.standard.Sides) PageRanges(javax.print.attribute.standard.PageRanges) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) RequestingUserName(javax.print.attribute.standard.RequestingUserName) Chromaticity(javax.print.attribute.standard.Chromaticity) File(java.io.File)

Aggregations

Destination (javax.print.attribute.standard.Destination)14 File (java.io.File)12 URI (java.net.URI)9 MediaSizeName (javax.print.attribute.standard.MediaSizeName)9 JobName (javax.print.attribute.standard.JobName)8 Copies (javax.print.attribute.standard.Copies)7 Media (javax.print.attribute.standard.Media)7 OrientationRequested (javax.print.attribute.standard.OrientationRequested)7 Fidelity (javax.print.attribute.standard.Fidelity)6 PageRanges (javax.print.attribute.standard.PageRanges)6 Sides (javax.print.attribute.standard.Sides)6 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)5 PrintService (javax.print.PrintService)5 Attribute (javax.print.attribute.Attribute)5 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)5 MediaPrintableArea (javax.print.attribute.standard.MediaPrintableArea)5 SheetCollate (javax.print.attribute.standard.SheetCollate)5 PrinterException (java.awt.print.PrinterException)4 PrintException (javax.print.PrintException)4