// file: $ISIP_IFC/class/java/ConfigPanel/PanelDisplay.java
// version: $Id: PanelDisplay.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.*;
import java.text.DecimalFormat;

/** 
 * A Panel to display the percentage for energy, waveform ans
 * spectrogram display
 */
public class PanelDisplay  extends ConfigPanel {

    // declare the public constants
    //
    /**
     * lower limit for energy.
     */
    public double min_energy = 0;
    
    /**
     * upper limit for energy.
     */
    public double max_energy = 100;
    
     /**
     * lower limit for waveform.
     */
    public double min_waveform = 0;
    
    /**
     * upper limit for waveform.
     */
    public double max_waveform = 100;
    
     /**
     * lower limit for spectrogram.
     */
    public double min_spectrogram = 0;
    
    /**
     * upper limit for waveform.
     */
    public double max_spectrogram = 100;

    /**
     * Main display configuration panel.
     */  
    public PanelDisplay() {

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

	association_d = "Display";
      
	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("Display 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("Energy (%)");
	labels_d.add("Waveform (%)");
	labels_d.add("Spectrogram (%)");

	types_d.add("double");
	types_d.add("double");
	types_d.add("double");	

	values_d.add("50.0");
	values_d.add("50.0");
	values_d.add("0");

	// minimum value
	//
	min_values_d.add("0");
	min_values_d.add("0");
	min_values_d.add("0");

	// maximum value
	//
	max_values_d.add("100");
	max_values_d.add("100");
	max_values_d.add("100");

	commands_d.add("energy_percentage_d");
	commands_d.add("waveform_percentage_d");
	commands_d.add("spectrogram_percentage_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
	    //
	    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");
	    }
 
	    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);
	    }

	}

	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 (parameter < min_value || parameter > max_value) {
	    
	    JFrame frame = new JFrame("Display Panel"); 
	    String warningMessage = new 
		String(parameter_name + " :" + " Please enter a value between " + min_value + " and " + max_value + ".");
	     JOptionPane.showMessageDialog(frame, warningMessage, "Display 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 parsed values
	// 
	boolean success = true;
	float energy_percentage = 0;
	float waveform_percentage = 0;
	float spectrogram_percentage = 0;

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

	    if (cfg_data_d.getName().get(i).equals("energy_percentage_d")) {
		energy_percentage = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("waveform_percentage_d")) {
		waveform_percentage = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	
	    }
	    else if (cfg_data_d.getName().get(i).equals("spectrogram_percentage_d")) {
		spectrogram_percentage = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }

	    else {
		// do nothing
	    }

	}

	System.out.println("energy_percentage = " + energy_percentage);
	System.out.println("waveform_percentage = " + waveform_percentage); 
	System.out.println("spectrogram_percentage = " + spectrogram_percentage);

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

	if (!(checkRange((double)waveform_percentage, min_waveform, max_waveform, "WaveformPercentage"))) {
	    
	    success = false;
	}

	if (!(checkRange((double)energy_percentage, min_energy, max_energy, "EnergyPercentage"))) {
	    
	    success = false;
	}


	//--------------------------------------------------------------
	//
	// dependency checking
	//
	//--------------------------------------------------------------

	if (energy_percentage + 
	    waveform_percentage + 
	    spectrogram_percentage != 100) {

	    JFrame frame = new JFrame("Display Panel"); 
	    String warningMessage = new 
		String("The percentages should sum up to 100.");
	     JOptionPane.showMessageDialog(frame, warningMessage, "Display Panel", JOptionPane.WARNING_MESSAGE);

	    success = false;
	}

	
	if (spectrogram_percentage != 0) {
	    
	    JFrame frame = new JFrame("Display Panel"); 
	    String warningMessage = new 
		String("Spectrogram: Spectrogram display is not yet functional. Set spectrogram percentage to zero.");
	     JOptionPane.showMessageDialog(frame, warningMessage, "Display Panel", JOptionPane.WARNING_MESSAGE);
	    
	    success = false;
	}

	return success;
    }
}