package bicat.gui.window;

import bicat.gui.BicatGui;

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

import bicat.run_machine.*;

import java.text.NumberFormat;

/**
 * <p>Title: BicAT Tool </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * @author Amela Prelic
 * @version 1.0
 *
 **/

public class RunDialog_BiclusterFind implements ActionListener {

  BicatGui owner;
  JDialog dialog;

  static String DEFAULT_PARAMETERS = "set_defaults";
  static String RUN_Bicluster_Find = "run_Bicluster_Find";
  static String RUN_Bicluster_Find_DIALOG_CANCEL = "cancel";

  static String DEFAULT_noise_Th_VALUE = "5";
  static String DEFAULT_PR_VALUE = "10";
  static String DEFAULT_NC_VALUE = "50";
  static String DEFAULT_PO_VALUE = "50";
  static String DEFAULT_Model_Type_VALUE = "a";

  static JTextField noise_Th_f;
  static JTextField pr_f;
  static JTextField nc_f;
  static JTextField po_f;
  static JTextField model_Type_f;

  Arguments_Bicluster_Find bicluster_Finda;

  // ===========================================================================
  public RunDialog_BiclusterFind() { }
  public RunDialog_BiclusterFind(BicatGui o, Arguments_Bicluster_Find args) {
    owner = o;
    bicluster_Finda = args;
  }

  // ===========================================================================
  public void makeWindow() {

    // user-defined parameters: seed, delta, alpha, n
    dialog = new JDialog(owner, "Run Bicluster_Find");

    JPanel parameters = new JPanel( new GridLayout(0,1) );

    // ....

    JPanel p0 = new JPanel( new FlowLayout() );
    JPanel parameter_values = new JPanel( new GridLayout(0,2) );

    JLabel noise_Th_l = new JLabel(" Noise Threshold: ");
    noise_Th_l.setAlignmentX(JPanel.LEFT_ALIGNMENT);
    parameter_values.add(noise_Th_l);

    noise_Th_f = new JTextField();
    noise_Th_f.setText(DEFAULT_noise_Th_VALUE);
    noise_Th_f.addActionListener(this);
    parameter_values.add(noise_Th_f);
    // ....

    JLabel pr_l = new JLabel(" Pr (min no of rows) :");
    pr_l.setAlignmentX(JPanel.LEFT_ALIGNMENT);
    parameter_values.add(pr_l);

    pr_f = new JTextField();
    pr_f.setText(DEFAULT_PR_VALUE);
    pr_f.addActionListener(this);
    parameter_values.add(pr_f);

    JLabel nc_l = new JLabel(" Nc (min no of columns): ");
    nc_l.setAlignmentX(JPanel.LEFT_ALIGNMENT);
    parameter_values.add(nc_l);

    nc_f = new JTextField();
    nc_f.setText(DEFAULT_NC_VALUE);
    nc_f.addActionListener(this);
    parameter_values.add(nc_f);

    // ....

    JLabel po_l = new JLabel(" Po (max % of overalpping allowed): ");
    po_l.setAlignmentX(JPanel.LEFT_ALIGNMENT);
    parameter_values.add(po_l);

    po_f = new JTextField();
    po_f.setText(DEFAULT_PO_VALUE);
    po_f.addActionListener(this);
    parameter_values.add(po_f);

    
    
    JLabel model_Type_l = new JLabel(" Type of the bicluster model (a or m): ");
    model_Type_l.setAlignmentX(JPanel.LEFT_ALIGNMENT);
    parameter_values.add(model_Type_l);

    model_Type_f = new JTextField();
    model_Type_f.setText(DEFAULT_Model_Type_VALUE);
    model_Type_f.addActionListener(this);
    parameter_values.add(model_Type_f);
    
    
    
        
    p0.add(parameter_values);

    // ........................................................................

    JPanel p1 = new JPanel( new GridBagLayout() );

    JButton okButton = new JButton("Run Bicluster_Find");
    okButton.setActionCommand(RUN_Bicluster_Find);
    okButton.addActionListener(this);
    p1.add(okButton);

    JButton cancelButton = new JButton("Cancel");
    cancelButton.setActionCommand(RUN_Bicluster_Find_DIALOG_CANCEL);
    cancelButton.addActionListener(this);
    p1.add(cancelButton);

    // ........................................................................

    parameters.add(p0);
    parameters.add(p1);

    dialog.setContentPane(parameters);

    // ........................................................................
    dialog.pack();
    dialog.setLocationRelativeTo(owner);
    dialog.setVisible(true);

  }

  // ===========================================================================
  /**
   * For <code>ActionListener</code> interface, reacts to user selections and
   * button clicks in this search window.
   *
   * */
  public void actionPerformed(ActionEvent e) {

    if(RUN_Bicluster_Find.equals(e.getActionCommand())) {

      try {
        int noise_Th = ( new Integer(noise_Th_f.getText()).intValue());
        int pr = ( new Integer(pr_f.getText()).intValue());
        int nc = ( new Integer(nc_f.getText()).intValue());
        int po = ( new Integer(po_f.getText()).intValue());
        char model_Type= model_Type_f.getText().toCharArray()[0];
        
        bicluster_Finda.setNoise_Th(noise_Th);
        bicluster_Finda.setPr(pr);
        bicluster_Finda.setNc(nc);
        bicluster_Finda.setPo(po);
        bicluster_Finda.setModel_Type(model_Type);
            
        
        
        JOptionPane.showMessageDialog(null, "Bicluster_Find algorithm is running... \nThe calculations may take some time");
        for (int i = 0; i < 500000000; i++) {} //wait
        owner.runMachine_Bicluster_Find.runBiclustering(bicluster_Finda);

        dialog.setVisible(false);
        dialog.dispose();
        
      }
      catch(NumberFormatException nfe) { nfe.printStackTrace(); }
    }

    else if(RUN_Bicluster_Find_DIALOG_CANCEL.equals(e.getActionCommand())) {
      dialog.setVisible(false);
      dialog.dispose();
    }

      }

}