/*******************************************************************************
 * 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.*;
import java.io.*;
import javax.swing.event.*;
import GenRGenS.utility.holder.*;

public class GenRGenSDialog extends JDialog
{
  JPanel panel1 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel jPanel1 = new JPanel();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  FlowLayout flowLayout1 = new FlowLayout();
  Box box1;
  JPanel jPanel2 = new JPanel();
  JPanel jPanel3 = new JPanel();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  IntField jTextField1 = new IntField();
  IntField jTextField2 = new IntField();

  // Partie logique :
  public static final int DLG_OK = 0;
  public static final int DLG_CANCEL = 1;
  private int returnValue = 1;
  private String outFilename = "";
  private Frame parent;
  JButton[] button2OptionIndex;
  String[] helpStrings;
  Component[] component2Option;
  Option[] optionsBackup;

  JPanel jPanel6 = new JPanel();
  JPanel jPanel10 = new JPanel();
  JPanel jPanel11 = new JPanel();
  JLabel GeneralTitle = new JLabel();
  JCheckBox OutputBuffer = new JCheckBox();
  JPanel jPanel12 = new JPanel();
  FlowLayout flowLayout2 = new FlowLayout();
  JLabel OutputTitle = new JLabel();
  JPanel jPanel13 = new JPanel();
  JLabel jLabel6 = new JLabel();
  JLabel shortFilename = new JLabel();
  FlowLayout flowLayout4 = new FlowLayout();
  FlowLayout flowLayout5 = new FlowLayout();
  JButton jButton3 = new JButton();
  JButton jButton4 = new JButton();
  ImageIcon image1;
  ImageIcon image2;
  JCheckBox jCheckBox3 = new JCheckBox();
  JPanel jPanel9 = new JPanel();
  BorderLayout borderLayout2 = new BorderLayout();
  JPanel jPanel4 = new JPanel();
  CardLayout cardLayout1 = new CardLayout();
  JPanel jPanel5 = new JPanel();
  JPanel jPanel7 = new JPanel();
  BorderLayout borderLayout3 = new BorderLayout();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextArea jTextArea1 = new JTextArea();
  JLabel jLabel3 = new JLabel();
  JButton jButton5 = new JButton();
  JLabel SpecificOptionsTitle = new JLabel();

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

  public GenRGenSDialog()
  {
    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.setText("OK");
    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.setText("Sequences Size :");
    jLabel2.setText("How Many Sequences : ");
    jTextField1.setPreferredSize(new Dimension(80, 21));
    jTextField1.setText("10");
    jTextField1.addFocusListener(new java.awt.event.FocusAdapter()
    {

      public void focusLost(FocusEvent e)
      {
        jTextField1_focusLost(e);
      }
    });
    jTextField2.setPreferredSize(new Dimension(80, 21));
    jTextField2.setText("1");
    jTextField2.addFocusListener(new java.awt.event.FocusAdapter()
    {

      public void focusLost(FocusEvent e)
      {
        jTextField2_focusLost(e);
      }
    });
    GeneralTitle.setFont(new java.awt.Font("Dialog", 1, 12));
    GeneralTitle.setHorizontalAlignment(SwingConstants.CENTER);
    GeneralTitle.setText("General Options :");
    OutputBuffer.setHorizontalTextPosition(SwingConstants.LEADING);
    OutputBuffer.setSelected(true);
    OutputBuffer.setText("Output to TextBuffer");
    jPanel10.setLayout(flowLayout2);
    flowLayout2.setHgap(0);
    flowLayout2.setVgap(0);
    OutputTitle.setFont(new java.awt.Font("Dialog", 1, 12));
    OutputTitle.setText("Output Options :");
    jLabel6.setEnabled(false);
    jLabel6.setText("Output File : ");
    shortFilename.setEnabled(false);
    shortFilename.setText("None");
    jPanel13.setLayout(flowLayout4);
    flowLayout4.setVgap(0);
    jPanel3.setLayout(flowLayout5);
    flowLayout5.setHgap(0);
    flowLayout5.setVgap(0);
    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);
      }
    });
    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);
      }
    });
    this.setTitle("Generation Configuration");
    panel1.setMinimumSize(new Dimension(320, 386));
    panel1.setPreferredSize(new Dimension(320, 386));
    jCheckBox3.setHorizontalTextPosition(SwingConstants.LEADING);
    jCheckBox3.setText("Verbose : ");
    jPanel11.setLayout(borderLayout2);
    jPanel4.setLayout(cardLayout1);
    jPanel5.setLayout(borderLayout3);
    jLabel3.setFont(new java.awt.Font("DialogInput", 1, 12));
    jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
    jLabel3.setText("Help :");
    jTextArea1.setLineWrap(true);
    jTextArea1.setWrapStyleWord(true);
    jTextArea1.setBackground(SystemColor.text);
    jButton5.setText("Back");
    jButton5.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        jButton5_actionPerformed(e);
      }
    });
    SpecificOptionsTitle.setText("Options :");
    SpecificOptionsTitle.setFont(new java.awt.Font("Dialog", 1, 12));
    jPanel13.setPreferredSize(new Dimension(171, 35));
    this.getContentPane().add(jPanel4, BorderLayout.CENTER);
    jPanel4.add(panel1,"options");
    jPanel1.add(jButton1, null);
    jPanel1.add(jButton2, null);
    panel1.add(jPanel1, BorderLayout.SOUTH);
    panel1.add(box1, BorderLayout.NORTH);
    //panel1.add(jPanel6, BorderLayout.CENTER);
    jPanel4.add(jPanel5, "help");
    jPanel5.add(jPanel7, BorderLayout.SOUTH);
    jPanel7.add(jButton5, null);
    jPanel5.add(jScrollPane1, BorderLayout.CENTER);
    jPanel5.add(jLabel3, BorderLayout.NORTH);
    jScrollPane1.getViewport().add(jTextArea1, null);
    this.getContentPane().add(jPanel4, BorderLayout.NORTH);
    box1.add(jPanel11, null);
    box1.add(jPanel2, null);
    box1.add(jPanel3, null);
    box1.add(jPanel9, null);
    box1.add(jPanel12, null);
    box1.add(jPanel10, null);
    box1.add(jPanel13, null);
    box1.add(jPanel6, null);
    jPanel6.add(SpecificOptionsTitle, null);
    jPanel12.add(OutputTitle, null);
    jPanel11.add(GeneralTitle, BorderLayout.NORTH);
    jPanel2.add(jLabel1, null);
    jPanel2.add(jTextField1, null);
    jPanel3.add(jLabel2, null);
    jPanel3.add(jTextField2, null);
    jPanel9.add(jCheckBox3, null);
    jPanel13.add(jLabel6, null);
    jPanel13.add(shortFilename, null);
    jPanel13.add(jButton4, null);
    jPanel13.add(jButton3, null);
    jPanel10.add(OutputBuffer, null);
  }

  void jTextField1_focusLost(FocusEvent e)
  {
      try
      {
        int i = Integer.parseInt(jTextField1.getText());
        if (i>0)
        {
          GenRGenS.setSize(i);
        }
        else
        {
          jTextField1.setText(""+GenRGenS.getSize());
          JOptionPane.showMessageDialog(null, "Field \"Size\" should contain a > 0 integer value.", "Error", JOptionPane.ERROR_MESSAGE);
        }
      }
      catch(Exception e2)
      {
        jTextField1.setText(""+GenRGenS.getSize());
        JOptionPane.showMessageDialog(null, "Field \"Size\" should contain a > 0 integer value.", "Error", JOptionPane.ERROR_MESSAGE);
      }
  }

  void jTextField2_focusLost(FocusEvent e)
  {
      try
      {
        int i = Integer.parseInt(jTextField2.getText());
        if (i>0)
        {
          GenRGenS.setHowMany(i);
        }
        else
        {
          jTextField2.setText(""+GenRGenS.getHowMany());
          JOptionPane.showMessageDialog(null, "Field \"HowMany\" should contain a > 0 integer value.", "Error", JOptionPane.ERROR_MESSAGE);
        }
      }
      catch(Exception e2)
      {
        jTextField2.setText(""+GenRGenS.getHowMany());
        JOptionPane.showMessageDialog(null, "Field \"HowMany\" should contain a > 0 integer value.", "Error", JOptionPane.ERROR_MESSAGE);
      }
  }

  public void b_actionPerformed(ActionEvent e)
  {
    for(int i = 0; i<button2OptionIndex.length; i++)
    {
      JButton b = button2OptionIndex[i];
      if ((Object)b ==e.getSource())
      {
        jTextArea1.setText(helpStrings[i]);
        cardLayout1.show(jPanel4,"help");
      }
    }
  }


  public void setDescriptionFileName(String filename)
  {
      try
      {
        box1.removeAll();
        box1.add(jPanel11, null);
        box1.add(jPanel2, null);
        box1.add(jPanel3, null);
        box1.add(jPanel9, null);
        box1.add(jPanel12, null);
        box1.add(jPanel10, null);
        box1.add(jPanel13, null);
        box1.add(jPanel6, null);

        String type = Linker.getDescriptionFileHeader(new File(filename));
        GeneratorConfigFile f = Linker.getGeneratorConfigFileFromType(type);
        SpecificOptionsTitle.setText(f.getLitteralName()+" Options :");
        Option[] opts = f.getOptions();
        optionsBackup = opts;
        button2OptionIndex = new JButton[opts.length];
        component2Option = new Component[opts.length];
        helpStrings =  new String[opts.length];

        for(int i =0;i<opts.length;i++)
        {
          Option o = opts[i];
          o.getOptionName();
          // On traitera dans les prochaines versions des parametrages plus riches (String, entiers etc ...)
          //if(o.getCallback().getTypename() == Callback.TYPE_BOOLEAN)
          {
            JPanel p = new JPanel();
            JCheckBox c = new JCheckBox(o.getOptionName());
            JButton b = new JButton("?");
            b.addActionListener(new java.awt.event.ActionListener()
            {
              public void actionPerformed(ActionEvent e)
              {
                b_actionPerformed(e);
              }
            });
            c.setSelected(((BooleanHolder)o.getHolder()).get());
            p.add(c);

            p.add(b);
            button2OptionIndex[i] = b;
            component2Option[i] = c;
            box1.add(p);
            helpStrings[i] = o.getLongHelp();
          }
        }
      }
      catch(FileNotFoundException e)
      {}
      catch(GeneratorCreationException e)
      {
        SpecificOptionsTitle.setText("Unknown Description File Type !");
        GenRGenS.err.println(e);
      }
      catch(IOException e3)
      {}
  }

  public void OnValidate()
  {
     Option[] opts = optionsBackup;

      for(int i =0;i<opts.length;i++)
      {
        Holder o = opts[i].getHolder();
        Component c = component2Option[i];

        // On traitera dans les prochaines versions des parametrages plus riches (String, entiers etc ...)
        if(o.getTypename() == Holder.TYPE_BOOLEAN)
        {
          ((BooleanHolder)o).set(((JCheckBox)c).isSelected());
        }
      }
  }

  public int getReturnValue()
  {
    return returnValue;
  }

  public int getSequenceSize()
  {
    try
    {
      int j = Integer.parseInt(jTextField1.getText());
      if (j>0)
      {
        return j;
      }
      else
      {
        return(GenRGenS.getSize());
      }
    }
    catch(Exception e2)
    {
      return (GenRGenS.getSize());
    }
  }


  public int getHowMany()
  {
    try
    {
      int j = Integer.parseInt(jTextField2.getText());
      if (j>0)
      {
        return j;
      }
      else
      {
        return(GenRGenS.getHowMany());
      }
    }
    catch(Exception e2)
    {
      return (GenRGenS.getHowMany());
    }
  }

  public boolean outToBuffer()
  { return OutputBuffer.isSelected(); }

  public boolean outToFile()
  { return !(outFilename.equals("")); }

  public String outputFilename()
  { return outFilename; }

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

  public void show()
  {
    returnValue = this.DLG_CANCEL;
    super.show();
  }

  void jButton1_actionPerformed(ActionEvent e)
  {
    try
    {
      int i = Integer.parseInt(jTextField1.getText());
      if (i>0)
      {
         try
         {
           int j = Integer.parseInt(jTextField2.getText());
           if (j>0)
           {
             returnValue = this.DLG_OK;
             GenRGenS.verbose = jCheckBox3.isSelected();
             OnValidate();
             hide();
           }
           else
           {
             jTextField2.setText(""+GenRGenS.getHowMany());
             JOptionPane.showMessageDialog(null, "Field \"HowMany\" should contain a > 0 integer value.", "Error", JOptionPane.ERROR_MESSAGE);
           }
         }
         catch(Exception e2)
         {
           jTextField2.setText(""+GenRGenS.getHowMany());
           JOptionPane.showMessageDialog(null, "Field \"HowMany\" should contain a > 0 integer value.", "Error", JOptionPane.ERROR_MESSAGE);
         }
      }
      else
      {
        jTextField1.setText(""+GenRGenS.getSize());
        JOptionPane.showMessageDialog(null, "Field \"Size\" should contain a > 0 integer value.", "Error", JOptionPane.ERROR_MESSAGE);
      }
    }
    catch(Exception e2)
    {
      jTextField1.setText(""+GenRGenS.getSize());
      JOptionPane.showMessageDialog(null, "Field \"Size\" should contain a > 0 integer value.", "Error", JOptionPane.ERROR_MESSAGE);
    }
  }

  void jButton4_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();
          shortFilename.setText(SelectedFile.getName());
          jLabel6.setEnabled(true);
          shortFilename.setEnabled(true);
        }

  }

  public void setVisible(boolean b)
  {
    if (b)
    {
      shortFilename.setText("None");
      jLabel6.setEnabled(false);
      shortFilename.setEnabled(false);
      outFilename = "";
      this.pack();
    }
    super.setVisible(b);
    cardLayout1.show(jPanel4,"options");
  }


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

  void jButton5_actionPerformed(ActionEvent e)
  {
     cardLayout1.show(jPanel4,"options");
  }
}