Example usage for javax.print PrintServiceLookup lookupPrintServices

List of usage examples for javax.print PrintServiceLookup lookupPrintServices

Introduction

In this page you can find the example usage for javax.print PrintServiceLookup lookupPrintServices.

Prototype

public static final PrintService[] lookupPrintServices(DocFlavor flavor, AttributeSet attributes) 

Source Link

Document

Locates print services capable of printing the specified DocFlavor .

Usage

From source file:org.pentaho.reporting.platform.plugin.SimpleReportingComponent.java

/**
 * Perform the primary function of this component, this is, to execute. This method will be invoked immediately following a successful validate().
 *
 * @return true if successful execution//ww w.  j  a va 2  s . c om
 * @throws Exception
 */
public boolean execute() throws Exception {
    final MasterReport report = getReport();

    try {
        final DefaultParameterContext parameterContext = new DefaultParameterContext(report);
        // open parameter context
        final ValidationResult vr = applyInputsToReportParameters(parameterContext, null);
        if (vr.isEmpty() == false) {
            return false;
        }
        parameterContext.close();

        if (isPrint()) {
            // handle printing
            // basic logic here is: get the default printer, attempt to resolve the user specified printer, default back as needed
            PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
            if (StringUtils.isEmpty(getPrinter()) == false) {
                final PrintService[] services = PrintServiceLookup
                        .lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
                for (final PrintService service : services) {
                    if (service.getName().equals(printer)) {
                        printService = service;
                    }
                }
                if ((printer == null) && (services.length > 0)) {
                    printService = services[0];
                }
            }
            Java14PrintUtil.printDirectly(report, printService);
            return true;
        }

        final String outputType = computeEffectiveOutputTarget();
        final ReportOutputHandler reportOutputHandler = createOutputHandlerForOutputType(outputType);
        if (reportOutputHandler == null) {
            log.warn(Messages.getInstance().getString("ReportPlugin.warnUnprocessableRequest", outputType));
            return false;
        }
        synchronized (reportOutputHandler.getReportLock()) {
            try {
                pageCount = reportOutputHandler.generate(report, acceptedPage, outputStream, getYieldRate());
                return pageCount != -1;
            } finally {
                reportOutputHandler.close();
            }
        }
    } catch (Throwable t) {
        log.error(Messages.getInstance().getString("ReportPlugin.executionFailed"), t); //$NON-NLS-1$
    }
    // lets not pretend we were successfull, if the export type was not a valid one.
    return false;
}

From source file:org.saiku.adhoc.service.report.SaikuReportingComponent.java

public boolean execute() throws Exception {
    final MasterReport report = getReport();

    try {/*  ww w  .ja  v a  2  s . com*/
        final DefaultParameterContext parameterContext = new DefaultParameterContext(report);
        // open parameter context
        final ValidationResult vr = applyInputsToReportParameters(parameterContext, null);
        if (vr.isEmpty() == false) {
            return false;
        }
        parameterContext.close();

        if (isPrint()) {
            // handle printing
            // basic logic here is: get the default printer, attempt to resolve the user specified printer, default
            // back as needed
            PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
            if (StringUtils.isEmpty(getPrinter()) == false) {
                final PrintService[] services = PrintServiceLookup
                        .lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
                for (final PrintService service : services) {
                    if (service.getName().equals(printer)) {
                        printService = service;
                    }
                }
                if ((printer == null) && (services.length > 0)) {
                    printService = services[0];
                }
            }
            Java14PrintUtil.printDirectly(report, printService);
            return true;
        }

        final String outputType = computeEffectiveOutputTarget();
        final ReportOutputHandler reportOutputHandler = createOutputHandlerForOutputType(outputType);
        if (reportOutputHandler == null) {
            log.warn(Messages.getInstance().getString("ReportPlugin.warnUnprocessableRequest", outputType));
            return false;
        }
        synchronized (reportOutputHandler.getReportLock()) {
            try {
                pageCount = reportOutputHandler.generate(report, acceptedPage, outputStream, getYieldRate());
                return pageCount != -1;
            } finally {
                reportOutputHandler.close();
            }
        }
    } catch (Throwable t) {
        log.error(Messages.getInstance().getString("ReportPlugin.executionFailed"), t); //$NON-NLS-1$
    }
    // lets not pretend we were successfull, if the export type was not a valid one.
    return false;
}

From source file:org.springframework.integration.print.core.PrintServiceExecutor.java

public static SortedSet<PrintService> getAvailablePrinterServices() {
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    printServices[0].getName();//from   w  w  w . j a v a  2 s. co  m

    SortedSet<PrintService> printServicesList = new TreeSet<PrintService>(new PrintServiceComparator());
    printServicesList.addAll(Arrays.asList(printServices));

    return printServicesList;
}

From source file:us.mn.state.health.lims.common.provider.reports.SampleLabelPrintProvider.java

private String validateInput(String accessionCount, String accessionStart, String accessionEnd,
        String printerName, String masterLabels, String itemLabels) {
    if ((StringUtil.isNullorNill(accessionCount) && StringUtil.isNullorNill(accessionStart)
            && StringUtil.isNullorNill(accessionEnd)) || StringUtil.isNullorNill(printerName)
            || StringUtil.isNullorNill(masterLabels) || "0".equals(masterLabels))
        return FWD_FAIL;

    masterCount = Integer.parseInt(masterLabels);
    itemCount = StringUtil.isNullorNill(itemLabels) ? 0 : Integer.parseInt(itemLabels);

    //bugzilla 2374 limit number of labels
    int maxNumberOfLabels = Integer.parseInt(SystemConfiguration.getInstance().getMaxNumberOfLabels());
    if (masterCount > maxNumberOfLabels || itemCount > maxNumberOfLabels) {
        LogEvent.logError("SampleLabelPrintProvider", "printLabels()",
                StringUtil.getMessageForKey("errors.labelprint.exceeded.maxnumber",
                        SystemConfiguration.getInstance().getMaxNumberOfLabels()));
        return FWD_FAIL_MAX_LABELS_EXCEED;
    }/*from   w  w  w. j  a va  2  s . c  om*/

    //validate printer
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    String printer = null;
    PrinterName printerToUse = new PrinterName(printerName, null);

    for (int i = 0; i < services.length; i++) {
        printer = services[i].getName();
        if (printer.equalsIgnoreCase(printerToUse.toString())) {
            //System.out.println("This is the printer I will use "
            //+ printerName);
            ps = services[i];
            //bugzilla 2380: load all valid printer names for error message
            //break;
        }
    }

    if (ps == null) {
        LogEvent.logError("SampleLabelPrintProvider", "printLabels()",
                StringUtil.getMessageForKey("errors.labelprint.no.printer"));
        return FWD_FAIL_BAD_PRINTER;
    }

    return FWD_SUCCESS;
}

From source file:us.mn.state.health.lims.common.provider.reports.SampleLabelPrintProvider.java

/**
 * Get all printer's names in server system
 * /* w w w.  j  av a  2 s.  c  om*/
 * @return printServiceNames : String List
 */
public List<String> getAllSystemPrintServiceNames() {
    List<String> printServiceNames = new ArrayList<String>();
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    if (services != null) {
        for (int i = 0; i < services.length; i++) {
            if (services[i] != null) {
                printServiceNames.add(services[i].getName());
            }
        }
    }
    return printServiceNames;
}