Search in sources :

Example 11 with Attribute

use of javax.print.attribute.Attribute 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 12 with Attribute

use of javax.print.attribute.Attribute in project jdk8u_jdk by JetBrains.

the class Win32MediaSize method getUpdatedAttributes.

public PrintServiceAttributeSet getUpdatedAttributes() {
    PrintServiceAttributeSet currSet = getDynamicAttributes();
    if (lastSet == null) {
        lastSet = currSet;
        return AttributeSetUtilities.unmodifiableView(currSet);
    } else {
        PrintServiceAttributeSet updates = new HashPrintServiceAttributeSet();
        Attribute[] attrs = currSet.toArray();
        for (int i = 0; i < attrs.length; i++) {
            Attribute attr = attrs[i];
            if (!lastSet.containsValue(attr)) {
                updates.add(attr);
            }
        }
        lastSet = currSet;
        return AttributeSetUtilities.unmodifiableView(updates);
    }
}
Also used : PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) Attribute(javax.print.attribute.Attribute) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet)

Example 13 with Attribute

use of javax.print.attribute.Attribute in project jdk8u_jdk by JetBrains.

the class Win32PrintServiceLookup method matchingService.

boolean matchingService(PrintService service, PrintServiceAttributeSet serviceSet) {
    if (serviceSet != null) {
        Attribute[] attrs = serviceSet.toArray();
        Attribute serviceAttr;
        for (int i = 0; i < attrs.length; i++) {
            serviceAttr = service.getAttribute((Class<PrintServiceAttribute>) attrs[i].getCategory());
            if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
                return false;
            }
        }
    }
    return true;
}
Also used : PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) Attribute(javax.print.attribute.Attribute) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute)

Example 14 with Attribute

use of javax.print.attribute.Attribute in project jdk8u_jdk by JetBrains.

the class Win32PrintServiceLookup method getPrintServices.

public PrintService[] getPrintServices(DocFlavor flavor, AttributeSet attributes) {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkPrintJobAccess();
    }
    PrintRequestAttributeSet requestSet = null;
    PrintServiceAttributeSet serviceSet = null;
    if (attributes != null && !attributes.isEmpty()) {
        requestSet = new HashPrintRequestAttributeSet();
        serviceSet = new HashPrintServiceAttributeSet();
        Attribute[] attrs = attributes.toArray();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i] instanceof PrintRequestAttribute) {
                requestSet.add(attrs[i]);
            } else if (attrs[i] instanceof PrintServiceAttribute) {
                serviceSet.add(attrs[i]);
            }
        }
    }
    /*
         * Special case: If client is asking for a particular printer
         * (by name) then we can save time by getting just that service
         * to check against the rest of the specified attributes.
         */
    PrintService[] services = null;
    if (serviceSet != null && serviceSet.get(PrinterName.class) != null) {
        PrinterName name = (PrinterName) serviceSet.get(PrinterName.class);
        PrintService service = getPrintServiceByName(name.getValue());
        if (service == null || !matchingService(service, serviceSet)) {
            services = new PrintService[0];
        } else {
            services = new PrintService[1];
            services[0] = service;
        }
    } else {
        services = getPrintServices();
    }
    if (services.length == 0) {
        return services;
    } else {
        ArrayList matchingServices = new ArrayList();
        for (int i = 0; i < services.length; i++) {
            try {
                if (services[i].getUnsupportedAttributes(flavor, requestSet) == null) {
                    matchingServices.add(services[i]);
                }
            } catch (IllegalArgumentException e) {
            }
        }
        services = new PrintService[matchingServices.size()];
        return (PrintService[]) matchingServices.toArray(services);
    }
}
Also used : PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) Attribute(javax.print.attribute.Attribute) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) ArrayList(java.util.ArrayList) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintService(javax.print.PrintService) MultiDocPrintService(javax.print.MultiDocPrintService) PrinterName(javax.print.attribute.standard.PrinterName) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) PrintServiceAttribute(javax.print.attribute.PrintServiceAttribute) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 15 with Attribute

use of javax.print.attribute.Attribute in project camel by apache.

the class PrinterPrintTest method printToMiddleTray.

@Test
public void printToMiddleTray() throws Exception {
    PrinterEndpoint endpoint = new PrinterEndpoint();
    PrinterConfiguration configuration = new PrinterConfiguration();
    configuration.setHostname("localhost");
    configuration.setPort(631);
    configuration.setPrintername("DefaultPrinter");
    configuration.setMediaSizeName(MediaSizeName.ISO_A4);
    configuration.setInternalSides(Sides.ONE_SIDED);
    configuration.setInternalOrientation(OrientationRequested.PORTRAIT);
    configuration.setMediaTray("middle");
    PrinterProducer producer = new PrinterProducer(endpoint, configuration);
    producer.start();
    PrinterOperations printerOperations = producer.getPrinterOperations();
    PrintRequestAttributeSet attributeSet = printerOperations.getPrintRequestAttributeSet();
    Attribute attribute = attributeSet.get(javax.print.attribute.standard.Media.class);
    assertNotNull(attribute);
    assertTrue(attribute instanceof MediaTray);
    MediaTray mediaTray = (MediaTray) attribute;
    assertEquals("middle", mediaTray.toString());
}
Also used : Attribute(javax.print.attribute.Attribute) MediaTray(javax.print.attribute.standard.MediaTray) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) Test(org.junit.Test)

Aggregations

Attribute (javax.print.attribute.Attribute)26 PrintServiceAttribute (javax.print.attribute.PrintServiceAttribute)13 PrintRequestAttribute (javax.print.attribute.PrintRequestAttribute)10 MediaSizeName (javax.print.attribute.standard.MediaSizeName)9 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)8 JobName (javax.print.attribute.standard.JobName)8 HashPrintServiceAttributeSet (javax.print.attribute.HashPrintServiceAttributeSet)7 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)7 PrintServiceAttributeSet (javax.print.attribute.PrintServiceAttributeSet)7 Fidelity (javax.print.attribute.standard.Fidelity)7 OrientationRequested (javax.print.attribute.standard.OrientationRequested)7 RequestingUserName (javax.print.attribute.standard.RequestingUserName)7 java.awt.print (java.awt.print)6 PrintJobAttribute (javax.print.attribute.PrintJobAttribute)6 Media (javax.print.attribute.standard.Media)6 File (java.io.File)5 IOException (java.io.IOException)5 URI (java.net.URI)5 Chromaticity (javax.print.attribute.standard.Chromaticity)5 Destination (javax.print.attribute.standard.Destination)5