// file: $ISIP_IFC/class/java/ConfigPanel/PanelEnergy.java
// version: $Id: PanelDetector.java 10227 2005-09-09 19:07:49Z stanley $

// import necessary java libraries
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
import java.lang.*;
import javax.swing.plaf.*;

/**
 * A panel to configure the energy parameters.
 */
public class PanelDetector extends ConfigPanel {


    // the minimum and maximum values for the signal detector values
    //
    
    /**
     * lower limit for frame duration.
     */
    public double min_frame_duration = 0;
    
     /**
     * upper limit for frame duration.
     */
    public double max_frame_duration = 0.5;
   
    /**
     * lower limit for window duration.
     */
    public double min_window_duration = 0;
           
    /**
     * upper limit for window duration.
     */
    public double max_window_duration = 0.5;
    
    /**
     * lower limit for nominal signal level.
     */
    public double min_nominal_signal_level = -50.0;
    
    /**
     * upper limit for nominal signal level.
     */
    public double max_nominal_signal_level = 0;
   
    /**
     * lower limit for signal adaptation delta.
     */
    public double min_signal_adaptation_delta = 0; 
           
    /**
     * upper limit for signal adaptation delta.
     */
    public double max_signal_adaptation_delta = 10; 
    
    /**
     * lower limit for signal adaptation constant.
     */
    public double min_signal_adaptation_constant = 0; 
    
    /**
     * upper limit for signal adaptation constant.
     */
    public double max_signal_adaptation_constant = 1; 
    
    /**
     * lower limit for nominal noise level.
     */
    public double min_nominal_noise_level = -50.0;
    
    /**
     * upper limit for nominal noise level.
     */
    public double max_nominal_noise_level = 0;
    
    /**
     * lower limit for noise adaptation delta.
     */
    public double min_noise_adaptation_delta = 0; 
    
    /**
     * upper limit for noise adaptation delta.
     */
    public double max_noise_adaptation_delta = 10; 
    
    /**
     * lower limit for noise adaptation constant.
     */
    public double min_noise_adaptation_constant = 0; 
    
    /**
     * upper limit for noise adaptation constant.
     */
    public double max_noise_adaptation_constant = 1; 
    
    /**
     * lower limit for noise floor.
     */
    public double min_noise_floor = -80.0;
    
    /**
     * upper limit for noise floor.
     */
    public double max_noise_floor = 0;
    
    /**
     * lower limit for utterance delta.
     */
    public double min_utterance_delta = 0;
    
    /**
     * upper limit for utterance delta.
     */
    public double max_utterance_delta = 10;
    
    /**
     * lower limit for minimum utterance duration.
     */
    public double min_minimum_utterance_duration = 0;
    
    /**
     * upper limit for minimum utterance duration.
     */
    public double max_minimum_utterance_duration = 10;
    
    /**
     * lower limit for maximum value.
     */
    public double min_minimum_utterance_separation = 0;
    
    /**
     * upper limit for maximum value.
     */
    public double max_minimum_utterance_separation = 10;
    
    /**
     * lower limit for maximum utterance duration.
     */
    public double min_maximum_utterance_duration = 0;
    
    /**
     * upper limit for maximum utterance duration.
     */
    public double max_maximum_utterance_duration = 100;
    
    /**
     * lower limit for pre emphasis.
     */
    public double min_preemphasis = 0;
    
    /**
     * upper limit for pre emphasis.
     */ 
    public double max_preemphasis = 1;
    
    /**
     * lower limit for pad time.
     */
    public double min_pad_time = 0;
    
    /**
     * upper limit for pad time.
     */
    public double max_pad_time = 2;

    // protected data types
    //
    /**
     * Window type values.
     */
    protected Vector win_type_values_d = new Vector(INIT, INC);
        
    /**
     * Main energy configuration panel.
     */        
    public PanelDetector() {

	// constructor for the class
	//
	super();
	
	association_d = "Detector";
	
	JPanel config_panel = new JPanel();
	
	//  make this config panel use grid bag layout
	//
	GridBagLayout gridbag = new GridBagLayout();
	config_panel.setLayout(gridbag);

	// create the border and set the proper font
	//
	BorderUIResource.TitledBorderUIResource border =
	    new BorderUIResource.TitledBorderUIResource("Signal Detector Configuration");

	border.setTitleColor(Color.black);

	// make the config panel use the proper border
	//
	this.setBorder(border);

	// define the lables, types default value, minimum value,
	// maximum value and action commands
	//
	// it will look like this in the panel
	//
	// lable     textfield(default value) or enum
	//    
	labels_d.add("Channel to be processed");
	labels_d.add("Frame duration (seconds)");
	labels_d.add("Window duration (seconds)");
	labels_d.add("Nominal Signal Level");
	labels_d.add("Signal Adaptation Delta");
	labels_d.add("Signal Adaptation Constant");
	labels_d.add("Nominal Noise Level");
	labels_d.add("Noise Adaptation Delta");
	labels_d.add("Noise Adaptation Constant");
	labels_d.add("Noise Floor");
	labels_d.add("Utterance Delta");
	labels_d.add("Minimum utterance duration (seconds)");
	labels_d.add("Minimum utterance separation (seconds)");
	labels_d.add("Maximum utterance duration (seconds)");
	//labels_d.add("Zero crossing utterance threshold");	
	labels_d.add("Preemphasis");
	labels_d.add("pad time");

	types_d.add("double");
	types_d.add("double");
 	types_d.add("double");		
	types_d.add("double");
	types_d.add("double");
	types_d.add("double");
	types_d.add("double");
	types_d.add("double");
	types_d.add("double");
	types_d.add("double");
	types_d.add("double");
	types_d.add("double");
	types_d.add("double");
	types_d.add("double");
	//types_d.add("double");
	types_d.add("double");
	types_d.add("double");
	
	
	values_d.add("1");
	values_d.add("0.020");
	values_d.add("0.030");
	values_d.add("-40.00");
	values_d.add("5.00");
	values_d.add("0.50");
	values_d.add("-50.00");
	values_d.add("5.00");
	values_d.add("0.75");
	values_d.add("-80.00");
	values_d.add("10.00");
	values_d.add("0.1");
	values_d.add("1.0");
	values_d.add("100");
	//values_d.add("0.1");
	values_d.add("0.95");
	values_d.add("0.142");

	min_values_d.add("0");
	min_values_d.add("0");	
	min_values_d.add("0");	
	min_values_d.add("-50.00");
	min_values_d.add("0");
	min_values_d.add("0");	
	min_values_d.add("-50.00");
	min_values_d.add("0");
	min_values_d.add("0");	
	min_values_d.add("-80.00");	
	min_values_d.add("0");	
	min_values_d.add("0");
	min_values_d.add("0");
	min_values_d.add("0");
	//min_values_d.add("0");	
	min_values_d.add("0");
	min_values_d.add("0");

	
	max_values_d.add("6");
	max_values_d.add("0.50");
	max_values_d.add("0.50");
	max_values_d.add("0.00");
	max_values_d.add("10.00");
	max_values_d.add("1.00");
	max_values_d.add("0");	
	max_values_d.add("10.00");
	max_values_d.add("1.00");
	max_values_d.add("0.00");
	max_values_d.add("10.00");
	max_values_d.add("10.00");
	max_values_d.add("100.00");
	max_values_d.add("100.00");
	//max_values_d.add("5.00");
	max_values_d.add("1");
	max_values_d.add("2.00");
	
	
	commands_d.add("channel_d");
	commands_d.add("frame_duration_d");
	commands_d.add("window_duration_d");
	commands_d.add("nominal_signal_d");
	commands_d.add("signal_adaptation_delta_d");
	commands_d.add("signal_adaptation_constant_d");
	commands_d.add("nominal_noise_d");
	commands_d.add("noise_adaptation_delta_d");
	commands_d.add("noise_adaptation_constant_d");
	commands_d.add("noise_floor_d");
	commands_d.add("utterance_delta_d");
	commands_d.add("min_utt_dur_d");
	commands_d.add("min_utt_sep_d");
	commands_d.add("max_utt_dur_d");
	//commands_d.add("zc_utt_thresh_d");
	commands_d.add("preemph_coef_d");
	commands_d.add("pad_time_d");

	for (int i = 0; i < labels_d.size(); i++) {
	    
	    // add a label for all labels
	    //
	    String configLabel = (String)labels_d.get(i);
	    JLabel jlabel = new JLabel(configLabel);

	    jlabel.setForeground(Color.black);
	    constrain(config_panel, jlabel, gridbag, 0, i,
		      1, 1, 1, 0, GridBagConstraints.WEST);
	    
	    // creation of input box for double
	    //
	    if (types_d.get(i).equals("double")) {
		
		int x = (int)(Float.parseFloat((String)values_d.get(i)) * 100);

		int min = (int)(Float.parseFloat((String)min_values_d.get(i)) * 100);
		int max = (int)(Float.parseFloat((String)max_values_d.get(i)) * 100);

		Dimension size = new Dimension(WIDTH, HEIGHT);

		FloatTextField hourField;

		hourField = new FloatTextField((int)Float.parseFloat((String)values_d.get(i)), 7);

		inputed_data_d.add((String)values_d.get(i));
      
		text_field_d.add(hourField);

		constrain(config_panel, (FloatTextField)(text_field_d).get(i), gridbag, 10, i,
			  1, 1, 1, 0, GridBagConstraints.EAST);

		entered_data_d.add(hourField);
		block_type_d.add("double");
	    }

	    // creation of input box for double
	    //
	    else if (types_d.get(i).equals("integer")) {
		
		int x =  Integer.parseInt((String)values_d.get(i));

		int min = Integer.parseInt((String)min_values_d.get(i));

		int max = Integer.parseInt((String)max_values_d.get(i));

		Dimension size = new Dimension(WIDTH, HEIGHT);

		FloatTextField hourField;

		hourField = new FloatTextField((int)Float.parseFloat((String)values_d.get(i)), 7);

		inputed_data_d.add((String)values_d.get(i));
      
		text_field_d.add(hourField);

		constrain(config_panel, (FloatTextField)(text_field_d).get(i), gridbag, 10, i,
			  1, 1, 1, 0, GridBagConstraints.EAST);

		entered_data_d.add(hourField);
		block_type_d.add("integer");
	    }
 

	    else if (types_d.get(i).equals("enum")){
		
		// create a drop down list to allow selection from
		//
		JComboBox select = new JComboBox();

		select.setForeground(Color.black);

		// retrieve and add all possible values to this list
		//
		Vector enum_values = (Vector)values_d.get(i);
		for(int j = 0; j < enum_values.size(); j++) {
		    select.addItem(enum_values.get(j));
		}

		// add the combo box to the panel
		//
		constrain(config_panel, select, gridbag, 10, i,
			  1, 1, 1, 0, GridBagConstraints.EAST);
		entered_data_d.add(select);
		block_type_d.add("enum");
	    }
	}

	this.setLayout(new BorderLayout());

	add(config_panel,  "North");

    }



    /**
     * Method to verify the input value
     * 
     * @param parameter the value of the parameter that is checked
     * @param min_value the minimum value of the parameter
     * @param max_value the maximum value of the parameter
     * @param parameter_name the name of the parameter to be displayed on the
     *                       warning screen
     * @return  a boolean value indicating status.
     *
     */
    public boolean checkRange(double parameter, double min_value, double max_value, String parameter_name ) {
    

	if (min_value == 0) {
	    if (parameter <= min_value || parameter > max_value) {
		
		JFrame frame = new JFrame("Warning"); 
		String warningMessage = new 
		    String(parameter_name + " :" + " Please enter a value greater than " + min_value 
			   + " and less than or equal to " + max_value + ".");
		JOptionPane.showMessageDialog(frame, warningMessage, "Detector Panel", JOptionPane.WARNING_MESSAGE);
		
		return false;
	    }
	}
	
	else {
	if (parameter < min_value || parameter > max_value) {
	    
	    JFrame frame = new JFrame("Warning"); 
	    String warningMessage = new 
		String(parameter_name + " :" + " Please enter a value between " + min_value + " and " + max_value + ".");
	    JOptionPane.showMessageDialog(frame, warningMessage, "Detector Panel", JOptionPane.WARNING_MESSAGE);
	    
	    return false;
	}
	}
	return true;
    }



   /**
     * Method to verify the input value
     *     
     * @return  a boolean value indicating status.
     *
     */
    public boolean apply() {

	update();

	// local variables to hold the parsed value
	//
	boolean success = true;
	int channel = 0;
	float frame_duration = 0;
	float window_duration = 0;
	float nominal_signal = 0;
	float signal_adaptation_delta = 0;
	float signal_adaptation_constant = 0;
	float nominal_noise = 0;
	float noise_adaptation_delta = 0;
	float noise_adaptation_constant = 0;
	float noise_floor = 0;
	float utterance_delta = 0;
	float min_utt_dur = 0;
	float min_utt_sep = 0;
	float max_utt_dur = 0;
	float zc_utt_thresh = 0;
	float preemph_coef = 0;
	float pad_time = 0;


	for (int i = 0; i < cfg_data_d.getAssociation().size(); i++) {

	    if (cfg_data_d.getName().get(i).equals("channel_d")) {
	        channel = (int)Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("frame_duration_d")) {
	        frame_duration = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("window_duration_d")) {
		window_duration = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("nominal_signal_d")) {
		nominal_signal = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	
	    }
	    else if (cfg_data_d.getName().get(i).equals("signal_adaptation_delta_d")) {
		signal_adaptation_delta = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));

	    }
	    else if (cfg_data_d.getName().get(i).equals("signal_adaptation_constant_d")) {
		signal_adaptation_constant = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("nominal_noise_d")) {
		nominal_noise = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("noise_adaptation_delta_d")) {
		noise_adaptation_delta = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("noise_adaptation_constant_d")) {
		noise_adaptation_constant = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }

	    else if (cfg_data_d.getName().get(i).equals("noise_floor_d")) {
		noise_floor = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("utterance_delta_d")) {
		utterance_delta = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("min_utt_dur_d")) {
		min_utt_dur = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    
	    else if (cfg_data_d.getName().get(i).equals("min_utt_sep_d")) {
		min_utt_sep = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    
	    else if (cfg_data_d.getName().get(i).equals("max_utt_dur_d")) {
		max_utt_dur = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("zc_utt_thresh_d")) {
		zc_utt_thresh = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("preemph_coef_d")) {
		preemph_coef = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("pad_time_d")) {
		pad_time = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else {
		// do nothing
	    }


	}

	System.out.println("channel = " + channel);
	System.out.println("frame_duration = " + frame_duration);
	System.out.println("window_duration = " + window_duration);
	System.out.println("nominal_signal = " + nominal_signal);	
	System.out.println("signal_adaptation_delta = " + signal_adaptation_delta);
	System.out.println("signal_adaptation_constant = " + signal_adaptation_constant);
	System.out.println("noimnal_noise = " + nominal_noise);
	System.out.println("noise_adaptation_delta = " + noise_adaptation_delta);	


	System.out.println("noise_adaptation_constant = " + noise_adaptation_constant);
	System.out.println("noise_floor = " + noise_floor);
	System.out.println("utterance_delta = " + utterance_delta);
	System.out.println("min_utt_dur = " + min_utt_dur);	
	System.out.println("min_utt_sep = " + min_utt_sep);
	System.out.println("max_utt_dur = " + max_utt_dur);
	System.out.println("zc_utt_thresh = " + zc_utt_thresh);
	System.out.println("preemph_coef = " + preemph_coef);	
	System.out.println("pad_time = " + pad_time);	



	
	//----------------------------------------------------
	//
	// range checking
	//
	//----------------------------------------------------

	if (!(checkRange(frame_duration, min_frame_duration, max_frame_duration, "FrameDuration"))) {
	    
	    success = false;
		
	}
	
	if (!(checkRange(window_duration, min_window_duration, max_window_duration, "WindowDuration"))) {

	    success = false;
	}
	
	if (!(checkRange(nominal_signal, min_nominal_signal_level, max_nominal_signal_level , "NominalSignalLevel"))) {

		success = false;
	}

	if (!(checkRange(signal_adaptation_delta, min_signal_adaptation_delta, max_signal_adaptation_delta , 
			 "SignalAdaptationDelta"))) {

		success = false;
	}
	
	if (!(checkRange(signal_adaptation_constant, min_signal_adaptation_constant, max_signal_adaptation_constant, 
			 "SignalAdaptationConstant"))) {

		success = false;
	}
	
	if (!(checkRange(nominal_noise, min_nominal_noise_level, max_nominal_noise_level , "NominalSignalLevel"))) {

		success = false;
	}

	if (!(checkRange(noise_adaptation_delta, min_noise_adaptation_delta, max_noise_adaptation_delta , 
			 "SignalAdaptationDelta"))) {

		success = false;
	}
	
	if (!(checkRange(noise_adaptation_constant, min_noise_adaptation_constant, max_noise_adaptation_constant, 
			 "SignalAdaptationConstant"))) {

		success = false;
	}
	

	if (!(checkRange(noise_floor, min_noise_floor, max_noise_floor, "NoiseFloor"))) {

		success = false;
	}
	
	if (!(checkRange(utterance_delta, min_utterance_delta, max_utterance_delta, "UtteranceDelta"))) {

		success = false;
	}
	
	if (!(checkRange(min_utt_dur, min_minimum_utterance_duration, max_minimum_utterance_duration, 
			 "MinimumUtteranceDuration"))) {

		success = false;
	}

	if (!(checkRange(min_utt_sep, min_minimum_utterance_separation, max_minimum_utterance_separation, 
			 "MinimumUtteranceSeparation"))) {

	    success = false;
	}

	if (!(checkRange(max_utt_dur, min_maximum_utterance_duration, max_maximum_utterance_duration, 
			 "MaximumUtteranceDuration"))) {
	    
	    success = false;
	}

	if (!(checkRange(preemph_coef, min_preemphasis, max_preemphasis, "Preemphasis"))) {

		success = false;
	}

	if (!(checkRange(pad_time, min_pad_time, max_pad_time, "PadTime"))) {

		success = false;
	}


	// start verification.  All values can not be less than zero.
	//
	if (channel < 0 ||
	    frame_duration < 0 ||
	    window_duration < 0 ||
	    signal_adaptation_delta < 0 ||
	    signal_adaptation_constant < 0 ||
	    noise_adaptation_delta < 0 ||
	    noise_adaptation_constant < 0 ||
	    utterance_delta < 0 ||
	    min_utt_dur < 0 ||
	    min_utt_sep < 0 ||
	    max_utt_dur < 0 ||
	    zc_utt_thresh < 0 ||
	    preemph_coef < 0 ||
	    pad_time < 0) {

	    JFrame frame = new JFrame("Warning"); 
	    String warningMessage = new 
		String("Detector Panel: Some values can not be less than zero.");
	    JOptionPane.showMessageDialog(frame,warningMessage);

	    success = false;
	}

	return success;
    }

}
