Search in sources :

Example 1 with PageRanges

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

the class CPrinterJob method print.

@Override
public void print(PrintRequestAttributeSet attributes) throws PrinterException {
    // NOTE: Some of this code is copied from RasterPrinterJob.
    // this code uses javax.print APIs
    // this will make it print directly to the printer
    // this will not work if the user clicks on the "Preview" button
    // However if the printer is a StreamPrintService, its the right path.
    PrintService psvc = getPrintService();
    if (psvc == null) {
        throw new PrinterException("No print service found.");
    }
    if (psvc instanceof StreamPrintService) {
        spoolToService(psvc, attributes);
        return;
    }
    setAttributes(attributes);
    // throw exception for invalid destination
    if (destinationAttr != null) {
        validateDestination(destinationAttr);
    }
    /* Get the range of pages we are to print. If the
         * last page to print is unknown, then we print to
         * the end of the document. Note that firstPage
         * and lastPage are 0 based page indices.
         */
    int firstPage = getFirstPage();
    int lastPage = getLastPage();
    if (lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) {
        int totalPages = mDocument.getNumberOfPages();
        if (totalPages != Pageable.UNKNOWN_NUMBER_OF_PAGES) {
            lastPage = mDocument.getNumberOfPages() - 1;
        }
    }
    try {
        synchronized (this) {
            performingPrinting = true;
            userCancelled = false;
        }
        //Add support for PageRange
        PageRanges pr = (attributes == null) ? null : (PageRanges) attributes.get(PageRanges.class);
        int[][] prMembers = (pr == null) ? new int[0][0] : pr.getMembers();
        int loopi = 0;
        do {
            if (EventQueue.isDispatchThread()) {
                // This is an AWT EventQueue, and this print rendering loop needs to block it.
                onEventThread = true;
                printingLoop = AccessController.doPrivileged(new PrivilegedAction<SecondaryLoop>() {

                    @Override
                    public SecondaryLoop run() {
                        return Toolkit.getDefaultToolkit().getSystemEventQueue().createSecondaryLoop();
                    }
                });
                try {
                    //  it wait and block this thread.
                    if (printLoop(false, firstPage, lastPage)) {
                        // Start a secondary loop on EDT until printing operation is finished or cancelled
                        printingLoop.enter();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                // Fire off the print rendering loop on the AppKit, and block this thread
                //  until it is done.
                // But don't actually block... we need to come back here!
                onEventThread = false;
                try {
                    printLoop(true, firstPage, lastPage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (++loopi < prMembers.length) {
                firstPage = prMembers[loopi][0] - 1;
                lastPage = prMembers[loopi][1] - 1;
            }
        } while (loopi < prMembers.length);
    } finally {
        synchronized (this) {
            // NOTE: Native code shouldn't allow exceptions out while
            // printing. They should cancel the print loop.
            performingPrinting = false;
            notify();
        }
        if (printingLoop != null) {
            printingLoop.exit();
        }
    }
// Normalize the collated, # copies, numPages, first/last pages. Need to
//  make note of pageRangesAttr.
// Set up NSPrintInfo with the java settings (PageFormat & Paper).
// Create an NSView for printing. Have knowsPageRange return YES, and give the correct
//  range, or MAX? if unknown. Have rectForPage do a peekGraphics check before returning
//  the rectangle. Have drawRect do the real render of the page. Have printJobTitle do
//  the right thing.
// Call NSPrintOperation, it will call NSView.drawRect: for each page.
// NSView.drawRect: will create a CPrinterGraphics with the current CGContextRef, and then
//  pass this Graphics onto the Printable with the appropriate PageFormat and index.
// Need to be able to cancel the NSPrintOperation (using code from RasterPrinterJob, be
//  sure to initialize userCancelled and performingPrinting member variables).
// Extensions available from AppKit: Print to PDF or EPS file!
}
Also used : PageRanges(javax.print.attribute.standard.PageRanges) PrivilegedAction(java.security.PrivilegedAction) java.awt.print(java.awt.print) javax.print(javax.print) sun.print(sun.print)

Example 2 with PageRanges

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

the class CPrinterJob method setAttributes.

@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);
        // 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);
        }
    }
}
Also used : PageRanges(javax.print.attribute.standard.PageRanges)

Example 3 with PageRanges

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

the class PSStreamPrintService method getSupportedAttributeValues.

public Object getSupportedAttributeValues(Class<? extends Attribute> category, DocFlavor flavor, AttributeSet attributes) {
    if (category == null) {
        throw new NullPointerException("null category");
    }
    if (!Attribute.class.isAssignableFrom(category)) {
        throw new IllegalArgumentException(category + " does not implement Attribute");
    }
    if (flavor != null && !isDocFlavorSupported(flavor)) {
        throw new IllegalArgumentException(flavor + " is an unsupported flavor");
    }
    if (!isAttributeCategorySupported(category)) {
        return null;
    }
    if (category == Chromaticity.class) {
        Chromaticity[] arr = new Chromaticity[1];
        arr[0] = Chromaticity.COLOR;
        //arr[1] = Chromaticity.MONOCHROME;
        return (arr);
    } else if (category == JobName.class) {
        return new JobName("", null);
    } else if (category == RequestingUserName.class) {
        return new RequestingUserName("", null);
    } else if (category == OrientationRequested.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            OrientationRequested[] arr = new OrientationRequested[3];
            arr[0] = OrientationRequested.PORTRAIT;
            arr[1] = OrientationRequested.LANDSCAPE;
            arr[2] = OrientationRequested.REVERSE_LANDSCAPE;
            return arr;
        } else {
            return null;
        }
    } else if ((category == Copies.class) || (category == CopiesSupported.class)) {
        return new CopiesSupported(1, MAXCOPIES);
    } else if (category == Media.class) {
        Media[] arr = new Media[mediaSizes.length];
        System.arraycopy(mediaSizes, 0, arr, 0, mediaSizes.length);
        return arr;
    } else if (category == Fidelity.class) {
        Fidelity[] arr = new Fidelity[2];
        arr[0] = Fidelity.FIDELITY_FALSE;
        arr[1] = Fidelity.FIDELITY_TRUE;
        return arr;
    } else if (category == MediaPrintableArea.class) {
        if (attributes == null) {
            return null;
        }
        MediaSize mediaSize = (MediaSize) attributes.get(MediaSize.class);
        if (mediaSize == null) {
            Media media = (Media) attributes.get(Media.class);
            if (media != null && media instanceof MediaSizeName) {
                MediaSizeName msn = (MediaSizeName) media;
                mediaSize = MediaSize.getMediaSizeForName(msn);
            }
        }
        if (mediaSize == null) {
            return null;
        } else {
            MediaPrintableArea[] arr = new MediaPrintableArea[1];
            float w = mediaSize.getX(MediaSize.INCH);
            float h = mediaSize.getY(MediaSize.INCH);
            /* For dimensions >= 5 inches use 0.5 inch margins.
                 * For smaller dimensions, use 10% margins.
                 */
            float xmargin = 0.5f;
            float ymargin = 0.5f;
            if (w < 5f) {
                xmargin = w / 10;
            }
            if (h < 5f) {
                ymargin = h / 10;
            }
            arr[0] = new MediaPrintableArea(xmargin, ymargin, w - 2 * xmargin, h - 2 * ymargin, MediaSize.INCH);
            return arr;
        }
    } else if (category == PageRanges.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            PageRanges[] arr = new PageRanges[1];
            arr[0] = new PageRanges(1, Integer.MAX_VALUE);
            return arr;
        } else {
            return null;
        }
    } else if (category == SheetCollate.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            SheetCollate[] arr = new SheetCollate[2];
            arr[0] = SheetCollate.UNCOLLATED;
            arr[1] = SheetCollate.COLLATED;
            return arr;
        } else {
            SheetCollate[] arr = new SheetCollate[1];
            arr[0] = SheetCollate.UNCOLLATED;
            return arr;
        }
    } else if (category == Sides.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            Sides[] arr = new Sides[3];
            arr[0] = Sides.ONE_SIDED;
            arr[1] = Sides.TWO_SIDED_LONG_EDGE;
            arr[2] = Sides.TWO_SIDED_SHORT_EDGE;
            return arr;
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : Fidelity(javax.print.attribute.standard.Fidelity) PageRanges(javax.print.attribute.standard.PageRanges) MediaSize(javax.print.attribute.standard.MediaSize) PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) Attribute(javax.print.attribute.Attribute) MediaSizeName(javax.print.attribute.standard.MediaSizeName) JobName(javax.print.attribute.standard.JobName) Media(javax.print.attribute.standard.Media) OrientationRequested(javax.print.attribute.standard.OrientationRequested) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) SheetCollate(javax.print.attribute.standard.SheetCollate) RequestingUserName(javax.print.attribute.standard.RequestingUserName) CopiesSupported(javax.print.attribute.standard.CopiesSupported) Chromaticity(javax.print.attribute.standard.Chromaticity) Sides(javax.print.attribute.standard.Sides)

Example 4 with PageRanges

use of javax.print.attribute.standard.PageRanges 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 5 with PageRanges

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

the class WPrinterJob method setRangeCopiesAttribute.

private final void setRangeCopiesAttribute(int from, int to, boolean isRangeSet, int copies) {
    if (attributes != null) {
        if (isRangeSet) {
            attributes.add(new PageRanges(from, to));
            setPageRange(from, to);
        }
        defaultCopies = false;
        attributes.add(new Copies(copies));
        /* Since this is called from native to tell Java to sync
             * up with native, we don't call this class's own setCopies()
             * method which is mainly to send the value down to native
             */
        super.setCopies(copies);
        mAttCopies = copies;
    }
}
Also used : PageRanges(javax.print.attribute.standard.PageRanges) Copies(javax.print.attribute.standard.Copies)

Aggregations

PageRanges (javax.print.attribute.standard.PageRanges)9 MediaSizeName (javax.print.attribute.standard.MediaSizeName)6 Chromaticity (javax.print.attribute.standard.Chromaticity)5 Destination (javax.print.attribute.standard.Destination)5 JobName (javax.print.attribute.standard.JobName)5 Media (javax.print.attribute.standard.Media)5 OrientationRequested (javax.print.attribute.standard.OrientationRequested)5 Sides (javax.print.attribute.standard.Sides)5 File (java.io.File)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 Attribute (javax.print.attribute.Attribute)4 PrintServiceAttribute (javax.print.attribute.PrintServiceAttribute)4 Copies (javax.print.attribute.standard.Copies)4 Fidelity (javax.print.attribute.standard.Fidelity)4 MediaPrintableArea (javax.print.attribute.standard.MediaPrintableArea)4 RequestingUserName (javax.print.attribute.standard.RequestingUserName)4 SheetCollate (javax.print.attribute.standard.SheetCollate)4 CopiesSupported (javax.print.attribute.standard.CopiesSupported)3 PrintService (javax.print.PrintService)2