Search in sources :

Example 16 with MediaSize

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

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

the class WPrinterJob method getWin32MediaAttrib.

/** MediaSizeName / dmPaper */
private final int[] getWin32MediaAttrib() {
    int[] wid_ht = { 0, 0 };
    if (attributes != null) {
        Media media = (Media) attributes.get(Media.class);
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName) media;
            MediaSize ms = MediaSize.getMediaSizeForName(msn);
            if (ms != null) {
                wid_ht[0] = (int) (ms.getX(MediaSize.INCH) * 72.0);
                wid_ht[1] = (int) (ms.getY(MediaSize.INCH) * 72.0);
            }
        }
    }
    return wid_ht;
}
Also used : MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) SunAlternateMedia(sun.print.SunAlternateMedia)

Example 18 with MediaSize

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

the class PrintJob2D method mapMedia.

public static MediaSizeName mapMedia(MediaType mType) {
    MediaSizeName media = null;
    // JAVAXSIZES.length and SIZES.length must be equal!
    // Attempt to recover by getting the smaller size.
    int length = Math.min(SIZES.length, JAVAXSIZES.length);
    for (int i = 0; i < length; i++) {
        if (SIZES[i] == mType) {
            if ((JAVAXSIZES[i] != null) && MediaSize.getMediaSizeForName(JAVAXSIZES[i]) != null) {
                media = JAVAXSIZES[i];
                break;
            } else {
                /* create Custom Media */
                media = new CustomMediaSizeName(SIZES[i].toString());
                float w = (float) Math.rint(WIDTHS[i] / 72.0);
                float h = (float) Math.rint(LENGTHS[i] / 72.0);
                if (w > 0.0 && h > 0.0) {
                    // add new created MediaSize to our static map
                    // so it will be found when we call findMedia
                    new MediaSize(w, h, Size2DSyntax.INCH, media);
                }
                break;
            }
        }
    }
    return media;
}
Also used : MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName)

Example 19 with MediaSize

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

the class CustomMediaSizeName method findMedia.

// moved from RasterPrinterJob
/**
     * Returns closest matching MediaSizeName among given array of Media
     */
public static MediaSizeName findMedia(Media[] media, float x, float y, int units) {
    if (x <= 0.0f || y <= 0.0f || units < 1) {
        throw new IllegalArgumentException("args must be +ve values");
    }
    if (media == null || media.length == 0) {
        throw new IllegalArgumentException("args must have valid array of media");
    }
    int size = 0;
    MediaSizeName[] msn = new MediaSizeName[media.length];
    for (int i = 0; i < media.length; i++) {
        if (media[i] instanceof MediaSizeName) {
            msn[size++] = (MediaSizeName) media[i];
        }
    }
    if (size == 0) {
        return null;
    }
    int match = 0;
    double ls = x * x + y * y;
    double tmp_ls;
    float[] dim;
    float diffx = x;
    float diffy = y;
    for (int i = 0; i < size; i++) {
        MediaSize mediaSize = MediaSize.getMediaSizeForName(msn[i]);
        if (mediaSize == null) {
            continue;
        }
        dim = mediaSize.getSize(units);
        if (x == dim[0] && y == dim[1]) {
            match = i;
            break;
        } else {
            diffx = x - dim[0];
            diffy = y - dim[1];
            tmp_ls = diffx * diffx + diffy * diffy;
            if (tmp_ls < ls) {
                ls = tmp_ls;
                match = i;
            }
        }
    }
    return msn[match];
}
Also used : MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName)

Example 20 with MediaSize

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

the class UnixPrintService method getSupportedAttributeValues.

public Object getSupportedAttributeValues(Class<? extends Attribute> category, DocFlavor flavor, AttributeSet attributes) {
    if (category == null) {
        throw new NullPointerException("null category");
    }
    if (!Attribute.class.isAssignableFrom(category)) {
        throw new IllegalArgumentException(category + " does not implement Attribute");
    }
    if (flavor != null) {
        if (!isDocFlavorSupported(flavor)) {
            throw new IllegalArgumentException(flavor + " is an unsupported flavor");
        } else if (isAutoSense(flavor)) {
            return null;
        }
    }
    if (!isAttributeCategorySupported(category)) {
        return null;
    }
    if (category == Chromaticity.class) {
        if (flavor == null || isServiceFormattedFlavor(flavor)) {
            Chromaticity[] arr = new Chromaticity[1];
            arr[0] = Chromaticity.COLOR;
            return (arr);
        } else {
            return null;
        }
    } else if (category == Destination.class) {
        try {
            return new Destination((new File("out.ps")).toURI());
        } catch (SecurityException se) {
            try {
                return new Destination(new URI("file:out.ps"));
            } catch (URISyntaxException e) {
                return null;
            }
        }
    } else if (category == JobName.class) {
        return new JobName("Java Printing", null);
    } else if (category == JobSheets.class) {
        JobSheets[] arr = new JobSheets[2];
        arr[0] = JobSheets.NONE;
        arr[1] = JobSheets.STANDARD;
        return arr;
    } else if (category == RequestingUserName.class) {
        String userName = "";
        try {
            userName = System.getProperty("user.name", "");
        } catch (SecurityException se) {
        }
        return new RequestingUserName(userName, null);
    } else if (category == OrientationRequested.class) {
        if (flavor == null || isServiceFormattedFlavor(flavor)) {
            OrientationRequested[] arr = new OrientationRequested[3];
            arr[0] = OrientationRequested.PORTRAIT;
            arr[1] = OrientationRequested.LANDSCAPE;
            arr[2] = OrientationRequested.REVERSE_LANDSCAPE;
            return arr;
        } else {
            return null;
        }
    } else if ((category == Copies.class) || (category == CopiesSupported.class)) {
        if (flavor == null || !(flavor.equals(DocFlavor.INPUT_STREAM.POSTSCRIPT) || flavor.equals(DocFlavor.URL.POSTSCRIPT) || flavor.equals(DocFlavor.BYTE_ARRAY.POSTSCRIPT))) {
            return new CopiesSupported(1, MAXCOPIES);
        } else {
            return null;
        }
    } else if (category == Media.class) {
        Media[] arr = new Media[mediaSizes.length];
        System.arraycopy(mediaSizes, 0, arr, 0, mediaSizes.length);
        return arr;
    } else if (category == Fidelity.class) {
        Fidelity[] arr = new Fidelity[2];
        arr[0] = Fidelity.FIDELITY_FALSE;
        arr[1] = Fidelity.FIDELITY_TRUE;
        return arr;
    } else if (category == MediaPrintableArea.class) {
        /* The code below implements the behaviour that if no Media or
             * MediaSize attribute is specified, return an array of
             * MediaPrintableArea, one for each supported Media.
             * If a MediaSize is specified, return a MPA consistent for that,
             * and if a Media is specified locate its MediaSize and return
             * its MPA, and if none is found, return an MPA for the default
             * Media for this service.
             */
        if (attributes == null) {
            return getAllPrintableAreas();
        }
        MediaSize mediaSize = (MediaSize) attributes.get(MediaSize.class);
        Media media = (Media) attributes.get(Media.class);
        MediaPrintableArea[] arr = new MediaPrintableArea[1];
        if (mediaSize == null) {
            if (media instanceof MediaSizeName) {
                MediaSizeName msn = (MediaSizeName) media;
                mediaSize = MediaSize.getMediaSizeForName(msn);
                if (mediaSize == null) {
                    /* try to get a size from the default media */
                    media = (Media) getDefaultAttributeValue(Media.class);
                    if (media instanceof MediaSizeName) {
                        msn = (MediaSizeName) media;
                        mediaSize = MediaSize.getMediaSizeForName(msn);
                    }
                    if (mediaSize == null) {
                        /* shouldn't happen, return a default */
                        arr[0] = new MediaPrintableArea(0.25f, 0.25f, 8f, 10.5f, MediaSize.INCH);
                        return arr;
                    }
                }
            } else {
                return getAllPrintableAreas();
            }
        }
        /* If reach here MediaSize is non-null */
        assert mediaSize != null;
        arr[0] = new MediaPrintableArea(0.25f, 0.25f, mediaSize.getX(MediaSize.INCH) - 0.5f, mediaSize.getY(MediaSize.INCH) - 0.5f, MediaSize.INCH);
        return arr;
    } else if (category == PageRanges.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            PageRanges[] arr = new PageRanges[1];
            arr[0] = new PageRanges(1, Integer.MAX_VALUE);
            return arr;
        } else {
            return null;
        }
    } else if (category == SheetCollate.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            SheetCollate[] arr = new SheetCollate[2];
            arr[0] = SheetCollate.UNCOLLATED;
            arr[1] = SheetCollate.COLLATED;
            return arr;
        } else {
            return null;
        }
    } else if (category == Sides.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            Sides[] arr = new Sides[3];
            arr[0] = Sides.ONE_SIDED;
            arr[1] = Sides.TWO_SIDED_LONG_EDGE;
            arr[2] = Sides.TWO_SIDED_SHORT_EDGE;
            return arr;
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : Destination(javax.print.attribute.standard.Destination) Fidelity(javax.print.attribute.standard.Fidelity) PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) Attribute(javax.print.attribute.Attribute) JobName(javax.print.attribute.standard.JobName) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) OrientationRequested(javax.print.attribute.standard.OrientationRequested) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) Sides(javax.print.attribute.standard.Sides) PageRanges(javax.print.attribute.standard.PageRanges) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) JobSheets(javax.print.attribute.standard.JobSheets) SheetCollate(javax.print.attribute.standard.SheetCollate) RequestingUserName(javax.print.attribute.standard.RequestingUserName) CopiesSupported(javax.print.attribute.standard.CopiesSupported) Chromaticity(javax.print.attribute.standard.Chromaticity) File(java.io.File)

Aggregations

MediaSize (javax.print.attribute.standard.MediaSize)21 MediaSizeName (javax.print.attribute.standard.MediaSizeName)16 Media (javax.print.attribute.standard.Media)10 MediaPrintableArea (javax.print.attribute.standard.MediaPrintableArea)10 OrientationRequested (javax.print.attribute.standard.OrientationRequested)8 PageFormat (java.awt.print.PageFormat)6 Paper (java.awt.print.Paper)6 PrintService (javax.print.PrintService)5 JobName (javax.print.attribute.standard.JobName)4 Attribute (javax.print.attribute.Attribute)3 Fidelity (javax.print.attribute.standard.Fidelity)3 RequestingUserName (javax.print.attribute.standard.RequestingUserName)3 SheetCollate (javax.print.attribute.standard.SheetCollate)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 StreamPrintService (javax.print.StreamPrintService)2 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)2 PrintServiceAttribute (javax.print.attribute.PrintServiceAttribute)2 Chromaticity (javax.print.attribute.standard.Chromaticity)2 CopiesSupported (javax.print.attribute.standard.CopiesSupported)2