Search in sources :

Example 1 with DialogTypeSelection

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

the class PSPrinterJob method printDialog.

/* Instance Methods */
/**
     * Presents the user a dialog for changing properties of the
     * print job interactively.
     * @returns 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() throws HeadlessException {
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }
    if (attributes == null) {
        attributes = new HashPrintRequestAttributeSet();
    }
    attributes.add(new Copies(getCopies()));
    attributes.add(new JobName(getJobName(), null));
    boolean doPrint = false;
    DialogTypeSelection dts = (DialogTypeSelection) attributes.get(DialogTypeSelection.class);
    if (dts == DialogTypeSelection.NATIVE) {
        // Remove DialogTypeSelection.NATIVE to prevent infinite loop in
        // RasterPrinterJob.
        attributes.remove(DialogTypeSelection.class);
        doPrint = printDialog(attributes);
        // restore attribute
        attributes.add(DialogTypeSelection.NATIVE);
    } else {
        doPrint = printDialog(attributes);
    }
    if (doPrint) {
        JobName jobName = (JobName) attributes.get(JobName.class);
        if (jobName != null) {
            setJobName(jobName.getValue());
        }
        Copies copies = (Copies) attributes.get(Copies.class);
        if (copies != null) {
            setCopies(copies.getValue());
        }
        Destination dest = (Destination) attributes.get(Destination.class);
        if (dest != null) {
            try {
                mDestType = RasterPrinterJob.FILE;
                mDestination = (new File(dest.getURI())).getPath();
            } catch (Exception e) {
                mDestination = "out.ps";
            }
        } else {
            mDestType = RasterPrinterJob.PRINTER;
            PrintService pServ = getPrintService();
            if (pServ != null) {
                mDestination = pServ.getName();
                if (isMac) {
                    PrintServiceAttributeSet psaSet = pServ.getAttributes();
                    if (psaSet != null) {
                        mDestination = psaSet.get(PrinterName.class).toString();
                    }
                }
            }
        }
    }
    return doPrint;
}
Also used : Destination(javax.print.attribute.standard.Destination) HeadlessException(java.awt.HeadlessException) Copies(javax.print.attribute.standard.Copies) JobName(javax.print.attribute.standard.JobName) File(java.io.File) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) HeadlessException(java.awt.HeadlessException) CharConversionException(java.io.CharConversionException) PrinterException(java.awt.print.PrinterException) PrinterIOException(java.awt.print.PrinterIOException) IOException(java.io.IOException) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) DialogTypeSelection(javax.print.attribute.standard.DialogTypeSelection) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService)

Example 2 with DialogTypeSelection

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

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

use of javax.print.attribute.standard.DialogTypeSelection 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)

Aggregations

HeadlessException (java.awt.HeadlessException)4 PrintService (javax.print.PrintService)4 StreamPrintService (javax.print.StreamPrintService)4 DialogTypeSelection (javax.print.attribute.standard.DialogTypeSelection)4 GraphicsConfiguration (java.awt.GraphicsConfiguration)3 Rectangle (java.awt.Rectangle)2 PageFormat (java.awt.print.PageFormat)2 PrinterException (java.awt.print.PrinterException)2 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)2 PrinterIOException (java.awt.print.PrinterIOException)1 CharConversionException (java.io.CharConversionException)1 File (java.io.File)1 IOException (java.io.IOException)1 StreamPrintServiceFactory (javax.print.StreamPrintServiceFactory)1 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)1 PrintServiceAttributeSet (javax.print.attribute.PrintServiceAttributeSet)1 Copies (javax.print.attribute.standard.Copies)1 Destination (javax.print.attribute.standard.Destination)1 JobName (javax.print.attribute.standard.JobName)1 ServiceDialog (sun.print.ServiceDialog)1