Search in sources :

Example 11 with PrintRequestAttributeSet

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

use of javax.print.attribute.PrintRequestAttributeSet in project jgnash by ccavanaugh.

the class PrintableCheckLayout method getPageFormat.

public PageFormat getPageFormat() {
    PrintRequestAttributeSet printAttributes = checkLayout.getPrintAttributes();
    if (pageFormat == null) {
        // create a default pageFormat
        PageFormat format = getPrinterJob().defaultPage();
        Paper paper = format.getPaper();
        MediaSizeName media = (MediaSizeName) printAttributes.get(Media.class);
        MediaSize ms = MediaSize.getMediaSizeForName(media);
        MediaPrintableArea ma = (MediaPrintableArea) printAttributes.get(MediaPrintableArea.class);
        if (ma != null) {
            int INCH = MediaPrintableArea.INCH;
            paper.setImageableArea((ma.getX(INCH) * 72), (ma.getY(INCH) * 72), (ma.getWidth(INCH) * 72), (ma.getHeight(INCH) * 72));
        }
        if (ms != null) {
            paper.setSize((ms.getX(Size2DSyntax.INCH) * 72), (ms.getY(Size2DSyntax.INCH) * 72));
        }
        format.setPaper(paper);
        OrientationRequested or = (OrientationRequested) printAttributes.get(OrientationRequested.class);
        if (or != null) {
            if (or == OrientationRequested.LANDSCAPE) {
                format.setOrientation(PageFormat.LANDSCAPE);
            } else if (or == OrientationRequested.REVERSE_LANDSCAPE) {
                format.setOrientation(PageFormat.REVERSE_LANDSCAPE);
            } else if (or == OrientationRequested.PORTRAIT) {
                format.setOrientation(PageFormat.PORTRAIT);
            } else if (or == OrientationRequested.REVERSE_PORTRAIT) {
                format.setOrientation(PageFormat.PORTRAIT);
            }
        }
        pageFormat = format;
    }
    return pageFormat;
}
Also used : PageFormat(java.awt.print.PageFormat) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) Paper(java.awt.print.Paper) OrientationRequested(javax.print.attribute.standard.OrientationRequested) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet)

Example 13 with PrintRequestAttributeSet

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

Example 14 with PrintRequestAttributeSet

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

the class PrinterOperations method print.

public void print(Doc doc, String jobName) throws PrintException {
    // we need create a new job for each print 
    DocPrintJob job = getPrintService().createPrintJob();
    PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(printRequestAttributeSet);
    attrs.add(new JobName(jobName, Locale.getDefault()));
    job.print(doc, attrs);
}
Also used : JobName(javax.print.attribute.standard.JobName) DocPrintJob(javax.print.DocPrintJob) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 15 with PrintRequestAttributeSet

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

the class PrinterProducer method assignPrintAttributes.

private PrintRequestAttributeSet assignPrintAttributes() throws PrintException {
    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
    if (config.getCopies() >= 1) {
        printRequestAttributeSet.add(new Copies(config.getCopies()));
    } else {
        throw new PrintException("Number of print copies should be greater than zero");
    }
    printRequestAttributeSet.add(config.getMediaSizeName());
    printRequestAttributeSet.add(config.getInternalSides());
    printRequestAttributeSet.add(config.getInternalOrientation());
    if (config.getMediaTray() != null) {
        MediaTray mediaTray = resolveMediaTray(config.getMediaTray());
        if (mediaTray == null) {
            throw new PrintException("mediatray not found " + config.getMediaTray());
        }
        printRequestAttributeSet.add(mediaTray);
    }
    return printRequestAttributeSet;
}
Also used : PrintException(javax.print.PrintException) Copies(javax.print.attribute.standard.Copies) MediaTray(javax.print.attribute.standard.MediaTray) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Aggregations

PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)26 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)21 PrintService (javax.print.PrintService)11 PrinterException (java.awt.print.PrinterException)7 PrinterJob (java.awt.print.PrinterJob)7 Copies (javax.print.attribute.standard.Copies)7 PageFormat (java.awt.print.PageFormat)6 Attribute (javax.print.attribute.Attribute)6 IOException (java.io.IOException)5 HeadlessException (java.awt.HeadlessException)4 Printable (java.awt.print.Printable)4 File (java.io.File)4 DocFlavor (javax.print.DocFlavor)4 JobName (javax.print.attribute.standard.JobName)4 DocPrintJob (javax.print.DocPrintJob)3 PrintException (javax.print.PrintException)3 StreamPrintService (javax.print.StreamPrintService)3 Destination (javax.print.attribute.standard.Destination)3 Rectangle (java.awt.Rectangle)2 Paper (java.awt.print.Paper)2