/*******************************************************************************
 * This file is part of GECAMed.
 * 
 * GECAMed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License (L-GPL) as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GECAMed 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 (L-GPL)
 * along with GECAMed.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * GECAMed is Copyrighted by the Centre de Recherche Public Henri Tudor (http://www.tudor.lu)
 * (c) CRP Henri Tudor, Luxembourg, 2008
 *******************************************************************************/
package lu.tudor.santec.gecamed.core.gui.utils;
 /* SimpleFilePrinter.java */
 
 import java.awt.Color;
import java.awt.Font;
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 java.io.IOException;
import java.io.RandomAccessFile;

import lu.tudor.santec.gecamed.core.gui.MainFrame;
 
 public class SimpleFilePrinter
 implements Printable
 {
   //---Konstanten--------------------------------------
   private static final int RESMUL = 4;
 
   //---Membervariablen---------------------------------
   private PrinterJob       pjob;
   private PageFormat       pageformat;
   private FilePrintHelper  fph;
   private String           fname;
   private RandomAccessFile in;

private int lastPages;
 
   //---Konstruktoren-----------------------------------
   /**
 * @param fname
 */
public SimpleFilePrinter(String fname)
   {
     this.pjob  = PrinterJob.getPrinterJob();
     this.fname = fname;
     this.lastPages = 0;
   }

/**
* @param fname
*/
public SimpleFilePrinter(String fname, int lastPages)
{
  this.pjob  = PrinterJob.getPrinterJob();
  this.fname = fname;
  this.lastPages = lastPages;
}
    
   //---Öffentliche Methoden----------------------------
   public boolean setupPageFormat()
   {
     PageFormat defaultPF = pjob.defaultPage();
     this.pageformat = pjob.pageDialog(defaultPF);
     pjob.setPrintable(this, this.pageformat);
     return (this.pageformat != defaultPF);
   }
 
   public boolean setupJobOptions()
   {
     return pjob.printDialog();
   }
 
   public void printFile()
   throws PrinterException, IOException
   {
     fph = new FilePrintHelper();
     in = new RandomAccessFile(fname, "r");
     pjob.print();
     in.close();
   }
 
   //---Implementierung von Printable-------------------
   public int print(Graphics g, PageFormat pf, int page)
   throws PrinterException
   {
       if (lastPages > 0) {
	   if (page < (fph.getPages()-lastPages))
	       return PAGE_EXISTS;
       }
       
     int ret = PAGE_EXISTS;
     String line = null;
     try {
       if (fph.knownPage(page)) {
         in.seek(fph.getFileOffset(page));
         line = in.readLine();
       } else {
         long offset = in.getFilePointer();
         line = in.readLine();
         if (line == null) {
           ret = NO_SUCH_PAGE;
         } else {
           fph.createPage(page);
           fph.setFileOffset(page, offset);
         }
       }
       if (ret == PAGE_EXISTS) {
         //Seite ausgeben, Grafikkontext vorbereiten
         Graphics2D g2 = (Graphics2D)g; 
         g2.scale(1.0 / RESMUL, 1.0 / RESMUL);
         int ypos = (int)pf.getImageableY() * RESMUL;
         int xpos = ((int)pf.getImageableX() + 2) * RESMUL;
         int yd = 12 * RESMUL;
         int ymax = ypos + (int)pf.getImageableHeight() * RESMUL - yd;
         //Seitentitel ausgeben
         ypos += yd; 
         g2.setColor(Color.black);
         g2.setFont(new Font("Monospaced", Font.BOLD, 8 * RESMUL));
         g.drawString(fname + ", Seite " + (page + 1), xpos, ypos);
         g.drawLine(
           xpos,
           ypos + 6 * RESMUL,
           xpos + (int)pf.getImageableWidth() * RESMUL,
           ypos + 6 * RESMUL
         );
         ypos += 2 * yd;
         //Zeilen ausgeben
         g2.setColor(new Color(0, 0, 127)); 
         g2.setFont(new Font("Monospaced", Font.PLAIN, 10 * RESMUL));
         while (line != null) {
           g.drawString(line, xpos, ypos);
           ypos += yd;
           if (ypos >= ymax) {
             break;
           }
           line = in.readLine();
         }
       }
     } catch (IOException e) {
       throw new PrinterException(e.toString());
     }
     return ret;
   }
 
   //---Main--------------------------------------------
   public static void main(String[] args)
   {
     SimpleFilePrinter sfp = new SimpleFilePrinter(args[0]);
     if (sfp.setupPageFormat()) {
       if (sfp.setupJobOptions()) {
         try {
           sfp.printFile();
         } catch (Exception e) {
           System.err.println(e.toString());
           MainFrame.exit(1);
         }
       }
     }
     MainFrame.exit(0);
   }
 }
