Search in sources :

Example 1 with RequestingUserName

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

the class PSStreamPrintService 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 && !isDocFlavorSupported(flavor)) {
        throw new IllegalArgumentException(flavor + " is an unsupported flavor");
    }
    if (!isAttributeCategorySupported(category)) {
        return null;
    }
    if (category == Chromaticity.class) {
        Chromaticity[] arr = new Chromaticity[1];
        arr[0] = Chromaticity.COLOR;
        //arr[1] = Chromaticity.MONOCHROME;
        return (arr);
    } else if (category == JobName.class) {
        return new JobName("", null);
    } else if (category == RequestingUserName.class) {
        return new RequestingUserName("", null);
    } else if (category == OrientationRequested.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
            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)) {
        return new CopiesSupported(1, MAXCOPIES);
    } 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) {
        if (attributes == null) {
            return null;
        }
        MediaSize mediaSize = (MediaSize) attributes.get(MediaSize.class);
        if (mediaSize == null) {
            Media media = (Media) attributes.get(Media.class);
            if (media != null && media instanceof MediaSizeName) {
                MediaSizeName msn = (MediaSizeName) media;
                mediaSize = MediaSize.getMediaSizeForName(msn);
            }
        }
        if (mediaSize == null) {
            return null;
        } else {
            MediaPrintableArea[] arr = new MediaPrintableArea[1];
            float w = mediaSize.getX(MediaSize.INCH);
            float h = mediaSize.getY(MediaSize.INCH);
            /* For dimensions >= 5 inches use 0.5 inch margins.
                 * For smaller dimensions, use 10% margins.
                 */
            float xmargin = 0.5f;
            float ymargin = 0.5f;
            if (w < 5f) {
                xmargin = w / 10;
            }
            if (h < 5f) {
                ymargin = h / 10;
            }
            arr[0] = new MediaPrintableArea(xmargin, ymargin, w - 2 * xmargin, h - 2 * ymargin, 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 {
            SheetCollate[] arr = new SheetCollate[1];
            arr[0] = SheetCollate.UNCOLLATED;
            return arr;
        }
    } 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 : Fidelity(javax.print.attribute.standard.Fidelity) PageRanges(javax.print.attribute.standard.PageRanges) MediaSize(javax.print.attribute.standard.MediaSize) PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) Attribute(javax.print.attribute.Attribute) MediaSizeName(javax.print.attribute.standard.MediaSizeName) JobName(javax.print.attribute.standard.JobName) Media(javax.print.attribute.standard.Media) OrientationRequested(javax.print.attribute.standard.OrientationRequested) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) SheetCollate(javax.print.attribute.standard.SheetCollate) RequestingUserName(javax.print.attribute.standard.RequestingUserName) CopiesSupported(javax.print.attribute.standard.CopiesSupported) Chromaticity(javax.print.attribute.standard.Chromaticity) Sides(javax.print.attribute.standard.Sides)

Example 2 with RequestingUserName

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

the class Win32PrintJob method initializeAttributeSets.

/* There's some inefficiency here as the job set is created even though
     * it may never be requested.
     */
private synchronized void initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) {
    reqAttrSet = new HashPrintRequestAttributeSet();
    jobAttrSet = new HashPrintJobAttributeSet();
    Attribute[] attrs;
    if (reqSet != null) {
        reqAttrSet.addAll(reqSet);
        attrs = reqSet.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintJobAttribute) {
                jobAttrSet.add(attrs[i]);
            }
        }
    }
    DocAttributeSet docSet = doc.getAttributes();
    if (docSet != null) {
        attrs = docSet.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintRequestAttribute) {
                reqAttrSet.add(attrs[i]);
            }
            if (attrs[i] instanceof PrintJobAttribute) {
                jobAttrSet.add(attrs[i]);
            }
        }
    }
    /* add the user name to the job */
    String userName = "";
    try {
        userName = System.getProperty("user.name");
    } catch (SecurityException se) {
    }
    if (userName == null || userName.equals("")) {
        RequestingUserName ruName = (RequestingUserName) reqSet.get(RequestingUserName.class);
        if (ruName != null) {
            jobAttrSet.add(new JobOriginatingUserName(ruName.getValue(), ruName.getLocale()));
        } else {
            jobAttrSet.add(new JobOriginatingUserName("", null));
        }
    } else {
        jobAttrSet.add(new JobOriginatingUserName(userName, null));
    }
    /* if no job name supplied use doc name (if supplied), if none and
         * its a URL use that, else finally anything .. */
    if (jobAttrSet.get(JobName.class) == null) {
        JobName jobName;
        if (docSet != null && docSet.get(DocumentName.class) != null) {
            DocumentName docName = (DocumentName) docSet.get(DocumentName.class);
            jobName = new JobName(docName.getValue(), docName.getLocale());
            jobAttrSet.add(jobName);
        } else {
            String str = "JPS Job:" + doc;
            try {
                Object printData = doc.getPrintData();
                if (printData instanceof URL) {
                    str = ((URL) (doc.getPrintData())).toString();
                }
            } catch (IOException e) {
            }
            jobName = new JobName(str, null);
            jobAttrSet.add(jobName);
        }
    }
    jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
}
Also used : PrintJobAttribute(javax.print.attribute.PrintJobAttribute) PrintJobAttribute(javax.print.attribute.PrintJobAttribute) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) Attribute(javax.print.attribute.Attribute) HashPrintJobAttributeSet(javax.print.attribute.HashPrintJobAttributeSet) JobName(javax.print.attribute.standard.JobName) IOException(java.io.IOException) java.awt.print(java.awt.print) URL(java.net.URL) DocAttributeSet(javax.print.attribute.DocAttributeSet) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) RequestingUserName(javax.print.attribute.standard.RequestingUserName) JobOriginatingUserName(javax.print.attribute.standard.JobOriginatingUserName) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) DocumentName(javax.print.attribute.standard.DocumentName)

Example 3 with RequestingUserName

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

the class Win32MediaSize method getDefaultAttributeValue.

public Object getDefaultAttributeValue(Class<? extends Attribute> category) {
    if (category == null) {
        throw new NullPointerException("null category");
    }
    if (!Attribute.class.isAssignableFrom(category)) {
        throw new IllegalArgumentException(category + " is not an Attribute");
    }
    if (!isAttributeCategorySupported(category)) {
        return null;
    }
    int[] defaults = getDefaultPrinterSettings();
    // indices must match those in WPrinterJob.cpp
    int defPaper = defaults[0];
    int defYRes = defaults[2];
    int defQuality = defaults[3];
    int defCopies = defaults[4];
    int defOrient = defaults[5];
    int defSides = defaults[6];
    int defCollate = defaults[7];
    int defColor = defaults[8];
    if (category == Copies.class) {
        if (defCopies > 0) {
            return new Copies(defCopies);
        } else {
            return new Copies(1);
        }
    } else if (category == Chromaticity.class) {
        if (defColor == DMCOLOR_COLOR) {
            return Chromaticity.COLOR;
        } else {
            return Chromaticity.MONOCHROME;
        }
    } else if (category == JobName.class) {
        return new JobName("Java Printing", null);
    } else if (category == OrientationRequested.class) {
        if (defOrient == DMORIENT_LANDSCAPE) {
            return OrientationRequested.LANDSCAPE;
        } else {
            return OrientationRequested.PORTRAIT;
        }
    } else if (category == PageRanges.class) {
        return new PageRanges(1, Integer.MAX_VALUE);
    } else if (category == Media.class) {
        MediaSizeName msn = findWin32Media(defPaper);
        if (msn != null) {
            if (!isSupportedMedia(msn) && mediaSizeNames != null) {
                msn = mediaSizeNames[0];
                defPaper = findPaperID(msn);
            }
            return msn;
        } else {
            initMedia();
            if ((mediaSizeNames != null) && (mediaSizeNames.length > 0)) {
                // cannot be null but to be safe, add a check
                if ((idList != null) && (mediaSizes != null) && (idList.size() == mediaSizes.length)) {
                    Integer defIdObj = Integer.valueOf(defPaper);
                    int index = idList.indexOf(defIdObj);
                    if (index >= 0 && index < mediaSizes.length) {
                        return mediaSizes[index].getMediaSizeName();
                    }
                }
                return mediaSizeNames[0];
            }
        }
    } else if (category == MediaPrintableArea.class) {
        /* Verify defPaper */
        MediaSizeName msn = findWin32Media(defPaper);
        if (msn != null && !isSupportedMedia(msn) && mediaSizeNames != null) {
            defPaper = findPaperID(mediaSizeNames[0]);
        }
        float[] prnArea = getMediaPrintableArea(printer, defPaper);
        if (prnArea != null) {
            MediaPrintableArea printableArea = null;
            try {
                printableArea = new MediaPrintableArea(prnArea[0], prnArea[1], prnArea[2], prnArea[3], MediaPrintableArea.INCH);
            } catch (IllegalArgumentException e) {
            }
            return printableArea;
        }
        return null;
    } else if (category == SunAlternateMedia.class) {
        return null;
    } else if (category == Destination.class) {
        try {
            return new Destination((new File("out.prn")).toURI());
        } catch (SecurityException se) {
            try {
                return new Destination(new URI("file:out.prn"));
            } catch (URISyntaxException e) {
                return null;
            }
        }
    } else if (category == Sides.class) {
        switch(defSides) {
            case DMDUP_VERTICAL:
                return Sides.TWO_SIDED_LONG_EDGE;
            case DMDUP_HORIZONTAL:
                return Sides.TWO_SIDED_SHORT_EDGE;
            default:
                return Sides.ONE_SIDED;
        }
    } else if (category == PrinterResolution.class) {
        int yRes = defYRes;
        int xRes = defQuality;
        if ((xRes < 0) || (yRes < 0)) {
            int res = (yRes > xRes) ? yRes : xRes;
            if (res > 0) {
                return new PrinterResolution(res, res, PrinterResolution.DPI);
            }
        } else {
            return new PrinterResolution(xRes, yRes, PrinterResolution.DPI);
        }
    } else if (category == ColorSupported.class) {
        int caps = getPrinterCapabilities();
        if ((caps & DEVCAP_COLOR) != 0) {
            return ColorSupported.SUPPORTED;
        } else {
            return ColorSupported.NOT_SUPPORTED;
        }
    } else if (category == PrintQuality.class) {
        if ((defQuality < 0) && (defQuality >= DMRES_HIGH)) {
            switch(defQuality) {
                case DMRES_HIGH:
                    return PrintQuality.HIGH;
                case DMRES_MEDIUM:
                    return PrintQuality.NORMAL;
                default:
                    return PrintQuality.DRAFT;
            }
        }
    } else if (category == RequestingUserName.class) {
        String userName = "";
        try {
            userName = System.getProperty("user.name", "");
        } catch (SecurityException se) {
        }
        return new RequestingUserName(userName, null);
    } else if (category == SheetCollate.class) {
        if (defCollate == DMCOLLATE_TRUE) {
            return SheetCollate.COLLATED;
        } else {
            return SheetCollate.UNCOLLATED;
        }
    } else if (category == Fidelity.class) {
        return Fidelity.FIDELITY_FALSE;
    }
    return null;
}
Also used : Destination(javax.print.attribute.standard.Destination) Fidelity(javax.print.attribute.standard.Fidelity) ColorSupported(javax.print.attribute.standard.ColorSupported) 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) PrinterResolution(javax.print.attribute.standard.PrinterResolution) Copies(javax.print.attribute.standard.Copies) Sides(javax.print.attribute.standard.Sides) PageRanges(javax.print.attribute.standard.PageRanges) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) RequestingUserName(javax.print.attribute.standard.RequestingUserName) Chromaticity(javax.print.attribute.standard.Chromaticity) File(java.io.File)

Example 4 with RequestingUserName

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

the class Win32MediaSize 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");
        // if postscript & category is already specified within the
        //  PostScript data we return null
        } else if (isAutoSense(flavor) || (isPostScriptFlavor(flavor) && (isPSDocAttr(category)))) {
            return null;
        }
    }
    if (!isAttributeCategorySupported(category)) {
        return null;
    }
    if (category == JobName.class) {
        return new JobName("Java Printing", null);
    } else if (category == RequestingUserName.class) {
        String userName = "";
        try {
            userName = System.getProperty("user.name", "");
        } catch (SecurityException se) {
        }
        return new RequestingUserName(userName, null);
    } else if (category == ColorSupported.class) {
        int caps = getPrinterCapabilities();
        if ((caps & DEVCAP_COLOR) != 0) {
            return ColorSupported.SUPPORTED;
        } else {
            return ColorSupported.NOT_SUPPORTED;
        }
    } else if (category == Chromaticity.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || flavor.equals(DocFlavor.URL.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG) || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.URL.PNG)) {
            int caps = getPrinterCapabilities();
            if ((caps & DEVCAP_COLOR) == 0) {
                Chromaticity[] arr = new Chromaticity[1];
                arr[0] = Chromaticity.MONOCHROME;
                return (arr);
            } else {
                Chromaticity[] arr = new Chromaticity[2];
                arr[0] = Chromaticity.MONOCHROME;
                arr[1] = Chromaticity.COLOR;
                return (arr);
            }
        } else {
            return null;
        }
    } else if (category == Destination.class) {
        try {
            return new Destination((new File("out.prn")).toURI());
        } catch (SecurityException se) {
            try {
                return new Destination(new URI("file:out.prn"));
            } catch (URISyntaxException e) {
                return null;
            }
        }
    } else if (category == OrientationRequested.class) {
        if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE) || flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG) || flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.URL.JPEG) || flavor.equals(DocFlavor.URL.PNG)) {
            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)) {
        synchronized (this) {
            if (gotCopies == false) {
                nCopies = getCopiesSupported(printer, getPort());
                gotCopies = true;
            }
        }
        return new CopiesSupported(1, nCopies);
    } else if (category == Media.class) {
        initMedia();
        int len = (mediaSizeNames == null) ? 0 : mediaSizeNames.length;
        MediaTray[] trays = getMediaTrays();
        len += (trays == null) ? 0 : trays.length;
        Media[] arr = new Media[len];
        if (mediaSizeNames != null) {
            System.arraycopy(mediaSizeNames, 0, arr, 0, mediaSizeNames.length);
        }
        if (trays != null) {
            System.arraycopy(trays, 0, arr, len - trays.length, trays.length);
        }
        return arr;
    } else if (category == MediaPrintableArea.class) {
        // if getting printable area for a specific media size
        Media mediaName = null;
        if ((attributes != null) && ((mediaName = (Media) attributes.get(Media.class)) != null)) {
            if (!(mediaName instanceof MediaSizeName)) {
                // if an instance of MediaTray, fall thru returning
                // all MediaPrintableAreas
                mediaName = null;
            }
        }
        MediaPrintableArea[] mpas = getMediaPrintables((MediaSizeName) mediaName);
        if (mpas != null) {
            MediaPrintableArea[] arr = new MediaPrintableArea[mpas.length];
            System.arraycopy(mpas, 0, arr, 0, mpas.length);
            return arr;
        } else {
            return null;
        }
    } else if (category == SunAlternateMedia.class) {
        return new SunAlternateMedia((Media) getDefaultAttributeValue(Media.class));
    } 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 == PrinterResolution.class) {
        PrinterResolution[] supportedRes = getPrintResolutions();
        if (supportedRes == null) {
            return null;
        }
        PrinterResolution[] arr = new PrinterResolution[supportedRes.length];
        System.arraycopy(supportedRes, 0, arr, 0, supportedRes.length);
        return arr;
    } 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 if (category == PrintQuality.class) {
        PrintQuality[] arr = new PrintQuality[3];
        arr[0] = PrintQuality.DRAFT;
        arr[1] = PrintQuality.HIGH;
        arr[2] = PrintQuality.NORMAL;
        return arr;
    } 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.COLLATED;
            arr[1] = SheetCollate.UNCOLLATED;
            return arr;
        } else {
            return null;
        }
    } else if (category == Fidelity.class) {
        Fidelity[] arr = new Fidelity[2];
        arr[0] = Fidelity.FIDELITY_FALSE;
        arr[1] = Fidelity.FIDELITY_TRUE;
        return arr;
    } 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) PrintQuality(javax.print.attribute.standard.PrintQuality) 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) PrinterResolution(javax.print.attribute.standard.PrinterResolution) Sides(javax.print.attribute.standard.Sides) PageRanges(javax.print.attribute.standard.PageRanges) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) 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)

Example 5 with RequestingUserName

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

the class RasterPrinterJob method setAttributes.

/* subclasses may need to pull extra information out of the attribute set
     * They can override this method & call super.setAttributes()
     */
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
    /*  reset all values to defaults */
    setCollated(false);
    sidesAttr = null;
    pageRangesAttr = null;
    copiesAttr = 0;
    jobNameAttr = null;
    userNameAttr = null;
    destinationAttr = null;
    collateAttReq = false;
    PrintService service = getPrintService();
    if (attributes == null || service == null) {
        return;
    }
    boolean fidelity = false;
    Fidelity attrFidelity = (Fidelity) attributes.get(Fidelity.class);
    if (attrFidelity != null && attrFidelity == Fidelity.FIDELITY_TRUE) {
        fidelity = true;
    }
    if (fidelity == true) {
        AttributeSet unsupported = service.getUnsupportedAttributes(DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes);
        if (unsupported != null) {
            throw new PrinterException("Fidelity cannot be satisfied");
        }
    }
    /*
         * Since we have verified supported values if fidelity is true,
         * we can either ignore unsupported values, or substitute a
         * reasonable alternative
         */
    SheetCollate collateAttr = (SheetCollate) attributes.get(SheetCollate.class);
    if (isSupportedValue(collateAttr, attributes)) {
        setCollated(collateAttr == SheetCollate.COLLATED);
    }
    sidesAttr = (Sides) attributes.get(Sides.class);
    if (!isSupportedValue(sidesAttr, attributes)) {
        sidesAttr = Sides.ONE_SIDED;
    }
    pageRangesAttr = (PageRanges) attributes.get(PageRanges.class);
    if (!isSupportedValue(pageRangesAttr, attributes)) {
        pageRangesAttr = null;
    } else {
        if ((SunPageSelection) attributes.get(SunPageSelection.class) == SunPageSelection.RANGE) {
            // get to, from, min, max page ranges
            int[][] range = pageRangesAttr.getMembers();
            // setPageRanges uses 0-based indexing so we subtract 1
            setPageRange(range[0][0] - 1, range[0][1] - 1);
        } else {
            setPageRange(-1, -1);
        }
    }
    Copies copies = (Copies) attributes.get(Copies.class);
    if (isSupportedValue(copies, attributes) || (!fidelity && copies != null)) {
        copiesAttr = copies.getValue();
        setCopies(copiesAttr);
    } else {
        copiesAttr = getCopies();
    }
    Destination destination = (Destination) attributes.get(Destination.class);
    if (isSupportedValue(destination, attributes)) {
        try {
            // Old code (new File(destination.getURI())).getPath()
            // would generate a "URI is not hierarchical" IAE
            // for "file:out.prn" so we use getSchemeSpecificPart instead
            destinationAttr = "" + new File(destination.getURI().getSchemeSpecificPart());
        } catch (Exception e) {
            // paranoid exception
            Destination defaultDest = (Destination) service.getDefaultAttributeValue(Destination.class);
            if (defaultDest != null) {
                destinationAttr = "" + new File(defaultDest.getURI().getSchemeSpecificPart());
            }
        }
    }
    JobSheets jobSheets = (JobSheets) attributes.get(JobSheets.class);
    if (jobSheets != null) {
        noJobSheet = jobSheets == JobSheets.NONE;
    }
    JobName jobName = (JobName) attributes.get(JobName.class);
    if (isSupportedValue(jobName, attributes) || (!fidelity && jobName != null)) {
        jobNameAttr = jobName.getValue();
        setJobName(jobNameAttr);
    } else {
        jobNameAttr = getJobName();
    }
    RequestingUserName userName = (RequestingUserName) attributes.get(RequestingUserName.class);
    if (isSupportedValue(userName, attributes) || (!fidelity && userName != null)) {
        userNameAttr = userName.getValue();
    } else {
        try {
            userNameAttr = getUserName();
        } catch (SecurityException e) {
            userNameAttr = "";
        }
    }
    /* OpenBook is used internally only when app uses Printable.
         * This is the case when we use the values from the attribute set.
         */
    Media media = (Media) attributes.get(Media.class);
    OrientationRequested orientReq = (OrientationRequested) attributes.get(OrientationRequested.class);
    MediaPrintableArea mpa = (MediaPrintableArea) attributes.get(MediaPrintableArea.class);
    if ((orientReq != null || media != null || mpa != null) && getPageable() instanceof OpenBook) {
        /* We could almost(!) use PrinterJob.getPageFormat() except
             * here we need to start with the PageFormat from the OpenBook :
             */
        Pageable pageable = getPageable();
        Printable printable = pageable.getPrintable(0);
        PageFormat pf = (PageFormat) pageable.getPageFormat(0).clone();
        Paper paper = pf.getPaper();
        /* If there's a media but no media printable area, we can try
             * to retrieve the default value for mpa and use that.
             */
        if (mpa == null && media != null && service.isAttributeCategorySupported(MediaPrintableArea.class)) {
            Object mpaVals = service.getSupportedAttributeValues(MediaPrintableArea.class, null, attributes);
            if (mpaVals instanceof MediaPrintableArea[] && ((MediaPrintableArea[]) mpaVals).length > 0) {
                mpa = ((MediaPrintableArea[]) mpaVals)[0];
            }
        }
        if (isSupportedValue(orientReq, attributes) || (!fidelity && orientReq != null)) {
            int orient;
            if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
                orient = PageFormat.REVERSE_LANDSCAPE;
            } else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
                orient = PageFormat.LANDSCAPE;
            } else {
                orient = PageFormat.PORTRAIT;
            }
            pf.setOrientation(orient);
        }
        if (isSupportedValue(media, attributes) || (!fidelity && media != null)) {
            if (media instanceof MediaSizeName) {
                MediaSizeName msn = (MediaSizeName) media;
                MediaSize msz = MediaSize.getMediaSizeForName(msn);
                if (msz != null) {
                    float paperWid = msz.getX(MediaSize.INCH) * 72.0f;
                    float paperHgt = msz.getY(MediaSize.INCH) * 72.0f;
                    paper.setSize(paperWid, paperHgt);
                    if (mpa == null) {
                        paper.setImageableArea(72.0, 72.0, paperWid - 144.0, paperHgt - 144.0);
                    }
                }
            }
        }
        if (isSupportedValue(mpa, attributes) || (!fidelity && mpa != null)) {
            float[] printableArea = mpa.getPrintableArea(MediaPrintableArea.INCH);
            for (int i = 0; i < printableArea.length; i++) {
                printableArea[i] = printableArea[i] * 72.0f;
            }
            paper.setImageableArea(printableArea[0], printableArea[1], printableArea[2], printableArea[3]);
        }
        pf.setPaper(paper);
        pf = validatePage(pf);
        setPrintable(printable, pf);
    } else {
        // for AWT where pageable is not an instance of OpenBook,
        // we need to save paper info
        this.attributes = attributes;
    }
}
Also used : Fidelity(javax.print.attribute.standard.Fidelity) Destination(javax.print.attribute.standard.Destination) JobName(javax.print.attribute.standard.JobName) PrinterException(java.awt.print.PrinterException) OrientationRequested(javax.print.attribute.standard.OrientationRequested) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) PageFormat(java.awt.print.PageFormat) Pageable(java.awt.print.Pageable) Copies(javax.print.attribute.standard.Copies) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) JobSheets(javax.print.attribute.standard.JobSheets) PrinterAbortException(java.awt.print.PrinterAbortException) HeadlessException(java.awt.HeadlessException) PrintException(javax.print.PrintException) PrinterException(java.awt.print.PrinterException) IOException(java.io.IOException) AttributeSet(javax.print.attribute.AttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) SheetCollate(javax.print.attribute.standard.SheetCollate) RequestingUserName(javax.print.attribute.standard.RequestingUserName) Paper(java.awt.print.Paper) Printable(java.awt.print.Printable) File(java.io.File)

Aggregations

JobName (javax.print.attribute.standard.JobName)8 RequestingUserName (javax.print.attribute.standard.RequestingUserName)8 Attribute (javax.print.attribute.Attribute)7 Fidelity (javax.print.attribute.standard.Fidelity)5 Media (javax.print.attribute.standard.Media)5 MediaPrintableArea (javax.print.attribute.standard.MediaPrintableArea)5 MediaSizeName (javax.print.attribute.standard.MediaSizeName)5 OrientationRequested (javax.print.attribute.standard.OrientationRequested)5 File (java.io.File)4 IOException (java.io.IOException)4 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)4 PrintServiceAttribute (javax.print.attribute.PrintServiceAttribute)4 Chromaticity (javax.print.attribute.standard.Chromaticity)4 Destination (javax.print.attribute.standard.Destination)4 SheetCollate (javax.print.attribute.standard.SheetCollate)4 java.awt.print (java.awt.print)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 URL (java.net.URL)3 DocAttributeSet (javax.print.attribute.DocAttributeSet)3