Search in sources :

Example 36 with PrintService

use of javax.print.PrintService 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 37 with PrintService

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

the class RasterPrinterJob method printDialog.

/**
     * Presents the user a dialog for changing properties of the
     * print job interactively.
     * The services browsable here are determined by the type of
     * service currently installed.
     * If the application installed a StreamPrintService on this
     * PrinterJob, only the available StreamPrintService (factories) are
     * browsable.
     *
     * @param attributes to store changed properties.
     * @return 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(final PrintRequestAttributeSet attributes) throws HeadlessException {
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }
    DialogTypeSelection dlg = (DialogTypeSelection) attributes.get(DialogTypeSelection.class);
    // Check for native, note that default dialog is COMMON.
    if (dlg == DialogTypeSelection.NATIVE) {
        this.attributes = attributes;
        try {
            debug_println("calling setAttributes in printDialog");
            setAttributes(attributes);
        } catch (PrinterException e) {
        }
        setParentWindowID(attributes);
        boolean ret = printDialog();
        clearParentWindowID();
        this.attributes = attributes;
        return ret;
    }
    /* A security check has already been performed in the
         * java.awt.print.printerJob.getPrinterJob method.
         * So by the time we get here, it is OK for the current thread
         * to print either to a file (from a Dialog we control!) or
         * to a chosen printer.
         *
         * We raise privilege when we put up the dialog, to avoid
         * the "warning applet window" banner.
         */
    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 false;
    }
    PrintService[] services;
    StreamPrintServiceFactory[] spsFactories = null;
    if (service instanceof StreamPrintService) {
        spsFactories = lookupStreamPrintServices(null);
        services = new StreamPrintService[spsFactories.length];
        for (int i = 0; i < spsFactories.length; i++) {
            services[i] = spsFactories[i].getPrintService(null);
        }
    } else {
        services = (PrintService[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

            public Object run() {
                PrintService[] services = PrinterJob.lookupPrintServices();
                return services;
            }
        });
        if ((services == null) || (services.length == 0)) {
            /*
                 * No services but default PrintService exists?
                 * Create services using defaultService.
                 */
            services = new PrintService[1];
            services[0] = service;
        }
    }
    Rectangle bounds = gc.getBounds();
    int x = bounds.x + bounds.width / 3;
    int y = bounds.y + bounds.height / 3;
    PrintService newService;
    // temporarily add an attribute pointing back to this job.
    PrinterJobWrapper jobWrapper = new PrinterJobWrapper(this);
    attributes.add(jobWrapper);
    try {
        newService = ServiceUI.printDialog(gc, x, y, services, service, DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes);
    } catch (IllegalArgumentException iae) {
        newService = ServiceUI.printDialog(gc, x, y, services, services[0], DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes);
    }
    attributes.remove(PrinterJobWrapper.class);
    if (newService == null) {
        return false;
    }
    if (!service.equals(newService)) {
        try {
            setPrintService(newService);
        } catch (PrinterException e) {
            /*
                 * The only time it would throw an exception is when
                 * newService is no longer available but we should still
                 * select this printer.
                 */
            myService = newService;
        }
    }
    return true;
}
Also used : HeadlessException(java.awt.HeadlessException) Rectangle(java.awt.Rectangle) PrinterException(java.awt.print.PrinterException) StreamPrintService(javax.print.StreamPrintService) DialogTypeSelection(javax.print.attribute.standard.DialogTypeSelection) GraphicsConfiguration(java.awt.GraphicsConfiguration) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService) StreamPrintServiceFactory(javax.print.StreamPrintServiceFactory)

Example 38 with PrintService

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

the class RasterPrinterJob method pageDialog.

/**
     * return a PageFormat corresponding to the updated attributes,
     * or null if the user cancelled the dialog.
     */
public PageFormat pageDialog(final PrintRequestAttributeSet attributes) throws HeadlessException {
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }
    DialogTypeSelection dlg = (DialogTypeSelection) attributes.get(DialogTypeSelection.class);
    // Check for native, note that default dialog is COMMON.
    if (dlg == DialogTypeSelection.NATIVE) {
        PrintService pservice = getPrintService();
        PageFormat pageFrmAttrib = attributeToPageFormat(pservice, attributes);
        setParentWindowID(attributes);
        PageFormat page = pageDialog(pageFrmAttrib);
        clearParentWindowID();
        // page object and as per spec, we should return null in that case.
        if (page == pageFrmAttrib) {
            return null;
        }
        updateAttributesWithPageFormat(pservice, page, attributes);
        return page;
    }
    final GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    Rectangle bounds = gc.getBounds();
    int x = bounds.x + bounds.width / 3;
    int y = bounds.y + bounds.height / 3;
    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 null;
    }
    if (onTop != null) {
        attributes.add(onTop);
    }
    ServiceDialog pageDialog = new ServiceDialog(gc, x, y, service, DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes, (Frame) null);
    pageDialog.show();
    if (pageDialog.getStatus() == ServiceDialog.APPROVE) {
        PrintRequestAttributeSet newas = pageDialog.getAttributes();
        Class amCategory = SunAlternateMedia.class;
        if (attributes.containsKey(amCategory) && !newas.containsKey(amCategory)) {
            attributes.remove(amCategory);
        }
        attributes.addAll(newas);
        return attributeToPageFormat(service, attributes);
    } else {
        return null;
    }
}
Also used : ServiceDialog(sun.print.ServiceDialog) HeadlessException(java.awt.HeadlessException) Rectangle(java.awt.Rectangle) DialogTypeSelection(javax.print.attribute.standard.DialogTypeSelection) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService) GraphicsConfiguration(java.awt.GraphicsConfiguration) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) PageFormat(java.awt.print.PageFormat)

Example 39 with PrintService

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

the class Win32PrintServiceLookup method refreshServices.

private synchronized void refreshServices() {
    printers = getAllPrinterNames();
    if (printers == null) {
        // In Windows it is safe to assume no default if printers == null so we
        // don't get the default.
        printServices = new PrintService[0];
        return;
    }
    PrintService[] newServices = new PrintService[printers.length];
    PrintService defService = getDefaultPrintService();
    for (int p = 0; p < printers.length; p++) {
        if (defService != null && printers[p].equals(defService.getName())) {
            newServices[p] = defService;
        } else {
            if (printServices == null) {
                newServices[p] = new Win32PrintService(printers[p]);
            } else {
                int j;
                for (j = 0; j < printServices.length; j++) {
                    if ((printServices[j] != null) && (printers[p].equals(printServices[j].getName()))) {
                        newServices[p] = printServices[j];
                        printServices[j] = null;
                        break;
                    }
                }
                if (j == printServices.length) {
                    newServices[p] = new Win32PrintService(printers[p]);
                }
            }
        }
    }
    // Look for deleted services and invalidate these
    if (printServices != null) {
        for (int j = 0; j < printServices.length; j++) {
            if ((printServices[j] instanceof Win32PrintService) && (!printServices[j].equals(defaultPrintService))) {
                ((Win32PrintService) printServices[j]).invalidateService();
            }
        }
    }
    printServices = newServices;
}
Also used : PrintService(javax.print.PrintService) MultiDocPrintService(javax.print.MultiDocPrintService)

Example 40 with PrintService

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

the class PSPrinterJob method startDoc.

/**
     * Invoked by the RasterPrinterJob super class
     * this method is called to mark the start of a
     * document.
     */
protected void startDoc() throws PrinterException {
    // A security check has been performed in the
    // java.awt.print.printerJob.getPrinterJob method.
    // We use an inner class to execute the privilged open operations.
    // Note that we only open a file if it has been nominated by
    // the end-user in a dialog that we ouselves put up.
    OutputStream output;
    if (epsPrinter == null) {
        if (getPrintService() instanceof PSStreamPrintService) {
            StreamPrintService sps = (StreamPrintService) getPrintService();
            mDestType = RasterPrinterJob.STREAM;
            if (sps.isDisposed()) {
                throw new PrinterException("service is disposed");
            }
            output = sps.getOutputStream();
            if (output == null) {
                throw new PrinterException("Null output stream");
            }
        } else {
            /* REMIND: This needs to be more maintainable */
            mNoJobSheet = super.noJobSheet;
            if (super.destinationAttr != null) {
                mDestType = RasterPrinterJob.FILE;
                mDestination = super.destinationAttr;
            }
            if (mDestType == RasterPrinterJob.FILE) {
                try {
                    spoolFile = new File(mDestination);
                    output = new FileOutputStream(spoolFile);
                } catch (IOException ex) {
                    throw new PrinterIOException(ex);
                }
            } else {
                PrinterOpener po = new PrinterOpener();
                java.security.AccessController.doPrivileged(po);
                if (po.pex != null) {
                    throw po.pex;
                }
                output = po.result;
            }
        }
        mPSStream = new PrintStream(new BufferedOutputStream(output));
        mPSStream.println(ADOBE_PS_STR);
    }
    mPSStream.println("%%BeginProlog");
    mPSStream.println(READIMAGEPROC);
    mPSStream.println("/BD {bind def} bind def");
    mPSStream.println("/D {def} BD");
    mPSStream.println("/C {curveto} BD");
    mPSStream.println("/L {lineto} BD");
    mPSStream.println("/M {moveto} BD");
    mPSStream.println("/R {grestore} BD");
    mPSStream.println("/G {gsave} BD");
    mPSStream.println("/N {newpath} BD");
    mPSStream.println("/P {closepath} BD");
    mPSStream.println("/EC {eoclip} BD");
    mPSStream.println("/WC {clip} BD");
    mPSStream.println("/EF {eofill} BD");
    mPSStream.println("/WF {fill} BD");
    mPSStream.println("/SG {setgray} BD");
    mPSStream.println("/SC {setrgbcolor} BD");
    mPSStream.println("/ISOF {");
    mPSStream.println("     dup findfont dup length 1 add dict begin {");
    mPSStream.println("             1 index /FID eq {pop pop} {D} ifelse");
    mPSStream.println("     } forall /Encoding ISOLatin1Encoding D");
    mPSStream.println("     currentdict end definefont");
    mPSStream.println("} BD");
    mPSStream.println("/NZ {dup 1 lt {pop 1} if} BD");
    /* The following procedure takes args: string, x, y, desiredWidth.
         * It calculates using stringwidth the width of the string in the
         * current font and subtracts it from the desiredWidth and divides
         * this by stringLen-1. This gives us a per-glyph adjustment in
         * the spacing needed (either +ve or -ve) to make the string
         * print at the desiredWidth. The ashow procedure call takes this
         * per-glyph adjustment as an argument. This is necessary for WYSIWYG
         */
    mPSStream.println("/" + DrawStringName + " {");
    mPSStream.println("     moveto 1 index stringwidth pop NZ sub");
    mPSStream.println("     1 index length 1 sub NZ div 0");
    mPSStream.println("     3 2 roll ashow newpath} BD");
    mPSStream.println("/FL [");
    if (mFontProps == null) {
        mPSStream.println(" /Helvetica ISOF");
        mPSStream.println(" /Helvetica-Bold ISOF");
        mPSStream.println(" /Helvetica-Oblique ISOF");
        mPSStream.println(" /Helvetica-BoldOblique ISOF");
        mPSStream.println(" /Times-Roman ISOF");
        mPSStream.println(" /Times-Bold ISOF");
        mPSStream.println(" /Times-Italic ISOF");
        mPSStream.println(" /Times-BoldItalic ISOF");
        mPSStream.println(" /Courier ISOF");
        mPSStream.println(" /Courier-Bold ISOF");
        mPSStream.println(" /Courier-Oblique ISOF");
        mPSStream.println(" /Courier-BoldOblique ISOF");
    } else {
        int cnt = Integer.parseInt(mFontProps.getProperty("font.num", "9"));
        for (int i = 0; i < cnt; i++) {
            mPSStream.println("    /" + mFontProps.getProperty("font." + String.valueOf(i), "Courier ISOF"));
        }
    }
    mPSStream.println("] D");
    mPSStream.println("/" + SetFontName + " {");
    mPSStream.println("     FL exch get exch scalefont");
    mPSStream.println("     [1 0 0 -1 0 0] makefont setfont} BD");
    mPSStream.println("%%EndProlog");
    mPSStream.println("%%BeginSetup");
    if (epsPrinter == null) {
        // Set Page Size using first page's format.
        PageFormat pageFormat = getPageable().getPageFormat(0);
        double paperHeight = pageFormat.getPaper().getHeight();
        double paperWidth = pageFormat.getPaper().getWidth();
        /* PostScript printers can always generate uncollated copies.
             */
        mPSStream.print("<< /PageSize [" + paperWidth + " " + paperHeight + "]");
        final PrintService pservice = getPrintService();
        Boolean isPS = (Boolean) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

            public Object run() {
                try {
                    Class psClass = Class.forName("sun.print.IPPPrintService");
                    if (psClass.isInstance(pservice)) {
                        Method isPSMethod = psClass.getMethod("isPostscript", (Class[]) null);
                        return (Boolean) isPSMethod.invoke(pservice, (Object[]) null);
                    }
                } catch (Throwable t) {
                }
                return Boolean.TRUE;
            }
        });
        if (isPS) {
            mPSStream.print(" /DeferredMediaSelection true");
        }
        mPSStream.print(" /ImagingBBox null /ManualFeed false");
        mPSStream.print(isCollated() ? " /Collate true" : "");
        mPSStream.print(" /NumCopies " + getCopiesInt());
        if (sidesAttr != Sides.ONE_SIDED) {
            if (sidesAttr == Sides.TWO_SIDED_LONG_EDGE) {
                mPSStream.print(" /Duplex true ");
            } else if (sidesAttr == Sides.TWO_SIDED_SHORT_EDGE) {
                mPSStream.print(" /Duplex true /Tumble true ");
            }
        }
        mPSStream.println(" >> setpagedevice ");
    }
    mPSStream.println("%%EndSetup");
}
Also used : PrintStream(java.io.PrintStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) PrinterIOException(java.awt.print.PrinterIOException) PrinterException(java.awt.print.PrinterException) PrinterIOException(java.awt.print.PrinterIOException) IOException(java.io.IOException) Method(java.lang.reflect.Method) StreamPrintService(javax.print.StreamPrintService) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService) PageFormat(java.awt.print.PageFormat) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

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