Search in sources :

Example 11 with HashPrintRequestAttributeSet

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

use of javax.print.attribute.HashPrintRequestAttributeSet 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 13 with HashPrintRequestAttributeSet

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

the class PSPrinterJob method printDialog.

/* Instance Methods */
/**
     * 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();
    }
    if (attributes == null) {
        attributes = new HashPrintRequestAttributeSet();
    }
    attributes.add(new Copies(getCopies()));
    attributes.add(new JobName(getJobName(), null));
    boolean doPrint = false;
    DialogTypeSelection dts = (DialogTypeSelection) attributes.get(DialogTypeSelection.class);
    if (dts == DialogTypeSelection.NATIVE) {
        // Remove DialogTypeSelection.NATIVE to prevent infinite loop in
        // RasterPrinterJob.
        attributes.remove(DialogTypeSelection.class);
        doPrint = printDialog(attributes);
        // restore attribute
        attributes.add(DialogTypeSelection.NATIVE);
    } else {
        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.ps";
            }
        } else {
            mDestType = RasterPrinterJob.PRINTER;
            PrintService pServ = getPrintService();
            if (pServ != null) {
                mDestination = pServ.getName();
                if (isMac) {
                    PrintServiceAttributeSet psaSet = pServ.getAttributes();
                    if (psaSet != null) {
                        mDestination = psaSet.get(PrinterName.class).toString();
                    }
                }
            }
        }
    }
    return doPrint;
}
Also used : Destination(javax.print.attribute.standard.Destination) HeadlessException(java.awt.HeadlessException) Copies(javax.print.attribute.standard.Copies) JobName(javax.print.attribute.standard.JobName) File(java.io.File) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) HeadlessException(java.awt.HeadlessException) CharConversionException(java.io.CharConversionException) PrinterException(java.awt.print.PrinterException) PrinterIOException(java.awt.print.PrinterIOException) IOException(java.io.IOException) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) DialogTypeSelection(javax.print.attribute.standard.DialogTypeSelection) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService)

Example 14 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project pdfbox by apache.

the class Printing method printWithDialogAndAttributes.

/**
 * Prints with a print preview dialog and custom PrintRequestAttribute values.
 */
private static void printWithDialogAndAttributes(PDDocument document) throws IOException, PrinterException {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(new PDFPageable(document));
    PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
    // pages 1 to 1
    attr.add(new PageRanges(1, 1));
    PDViewerPreferences vp = document.getDocumentCatalog().getViewerPreferences();
    if (vp != null && vp.getDuplex() != null) {
        String dp = vp.getDuplex();
        if (PDViewerPreferences.DUPLEX.DuplexFlipLongEdge.toString().equals(dp)) {
            attr.add(Sides.TWO_SIDED_LONG_EDGE);
        } else if (PDViewerPreferences.DUPLEX.DuplexFlipShortEdge.toString().equals(dp)) {
            attr.add(Sides.TWO_SIDED_SHORT_EDGE);
        } else if (PDViewerPreferences.DUPLEX.Simplex.toString().equals(dp)) {
            attr.add(Sides.ONE_SIDED);
        }
    }
    if (job.printDialog(attr)) {
        job.print(attr);
    }
}
Also used : PDFPageable(org.apache.pdfbox.printing.PDFPageable) PageRanges(javax.print.attribute.standard.PageRanges) PDViewerPreferences(org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 15 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project pdfbox by apache.

the class Printing method printWithAttributes.

/**
 * Prints using custom PrintRequestAttribute values.
 */
private static void printWithAttributes(PDDocument document) throws IOException, PrinterException {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(new PDFPageable(document));
    PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
    // pages 1 to 1
    attr.add(new PageRanges(1, 1));
    job.print(attr);
}
Also used : PDFPageable(org.apache.pdfbox.printing.PDFPageable) PageRanges(javax.print.attribute.standard.PageRanges) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Aggregations

HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)40 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)28 PrinterJob (java.awt.print.PrinterJob)16 PrintService (javax.print.PrintService)13 IOException (java.io.IOException)12 JobName (javax.print.attribute.standard.JobName)12 PrinterException (java.awt.print.PrinterException)10 File (java.io.File)9 DocFlavor (javax.print.DocFlavor)9 Copies (javax.print.attribute.standard.Copies)9 DocPrintJob (javax.print.DocPrintJob)8 Attribute (javax.print.attribute.Attribute)7 SimpleDoc (javax.print.SimpleDoc)6 PageFormat (java.awt.print.PageFormat)5 PrintException (javax.print.PrintException)5 PrintRequestAttribute (javax.print.attribute.PrintRequestAttribute)5 PrintServiceAttributeSet (javax.print.attribute.PrintServiceAttributeSet)4 Destination (javax.print.attribute.standard.Destination)4 java.awt.print (java.awt.print)3 Printable (java.awt.print.Printable)3