/*******************************************************************************
 * GenRGenS v.1.0, Generation of Random Genomic Sequences                      *
 * Copyright (C) 2001  Alain Denise/Yann Ponty                                 *
 *                                                                             *
 * This program is free software; you can redistribute it and/or               *
 * modify it under the terms of the GNU General Public License                 *
 * as published by the Free Software Foundation; either version 2              *
 * of the License, or (at your option) any later version.                      *
 *                                                                             *
 * This program 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 General Public License for more details.                                *
 *                                                                             *
 * You should have received a copy of the GNU General Public License           *
 * along with this program; if not, write to the Free Software                 *
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
 *******************************************************************************/

package GenRGenS.IU;

/**
 * Title:        <p>
 * Description:  <p>
 * Copyright:    Copyright (c) <p>
 * Company:      <p>
 * @author
 * @version 1.0
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import GenRGenS.utility.*;
import java.io.*;

public class ComptageDialog extends JDialog
{
  JPanel panel1 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  Box box1;
  JPanel jPanel1 = new JPanel();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  FlowLayout flowLayout1 = new FlowLayout();
  JPanel jPanel2 = new JPanel();
  JPanel jPanel3 = new JPanel();
  JPanel jPanel4 = new JPanel();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  IntField jTextField1 = new IntField();
  IntField jTextField2 = new IntField();
  JLabel jLabel3 = new JLabel();
  JPanel jPanel5 = new JPanel();
  JPanel jPanel6 = new JPanel();
  JPanel jPanel7 = new JPanel();
  JLabel jLabel4 = new JLabel();
  JPanel jPanel13 = new JPanel();
  JButton jButton4 = new JButton();
  JButton jButton3 = new JButton();
  FlowLayout flowLayout4 = new FlowLayout();
  JLabel shortFilename = new JLabel();
  JLabel jLabel7 = new JLabel();
  JPanel jPanel14 = new JPanel();
  JButton jButton5 = new JButton();
  JButton jButton6 = new JButton();
  FlowLayout flowLayout5 = new FlowLayout();
  JLabel shortFilename1 = new JLabel();
  JLabel jLabel8 = new JLabel();
  ImageIcon image1;
  ImageIcon image2;
  Frame parent = null;

  // Partie logique :
  private String inFilename = "";
  private String outFilename = "";


  public ComptageDialog(Frame frame, String title, boolean modal)
  {
    super(frame, title, modal);
    parent = frame;
    try
    {
      jbInit();
      pack();
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }
  }

  public ComptageDialog()
  {
    this(null, "", false);
  }

  void jbInit() throws Exception
  {
    image1 = new ImageIcon(GenRGenSDialog.class.getResource("bin/openFile.gif"));
    image2 = new ImageIcon(GenRGenSDialog.class.getResource("bin/clearFile.gif"));
    box1 = Box.createVerticalBox();
    panel1.setLayout(borderLayout1);
    jButton1.setEnabled(false);
    jButton1.setText("Go !");
    jButton1.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.setText("Cancel");
    jButton2.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        jButton2_actionPerformed(e);
      }
    });
    jPanel1.setLayout(flowLayout1);
    flowLayout1.setHgap(50);
    jLabel1.setFont(new java.awt.Font("Dialog", 1, 12));
    jLabel1.setText("Markovian Options :");
    jLabel2.setText("Markovian Order : ");
    jLabel3.setText("Number of Phases : ");
    jTextField1.setMinimumSize(new Dimension(80, 21));
    jTextField1.setPreferredSize(new Dimension(80, 21));
    jTextField1.setText("0");
    jTextField1.setHorizontalAlignment(SwingConstants.CENTER);
    jTextField2.setMinimumSize(new Dimension(80, 21));
    jTextField2.setPreferredSize(new Dimension(80, 21));
    jTextField2.setText("1");
    jTextField2.setHorizontalAlignment(SwingConstants.CENTER);
    jLabel4.setText("File Options :");
    jLabel4.setFont(new java.awt.Font("Dialog", 1, 12));
    jPanel13.setLayout(flowLayout4);
    jButton4.setMaximumSize(new Dimension(24, 24));
    jButton4.setPreferredSize(new Dimension(24, 24));
    jButton4.setIcon(image1);
    jButton4.setMargin(new Insets(1, 14, 3, 14));
    jButton4.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        jButton4_actionPerformed(e);
      }
    });
    jButton3.setMaximumSize(new Dimension(24, 24));
    jButton3.setMinimumSize(new Dimension(24, 24));
    jButton3.setPreferredSize(new Dimension(24, 24));
    jButton3.setIcon(image2);
    jButton3.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        jButton3_actionPerformed(e);
      }
    });
    flowLayout4.setVgap(0);
    shortFilename.setEnabled(false);
    shortFilename.setText("None");
    jLabel7.setToolTipText("");
    jLabel7.setText("Input File : ");
    jPanel14.setLayout(flowLayout5);
    jButton5.setMaximumSize(new Dimension(24, 24));
    jButton5.setPreferredSize(new Dimension(24, 24));
    jButton5.setIcon(image1);
    jButton5.setMargin(new Insets(1, 14, 3, 14));
    jButton5.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        jButton5_actionPerformed(e);
      }
    });
    jButton6.setMaximumSize(new Dimension(24, 24));
    jButton6.setMinimumSize(new Dimension(24, 24));
    jButton6.setPreferredSize(new Dimension(24, 24));
    jButton6.setIcon(image2);
    jButton6.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        jButton6_actionPerformed(e);
      }
    });
    flowLayout5.setVgap(0);
    shortFilename1.setEnabled(false);
    shortFilename1.setText("None");
    jLabel8.setText("Output File : ");
    getContentPane().add(panel1);
    panel1.add(box1, BorderLayout.CENTER);
    box1.add(jPanel5, null);
    jPanel5.add(jLabel4, null);
    box1.add(jPanel6, null);
    jPanel6.add(jPanel13, null);
    jPanel13.add(jLabel7, null);
    jPanel13.add(shortFilename, null);
    jPanel13.add(jButton4, null);
    jPanel13.add(jButton3, null);
    box1.add(jPanel7, null);
    jPanel7.add(jPanel14, null);
    jPanel14.add(jLabel8, null);
    jPanel14.add(shortFilename1, null);
    jPanel14.add(jButton5, null);
    jPanel14.add(jButton6, null);
    box1.add(jPanel2, null);
    jPanel2.add(jLabel1, null);
    box1.add(jPanel3, null);
    jPanel3.add(jLabel2, null);
    jPanel3.add(jTextField1, null);
    box1.add(jPanel4, null);
    jPanel4.add(jLabel3, null);
    jPanel4.add(jTextField2, null);
    panel1.add(jPanel1, BorderLayout.SOUTH);
    jPanel1.add(jButton1, null);
    jPanel1.add(jButton2, null);
  }

  void jButton4_actionPerformed(ActionEvent e)
  {
      FileDialog d = new FileDialog(parent, "Read data from ...", FileDialog.LOAD);
      d.show();
        if ((d.getFile() != null)&&(d.getDirectory() != null))
        {
          File SelectedFile = new File(d.getDirectory() +  d.getFile()) ;
          inFilename = SelectedFile.getAbsolutePath();
          shortFilename.setText(SelectedFile.getName());
          shortFilename.setEnabled(true);
          if (!outFilename.equals(""))
          {
            jButton1.setEnabled(true);
          }
        }
  }

  void jButton3_actionPerformed(ActionEvent e)
  {
    shortFilename.setText("None");
    shortFilename.setEnabled(false);
    inFilename = "";
    jButton1.setEnabled(false);
  }

  void jButton5_actionPerformed(ActionEvent e)
  {
      FileDialog d = new FileDialog(parent, "Write results to ...", FileDialog.SAVE);
      d.show();
        if ((d.getFile() != null)&&(d.getDirectory() != null))
        {
          File SelectedFile = new File(d.getDirectory() +  d.getFile()) ;
          outFilename = SelectedFile.getAbsolutePath();
          shortFilename1.setText(SelectedFile.getName());
          shortFilename1.setEnabled(true);
          if (!inFilename.equals(""))
          {
            jButton1.setEnabled(true);
          }
        }
  }

  void jButton6_actionPerformed(ActionEvent e)
  {
    shortFilename1.setText("None");
    shortFilename1.setEnabled(false);
    outFilename = "";
    jButton1.setEnabled(false);
  }

  void jButton2_actionPerformed(ActionEvent e)
  {
    this.hide();
  }

  void jButton1_actionPerformed(ActionEvent e)
  {
   try
   {
    int order = Integer.parseInt(jTextField1.getText());
    int phase = Integer.parseInt(jTextField2.getText());
    if (phase>0)
    {
      Comptage counter = new Comptage(order,phase);
      try
      {
        counter.countFile(inFilename,false);
        FileWriter out = new FileWriter(outFilename);
        out.write(counter.toString());
        out.close();
        JOptionPane.showMessageDialog(null, "Counted File \""+shortFilename.getText()+"\" to File \""+shortFilename1.getText()+"\" !", "Finished Counting",JOptionPane.INFORMATION_MESSAGE);
      }
      catch(Exception e3)
      {
        JOptionPane.showMessageDialog(null, e3.toString(), "Error", JOptionPane.ERROR_MESSAGE);
      }
      hide();
    }
    else
    {
      JOptionPane.showMessageDialog(null, "Field \"Number of Phases\" should contain a > 0 integer value.", "Error", JOptionPane.ERROR_MESSAGE);
    }
    }
    catch(Exception e2)
    {
      JOptionPane.showMessageDialog(null, "Fields should contain integer values !", "Error", JOptionPane.ERROR_MESSAGE);
    }
  }


}
