Search in sources :

Example 21 with PrinterException

use of java.awt.print.PrinterException in project jdk8u_jdk by JetBrains.

the class RasterPrinterJob method spoolToService.

/*
     * Services we don't recognize as built-in services can't be
     * implemented as subclasses of PrinterJob, therefore we create
     * a DocPrintJob from their service and pass a Doc representing
     * the application's printjob
     */
// MacOSX - made protected so subclasses can reference it.
protected void spoolToService(PrintService psvc, PrintRequestAttributeSet attributes) throws PrinterException {
    if (psvc == null) {
        throw new PrinterException("No print service found.");
    }
    DocPrintJob job = psvc.createPrintJob();
    Doc doc = new PageableDoc(getPageable());
    if (attributes == null) {
        attributes = new HashPrintRequestAttributeSet();
    }
    try {
        job.print(doc, attributes);
    } catch (PrintException e) {
        throw new PrinterException(e.toString());
    }
}
Also used : PrintException(javax.print.PrintException) Doc(javax.print.Doc) PageableDoc(sun.print.PageableDoc) PrinterException(java.awt.print.PrinterException) DocPrintJob(javax.print.DocPrintJob) PageableDoc(sun.print.PageableDoc) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 22 with PrinterException

use of java.awt.print.PrinterException 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 23 with PrinterException

use of java.awt.print.PrinterException in project jdk8u_jdk by JetBrains.

the class ImageableAreaTest method printWithJavaPrintDialog.

private static void printWithJavaPrintDialog() {
    final JTable table = createAuthorTable(42);
    Printable printable = table.getPrintable(JTable.PrintMode.NORMAL, new MessageFormat("Author Table"), new MessageFormat("Page - {0}"));
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);
    boolean printAccepted = job.printDialog();
    if (printAccepted) {
        try {
            job.print();
            closeFrame();
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : MessageFormat(java.text.MessageFormat) JTable(javax.swing.JTable) Printable(java.awt.print.Printable) PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob)

Example 24 with PrinterException

use of java.awt.print.PrinterException 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)

Example 25 with PrinterException

use of java.awt.print.PrinterException in project jdk8u_jdk by JetBrains.

the class PrintLatinCJKTest method actionPerformed.

public void actionPerformed(ActionEvent e) {
    try {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(testInstance);
        if (job.printDialog()) {
            job.print();
        }
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}
Also used : PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob)

Aggregations

PrinterException (java.awt.print.PrinterException)27 PrinterJob (java.awt.print.PrinterJob)15 PageFormat (java.awt.print.PageFormat)11 PrintService (javax.print.PrintService)8 Printable (java.awt.print.Printable)5 StreamPrintService (javax.print.StreamPrintService)5 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)5 HeadlessException (java.awt.HeadlessException)4 File (java.io.File)4 IOException (java.io.IOException)4 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)4 Container (java.awt.Container)3 ActionEvent (java.awt.event.ActionEvent)3 Paper (java.awt.print.Paper)3 PrintException (javax.print.PrintException)3 AbstractAction (javax.swing.AbstractAction)3 JButton (javax.swing.JButton)3 JFrame (javax.swing.JFrame)3 Graphics2D (java.awt.Graphics2D)2 Rectangle (java.awt.Rectangle)2