javax.print.PrintService Java Examples

The following examples show how to use javax.print.PrintService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RasterPrinterJob.java    From jdk8u-dev-jdk with GNU General Public License v2.0 7 votes vote down vote up
protected static PrintService lookupDefaultPrintService() {
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();

    /* Pageable implies Printable so checking both isn't strictly needed */
    if (service != null &&
        service.isDocFlavorSupported(
                            DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&
        service.isDocFlavorSupported(
                            DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
        return service;
    } else {
       PrintService []services =
         PrintServiceLookup.lookupPrintServices(
                            DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
       if (services.length > 0) {
           return services[0];
       }
    }
    return null;
}
 
Example #2
Source File: BannerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args)  throws Exception {
    job = PrinterJob.getPrinterJob();
    PrintService prtSrv = job.getPrintService();
    if (job.getPrintService() == null) {
        System.out.println("No printers. Test cannot continue");
        return;
    }
    if (!prtSrv.isAttributeCategorySupported(JobSheets.class)) {
        return;
    }
    SwingUtilities.invokeAndWait(() -> {
        doTest(BannerTest::printTest);
    });
    mainThread = Thread.currentThread();
    try {
        Thread.sleep(180000);
    } catch (InterruptedException e) {
        if (!testPassed && testGeneratedInterrupt) {
            throw new RuntimeException("Banner page did not print");
        }
    }
    if (!testGeneratedInterrupt) {
        throw new RuntimeException("user has not executed the test");
    }
}
 
Example #3
Source File: TestRaceCond.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void trial() {
    PrintService pserv1 = PrintServiceLookup.lookupDefaultPrintService();
    PrintService[] pservs = PrintServiceLookup.lookupPrintServices(null, null);
    PrintService pserv2 = PrintServiceLookup.lookupDefaultPrintService();

    if ((pserv1 == null) || (pserv2==null)) {
        return;
    }

    if (pserv1.hashCode() != pserv2.hashCode()) {
        throw new RuntimeException("Different hashCodes for equal print "
                        + "services: " + pserv1.hashCode() + " "
                        + pserv2.hashCode());
    }
}
 
Example #4
Source File: Win32PrintServiceLookup.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized PrintService getDefaultPrintService() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
      security.checkPrintJobAccess();
    }


    // Windows does not have notification for a change in default
    // so we always get the latest.
    defaultPrinter = getDefaultPrinterName();
    if (defaultPrinter == null) {
        return null;
    }

    if ((defaultPrintService != null) &&
        defaultPrintService.getName().equals(defaultPrinter)) {

        return defaultPrintService;
    }

     // Not the same as default so proceed to get new PrintService.

    // clear defaultPrintService
    defaultPrintService = null;

    if (printServices != null) {
        for (int j=0; j<printServices.length; j++) {
            if (defaultPrinter.equals(printServices[j].getName())) {
                defaultPrintService = printServices[j];
                break;
            }
        }
    }

    if (defaultPrintService == null) {
        defaultPrintService = new Win32PrintService(defaultPrinter);
    }
    return defaultPrintService;
}
 
Example #5
Source File: GetMediasTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    for(final PrintService service: services) {
        Thread thread = new Thread() {
            public void run() {
                service.getSupportedAttributeValues(Media.class, null, null);
            }
        };
        thread.start();
    }
}
 
Example #6
Source File: Win32PrintJob.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void pageableJob(Pageable pageable) throws PrintException {
    try {
        synchronized(this) {
            if (job != null) { // shouldn't happen
                throw new PrintException("already printing");
            } else {
                job = new sun.awt.windows.WPrinterJob();
            }
        }
        PrintService svc = getPrintService();
        job.setPrintService(svc);
        if (copies == 0) {
            Copies c = (Copies)svc.getDefaultAttributeValue(Copies.class);
            copies = c.getValue();
        }
        job.setCopies(copies);
        job.setJobName(jobName);
        job.setPageable(pageable);
        job.print(reqAttrSet);
        notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
        return;
    } catch (PrinterException pe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(pe);
    } finally {
        printReturned = true;
        notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
    }
}
 
Example #7
Source File: PrintServiceLookupProvider.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private PrintService getNamedPrinterNameSysV(String name) {

        String command = "/usr/bin/lpstat -v " + name;
        String []result = execCmd(command);

        if (result == null || result[0].indexOf("unknown printer") > 0) {
            return null;
        } else {
            return new UnixPrintService(name);
        }
    }
 
Example #8
Source File: PrintToDir.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void doPrinterJob(String fileStr, OrientationRequested o) {
    PrinterJob  pj = PrinterJob.getPrinterJob();
    PrintService ps = pj.getPrintService();
    if (ps == null) {
      System.out.println("No print service found.");
      return;
    }
    pj.setPrintable(new PrintToDir());
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(o);
    File f = new File(fileStr);
    //      f.deleteOnExit();
    URI dest = f.toURI();
    Destination d = new Destination(dest);
    if (ps.isAttributeValueSupported(d, null, null)) {
        aset.add(d);
        try {
            pj.print(aset);
        } catch (PrinterException e) {
            System.out.println("PrinterJob passed.");
            return;
        }
        throw new RuntimeException("PrinterJob:PrinterException expected but not thrown. \nTEST FAILED");
    } else {
        System.out.println("Destination attribute is not a supported value.  PrinterJob passed.");
    }
}
 
Example #9
Source File: RasterPrinterJob.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected boolean isSupportedValue(Attribute attrval,
                                 PrintRequestAttributeSet attrset) {
    PrintService ps = getPrintService();
    return
        (attrval != null && ps != null &&
         ps.isAttributeValueSupported(attrval,
                                      DocFlavor.SERVICE_FORMATTED.PAGEABLE,
                                      attrset));
}
 
Example #10
Source File: TestRaceCond.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void trial() {
    PrintService pserv1 = PrintServiceLookup.lookupDefaultPrintService();
    PrintService[] pservs = PrintServiceLookup.lookupPrintServices(null, null);
    PrintService pserv2 = PrintServiceLookup.lookupDefaultPrintService();

    if ((pserv1 == null) || (pserv2==null)) {
        return;
    }

    if (pserv1.hashCode() != pserv2.hashCode()) {
        throw new RuntimeException("Different hashCodes for equal print "
                        + "services: " + pserv1.hashCode() + " "
                        + pserv2.hashCode());
    }
}
 
Example #11
Source File: SettingController.java    From webapp-hardware-bridge with MIT License 5 votes vote down vote up
private ArrayList<String> listPrinters() {
    ArrayList<String> printerList = new ArrayList<>();
    PrintService[] printServices = PrinterJob.lookupPrintServices();
    for (PrintService printService : printServices) {
        printerList.add(printService.getName());
    }
    return printerList;
}
 
Example #12
Source File: GetMediasTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    for(final PrintService service: services) {
        Thread thread = new Thread() {
            public void run() {
                service.getSupportedAttributeValues(Media.class, null, null);
            }
        };
        thread.start();
    }
}
 
Example #13
Source File: RasterPrinterJob.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected boolean isSupportedValue(Attribute attrval,
                                 PrintRequestAttributeSet attrset) {
    PrintService ps = getPrintService();
    return
        (attrval != null && ps != null &&
         ps.isAttributeValueSupported(attrval,
                                      DocFlavor.SERVICE_FORMATTED.PAGEABLE,
                                      attrset));
}
 
Example #14
Source File: PrintUtilities.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @param service The {@link PrintService} to use.
 * @param set     The {@link PrintRequestAttributeSet} to use.
 * @param units   The units to return.
 * @return The width and height of the page.
 */
public static double[] getPageSize(PrintService service, PrintRequestAttributeSet set, LengthUnits units) {
    PageOrientation orientation = getPageOrientation(service, set);
    double[]        size        = getPaperSize(service, set, units);

    if (orientation == PageOrientation.LANDSCAPE || orientation == PageOrientation.REVERSE_LANDSCAPE) {
        double tmp = size[0];

        size[0] = size[1];
        size[1] = tmp;
    }

    return size;
}
 
Example #15
Source File: PrintServiceLookupProvider.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized PrintService[] getPrintServices() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkPrintJobAccess();
    }
    if (printServices == null) {
        refreshServices();
    }
    return printServices;
}
 
Example #16
Source File: PrintPanel.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void createPageRangeFields(PrintRequestAttributeSet set) {
    PrintService service = getService();
    if (service.isAttributeCategorySupported(PageRanges.class)) {
        ButtonGroup group      = new ButtonGroup();
        int         start      = 1;
        int         end        = 9999;
        PageRanges  pageRanges = (PageRanges) set.get(PageRanges.class);
        if (pageRanges != null) {
            int[][] ranges = pageRanges.getMembers();
            if (ranges.length > 0 && ranges[0].length > 1) {
                start = ranges[0][0];
                end = ranges[0][1];
            } else {
                pageRanges = null;
            }
        }
        JLabel label = new JLabel(I18n.Text("Print Range"), SwingConstants.CENTER);
        add(label, new PrecisionLayoutData().setEndHorizontalAlignment());
        JPanel wrapper = new JPanel(new PrecisionLayout().setMargins(0).setColumns(5));
        mPageRangeAll = new JRadioButton(I18n.Text("All"), pageRanges == null);
        wrapper.add(mPageRangeAll);
        mPageRangeSome = new JRadioButton(I18n.Text("Pages"), pageRanges != null);
        wrapper.add(mPageRangeSome);
        mPageRangeStart = createPageRangeField(start, wrapper);
        wrapper.add(new JLabel(I18n.Text("to"), SwingConstants.CENTER));
        mPageRangeEnd = createPageRangeField(end, wrapper);
        add(wrapper);
        group.add(mPageRangeAll);
        group.add(mPageRangeSome);
        adjustPageRanges();
        mPageRangeAll.addActionListener(this);
        mPageRangeSome.addActionListener(this);
    } else {
        mPageRangeAll = null;
        mPageRangeSome = null;
        mPageRangeStart = null;
        mPageRangeEnd = null;
    }
}
 
Example #17
Source File: GetPrintServices.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static PrintService lookupByName(String name) {
  AttributeSet attributes = new HashAttributeSet();
  attributes.add(new PrinterName(name, null));
  for (PrintService service : PrintServiceLookup.lookupPrintServices(null, attributes)) {
    return service;
  }
  return null;
}
 
Example #18
Source File: UnixPrintJob.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
UnixPrintJob(PrintService service) {
    this.service = service;
    mDestination = service.getName();
    if (UnixPrintServiceLookup.isMac()) {
        mDestination = ((IPPPrintService)service).getDest();
    }
    mDestType = UnixPrintJob.DESTPRINTER;
}
 
Example #19
Source File: RasterPrinterJob.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected boolean isSupportedValue(Attribute attrval,
                                 PrintRequestAttributeSet attrset) {
    PrintService ps = getPrintService();
    return
        (attrval != null && ps != null &&
         ps.isAttributeValueSupported(attrval,
                                      DocFlavor.SERVICE_FORMATTED.PAGEABLE,
                                      attrset));
}
 
Example #20
Source File: UnixPrintServiceLookup.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean matchesAttributes(PrintService service,
                                  PrintServiceAttributeSet attributes) {

    Attribute [] attrs =  attributes.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;
}
 
Example #21
Source File: UnixPrintServiceLookup.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    PrintService[] services = getPrintServices();
    synchronized (this) {
        BackgroundLookupListener listener;
        for (int i=0; i<lookupListeners.size(); i++) {
            listener =
                (BackgroundLookupListener)lookupListeners.elementAt(i);
            listener.notifyServices(copyOf(services));
        }
        lookupListeners = null;
    }
}
 
Example #22
Source File: GetPrintServices.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  for (PrintService service : PrintServiceLookup.lookupPrintServices(null, null)) {
    String serviceName = service.getName();
    PrintService serviceByName = lookupByName(serviceName);
    if (!service.equals(serviceByName)) {
      throw new RuntimeException("NOK " + serviceName
                                 + " expected: " + service.getClass().getName()
                                 + " got: " + serviceByName.getClass().getName());
    }
  }
  System.out.println("Test PASSED");
}
 
Example #23
Source File: UnixPrintServiceLookup.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private PrintService getNamedPrinterNameSysV(String name) {

        String command = "/usr/bin/lpstat -v " + name;
        String []result = execCmd(command);

        if (result == null || result[0].indexOf("unknown printer") > 0) {
            return null;
        } else {
            return new UnixPrintService(name);
        }
    }
 
Example #24
Source File: RasterPrinterJob.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected void updatePageAttributes(PrintService service,
                                    PageFormat page) {
    if (this.attributes == null) {
        this.attributes = new HashPrintRequestAttributeSet();
    }

    updateAttributesWithPageFormat(service, page, this.attributes);
}
 
Example #25
Source File: TestRaceCond.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void trial() {
    PrintService pserv1 = PrintServiceLookup.lookupDefaultPrintService();
    PrintService[] pservs = PrintServiceLookup.lookupPrintServices(null, null);
    PrintService pserv2 = PrintServiceLookup.lookupDefaultPrintService();

    if ((pserv1 == null) || (pserv2==null)) {
        return;
    }

    if (pserv1.hashCode() != pserv2.hashCode()) {
        throw new RuntimeException("Different hashCodes for equal print "
                        + "services: " + pserv1.hashCode() + " "
                        + pserv2.hashCode());
    }
}
 
Example #26
Source File: Win32PrintServiceLookup.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public synchronized PrintService getDefaultPrintService() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
      security.checkPrintJobAccess();
    }


    // Windows does not have notification for a change in default
    // so we always get the latest.
    defaultPrinter = getDefaultPrinterName();
    if (defaultPrinter == null) {
        return null;
    }

    if ((defaultPrintService != null) &&
        defaultPrintService.getName().equals(defaultPrinter)) {

        return defaultPrintService;
    }

     // Not the same as default so proceed to get new PrintService.

    // clear defaultPrintService
    defaultPrintService = null;

    if (printServices != null) {
        for (int j=0; j<printServices.length; j++) {
            if (defaultPrinter.equals(printServices[j].getName())) {
                defaultPrintService = printServices[j];
                break;
            }
        }
    }

    if (defaultPrintService == null) {
        defaultPrintService = new Win32PrintService(defaultPrinter);
    }
    return defaultPrintService;
}
 
Example #27
Source File: RemotePrinterStatusRefresh.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static List<ServiceItem> collectPrinterList() {
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    List<ServiceItem> list = new ArrayList<>(printServices.length);
    for (PrintService service : printServices) {
        list.add(new ServiceItem(service.getName()));
    }
    return list;
}
 
Example #28
Source File: RasterPrinterJob.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
synchronized PrinterGraphicsConfig getPrinterGraphicsConfig() {
    if (pgConfig != null) {
        return pgConfig;
    }
    String deviceID = "Printer Device";
    PrintService service = getPrintService();
    if (service != null) {
        deviceID = service.toString();
    }
    pgConfig = new PrinterGraphicsConfig(deviceID,
                                         defaultDeviceTransform,
                                         deviceWidth, deviceHeight);
    return pgConfig;
}
 
Example #29
Source File: PrintServiceLookupProvider.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized PrintService getPrintServiceByName(String name) {

        if (name == null || name.equals("")) {
            return null;
        } else {
            /* getPrintServices() is now very fast. */
            PrintService[] printServices = getPrintServices();
            for (int i=0; i<printServices.length; i++) {
                if (printServices[i].getName().equals(name)) {
                    return printServices[i];
                }
            }
            return null;
        }
    }
 
Example #30
Source File: TextTemplatePrintSettingsDialog.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
private List<MediaTray> loadAvailableTrays(PrintService printService){
	mediaTrays = new ArrayList<MediaTray>();
	Object attributes =
		printService.getSupportedAttributeValues(Media.class,
			DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);
	if (attributes != null && attributes.getClass().isArray()) {
		for (Media media : (Media[]) attributes) {
			if (media instanceof MediaTray) {
				mediaTrays.add((MediaTray) media);
			}
		}
	}
	return mediaTrays;
}