/* ComparatorDialog.java
 *
 * created: Wed May 10 2000
 *
 * This file is part of Artemis
 *
 * Copyright (C) 2000  Genome Research Limited
 *
 * 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.
 *
 * $Header: /nfs/pathsoft/repository/powmap/diana/components/ComparatorDialog.java,v 1.10 2002/07/31 13:11:50 kmr Exp $
 */

package diana.components;

import diana.*;

import uk.ac.sanger.pathogens.OutOfRangeException;
import uk.ac.sanger.pathogens.embl.EntryInformationException;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;

/**
 *  ComparatorDialog a dialog that allows the user to the choose two files to
 *  compare with a Comparator component and a file containing comparison data.
 *
 *  @author Kim Rutherford <kmr@sanger.ac.uk>
 *  @version $Id: ComparatorDialog.java,v 1.10 2002/07/31 13:11:50 kmr Exp $
 **/

public class ComparatorDialog extends Frame {
  /**
   *  Create a dialog that allow the user to the choose two files to compare
   *  and a file containing comparison data.
   **/
  public ComparatorDialog () {
    final Font default_font = Options.getOptions ().getFont ();

    setFont (default_font);

    final Panel top_panel = new Panel ();

    add (top_panel, "Center");

    final GridBagLayout gridbag = new GridBagLayout();
    top_panel.setLayout (gridbag);

    final Vector text_field_vector = new Vector ();

    for (int i = 0 ; i < 3 ; ++i) {
      final String label;
      switch (i) {
      case 0:
        label = "Sequence file 1";
        break;
      case 1:
        label = "Comparison file 1"; 
        break;
      case 2:
        label = "Sequence file 2";
        break;
      default:
        throw new Error ("internal error");
      }

      final TextField text_field =
        makeFileNamePanel (label, top_panel, gridbag);

      text_field_vector.addElement (text_field);
    }


    final GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.NORTH;
    c.weighty = 0;
    c.gridwidth = 1;

    final Button more_button = new Button ("more files ...");

    final Panel more_button_panel = new Panel ();
    more_button_panel.setLayout (new FlowLayout (FlowLayout.LEFT));
    more_button_panel.add (more_button);

    c.gridwidth = 1;
    gridbag.setConstraints (more_button_panel, c);
    top_panel.add (more_button_panel);

    more_button.addActionListener (new ActionListener () {
      public void actionPerformed (ActionEvent e) {
        top_panel.remove (more_button_panel);

        final boolean add_sequence_flag;

        if (text_field_vector.size () % 2 == 0 ||
            Options.getOptions ().isNoddyMode ()) {
          add_sequence_flag = true;
        } else {
          add_sequence_flag = false;
        }

        if (text_field_vector.size () % 2 == 1 ||
            Options.getOptions ().isNoddyMode ()) {
          final String seq_label =
            "Comparison file " + (text_field_vector.size () / 2 + 1);
          final TextField seq_text_field =
            makeFileNamePanel (seq_label, top_panel, gridbag);

          text_field_vector.addElement (seq_text_field);
        }

        if (add_sequence_flag) {
          final String comp_label =
            "Sequence file " + (text_field_vector.size () / 2 + 1);
          final TextField comp_text_field =
            makeFileNamePanel (comp_label, top_panel, gridbag);
          
          text_field_vector.addElement (comp_text_field);
        }

        top_panel.add (more_button_panel);

        packAndCentre ();
      }
    });


    final Button apply_button = new Button ("Apply");

    apply_button.addActionListener (new ActionListener () {
      public void actionPerformed (ActionEvent e) {
        doApply (text_field_vector);
      }
    });


    final Button close_button = new Button ("Close");

    close_button.addActionListener (new ActionListener () {
      public void actionPerformed (ActionEvent e) {
        ComparatorDialog.this.dispose ();
      }
    });


    final FlowLayout flow_layout =
      new FlowLayout (FlowLayout.CENTER, 15, 5);

    final Panel close_and_apply_panel = new Panel (flow_layout);

    close_and_apply_panel.add (apply_button);
    close_and_apply_panel.add (close_button);

    add (close_and_apply_panel, "South");


    addWindowListener (new WindowAdapter () {
      public void windowClosing (WindowEvent event) {
        ComparatorDialog.this.dispose ();
      }
    });


    packAndCentre ();
  }

  /**
   *  Call pack() and then centre the Frame in the middle of the screen.
   **/
  private void packAndCentre () {
    pack ();

    final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation (new Point ((screen.width - getSize ().width) / 2,
                            (screen.height - getSize ().height) / 2));
  }

  /**
   *  Make a panel for choosing one file name panel.
   **/
  private TextField makeFileNamePanel (final String label_string,
                                       final Panel parent_panel,
                                       final GridBagLayout gridbag) {

    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.NORTH;
    c.weighty = 0;

    final Label label = new Label (label_string);

    final Panel panel = new Panel ();
    panel.setLayout (new FlowLayout (FlowLayout.LEFT));
    panel.add (label);

    c.gridwidth = 1;
    gridbag.setConstraints (panel, c);
    parent_panel.add (panel);

    final TextField text_field = new TextField ("", 28);
    gridbag.setConstraints (text_field, c);
    parent_panel.add (text_field);

    final Button choose_button = new Button ("Choose ...");

    choose_button.addActionListener (new ActionListener () {
      public void actionPerformed (ActionEvent _) {
        final FileDialog file_dialog =
          new FileDialog (ComparatorDialog.this,
                          "Choose first sequence ...", FileDialog.LOAD);

        file_dialog.setVisible (true);

        if (file_dialog.getFile () != null) {
          text_field.setText (file_dialog.getDirectory () +
                              file_dialog.getFile ());
        }
      }
    });

    c.gridwidth = GridBagConstraints.REMAINDER;
    gridbag.setConstraints (choose_button, c);
    parent_panel.add (choose_button);

    return text_field;
  }

  /**
   *  Attempt to call ActMain.makeComparator () for the given file names.
   **/
  private void doApply (final Vector text_field_vector) {
    if (text_field_vector.size () < 3) {
      throw new Error ("internal error - not enough file names given to " +
                       "ComparatorDialog.doApply()");
    }

    String [] file_names = new String [text_field_vector.size ()];

    for (int i = 0 ; i < text_field_vector.size () ; ++i) {
      file_names[i] =
        ((TextField) text_field_vector.elementAt (i)).getText ().trim ();

      if (file_names[i].length () == 0) {
        // ignore the problem if there are at least 3 files listed (ie. seq1
        // comp1v2 seq2) and the remaining files lengths are zero
        if (i > 3) {
          // set to true if a text field is found that isn't zero length
          boolean found_file = false;

          for (int sub_index = i ;
               sub_index < text_field_vector.size () ;
               ++sub_index) {
            final TextField text_field =
              (TextField) text_field_vector.elementAt (sub_index);

            final String this_text = text_field.getText ().trim ();

            if (this_text.length () != 0) {
              found_file = true;
            }
          }

          if (!found_file) {
            // truncate file_names
            final String [] new_file_names = new String [i];
            System.arraycopy (file_names, 0, new_file_names, 0, i);
            file_names = new_file_names;
            break;
          }
        }

        new MessageDialog (this, "one of the file names is missing");
        return;
      }
    }

    final MessageFrame reading_message = new MessageFrame ("reading ...");

    if (ActMain.makeMultiComparator (this, null, file_names)) {
      ComparatorDialog.this.dispose ();
    }
    reading_message.dispose ();
  }
}
