package bicat.gui.window;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


import bicat.gui.BicatGui;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

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

public class RunDialog_Function implements ActionListener {

	BicatGui owner;

	static JDialog dialog;

	//static JFormattedTextField pvalue_thr;
        static JTextField pvalue_thr;
	
	static String SET_PVALUE_DIALOG_OK = "ok";
        static String SET_PVALUE_DIALOG_CANCEL = "cancel";

	static String SELECT_PVALUE_LIST = "select_pvalue";

	int radioIndex=0;

	

	
        JComboBox cb=null;
        ButtonGroup radioGroup;
	JRadioButton rb_list;
        JRadioButton rb_text;

	// ===========================================================================
	public RunDialog_Function() {
	}

	public RunDialog_Function(BicatGui o) {
		owner = o;
              
	}

	

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

		dialog = new JDialog(owner, "Set the Pvalue threshold");

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

		
                radioGroup = new ButtonGroup(); 
                
                
                // list radio Button/////////////////////
                rb_list=new JRadioButton("Select Pvalue threshold:",true);
                //rb_single.setText(DEFAULT_BETA_VALUE);
		rb_list.addActionListener(this);
		
                parameter_values.add(rb_list);
                
                
                Vector pValues_Stand = new Vector();
		pValues_Stand.add("0.00001");
		pValues_Stand.add("0.00005");
		pValues_Stand.add("0.0001");
		pValues_Stand.add("0.0005");
		pValues_Stand.add("0.001");
		pValues_Stand.add("0.005");
                pValues_Stand.add("0.01");
		pValues_Stand.add("0.05");
		

		cb = new JComboBox(pValues_Stand);
		cb.setActionCommand(SELECT_PVALUE_LIST);
		cb.setSelectedIndex(0);
		cb.setAlignmentX(JComboBox.CENTER_ALIGNMENT);
		cb.addActionListener(this);
                
                 parameter_values.add(cb);
                
                
                // text radio Button ///////////////////////////////
                rb_text=new JRadioButton("Enter Pvalue threshold:");
                rb_text.addActionListener(this);
		parameter_values.add(rb_text);
                
                
               // pvalue_thr = new JFormattedTextField(NumberFormat.getNumberInstance());
                pvalue_thr = new JTextField();
		DecimalFormat df=new DecimalFormat("0.0000000");
                
                
                
                //pvalue_thr.setText(new Double(owner.P_VALUE_THRESHOLD).toString());
		pvalue_thr.setText(df.format(owner.P_VALUE_THRESHOLD).toString());
                
                pvalue_thr.addActionListener(this);
		parameter_values.add(pvalue_thr);

		
                p0.add(parameter_values);
                
                radioGroup.add(rb_list);
                radioGroup.add(rb_text);
                
                RadioButtonHandler handler = new RadioButtonHandler();
			rb_list.addItemListener(handler);
			rb_text.addItemListener(handler);
			rb_list.addItemListener(handler);
                
                
            

	
		JPanel p3 = new JPanel(new FlowLayout()); // GridBagLayout() );

		JButton okButton = new JButton("Set Pvalue threshold");
		okButton.setActionCommand(SET_PVALUE_DIALOG_OK);
		okButton.addActionListener(this);
		p3.add(okButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.setActionCommand(SET_PVALUE_DIALOG_CANCEL);
		cancelButton.addActionListener(this);
		p3.add(cancelButton);

		// parameters.add(p1);

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

		JPanel parameters = new JPanel(new GridLayout(0, 1));
		parameters.add(p0); // arameter_values);
		parameters.add(p3);

		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 (SET_PVALUE_DIALOG_OK.equals(e.getActionCommand())) {

			try {
                            double selected_Pvalue=owner.P_VALUE_THRESHOLD;
                           
                             //java.text.DecimalFormat df = new java.text.DecimalFormat("0.00000");               
                             
                            if(radioIndex==0) // list
                            {
                                selected_Pvalue =Double.parseDouble((String)cb.getSelectedItem());
                               //selected_Pvalue= df.parse(df.format(selected_Pvalue)).doubleValue();
                               
                            }else if(radioIndex==1)
                            {
                                selected_Pvalue=Double.parseDouble(pvalue_thr.getText());
                                //selected_Pvalue= df.format(selected_Pvalue).;
                            }
        
                            System.out.println("selected Pvalue threshould >>>>>>>"+selected_Pvalue+"");
                            owner.P_VALUE_THRESHOLD=selected_Pvalue;

					dialog.setVisible(false);
					dialog.dispose();
					//JOptionPane.showMessageDialog(null,"Pvalue threshold is configured succesfully to : "+ df.format(owner.P_VALUE_THRESHOLD));
                                        JOptionPane.showMessageDialog(null,"Pvalue threshold is configured succesfully to : "+ owner.P_VALUE_THRESHOLD);
				
			} catch (NumberFormatException nfe) {
				nfe.printStackTrace();
			}catch(Exception ex)
                        {
                            ex.printStackTrace();
                        }
		}

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

	}

	

	
        
        
        
 //Creates class to handle buttons and assigns if/else tasks
public class RadioButtonHandler implements ItemListener
 
	{
 			public void itemStateChanged(ItemEvent event)
 				{
 
					if (event.getSource() == rb_list )
					radioIndex = 0;
 
					else if(event.getSource() == rb_text)
					radioIndex = 1;
 
					
 		
                        System.out.println("radio<<"+radioIndex);
                        }
		}        
        
        
        
        
        
}