/*  J2PrinterPDFTTest
    (C) Copyright 2007
    Wildcrest Associates
    http://www.wildcrest.com
This source code may be freely used, modified, incorporated, and
distributed without restriction as part of any software that uses
J2PrinterWorks by Wildcrest Associates.
*/

import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import com.wildcrest.j2printerworks.*;


class J2PrinterPDFTest {
    static J2TextPrinter textPrinter;
    static J2Printer printer;
    //static J2Printer14 printer;
    static JFrame frame;
    static JTextPane pane;

    static public void main(String args[]) {
       JOptionPane.showMessageDialog(null,
           "To run this program, you need to:\n1) download the iText jar from http://www.lowagie.com/iText\n"
		+ "2) rename to itext.jar and copy into same folder as J2PrinterPDFTest.jar\n"
		+ "3) have Adobe Acrobat Reader installed on your (Windows) system",
           "J2PrinterPDFTest",
           JOptionPane.INFORMATION_MESSAGE);

        printer = new J2Printer();
        //printer = new J2Printer14();
        printer.setLeftMargin(.25); printer.setRightMargin(.25);
        printer.setTopMargin(.25); printer.setBottomMargin(.25);
        printer.setPrintPreviewScale(1.0);

        pane = new JTextPane();
        textPrinter = new J2TextPrinter(pane);
        readFile("test.html");
        textPrinter.setCloningUsed(false); // else HTML table border is lost (Java bug)
        //test: textPrinter.setOrientation(textPrinter.LANDSCAPE);
        printer.addPageable(textPrinter);
        //test: textPrinter.setMaximumPages(2);
        //test: textPrinter.setScale(.75);
        printer.setHeaderStyle(J2Printer.LINE);

        frame = new JFrame("J2PrinterPDFTest");
        printer.setParentFrame(frame);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(new JScrollPane(pane), "Center");

        JPanel buttonPanel = new JPanel();
        JButton readFile = new JButton("Read .HTML file");
        JButton printpreview = new JButton("Print Preview");
        JButton printToPDF = new JButton("Print to PDF File");
        JButton displayPDFFile = new JButton("Display PDF File");
        JButton printPDFFile = new JButton("Print PDF File");
        buttonPanel.add(readFile); buttonPanel.add(printpreview);
        buttonPanel.add(printToPDF);
        buttonPanel.add(displayPDFFile);
        buttonPanel.add(printPDFFile);
        frame.getContentPane().add(buttonPanel, "South");

        frame.setBounds(25,25,650,700);
        frame.setVisible(true);

        readFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { openHTMLFile(); }});
        printpreview.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { printer.showPrintPreviewDialog(frame); }});
        printToPDF.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               String fileName = getFileName("Write", "*.pdf", "Specify PDF output file", "out.pdf");
               printer.printToPDF(fileName); }});
        displayPDFFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               String fileName = getFileName("Read", "*.pdf", "Open PDF file to display", "out.pdf");
               printer.displayPDFFile(fileName); }});
        printPDFFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               String fileName = getFileName("Read", "*.pdf", "Open PDF file to print", "out.pdf");
               // prints to default printer
               // can print to specified printer using J2Printer14, getPrinterNames(), and setPrinter()
               // eg: printer.setPrinter("\\\\INSPIRON\\hp officejet g series");
               printer.printPDFFile(fileName); }});
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) { System.exit(0); }});
    }

    static String rememberDir = ".";

    static private String getFileName(String readOrWrite, String ext, String prompt, String defaultName) {
       FileDialog openFileDialog = new FileDialog(frame);
       if (readOrWrite.equalsIgnoreCase("Read")) openFileDialog.setMode(FileDialog.LOAD);
       else openFileDialog.setMode(FileDialog.SAVE);
       openFileDialog.setDirectory(rememberDir);
       openFileDialog.setFile(ext);
       openFileDialog.setTitle(prompt);
       openFileDialog.setVisible(true);

       String fDir = openFileDialog.getDirectory();
       String fName = openFileDialog.getFile();
       if (fName == null || fDir == null) {
          rememberDir = "./";
          return defaultName;
       }
       rememberDir = fDir;
       return fDir + fName;
    }

    static private void openHTMLFile() {
       String fileName = getFileName("Read", "*.html; *.htm", "Open HTML file", "file.html");
       readFile(fileName);
       frame.repaint();
    }

    static private void readFile(String fileName) {
       pane.setContentType("text/html");
       try {
          java.net.URL url = new File(fileName).toURL();
          pane.setPage(url);
       } catch (Exception e) { System.out.println("Exception reading file: " + e);
          e.printStackTrace();
       }
    }

}
