// file: $ISIP_IFC/class/java/ConfigPanel/PanelSpectrogram.java
// version: $Id: PanelSpectrogram.java 10119 2005-05-23 20:25:32Z stanley $

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

/** 
 * A panel to configure spectrogram parameters
 */
public class PanelSpectrogram extends ConfigPanel {

    //-------------------------------------------------------------------------
    //
    // public constants
    //
    //-------------------------------------------------------------------------

    /**
     * The constant for initialization value
     */
    public static final int SIGNAL_SIZE = 7;

    //------------------------------------------------------------------------
    //
    // protected data
    //
    //-----------------------------------------------------------------------

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

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

    /** 
     * Color map values.
     */
    protected Vector<Object> color_map_values_d = new Vector<Object>(INIT, INC);

    /**
     * Main spectrogram configuration panel.
     */    
    public PanelSpectrogram() {

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

	association_d = "Spectrogram";

	JPanel cfg_panel = new JPanel();

	JPanel cfg_signal_panel = new JPanel();

	JPanel cfg_display_panel = new JPanel();


	//  make this config panel use grid bag layout
	//
	GridBagLayout gridbag = new GridBagLayout();
	cfg_signal_panel.setLayout(gridbag);

	GridBagLayout gridbag2 = new GridBagLayout();
	cfg_display_panel.setLayout(gridbag2);


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

	border.setTitleColor(Color.black);

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

	// create the border and set the proper font
	//
	BorderUIResource.TitledBorderUIResource border2 =
	    new BorderUIResource.TitledBorderUIResource("Spectrogram Display Configuration");

	border2.setTitleColor(Color.black);

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

	fft_type_values_d.add("RAD_2");
	fft_type_values_d.add("RAD_4");

	win_type_values_d.add("Hamming");

	color_map_values_d.add("AI");

	labels_d.add("Preemphasis");
	labels_d.add("Window duration (seconds)");
	labels_d.add("Window Type");
	labels_d.add("FFT num points");
	labels_d.add("FFT type");
	labels_d.add("Minimum Frequency (Hz)");
	labels_d.add("Maximum Frequency (Hz)");

	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 (Hz)");
	labels_d.add("Color Map");
	labels_d.add("Brightness");
	labels_d.add("Contrast");

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

	values_d.add("0.9375");
	values_d.add("0.06");
	values_d.add(win_type_values_d);
	values_d.add("512");
	values_d.add(fft_type_values_d);
	values_d.add("0");
	values_d.add("4000");

	values_d.add("1");
	values_d.add("0.25");
	values_d.add("0.1");
	values_d.add("10");
	values_d.add(color_map_values_d);
	values_d.add("0");
	values_d.add("0");

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

	// maximum value
	//
	max_values_d.add("1.0");
	max_values_d.add("1");
	max_values_d.add("0");
	max_values_d.add("1024");
	max_values_d.add("0");
	max_values_d.add("4000");
	max_values_d.add("4000");

	max_values_d.add("10");
	max_values_d.add("2.5");
	max_values_d.add("1");
	max_values_d.add("1000");
	max_values_d.add("0");
	max_values_d.add("100");
	max_values_d.add("100");

	commands_d.add("preemph_coef_d");
	commands_d.add("window_duration_d");
	commands_d.add("window_type_d");
	commands_d.add("fft_num_d");
	commands_d.add("fft_type_d");
	commands_d.add("min_frequency_d");
	commands_d.add("max_frequency_d");

	commands_d.add("s_super_tick_d");
	commands_d.add("s_major_tick_d");
	commands_d.add("s_minor_tick_d");
	commands_d.add("s_vertical_d");
	commands_d.add("color_map_d");
	commands_d.add("brightness_d");
	commands_d.add("contrast_d");
	
	for (int i = 0; i < SIGNAL_SIZE; i++) {

	    // add a label for all labels_d
	    //
	    String configLabel = (String)labels_d.get(i);
	    JLabel jlabel = new JLabel(configLabel);

	    jlabel.setForeground(Color.black);
	    constrain(cfg_signal_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(cfg_signal_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 integer
	    //
	    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(cfg_signal_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");
	    } 

	    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(cfg_signal_panel, select, gridbag, 10, i,
			  1, 1, 1, 0, GridBagConstraints.EAST);
	
		entered_data_d.add(select);
		block_type_d.add("enum");
	    }

	    if (!types_d.get(i).equals("double") && !types_d.get(i).equals("integer")) {
		// add dummy item to keep track the index
		//
		String x = "NULL";
		inputed_data_d.add(x);
		text_field_d.add(x);
	    }
	}
	

	for (int i = SIGNAL_SIZE; 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(cfg_display_panel, jlabel, gridbag2, 0,  i - 7,
		      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(cfg_display_panel, (FloatTextField)(text_field_d).get(i), gridbag2, 10, i - 7,
			  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(cfg_display_panel, (FloatTextField)(text_field_d).get(i), gridbag2, 10,  i - 7,
			  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(cfg_display_panel, select, gridbag2, 10,  i - 7,
			  1, 1, 1, 0, GridBagConstraints.EAST);
		entered_data_d.add(select);
		block_type_d.add("enum");
	    }

	    if (!types_d.get(i).equals("double") && !types_d.get(i).equals("integer")) {
		// add dummy item to keep track the index
		//
		String x = "NULL";
		inputed_data_d.add(x);
		text_field_d.add(x);
	    }
	}
	
	cfg_panel.setLayout(new BoxLayout(cfg_panel, BoxLayout.Y_AXIS));
	
	JPanel temp_panel = new JPanel();
	cfg_panel.add(cfg_signal_panel);
	
	cfg_panel.add(temp_panel);
	cfg_panel.add(cfg_display_panel);

	add(cfg_panel);
    }


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

	update();

	boolean success = true;

	float preemph_coef = 0;
	float window_duration = 0;
	float fft_num = 0;
	float min_frequency = 0;
	float max_frequency = 0;
	float s_super_tick = 0;
	float s_major_tick = 0;
	float s_minor_tick = 0;
	float s_vertical_tick = 0;
	float brightness = 0;
	float contrast = 0;


	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("fft_num_d")) {
		fft_num = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("min_frequency_d")) {
		min_frequency = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	
	    }
	    else if (cfg_data_d.getName().get(i).equals("max_frequency_d")) {
		max_frequency = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	
	    }
	    else if (cfg_data_d.getName().get(i).equals("s_super_tick_d")) {
		s_super_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));

	    }
	    else if (cfg_data_d.getName().get(i).equals("s_major_tick_d")) {
		s_major_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("s_minor_tick_d")) {
		s_minor_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("s_vertical_tick_d")) {
		s_vertical_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("brightness_d")) {
		brightness = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("contrast_d")) {
	       contrast = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else {
		// do nothing
	    }

	}

	System.out.println("preemph_coef = " + preemph_coef);
	System.out.println("window_duration = " + window_duration);
	System.out.println("fft_num = " + fft_num);
	System.out.println("min_frequency = " + min_frequency);
	System.out.println("max_frequency = " + max_frequency);	
	System.out.println("s_super_tick = " + s_super_tick);
	System.out.println("s_major_tick = " + s_major_tick);
	System.out.println("s_minor_tick = " + s_minor_tick);
	System.out.println("s_vertical = " + s_vertical_tick);	
	System.out.println("brightness = " + brightness);	
	System.out.println("contrast = " + contrast);	


	// start verification.  The maximum energy should be greater
	// than minimum energy
	//
	if (max_frequency < min_frequency) {
	    System.out.println("The maximum energy can not be less than the minimum energy");
	    JFrame frame = new JFrame("Warning"); 
	    String warningMessage = new 
		String("The maximum frequency can not be less than the minimum frequency.");
	    JOptionPane.showMessageDialog(frame,warningMessage);

	    success = false;
	}
	
	// check if the super tick mark is greater than major or minor
	// tick mark
	//
	if (s_super_tick < s_major_tick || 
	    s_super_tick < s_minor_tick ||
	    s_major_tick < s_minor_tick) {

	    JFrame frame = new JFrame("Warning"); 
	    String warningMessage = new 
		String("The super or major tick can not be less than the minor tick.");
	    JOptionPane.showMessageDialog(frame,warningMessage);

	    success = false;
	}
	
	// check if the tick mark is greater than zero
	//
	if (s_super_tick < 0 || s_major_tick < 0 ||
	    s_minor_tick < 0 || window_duration < 0 ||
	    s_vertical_tick < 0 || preemph_coef < 0 ||
	    min_frequency < 0 || max_frequency < 0) {
	    
	    JFrame frame = new JFrame("Warning"); 
	    String warningMessage = new 
		String("Some spectrogram values can not be less than zero.");
	    JOptionPane.showMessageDialog(frame,warningMessage);

	    success = false;

	}

	return success;
    }
}
