Package tcg.print

Source Code of tcg.print.JavaLinePrinter

/**
* JavaLinePrinter Class
*
* Printer driver implementation to communicate to printer using native java printing.
*
* Unfortunately, this is a nightmare scenario:
* - Java printing by default support page concept istead on line-by-line concept.
* - It depends very much on the operating system printing setup. In Unix/Linux it is
*   a nightmare! Not to mention how complex it is to setup the printing service!
* - In most Unix/Linux using old lp protocol, every printing job always ends with
*   form feed. Trying to suppress this form feed nearly drives me insane!
* - In Solaris (SPARC/X86) it does not work with lprng package.
* - It is very slow and overkill for the purpose that I have in mind, ie. line printing
*   of text only!
*
* As such this class should never be used in code that would need to be portable
* across different operating system.
*
* @author    Wahyu Yoga Pratama (wyogap@thatcoolguy.com)
*
* @created      Jan 29, 2010
* @version    $$
*
* HISTORY:
* - 2010/01/29  Created.
*
*/


package tcg.print;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;


public class JavaLinePrinter implements ILinePrinter {

  PrintService   printService_ = null;
  String      printerName_ = "";
 
  //default ctor
  public JavaLinePrinter()
  {
    //use default printer as configured in operating system
  }
 
  //preferred ctor
  public JavaLinePrinter(String printerName)
  {
    printerName_ = printerName;
  }
 
  public boolean ping()
  {
    //get the print service if it is not yet initialized
    if (printService_ == null)
    {
      if (printerName_ == null || printerName_.length() == 0)
      {
        printService_ = PrintServiceLookup.lookupDefaultPrintService();
      }
      else
      {
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        for (PrintService service : services)
        { 
           if (!service.getName().equalsIgnoreCase(printerName_))
           {
             continue;
           }
           printService_ = service;
        }
      }
    }
   
    //check if we have valid print service
    if (printService_ == null)
    {
      return false;
    }   
    return true;
  }

  public boolean print(IPrintable printable)
  {
    if (printable == null) return true;
   
    //get the print service if it is not yet initialized
    if (printService_ == null)
    {
      if (printerName_ == null || printerName_.length() == 0)
      {
        //printer name not specified. use operating system default printer
        printService_ = PrintServiceLookup.lookupDefaultPrintService();
       
        //check if we can get print service
        if (printService_ == null)
        {
          throw new RuntimeException("print: can not get default printer.");
        }     
      }
      else
      {
        //use user provided printer name
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        for (PrintService service : services)
        { 
           if (!service.getName().equalsIgnoreCase(printerName_))
           {
             continue;
           }
           printService_ = service;
        }
       
        //check if we can get print service
        if (printService_ == null)
        {
          throw new RuntimeException("print: can not get selected printer " + printerName_);
        }     
      }
    }
   
    //check if the selected printer support the required doc flavor
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    if (!printService_.isDocFlavorSupported(flavor))
    {
      throw new RuntimeException("print: required doc flavor is not supported");
    }
   
    //create print job
    DocPrintJob job = printService_.createPrintJob();

    //create the document to print
    byte[] bytes = printable.getBytes();
    InputStream is = new ByteArrayInputStream(bytes);
    DocAttributeSet docAttribs   = new HashDocAttributeSet();
    javax.print.Doc doc = new javax.print.SimpleDoc(is, flavor, docAttribs);
   
    try
    {
      PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
      job.print(doc, printAttributes);
    }
    catch (PrintException e)
    {
      throw new RuntimeException("print: fail to print the data. Error: " + e.getMessage());
    }
   
    return true;
  }
 
  public boolean _print(IPrintable printable)
  {
    try
    {
      return print(printable);
    }
    catch (RuntimeException re)
    {
      //ignore
      return false;
    }
  } 
}
TOP

Related Classes of tcg.print.JavaLinePrinter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.