Search in sources :

Example 1 with PrinterState

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

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

the class UnixPrintService method getAttributes.

public PrintServiceAttributeSet getAttributes() {
    PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet();
    attrs.add(getPrinterName());
    attrs.add(getPrinterIsAcceptingJobs());
    PrinterState prnState = getPrinterState();
    if (prnState != null) {
        attrs.add(prnState);
    }
    PrinterStateReasons prnStateReasons = getPrinterStateReasons();
    if (prnStateReasons != null) {
        attrs.add(prnStateReasons);
    }
    attrs.add(getQueuedJobCount());
    return AttributeSetUtilities.unmodifiableView(attrs);
}
Also used : PrinterState(javax.print.attribute.standard.PrinterState) PrinterStateReasons(javax.print.attribute.standard.PrinterStateReasons) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet)

Example 3 with PrinterState

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

the class Win32MediaSize method getAttributes.

public PrintServiceAttributeSet getAttributes() {
    PrintServiceAttributeSet attrs = new HashPrintServiceAttributeSet();
    attrs.add(getPrinterName());
    attrs.add(getPrinterIsAcceptingJobs());
    PrinterState prnState = getPrinterState();
    if (prnState != null) {
        attrs.add(prnState);
    }
    PrinterStateReasons prnStateReasons = getPrinterStateReasons();
    if (prnStateReasons != null) {
        attrs.add(prnStateReasons);
    }
    attrs.add(getQueuedJobCount());
    int caps = getPrinterCapabilities();
    if ((caps & DEVCAP_COLOR) != 0) {
        attrs.add(ColorSupported.SUPPORTED);
    } else {
        attrs.add(ColorSupported.NOT_SUPPORTED);
    }
    return AttributeSetUtilities.unmodifiableView(attrs);
}
Also used : PrinterState(javax.print.attribute.standard.PrinterState) PrinterStateReasons(javax.print.attribute.standard.PrinterStateReasons) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet)

Example 4 with PrinterState

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

the class Win32PrintJob method print.

public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException {
    synchronized (this) {
        if (printing) {
            throw new PrintException("already printing");
        } else {
            printing = true;
        }
    }
    PrinterState prnState = (PrinterState) service.getAttribute(PrinterState.class);
    if (prnState == PrinterState.STOPPED) {
        PrinterStateReasons prnStateReasons = (PrinterStateReasons) service.getAttribute(PrinterStateReasons.class);
        if ((prnStateReasons != null) && (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))) {
            throw new PrintException("PrintService is no longer available.");
        }
    }
    if ((PrinterIsAcceptingJobs) (service.getAttribute(PrinterIsAcceptingJobs.class)) == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
        throw new PrintException("Printer is not accepting job.");
    }
    this.doc = doc;
    /* check if the parameters are valid before doing much processing */
    DocFlavor flavor = doc.getDocFlavor();
    Object data;
    try {
        data = doc.getPrintData();
    } catch (IOException e) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("can't get print data: " + e.toString());
    }
    if (data == null) {
        throw new PrintException("Null print data.");
    }
    if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintJobFlavorException("invalid flavor", flavor);
    }
    initializeAttributeSets(doc, attributes);
    getAttributeValues(flavor);
    String repClassName = flavor.getRepresentationClassName();
    if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
        try {
            instream = doc.getStreamForBytes();
            if (instream == null) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("No stream for data");
            }
            printableJob(new ImagePrinter(instream));
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.URL.JPEG) || flavor.equals(DocFlavor.URL.PNG)) {
        try {
            printableJob(new ImagePrinter((URL) data));
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        }
    } else if (repClassName.equals("java.awt.print.Pageable")) {
        try {
            pageableJob((Pageable) doc.getPrintData());
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (repClassName.equals("java.awt.print.Printable")) {
        try {
            printableJob((Printable) doc.getPrintData());
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (repClassName.equals("[B") || repClassName.equals("java.io.InputStream") || repClassName.equals("java.net.URL")) {
        if (repClassName.equals("java.net.URL")) {
            URL url = (URL) data;
            try {
                instream = url.openStream();
            } catch (IOException e) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(e.toString());
            }
        } else {
            try {
                instream = doc.getStreamForBytes();
            } catch (IOException ioe) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(ioe.toString());
            }
        }
        if (instream == null) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("No stream for data");
        }
        if (mDestination != null) {
            // if destination attribute is set
            try {
                FileOutputStream fos = new FileOutputStream(mDestination);
                byte[] buffer = new byte[1024];
                int cread;
                while ((cread = instream.read(buffer, 0, buffer.length)) >= 0) {
                    fos.write(buffer, 0, cread);
                }
                fos.flush();
                fos.close();
            } catch (FileNotFoundException fnfe) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(fnfe.toString());
            } catch (IOException ioe) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(ioe.toString());
            }
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
            notifyEvent(PrintJobEvent.JOB_COMPLETE);
            service.wakeNotifier();
            return;
        }
        if (!startPrintRawData(service.getName(), jobName)) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("Print job failed to start.");
        }
        BufferedInputStream bin = new BufferedInputStream(instream);
        int bread = 0;
        try {
            byte[] buffer = new byte[PRINTBUFFERLEN];
            while ((bread = bin.read(buffer, 0, PRINTBUFFERLEN)) >= 0) {
                if (!printRawData(buffer, bread)) {
                    bin.close();
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException("Problem while spooling data");
                }
            }
            bin.close();
            if (!endPrintRawData()) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("Print job failed to close properly.");
            }
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e.toString());
        } finally {
            notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
        }
    } else {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("unrecognized class: " + repClassName);
    }
    service.wakeNotifier();
}
Also used : PrinterState(javax.print.attribute.standard.PrinterState) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URL(java.net.URL) java.awt.print(java.awt.print) PrintException(javax.print.PrintException) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) PrinterStateReasons(javax.print.attribute.standard.PrinterStateReasons) DocFlavor(javax.print.DocFlavor) PrinterIsAcceptingJobs(javax.print.attribute.standard.PrinterIsAcceptingJobs)

Aggregations

PrinterState (javax.print.attribute.standard.PrinterState)4 PrinterStateReasons (javax.print.attribute.standard.PrinterStateReasons)4 HashPrintServiceAttributeSet (javax.print.attribute.HashPrintServiceAttributeSet)2 PrintServiceAttributeSet (javax.print.attribute.PrintServiceAttributeSet)2 PrinterIsAcceptingJobs (javax.print.attribute.standard.PrinterIsAcceptingJobs)2 java.awt.print (java.awt.print)1 PrinterException (java.awt.print.PrinterException)1 BufferedInputStream (java.io.BufferedInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 URL (java.net.URL)1 DocFlavor (javax.print.DocFlavor)1 PrintException (javax.print.PrintException)1 PrintService (javax.print.PrintService)1 StreamPrintService (javax.print.StreamPrintService)1 SunPageSelection (sun.print.SunPageSelection)1 SunPrinterJobService (sun.print.SunPrinterJobService)1