/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.io;

import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.JOptionPane;

/** Class providing printing support for the entire application
 * 
 * @author Jeffrey Armstrong
 *
 */
public class PrintSupport {
	
	/** Static method that handles the messy Java printing calls for all
	 * dialogs.  A simple Printable object must be passed in.  The printer
	 * always defaults to landscape, but this can be changed in the printer
	 * dialog.
	 * 
	 * @param data the object to print
	 */
	public static void RequestPrint(Printable data) {

    	//String filename = ...;
    	PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    	pras.add(javax.print.attribute.standard.OrientationRequested.LANDSCAPE);
    	
    	DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    	//DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
    	PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
    	PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    	PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
    	
    	if (service != null) {
    	    DocPrintJob job = service.createPrintJob();
    	    DocAttributeSet das = new HashDocAttributeSet();
    	    Doc doc = new SimpleDoc(data, flavor, das);
    	    try {
    	    	job.print(doc, pras);
    	    } catch(PrintException pe) {
    	    	JOptionPane.showMessageDialog(null,
        			    "Printing Failed",
        			    "Error",
        			    JOptionPane.ERROR_MESSAGE);
    	    }
    	}
	}
	
}
