// file: $ISIP_IFC/class/java/ConfigPanel/PanelEnergy.java
// version: $Id: PanelEnergy.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 PanelEnergy extends ConfigPanel {


    // the minimum and maximum ranges for the energy parameters
    //
    
    /**
     * lower limit for maximum value.
     */
    public double min_maximum_value = 0;
    
    /**
     * upper limit for maximum value.
     */
    public double max_maximum_value = 10000;
    
    /**
     * lower limit for the minimum value.
     */
    public double min_minimum_value = 0;
    
    /**
     * upper limit for the minimum value.
     */
    public double max_minimum_value = 10000;
    
    /**
     * lower limit for the super tick marks.
     */
    public double min_super_tick_marks = 0;
    
    /**
     * upper limit for the super tick marks.
     */
    public double max_super_tick_marks = 10;
    
    /**
     * lower limit for the major tick marks.
     */
    public double min_major_tick_marks = 0;
    
     /**
     * upper limit for the major tick marks.
     */
    public double max_major_tick_marks = 2.5;
    
    /**
     * lower limit for the minor tick marks.
     */
    public double min_minor_tick_marks = 0;
    
    /**
     * upper limit for the minor tick marks.
     */
    public double max_minor_tick_marks = 1;
    
    /**
     * lower limit for the vertical tick marks.
     */
    public double min_vertical_tick_marks = 0;
    
    /**
     * upper limit for the vertical tick marks.
     */
    public double max_vertical_tick_marks = 100;
    
    /**
     * lower limit for pre-emphasis.
     */
    public double min_preemphasis = 0;
    
    /**
     * upper limit for pre-emphasis.
     */
    public double max_preemphasis = 1;
    
    /**
     * lower limit for window duration.
     */
    public double min_window_duration = 0;
    
    /**
     * upper limit for window duration.
     */
    public double max_window_duration = 1.0;
    	

    /**
     * Window type values.
     */
    protected Vector<Object> win_type_values_d = new Vector<Object>(INIT, INC);

    /**
     * Main energy configuration panel.
     */        
    public PanelEnergy() {

	// constructor for the class
	//
	super();

	association_d = "Energy";
      
	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("Energy Configuration");
	border.setTitleColor(Color.black);

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

	// to simplify the creation, the data vectors will be trimmed to only
	// include the transform we're looking at and saved into these three
	// vectors for use in a bit
	//
	scale_values_d.add("dB");
	scale_values_d.add("LINEAR");

	win_type_values_d.add("Hamming");

	// 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("Maximum value");
	labels_d.add("Minimum value");
	labels_d.add("Super tick marks (seconds)");
	labels_d.add("Major tick marks (seconds)");
	labels_d.add("Minor tick marks (seconds)");

	labels_d.add("Vertical tick marks (dB)");
	labels_d.add("Preemphasis");
	labels_d.add("Window duration (seconds)");
	labels_d.add("Scale");
	labels_d.add("Window Type");


	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("enum");
	types_d.add("enum");
	

	values_d.add("100.0");
	values_d.add("0");
	values_d.add("1.0");
	values_d.add("0.2");
	values_d.add("0.1");

	values_d.add("10.0");
	values_d.add("0.9375");
	values_d.add("0.025");
	values_d.add(scale_values_d);
	values_d.add(win_type_values_d);


	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");
	min_values_d.add("0");
	min_values_d.add("0");
	min_values_d.add("0");

	max_values_d.add("100000");
	max_values_d.add("100000");
	max_values_d.add("10");
	max_values_d.add("2.5");
	max_values_d.add("1");

	max_values_d.add("100");
	max_values_d.add("1");
	max_values_d.add("0.10");
	max_values_d.add("0");
	max_values_d.add("0");

	commands_d.add("maximum_energy_d");
	commands_d.add("minimum_energy_d");
	commands_d.add("e_super_tick_d");
	commands_d.add("e_major_tick_d");
	commands_d.add("e_minor_tick_d");

	commands_d.add("e_vertical_tick_d");
	commands_d.add("preemph_coef_d");
	commands_d.add("window_duration_d");
	commands_d.add("scale_d");
	commands_d.add("window_type_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 && (!parameter_name.equals("MinimumValue"))) {
	    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, "EnergyPanel", 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,"Energy Panel", JOptionPane.WARNING_MESSAGE);
		    
		    return false;
		}
	    }
		return true;
    }
    
    

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

	// these local variables hold the parsed values
	//
	boolean success = true;
	float preemph_coef = 0;
	float window_duration = 0;
	float maximum_energy = 0;
	float minimum_energy = 0;
	float e_super_tick = 0;
	float e_major_tick = 0;
	float e_minor_tick = 0;
	float e_vertical_tick = 0;

	System.out.println("Config Data" + cfg_data_d.getAssociation() + 
			   " " +
			   cfg_data_d.getAssociation().size());

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

	    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("window_duration_d")) {
	        window_duration = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("maximum_energy_d")) {
		maximum_energy = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("minimum_energy_d")) {
		minimum_energy = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	
	    }
	    else if (cfg_data_d.getName().get(i).equals("e_super_tick_d")) {
		e_super_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));

	    }
	    else if (cfg_data_d.getName().get(i).equals("e_major_tick_d")) {
		e_major_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("e_minor_tick_d")) {
		e_minor_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("e_vertical_tick_d")) {
		e_vertical_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("scale_d")) {
		// do nothing
	    }
	    else if (cfg_data_d.getName().get(i).equals("window_type_d")) {
		// do nothing
	    }

	}

	System.out.println("preemph_coef = " + preemph_coef);
	System.out.println("window_duration = " + window_duration);
	System.out.println("maximum_energy = " + maximum_energy);
	System.out.println("minimum_energy = " + minimum_energy);	
	System.out.println("e_super_tick = " + e_super_tick);
	System.out.println("e_major_tick = " + e_major_tick);
	System.out.println("e_minor_tick = " + e_minor_tick);
	System.out.println("e_vertical = " + e_vertical_tick);	

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

	if (!(checkRange(maximum_energy, min_maximum_value, max_maximum_value, "MaximumValue"))) {
	    
	    success = false;
	}

	if (!(checkRange(minimum_energy, min_minimum_value, max_minimum_value, "MinimumValue"))) {
	    
	    success = false;
	}
	if (!(checkRange(e_super_tick, min_super_tick_marks, max_super_tick_marks, "SuperTickMarks"))) {
	    
	    success = false;
	}
	if (!(checkRange(e_major_tick, min_major_tick_marks, max_major_tick_marks, "MajorTickMarks"))) {
	    
	    success = false;
	}
	if (!(checkRange(e_minor_tick, min_minor_tick_marks, max_minor_tick_marks, "MinorTickMarks"))) {
	    
	    success = false;
	}
	if (!(checkRange(e_vertical_tick, min_vertical_tick_marks, max_vertical_tick_marks, "VerticalTickMarks"))) {
	    
	    success = false;
	}
	if (!(checkRange(preemph_coef, min_preemphasis, max_preemphasis, "Preemphasis"))) {
	    
	    success = false;
	}
	if (!(checkRange(window_duration, min_window_duration, max_window_duration, "WindowDuration"))) {
	    
	    success = false;
	}



	//--------------------------------------------------------------
	//
	// dependency checking
	//
	//--------------------------------------------------------------
	if (maximum_energy < minimum_energy) {
	    System.out.println("The maximum energy can not be less than the minimum energy");
	    JFrame frame = new JFrame("Energy Panel"); 
	    String warningMessage = new 
		String("MaximumEnergy: The maximum energy cannot be less than the minimum energy.");
	    JOptionPane.showMessageDialog(frame, warningMessage, "Energy Panel", JOptionPane.WARNING_MESSAGE);
	    
	    success = false;
	}
	
	// check if the super tick mark is greater than major or minor
	// tick mark
	//
	if (e_super_tick < e_major_tick) {

	    JFrame frame = new JFrame("Energy Panel"); 
	    String warningMessage = new 
		String("SuperTickMark: The super tick cannot be less than the major tick.");
	  JOptionPane.showMessageDialog(frame, warningMessage, "Energy Panel", JOptionPane.WARNING_MESSAGE);

	    success = false;

	}
	    
	if (e_super_tick < e_minor_tick) {

	     JFrame frame = new JFrame("Energy Panel"); 
	    String warningMessage = new 
		String("SuperTickMark: The super tick cannot be less than the minor tick.");
	    JOptionPane.showMessageDialog(frame, warningMessage, "Energy Panel", JOptionPane.WARNING_MESSAGE);

	    success = false;
	}
	
	if (e_major_tick < e_minor_tick) {
	    
	    JFrame frame = new JFrame("Energy Panel"); 
	    String warningMessage = new 
		String("MajorTickMark: The major tick cannot be less than the minor tick.");
	    JOptionPane.showMessageDialog(frame, warningMessage, "Energy Panel", JOptionPane.WARNING_MESSAGE);

	    success = false;
	}

	return success;
    }
}
