/*******************************************************************************
 * Copyright (c) 2007 by CRP Henri TUDOR - SANTEC LUXEMBOURG 
 * check http://www.santec.tudor.lu for more information
 *  
 * Contributor(s):
 * Johannes Hermen  johannes.hermen(at)tudor.lu                            
 * Martin Heinemann martin.heinemann(at)tudor.lu  
 *  
 * This library is free software; you can redistribute it and/or modify it  
 * under the terms of the GNU Lesser General Public License (version 2.1)
 * as published by the Free Software Foundation.
 * 
 * This software 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        
 * Lesser General Public License for more details.                          
 * 
 * You should have received a copy of the GNU Lesser General Public         
 * License along with this library; if not, write to the Free Software      
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 *******************************************************************************/
package lu.tudor.santec.bizcal.print;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.OrientationRequested;
import javax.swing.RepaintManager;

/** A simple utility class that lets you very simply print
 *  an arbitrary component. Just pass the component to the
 *  PrintUtilities.printComponent. The component you want to
 *  print doesn't need a print method and doesn't have to
 *  implement any interface or do anything special at all.
 *  <P>
 *  If you are going to be printing many times, it is marginally more 
 *  efficient to first do the following:
 *  <PRE>
 *    PrintUtilities printHelper = new PrintUtilities(theComponent);
 *  </PRE>
 *  then later do printHelper.print(). But this is a very tiny
 *  difference, so in most cases just do the simpler
 *  PrintUtilities.printComponent(componentToBePrinted).
 *
 *  7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
 *  May be freely used or adapted.
 */

public class PrintUtilities implements Printable {
  private Component componentToBePrinted;
private HashPrintRequestAttributeSet aset;

  public static void printComponent(Component c, boolean showPrinterDialog, boolean landScape) {
    new PrintUtilities(c, landScape).print(showPrinterDialog);
  }
  
  public PrintUtilities(Component componentToBePrinted, boolean landScape) {
    this.componentToBePrinted = componentToBePrinted;
    aset = new HashPrintRequestAttributeSet();
    if (landScape) {
    	aset.add(OrientationRequested.LANDSCAPE);
    } else {
    	aset.add(OrientationRequested.PORTRAIT);
    }
  }
  
  public void print(boolean showPrinterDialog) {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    if (showPrinterDialog) {
	    if (printJob.printDialog())
	      try {
	        printJob.print(aset);
	      } catch(PrinterException pe) {
	        System.out.println("Error printing: " + pe);
	      }
    } else {
    	try {
	        printJob.print(aset);
	      } catch(PrinterException pe) {
	        System.out.println("Error printing: " + pe);
	      }
    }
  }

  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
      return(NO_SUCH_PAGE);
    } else {
      Graphics2D g2d = (Graphics2D)g;
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
      double width = pageFormat.getImageableWidth() / componentToBePrinted.getWidth();
      double height = pageFormat.getImageableHeight() / componentToBePrinted.getHeight() ;
      g2d.scale(width, height);
      disableDoubleBuffering(componentToBePrinted);
      componentToBePrinted.paint(g2d);
      enableDoubleBuffering(componentToBePrinted);
      return(PAGE_EXISTS);
    }
  }

  /** The speed and quality of printing suffers dramatically if
   *  any of the containers have double buffering turned on.
   *  So this turns if off globally.
   *  @see enableDoubleBuffering
   */
  public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
  }

  /** Re-enables double buffering globally. */
  
  public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
  }
    
}


@@ -3159,21 +3159,21 @@
 alog) %7B%0A
-%09
     
+%09
 if (prin
@@ -3196,30 +3196,27 @@
 ())%0A
-%09  
     
+%09%09
 try %7B%0A
-%09    
     
+%09%09%09
 prin
@@ -3225,39 +3225,38 @@
 ob.print(aset);%0A
-%09  
     
+%09%09
 %7D catch(PrinterE
@@ -3266,33 +3266,31 @@
 ption pe) %7B%0A
-%09
     
-    
+%09%09%09
 System.out.p
@@ -3318,31 +3318,30 @@
 g: %22 + pe);%0A
-%09  
     
+%09%09
 %7D%0A    %7D else
@@ -3354,25 +3354,22 @@
  %09try %7B%0A
-%09
     
-    
+%09%09
 printJob
@@ -3382,23 +3382,28 @@
 (aset);%0A
-%09
     
-  
+%09%09%0A    %09
 %7D catch(
@@ -3484,18 +3484,16 @@
 );%0A%09    
-  
 %7D%0A    %7D%0A
@@ -4043,16 +4043,19 @@
 ed.paint
+All
 (g2d);%0A 
@@ -4138,24 +4138,27 @@
 %0A    %7D%0A  %7D%0A%0A
+  %0A
   /** The sp
@@ -4533,16 +4533,19 @@
 );%0A  %7D%0A%0A
+  %0A
   /** Re
@@ -4583,19 +4583,16 @@
 lly. */%0A
-  %0A
   public

fix Ticket #851 Printout for Agenda doesn't work anymore