/*
  i-net software provides programming examples for illustration only, without warranty
  either expressed or implied, including, but not limited to, the implied warranties
  of merchantability and/or fitness for a particular purpose. This programming example
  assumes that you are familiar with the programming language being demonstrated and
  the tools used to create and debug procedures. i-net software support professionals
  can help explain the functionality of a particular procedure, but they will not modify
  these examples to provide added functionality or construct procedures to meet your
  specific needs.
  © i-net software 1998-2010
*/

package samples.printing;

import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.PrinterName;
import javax.print.attribute.standard.SheetCollate;

import java.awt.print.PrinterJob;
import com.inet.viewer.PrinterJobProgress;
import com.inet.viewer.Progress;

import com.inet.viewer.ReportView;
import com.inet.viewer.SwingReportViewer;
import com.inet.viewer.URLRenderData;

/**
 * This example demonstrates how to set a printer job from the outside to
 * print a report to a custom printer with the i-net Clear Reports viewer bean.
 * We create a print job object, examine the orientation of the report and then 
 * set the page and job attributes to the print job object.
 * The report is then printed using the bean.printView(PrintJob pj) call.
 */
public class CustomPrinter extends java.awt.Frame {
    // 7.0
	private URLRenderData renderConnection;   // This is our main render data connection - the source of our raw report data coming from our report server
	private SwingReportViewer viewer;         // Our top-level viewer object
    private ReportView currentReportView;     // Our report view, responsible for a single report
        
    private PrinterJob pj;

    // executes the report and provides the report data.
    private static com.inet.report.Listener reportListener = new com.inet.report.Listener(true);

	/**
	 * An array of printers we have.  We have obtained this list by
	 * clicking on the print button of the Java viewer and then choose the
	 * "select printer dialog".  -- You can also call the method
	 * bean.printView() without any parameters to see the printer dialog.
	 */
    private String[] printer = { 
		// Syntax: \\hostname\PrinterDescription 
		//"\\\\serv2\\Lexmark Optra S 1250",
		// Dummy printer attached to local machine. 
		//"HP LaserJet 5/5M PostScript",
    	"Lexmark Optra E312 (MS)"
	};
    

    /**
     * Print the report on a custom printer
     */
    public CustomPrinter() {
    	// STEP 1:
        try {
              // to initialize we first create a top level ReportViewer:
      	      viewer = new SwingReportViewer();
      	  
	          // then initialize the render data connection.
	   		  renderConnection = new URLRenderData( reportListener.getUrlString()+ "/?report=file:C:/java/sample.rpt" );
	   		  //renderConnection.setReportURL( reportListener.getUrlString()+ "/?report=file:c:/java/sample.rpt" );
	   		  
	   		  // addNewReportView causes a new report view to be created using the given connection as its data source, and then
	   	      // for this newly created report to be added to the viewer.
	   		  currentReportView = viewer.addNewReportView( renderConnection );
        } 
		catch (Exception exc) {
	            exc.printStackTrace();
	    }
        
		// STEP 2: 
        // Construct the print request specification.
        // The print data is a Printable object.
        // the request additonally specifies a job name, 2 copies, and
        // portrait orientation of the media.
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
        
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(new JobName("My job", null));
        aset.add( MediaSizeName.ISO_A4 );
        // aset.add(OrientationRequested.PORTRAIT);   // AUTO from report
		aset.add(new Copies(2)); 
		aset.add( SheetCollate.COLLATED );
         
		// STEP 3: now print to all printers
        for (int i = 0; i < printer.length; i++ ) { 
			// set the printer
            // Default-Printer if it is not specified
            PrintServiceAttributeSet pset = new HashPrintServiceAttributeSet();
            pset.add( new PrinterName( printer[i], null ) );
               
            // locate a print service that can handle the request 
            PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, aset);
                           services = PrintServiceLookup.lookupPrintServices(flavor, pset);  

    		if (services.length > 0) {
                int whichPrinter = -1;
                for (int p = 0; p < services.length; p++) {
                    if (services[p].getName().equals( printer[ i ] )) {
                        whichPrinter = p;
                        break;
                    }
                }
                if( whichPrinter == -1 ) {
                    continue;
                }
                System.out.println("\tselected printer: " + services[whichPrinter].getName());
                try {
                    PrinterJob pj = PrinterJob.getPrinterJob();
                    pj.setPrintService( services[whichPrinter] );
     
                    //currentReportView.print( 1, -1, pj );  // all pages -> printer default
                    
                    Progress progress = new PrinterJobProgress(null, pj, aset, renderConnection);
                    progress.startProgress();
                    progress.waitUntilFinished();
                
    				// Do not explicitly call System.exit() when print returns.
    				// Printing can be asynchronous so may be executing in a
    				// separate thread.
    				// If you want to explicitly exit the VM, use a print job 
    				// listener to be notified when it is safe to do so.
                } catch (Exception e) { e.printStackTrace();} // handle exception
                System.out.println(" printer job finished.");
    		}
        }
    }
    
    /**
     * Main method of the sample.
     * @param args no parameters used
     */
    public static void main(String [] args ) {
       new CustomPrinter(); 
    }
}
