/*
 * CeltxToOdt.java
 *
 * Created on Jan 1, 2009, 10:03:12 PM
 */

package com.langintro.celtx_to_odt;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author J David Eisenberg
 */
public class CeltxToOdt extends javax.swing.JFrame {

	File inputFile = null;
	File outputFile = null;
	boolean showCast = false;
	String outputType;

    /** Creates new form CeltxToOdt */
    public CeltxToOdt() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        inputTextField = new javax.swing.JTextField();
        inputBrowseButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        outputTextField = new javax.swing.JTextField();
        outputBrowseButton = new javax.swing.JButton();
        convertButton = new javax.swing.JButton();
        showCastCheckbox = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        outputTypeComboBox = new javax.swing.JComboBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        quitItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Celtx to ODT Converter");

        jLabel1.setText("Input:");

        inputTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inputTextFieldActionPerformed(evt);
            }
        });

        inputBrowseButton.setText("Browse...");
        inputBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inputBrowseButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Output:");

        outputTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outputTextFieldActionPerformed(evt);
            }
        });

        outputBrowseButton.setText("Browse...");
        outputBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outputBrowseButtonActionPerformed(evt);
            }
        });

        convertButton.setText("Convert");
        convertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                convertButtonActionPerformed(evt);
            }
        });

        showCastCheckbox.setText("Show cast of characters");

        jLabel3.setText("Output Type:");

        outputTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Script", "Comic" }));
        outputTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outputTypeComboBoxActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        quitItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        quitItem.setText("Quit");
        quitItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitItemActionPerformed(evt);
            }
        });
        jMenu1.add(quitItem);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                                .addComponent(showCastCheckbox))
                            .addComponent(outputTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(outputTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                            .addComponent(inputTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE))
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputBrowseButton)
                            .addComponent(outputBrowseButton))
                        .addGap(28, 28, 28))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(convertButton)
                        .addGap(148, 148, 148))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputBrowseButton)
                    .addComponent(jLabel1)
                    .addComponent(inputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(outputBrowseButton)
                    .addComponent(jLabel2))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(showCastCheckbox)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(outputTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addComponent(convertButton)
                .addGap(52, 52, 52))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void inputTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputTextFieldActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_inputTextFieldActionPerformed

	private void outputTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outputTextFieldActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_outputTextFieldActionPerformed

	private void outputTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outputTypeComboBoxActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_outputTypeComboBoxActionPerformed

	private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertButtonActionPerformed
		// TODO add your handling code here:
		/* collect information from form */
		String inputFileName = inputTextField.getText();
		String outputFileName = outputTextField.getText();
		showCast = showCastCheckbox.isSelected();
		outputType = (String) outputTypeComboBox.getSelectedItem();

		if (!inputFileName.isEmpty() && !outputFileName.isEmpty())
		{
			CeltxConverter converter = new CeltxConverter(this);

			converter.run(inputFileName, outputFileName, outputType, showCast);
			javax.swing.JOptionPane.showMessageDialog(this, "Finished.");

		}
	}//GEN-LAST:event_convertButtonActionPerformed

	private void inputBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputBrowseButtonActionPerformed
		// TODO add your handling code here:
		int result;
		Pattern p;
		Matcher m;
		String inputName;
		String outputName;

		FileFilter filter = new FileNameExtensionFilter("Celtx file", "celtx");
		JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
		chooser.addChoosableFileFilter(filter);
		result = chooser.showOpenDialog(this);
		if (result == JFileChooser.APPROVE_OPTION)
		{
			inputFile = chooser.getSelectedFile();
			inputName = inputFile.getAbsolutePath();
			inputTextField.setText(inputFile.getAbsolutePath());
			p = Pattern.compile("celtx$", Pattern.CASE_INSENSITIVE);
			m = p.matcher(inputName);
			if (m.find())
			{
				outputName = m.replaceFirst("odt");
				outputTextField.setText(outputName);
			}
		}
	}//GEN-LAST:event_inputBrowseButtonActionPerformed

	private void quitItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitItemActionPerformed
		// TODO add your handling code here:
		System.exit(0);
}//GEN-LAST:event_quitItemActionPerformed

	private void outputBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outputBrowseButtonActionPerformed
		// TODO add your handling code here:
		int result;
		FileFilter filter = new FileNameExtensionFilter("OpenDoc file", "odt");
		JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
		chooser.addChoosableFileFilter(filter);
		result = chooser.showSaveDialog(this);
		if (result == JFileChooser.APPROVE_OPTION)
		{
			outputFile = chooser.getSelectedFile();
			outputTextField.setText(outputFile.getAbsolutePath());
		}

	}//GEN-LAST:event_outputBrowseButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CeltxToOdt().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton convertButton;
    private javax.swing.JButton inputBrowseButton;
    private javax.swing.JTextField inputTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JButton outputBrowseButton;
    private javax.swing.JTextField outputTextField;
    private javax.swing.JComboBox outputTypeComboBox;
    private javax.swing.JMenuItem quitItem;
    private javax.swing.JCheckBox showCastCheckbox;
    // End of variables declaration//GEN-END:variables

}
