Search in sources :

Example 16 with PrintService

use of javax.print.PrintService in project jdk8u_jdk by JetBrains.

the class RasterPrinterJob method pageDialog.

/**
     * Display a dialog to the user allowing the modification of a
     * PageFormat instance.
     * The <code>page</code> argument is used to initialize controls
     * in the page setup dialog.
     * If the user cancels the dialog, then the method returns the
     * original <code>page</code> object unmodified.
     * If the user okays the dialog then the method returns a new
     * PageFormat object with the indicated changes.
     * In either case the original <code>page</code> object will
     * not be modified.
     * @param     page    the default PageFormat presented to the user
     *                    for modification
     * @return    the original <code>page</code> object if the dialog
     *            is cancelled, or a new PageFormat object containing
     *            the format indicated by the user if the dialog is
     *            acknowledged
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
     * returns true.
     * @see java.awt.GraphicsEnvironment#isHeadless
     * @since     1.2
     */
public PageFormat pageDialog(PageFormat page) throws HeadlessException {
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }
    final GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    PrintService service = (PrintService) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

        public Object run() {
            PrintService service = getPrintService();
            if (service == null) {
                ServiceDialog.showNoPrintService(gc);
                return null;
            }
            return service;
        }
    });
    if (service == null) {
        return page;
    }
    updatePageAttributes(service, page);
    PageFormat newPage = null;
    DialogTypeSelection dts = (DialogTypeSelection) attributes.get(DialogTypeSelection.class);
    if (dts == DialogTypeSelection.NATIVE) {
        // Remove DialogTypeSelection.NATIVE to prevent infinite loop in
        // RasterPrinterJob.
        attributes.remove(DialogTypeSelection.class);
        newPage = pageDialog(attributes);
        // restore attribute
        attributes.add(DialogTypeSelection.NATIVE);
    } else {
        newPage = pageDialog(attributes);
    }
    if (newPage == null) {
        return page;
    } else {
        return newPage;
    }
}
Also used : PageFormat(java.awt.print.PageFormat) HeadlessException(java.awt.HeadlessException) GraphicsConfiguration(java.awt.GraphicsConfiguration) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService) DialogTypeSelection(javax.print.attribute.standard.DialogTypeSelection)

Example 17 with PrintService

use of javax.print.PrintService in project jdk8u_jdk by JetBrains.

the class RasterPrinterJob method print.

public void print(PrintRequestAttributeSet attributes) throws PrinterException {
    /*
         * In the future PrinterJob will probably always dispatch
         * the print job to the PrintService.
         * This is how third party 2D Print Services will be invoked
         * when applications use the PrinterJob API.
         * However the JRE's concrete PrinterJob implementations have
         * not yet been re-worked to be implemented as standalone
         * services, and are implemented only as subclasses of PrinterJob.
         * So here we dispatch only those services we do not recognize
         * as implemented through platform subclasses of PrinterJob
         * (and this class).
         */
    PrintService psvc = getPrintService();
    debug_println("psvc = " + psvc);
    if (psvc == null) {
        throw new PrinterException("No print service found.");
    }
    // Check the list of services.  This service may have been
    // deleted already
    PrinterState prnState = (PrinterState) psvc.getAttribute(PrinterState.class);
    if (prnState == PrinterState.STOPPED) {
        PrinterStateReasons prnStateReasons = (PrinterStateReasons) psvc.getAttribute(PrinterStateReasons.class);
        if ((prnStateReasons != null) && (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))) {
            throw new PrinterException("PrintService is no longer available.");
        }
    }
    if ((PrinterIsAcceptingJobs) (psvc.getAttribute(PrinterIsAcceptingJobs.class)) == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
        throw new PrinterException("Printer is not accepting job.");
    }
    if ((psvc instanceof SunPrinterJobService) && ((SunPrinterJobService) psvc).usesClass(getClass())) {
        setAttributes(attributes);
        // throw exception for invalid destination
        if (destinationAttr != null) {
            validateDestination(destinationAttr);
        }
    } else {
        spoolToService(psvc, attributes);
        return;
    }
    /* We need to make sure that the collation and copies
         * settings are initialised */
    initPrinter();
    int numCollatedCopies = getCollatedCopies();
    int numNonCollatedCopies = getNoncollatedCopies();
    debug_println("getCollatedCopies()  " + numCollatedCopies + " getNoncollatedCopies() " + numNonCollatedCopies);
    /* 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 numPages = mDocument.getNumberOfPages();
    if (numPages == 0) {
        return;
    }
    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;
        }
        startDoc();
        if (isCancelled()) {
            cancelDoc();
        }
        // PageRanges can be set even if RANGE is not selected
        // so we need to check if it is selected.
        boolean rangeIsSelected = true;
        if (attributes != null) {
            SunPageSelection pages = (SunPageSelection) attributes.get(SunPageSelection.class);
            if ((pages != null) && (pages != SunPageSelection.RANGE)) {
                rangeIsSelected = false;
            }
        }
        debug_println("after startDoc rangeSelected? " + rangeIsSelected + " numNonCollatedCopies " + numNonCollatedCopies);
        /* Three nested loops iterate over the document. The outer loop
             * counts the number of collated copies while the inner loop
             * counts the number of nonCollated copies. Normally, one of
             * these two loops will only execute once; that is we will
             * either print collated copies or noncollated copies. The
             * middle loop iterates over the pages.
             * If a PageRanges attribute is used, it constrains the pages
             * that are imaged. If a platform subclass (though a user dialog)
             * requests a page range via setPageRange(). it too can
             * constrain the page ranges that are imaged.
             * It is expected that only one of these will be used in a
             * job but both should be able to co-exist.
             */
        for (int collated = 0; collated < numCollatedCopies; collated++) {
            for (int i = firstPage, pageResult = Printable.PAGE_EXISTS; (i <= lastPage || lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) && pageResult == Printable.PAGE_EXISTS; i++) {
                if ((pageRangesAttr != null) && rangeIsSelected) {
                    int nexti = pageRangesAttr.next(i);
                    if (nexti == -1) {
                        break;
                    } else if (nexti != i + 1) {
                        continue;
                    }
                }
                for (int nonCollated = 0; nonCollated < numNonCollatedCopies && pageResult == Printable.PAGE_EXISTS; nonCollated++) {
                    if (isCancelled()) {
                        cancelDoc();
                    }
                    debug_println("printPage " + i);
                    pageResult = printPage(mDocument, i);
                }
            }
        }
        if (isCancelled()) {
            cancelDoc();
        }
    } finally {
        // reset previousPaper in case this job is invoked again.
        previousPaper = null;
        synchronized (this) {
            if (performingPrinting) {
                endDoc();
            }
            performingPrinting = false;
            notify();
        }
    }
}
Also used : PrinterState(javax.print.attribute.standard.PrinterState) PrinterStateReasons(javax.print.attribute.standard.PrinterStateReasons) SunPrinterJobService(sun.print.SunPrinterJobService) SunPageSelection(sun.print.SunPageSelection) PrinterException(java.awt.print.PrinterException) PrinterIsAcceptingJobs(javax.print.attribute.standard.PrinterIsAcceptingJobs) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService)

Example 18 with PrintService

use of javax.print.PrintService in project jdk8u_jdk by JetBrains.

the class RasterPrinterJob method defaultPage.

/**
     * The passed in PageFormat will be copied and altered to describe
     * the default page size and orientation of the PrinterJob's
     * current printer.
     * Platform subclasses which can access the actual default paper size
     * for a printer may override this method.
     */
public PageFormat defaultPage(PageFormat page) {
    PageFormat newPage = (PageFormat) page.clone();
    newPage.setOrientation(PageFormat.PORTRAIT);
    Paper newPaper = new Paper();
    double ptsPerInch = 72.0;
    double w, h;
    Media media = null;
    PrintService service = getPrintService();
    if (service != null) {
        MediaSize size;
        media = (Media) service.getDefaultAttributeValue(Media.class);
        if (media instanceof MediaSizeName && ((size = MediaSize.getMediaSizeForName((MediaSizeName) media)) != null)) {
            w = size.getX(MediaSize.INCH) * ptsPerInch;
            h = size.getY(MediaSize.INCH) * ptsPerInch;
            newPaper.setSize(w, h);
            newPaper.setImageableArea(ptsPerInch, ptsPerInch, w - 2.0 * ptsPerInch, h - 2.0 * ptsPerInch);
            newPage.setPaper(newPaper);
            return newPage;
        }
    }
    /* Default to A4 paper outside North America.
         */
    String defaultCountry = Locale.getDefault().getCountry();
    if (// ie "C"
    !Locale.getDefault().equals(Locale.ENGLISH) && defaultCountry != null && !defaultCountry.equals(Locale.US.getCountry()) && !defaultCountry.equals(Locale.CANADA.getCountry())) {
        double mmPerInch = 25.4;
        w = Math.rint((210.0 * ptsPerInch) / mmPerInch);
        h = Math.rint((297.0 * ptsPerInch) / mmPerInch);
        newPaper.setSize(w, h);
        newPaper.setImageableArea(ptsPerInch, ptsPerInch, w - 2.0 * ptsPerInch, h - 2.0 * ptsPerInch);
    }
    newPage.setPaper(newPaper);
    return newPage;
}
Also used : PageFormat(java.awt.print.PageFormat) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) Paper(java.awt.print.Paper) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService)

Example 19 with PrintService

use of javax.print.PrintService in project jdk8u_jdk by JetBrains.

the class UnixPrintServiceLookup method getDefaultPrintService.

public synchronized PrintService getDefaultPrintService() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkPrintJobAccess();
    }
    // clear defaultPrintService
    defaultPrintService = null;
    String psuri = null;
    IPPPrintService.debug_println("isRunning ? " + (CUPSPrinter.isCupsRunning()));
    if (CUPSPrinter.isCupsRunning()) {
        String[] printerInfo = CUPSPrinter.getDefaultPrinter();
        if (printerInfo != null && printerInfo.length >= 2) {
            defaultPrinter = printerInfo[0];
            psuri = printerInfo[1];
        }
    } else {
        if (isMac() || isSysV()) {
            defaultPrinter = getDefaultPrinterNameSysV();
        } else if (isAIX()) {
            defaultPrinter = getDefaultPrinterNameAIX();
        } else {
            defaultPrinter = getDefaultPrinterNameBSD();
        }
    }
    if (defaultPrinter == null) {
        return null;
    }
    defaultPrintService = null;
    if (printServices != null) {
        for (int j = 0; j < printServices.length; j++) {
            if (defaultPrinter.equals(getPrinterDestName(printServices[j]))) {
                defaultPrintService = printServices[j];
                break;
            }
        }
    }
    if (defaultPrintService == null) {
        if (CUPSPrinter.isCupsRunning()) {
            try {
                PrintService defaultPS;
                if ((psuri != null) && !psuri.startsWith("file")) {
                    defaultPS = new IPPPrintService(defaultPrinter, psuri, true);
                } else {
                    defaultPS = new IPPPrintService(defaultPrinter, new URL("http://" + CUPSPrinter.getServer() + ":" + CUPSPrinter.getPort() + "/" + defaultPrinter));
                }
                defaultPrintService = defaultPS;
            } catch (Exception e) {
            }
        } else {
            defaultPrintService = new UnixPrintService(defaultPrinter);
        }
    }
    return defaultPrintService;
}
Also used : URL(java.net.URL) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) MultiDocPrintService(javax.print.MultiDocPrintService) PrintService(javax.print.PrintService)

Example 20 with PrintService

use of javax.print.PrintService in project jdk8u_jdk by JetBrains.

the class UnixPrintServiceLookup method addPrintServiceToList.

private int addPrintServiceToList(ArrayList printerList, PrintService ps) {
    int index = printerList.indexOf(ps);
    // Check if PrintService with same name is already in the list.
    if (CUPSPrinter.isCupsRunning() && index != -1) {
        // Bug in Linux: Duplicate entry of a remote printer
        // and treats it as local printer but it is returning wrong
        // information when queried using IPP. Workaround is to remove it.
        // Even CUPS ignores these entries as shown in lpstat or using
        // their web configuration.
        PrinterURI uri = (PrinterURI) ps.getAttribute(PrinterURI.class);
        if (uri.getURI().getHost().equals("localhost")) {
            IPPPrintService.debug_println(debugPrefix + "duplicate PrintService, ignoring the new local printer: " + ps);
            // Do not add this.
            return index;
        }
        PrintService oldPS = (PrintService) (printerList.get(index));
        uri = (PrinterURI) oldPS.getAttribute(PrinterURI.class);
        if (uri.getURI().getHost().equals("localhost")) {
            IPPPrintService.debug_println(debugPrefix + "duplicate PrintService, removing existing local printer: " + oldPS);
            printerList.remove(oldPS);
        } else {
            return index;
        }
    }
    printerList.add(ps);
    return (printerList.size() - 1);
}
Also used : PrinterURI(javax.print.attribute.standard.PrinterURI) MultiDocPrintService(javax.print.MultiDocPrintService) PrintService(javax.print.PrintService)

Aggregations

PrintService (javax.print.PrintService)48 StreamPrintService (javax.print.StreamPrintService)13 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)13 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)13 PrinterException (java.awt.print.PrinterException)10 PageFormat (java.awt.print.PageFormat)9 IOException (java.io.IOException)8 MultiDocPrintService (javax.print.MultiDocPrintService)8 DocFlavor (javax.print.DocFlavor)7 Copies (javax.print.attribute.standard.Copies)7 MediaSizeName (javax.print.attribute.standard.MediaSizeName)7 HeadlessException (java.awt.HeadlessException)6 File (java.io.File)6 DocPrintJob (javax.print.DocPrintJob)5 PrintException (javax.print.PrintException)5 Destination (javax.print.attribute.standard.Destination)5 Media (javax.print.attribute.standard.Media)5 MediaSize (javax.print.attribute.standard.MediaSize)5 Paper (java.awt.print.Paper)4 ArrayList (java.util.ArrayList)4