/*  J2Printer14PS Test Application
    (C) Copyright 2009
    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.*;
import javax.swing.text.html.*;
import javax.swing.text.Document;

class J2Printer14PSTest {
    static J2Printer14 printer;
    static JFrame frame;
    static JTextPane pane;
    static J2TextPrinter textPrinter;

    static public void main(String args[]) {
        if (System.getProperty("os.name").startsWith("Mac")) {
          JOptionPane.showMessageDialog(frame, "The J2PrinterWorks print-to-Postscript feature\n"
            + "does not currently work under Mac OS X\n"
            + "due to a bug in the current JDK.\n\n"
            + "Throws ArrayIndexOutOfBoundsException for\n"
            + "most documents containing formatted text.\n\n"
            + "Use printToPDF method instead.",
             "Not supported under Mac OS X", JOptionPane.WARNING_MESSAGE);
        }
        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)

        printer.addPageable(textPrinter);

        frame = new JFrame("J2Printer14 printToPS test");
        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 printToPS = new JButton("Print to PS");
        buttonPanel.add(readFile); buttonPanel.add(printpreview);
        buttonPanel.add(printToPS);
        frame.getContentPane().add(buttonPanel, "South");

        frame.setBounds(25,25,550,700);
        frame.setVisible(true);

        readFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { openFile(); }});
        printpreview.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { printer.showPrintPreviewDialog(frame); }});
        printToPS.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { printer.printToPS("out.ps"); }});

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) { System.exit(0); }});
    }

    static String rememberDir = ".";

    static private void openFile() {
       FileDialog openFileDialog = new FileDialog(frame);
       openFileDialog.setDirectory(rememberDir);
       openFileDialog.setFile("*.html; *.htm");
       openFileDialog.setTitle("Open HTML file");
       openFileDialog.setVisible(true);

       String fDir = openFileDialog.getDirectory();
       String fName = openFileDialog.getFile();
       if (fName == null || fDir == null) return;
       rememberDir = fDir;

       String fileName = fDir + fName;
       readFile(fileName);
       frame.repaint();
    }

    static private void readFile(String fileName) {
       // will make JTextPane.setPage load synchronously:
      pane.setEditorKit(new HTMLEditorKit() {
         public Document createDefaultDocument() {
            Document doc = super.createDefaultDocument();
            ( (HTMLDocument) doc).setAsynchronousLoadPriority(-1);
            return doc;
         } });

      pane.setContentType("text/html");
       try {
          java.net.URL url = new File(fileName).toURL(); // this one does
          pane.setPage(url);
       } catch (Exception e) { System.out.println("Exception reading file: " + e);
          e.printStackTrace();
       }
       textPrinter.setPane(pane); // notify textPrinter of change to pane
    }

}

