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

// import necessary java libraries
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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 configuration panel for waveform.
 */
public class PanelWaveform extends ConfigPanel {

    // the minimum and the maximum values for the waveform parameter
    //
    
    /**
     * lower limit for maximum value.
     */
    public double min_maximum_value = 0;
    
    /**
     * upper limit for maximum value.
     */
    public double max_maximum_value = 32767;
    
    /**
     * lower limit for minimum value.
     */
    public double min_minimum_value = -32767;
    
    /**
     * upper limit for minimum value.
     */
    public double max_minimum_value = 0;
    
    /**
     * lower limit for super tick marks.
     */
    public double min_super_tick_marks = 0;
    
    /**
     * upper limit for super tick marks.
     */
    public double max_super_tick_marks = 10;
    
    /**
     * lower limit for major tick marks.
     */
    public double min_major_tick_marks = 0;
    
     /**
     * upper limit for major tick marks.
     */
    public double max_major_tick_marks = 2.5;
    
    /**
     * lower limit for minor tick marks.
     */
    public double min_minor_tick_marks = 0;
    
     /**
     * upper limit for minor tick marks.
     */
    public double max_minor_tick_marks = 1;
    
    /**
     * upper limit for gain.
     */
    public double max_gain = 10;
    
    /**
     * lower limit for gain.
     */
    public double min_gain = 0;

    /**
     * Constructor for waveform configuration panel.
     */
    public PanelWaveform() {

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

	association_d = "Waveform";
      
	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);

	// 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("Gain");

	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("32767.0");
	values_d.add("-32767.0");
	values_d.add("1.0");
	values_d.add("0.2");
	values_d.add("0.1");
	values_d.add("1.0");


	// minimum value
	//
	min_values_d.add("0");
	min_values_d.add("-32767");
	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("32767");
	max_values_d.add("0");
	max_values_d.add("10");
	max_values_d.add("2.5");
	max_values_d.add("1");
	max_values_d.add("10.0");

	commands_d.add("maximum_waveform_d");
	commands_d.add("minimum_waveform_d");
	commands_d.add("w_super_tick_d");
	commands_d.add("w_major_tick_d");
	commands_d.add("w_minor_tick_d");
	commands_d.add("gain_d");

	for (int i = 0; i < labels_d.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(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 (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, "Waveform Panel", JOptionPane.WARNING_MESSAGE);
		
		return false;
	    }
	}
	
	    else {
		
		if (parameter < min_value || parameter > max_value) {
		    
		    JFrame frame = new JFrame("Waveform Panel"); 
		    String warningMessage = new 
			String(parameter_name + " :" + " Please enter a value between " 
			       + min_value + " and " + max_value + ".");
		    JOptionPane.showMessageDialog(frame, warningMessage, "Waveform 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;
	float maximum_waveform = 0;
	float minimum_waveform= 0;
	float w_super_tick = 0;
	float w_major_tick = 0;
	float w_minor_tick = 0;
	float gain = 0;

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

	    if (cfg_data_d.getName().get(i).equals("maximum_waveform_d")) {
		maximum_waveform = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }

	    else if (cfg_data_d.getName().get(i).equals("minimum_waveform_d")) {
		minimum_waveform = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	
	    }
	    else if (cfg_data_d.getName().get(i).equals("w_super_tick_d")) {
		w_super_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));

	    }
	    else if (cfg_data_d.getName().get(i).equals("w_major_tick_d")) {
		w_major_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("w_minor_tick_d")) {
		w_minor_tick = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }
	    else if (cfg_data_d.getName().get(i).equals("gain_d")) {
		gain = Float.parseFloat((String)(cfg_data_d.getValue().get(i)));
	    }

	    else {
		// do nothing
	    }

	}

	System.out.println("maximum_waveform = " + maximum_waveform);
	System.out.println("minimum_waveform = " + minimum_waveform);	
	System.out.println("w_super_tick = " + w_super_tick);
	System.out.println("w_major_tick = " + w_major_tick);
	System.out.println("w_minor_tick = " + w_minor_tick);
	System.out.println("gain = " + gain);	


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

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

	if (!(checkRange(minimum_waveform, min_minimum_value, max_minimum_value, "MinimumValue"))) {
	    
	    success = false;
	}
	if (!(checkRange(w_super_tick, min_super_tick_marks, max_super_tick_marks, "SuperTickMarks"))) {
	    
	    success = false;
	}
	if (!(checkRange(w_major_tick, min_major_tick_marks, max_major_tick_marks, "MajorTickMarks"))) {
	    
	    success = false;
	}
	if (!(checkRange(w_minor_tick, min_minor_tick_marks, max_minor_tick_marks, "MinorTickMarks"))) {
	    
	    success = false;
	}

	if (!(checkRange(gain, min_gain, max_gain, "Gain"))) {
	    
	    success = false;
	}

		
	
	//--------------------------------------------------------------
	//
	// dependency checking
	//
	//--------------------------------------------------------------
	if (maximum_waveform < minimum_waveform) {
 
	    JFrame frame = new JFrame("Waveform Panel"); 
	    String warningMessage = new 
		String("MaximumWaveform: The maximum waveform cannot be less than the minimum waveform.");
	     JOptionPane.showMessageDialog(frame, warningMessage, "Waveform Panel", JOptionPane.WARNING_MESSAGE);
	    
	    success = false;
	}
	
	// check if the super tick mark is greater than major or minor
	// tick mark
	//
	if (w_super_tick < w_major_tick) {

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

	    success = false;

	}
	    
	if (w_super_tick < w_minor_tick) {

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

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

	    success = false;
	}

	return success;
    }
}
