2283 lines · 83.3 KB · Java
RawDownload
            // deleted already            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 PrinterException("PrintService is no longer available.");                }            }              if (service.isDocFlavorSupported(                                             DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&                service.isDocFlavorSupported(                                             DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {                myService = service;            } else {                throw new PrinterException("Not a 2D print service: " + service);            }        }    }      protected void updatePageAttributes(PrintService service,                                        PageFormat page) {        if (service == null || page == null) {            return;        }         float x = (float)Math.rint(                         (page.getPaper().getWidth()*Size2DSyntax.INCH)/                         (72.0))/(float)Size2DSyntax.INCH;        float y = (float)Math.rint(                         (page.getPaper().getHeight()*Size2DSyntax.INCH)/                         (72.0))/(float)Size2DSyntax.INCH;         // We should limit the list where we search the matching        // media, this will prevent mapping to wrong media ex. Ledger        // can be mapped to B.  Especially useful when creating        // custom MediaSize.        Media[] mediaList = (Media[])service.getSupportedAttributeValues(                                      Media.class, null, null);        Media media = null;        try {            media = CustomMediaSizeName.findMedia(mediaList, x, y,                                   Size2DSyntax.INCH);        } catch (IllegalArgumentException iae) {        }        if ((media == null) ||             !(service.isAttributeValueSupported(media, null, null))) {            media = (Media)service.getDefaultAttributeValue(Media.class);        }         OrientationRequested orient;        switch (page.getOrientation()) {        case PageFormat.LANDSCAPE :            orient = OrientationRequested.LANDSCAPE;            break;        case PageFormat.REVERSE_LANDSCAPE:            orient = OrientationRequested.REVERSE_LANDSCAPE;            break;        default:            orient = OrientationRequested.PORTRAIT;        }         if (attributes == null) {            attributes = new HashPrintRequestAttributeSet();        }        if (media != null) {            attributes.add(media);        }        attributes.add(orient);         float ix = (float)(page.getPaper().getImageableX()/DPI);        float iw = (float)(page.getPaper().getImageableWidth()/DPI);        float iy = (float)(page.getPaper().getImageableY()/DPI);        float ih = (float)(page.getPaper().getImageableHeight()/DPI);        if (ix < 0) ix = 0f; if (iy < 0) iy = 0f;        try {            attributes.add(new MediaPrintableArea(ix, iy, iw, ih,                                                  MediaPrintableArea.INCH));        } catch (IllegalArgumentException iae) {        }    }    /**     * 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 = pageDialog(attributes);         if (newPage == null) {            return page;        } else {            return newPage;        }    }     /**     * 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();        }         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;        }         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);             PageFormat page = defaultPage();             OrientationRequested orient =                (OrientationRequested)                attributes.get(OrientationRequested.class);            int pfOrient =  PageFormat.PORTRAIT;            if (orient != null) {                if (orient == OrientationRequested.REVERSE_LANDSCAPE) {                    pfOrient = PageFormat.REVERSE_LANDSCAPE;                } else if (orient == OrientationRequested.LANDSCAPE) {                    pfOrient = PageFormat.LANDSCAPE;                }            }            page.setOrientation(pfOrient);             Media media = (Media)attributes.get(Media.class);            if (media == null) {                media =                    (Media)service.getDefaultAttributeValue(Media.class);            }            if (!(media instanceof MediaSizeName)) {                media = MediaSizeName.NA_LETTER;            }            MediaSize size =                MediaSize.getMediaSizeForName((MediaSizeName)media);            if (size == null) {                size = MediaSize.NA.LETTER;            }            Paper paper = new Paper();            float dim[] = size.getSize(1); //units == 1 to avoid FP error            double w = Math.rint((dim[0]*72.0)/Size2DSyntax.INCH);            double h = Math.rint((dim[1]*72.0)/Size2DSyntax.INCH);            paper.setSize(w, h);            MediaPrintableArea area =                (MediaPrintableArea)                attributes.get(MediaPrintableArea.class);            double ix, iw, iy, ih;             if (area != null) {                // Should pass in same unit as updatePageAttributes                // to avoid rounding off errors.                ix = Math.rint(                               area.getX(MediaPrintableArea.INCH) * DPI);                iy = Math.rint(                               area.getY(MediaPrintableArea.INCH) * DPI);                iw = Math.rint(                               area.getWidth(MediaPrintableArea.INCH) * DPI);                ih = Math.rint(                               area.getHeight(MediaPrintableArea.INCH) * DPI);            }            else {                if (w >= 72.0 * 6.0) {                    ix = 72.0;                    iw = w - 2 * 72.0;                } else {                    ix = w / 6.0;                    iw = w * 0.75;                }                if (h >= 72.0 * 6.0) {                    iy = 72.0;                    ih = h - 2 * 72.0;                } else {                    iy = h / 6.0;                    ih = h * 0.75;                }            }            paper.setImageableArea(ix, iy, iw, ih);            page.setPaper(paper);             return page;        } else {            return null;        }   }    /**     * 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) {             }             boolean ret = printDialog();            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;        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);        }         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;    }    /**     * 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();        }         PrintRequestAttributeSet attributes =          new HashPrintRequestAttributeSet();        attributes.add(new Copies(getCopies()));        attributes.add(new JobName(getJobName(), null));        boolean 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.prn";                    PrintService ps = getPrintService();                    if (ps != null) {                        Destination defaultDest = (Destination)ps.                            getDefaultAttributeValue(Destination.class);                        if (defaultDest != null) {                            mDestination = (new File(defaultDest.getURI())).getPath();                        }                    }                }            } else {                mDestType = RasterPrinterJob.PRINTER;                PrintService ps = getPrintService();                if (ps != null) {                    mDestination = ps.getName();                }            }        }         return doPrint;    }     /**     * The pages in the document to be printed by this PrinterJob     * are drawn by the Printable object 'painter'. The PageFormat     * for each page is the default page format.     * @param Printable Called to render each page of the document.     */    public void setPrintable(Printable painter) {        setPageable(new OpenBook(defaultPage(new PageFormat()), painter));    }     /**     * The pages in the document to be printed by this PrinterJob     * are drawn by the Printable object 'painter'. The PageFormat     * of each page is 'format'.     * @param Printable Called to render each page of the document.     * @param PageFormat The size and orientation of each page to     *                   be printed.     */    public void setPrintable(Printable painter, PageFormat format) {        setPageable(new OpenBook(format, painter));        updatePageAttributes(getPrintService(), format);    }     /**     * The pages in the document to be printed are held by the     * Pageable instance 'document'. 'document' will be queried     * for the number of pages as well as the PageFormat and     * Printable for each page.     * @param Pageable The document to be printed. It may not be null.     * @exception NullPointerException the Pageable passed in was null.     * @see PageFormat

Lines 501–1000 of 2283. Previous lines