Java Printer Usage lookupPrintNames(PrintService[] prtSrvs)

Here you can find the source of lookupPrintNames(PrintService[] prtSrvs)

Description

lookup Print Names

License

GNU General Public License

Declaration

public static String[] lookupPrintNames(PrintService[] prtSrvs) 

Method Source Code

//package com.java2s;
/*//from w  w w . j a va2  s  .  c  o  m
 * Copyright (C) 2015  University of Oregon
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Apache License, as specified in the LICENSE file.
 *
 * For more information, see the LICENSE file.
 */

import java.io.*;

import javax.print.*;

public class Main {
    private static String[] psList;
    private static boolean bWaitForPrinterServer = false;
    private static PrintService[] printSrvs = null;
    private static long lastQueryTime_all = 0;

    public static String[] lookupPrintNames(PrintService[] prtSrvs) {
        int i, n;
        int total = 0;
        String[] psnames;
        int nums = 6;
        String name;

        if (prtSrvs != null) {
            nums += prtSrvs.length;
        }
        psnames = new String[nums];
        if (prtSrvs != null) {
            for (n = 0; n < prtSrvs.length; n++) {
                if (prtSrvs[n] != null) {
                    name = prtSrvs[n].getName();
                    if (name != null) {
                        if (isGoodPrinterName(name))
                            psnames[total++] = name;
                    }
                }
            }
        }
        String cupsPath = new StringBuffer().append(File.separator)
                .append("etc").append(File.separator).append("cups")
                .append(File.separator).append("ppd").toString();
        File dir = new File(cupsPath);
        File files[] = null;
        if (dir.exists() && dir.isDirectory())
            if (dir.canRead())
                files = dir.listFiles();
        if (files != null) {
            for (i = 0; i < files.length; i++) {
                if (files[i].isFile()) {
                    String fname = files[i].getName();
                    if (fname.endsWith(".ppd")) {
                        name = fname.substring(0, fname.indexOf(".ppd"));
                        if (name.length() > 0) {
                            for (n = 0; n < nums; n++) {
                                if (psnames[n] != null) {
                                    if (name.equals(psnames[n])) {
                                        name = null;
                                        break;
                                    }
                                }
                            }
                            if (name != null && total < nums) {
                                psnames[total++] = name;
                            }
                        }
                    }
                    if (total >= nums)
                        break;
                }
            }
        }
        if (total > 0) {
            psList = new String[total];
            for (i = 0; i < total; i++)
                psList[i] = psnames[i];
        }

        return psList;
    }

    public static String[] lookupPrintNames() {
        lookupPrintServices();
        return lookupPrintNames(printSrvs);
    }

    public static boolean isGoodPrinterName(String name) {
        if (name == null)
            return false;
        if (name.equals("jeff_pl") || name.equals("jeff_pr"))
            return false;
        if (name.equals("steve_pl") || name.equals("steve_pr"))
            return false;

        return true;
    }

    public static PrintService[] lookupPrintServices() {
        if (bWaitForPrinterServer)
            return printSrvs;

        bWaitForPrinterServer = true;
        if (printSrvs == null || printSrvs.length < 1) {
            long t = System.currentTimeMillis() / 1000;
            if ((t - lastQueryTime_all) < 120) {
                bWaitForPrinterServer = false;
                return printSrvs;
            }
            printSrvs = PrintServiceLookup.lookupPrintServices(null, null);
            lastQueryTime_all = System.currentTimeMillis() / 1000;
        }
        bWaitForPrinterServer = false;
        return printSrvs;
    }
}

Related

  1. getSetting(PrintService service, PrintRequestAttributeSet set, Class type, boolean tryDefaultIfNull)
  2. getSupportedAttributes(PrintService p)
  3. getSupportedAttrName(Class clazz)
  4. isValueSupported(PrintService ps, Attribute attr, DocFlavor flavor, AttributeSet attributes)
  5. lookupMediaSize(final Media media)
  6. lookupPrintService()
  7. lookupPrintServices()
  8. notInArray(PrintService key, PrintService[] array)
  9. print(PrintService printService, byte[] pdf)