// file: DataInput.java
//

// include the necessary system files
//
import java.awt.*;
import java.lang.*;
import javax.swing.*;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Event;
import java.awt.event.*;

// include the necessary user files
//
//import Constants;

// class: DataInput
//
// this class implements a canvas which allows one to enter
// a pole or a zero and causes it to be displayed on a
// pole-zero map
//
public class DataInputPanel extends SubPanel 
    implements ActionListener, Constants {
    
    // set class constants
    //
    
    // constant to define the sleep time of the thread
    // measured in milliseconds
    //
    
    // a default frequency for input
    //
    public final static double DEFAULT_FREQ = Constants.DEFAULT_SAMPLE_FREQ;  
    
    // default text field length for entries
    //
    public final static int TEXT_FIELD_LENGTH = 10;
    
    // number of critical point types (i.e. pole or zero 
    // are the only choices)
    //
    public final static int NUM_CHOICES = 2;  
    
    
    // declare class variables
    //
    
    // declare gui components
    //
    private String freq_string = new String("Frequency"); 
    private String band_string = new String("Bandwidth");
    private String sf_string = new String("Sample Frequency");
    private String range_string = new String("Frequency Range");
    private String dc_gain_string = new String("DC Gain");
    private JTextField entry_text_0, entry_text_1;
    private JLabel band_label, freq_label;
    private JLabel sf_range_label;
    private JLabel dc_gain_label = new JLabel(dc_gain_string); 
    
    private JTextField sf_area = new JTextField(String.valueOf
						(Constants.DEFAULT_SAMPLE_FREQ), 
						TEXT_FIELD_LENGTH);
    private JTextField dc_gain_area = new JTextField(String.valueOf(1), 
						     TEXT_FIELD_LENGTH); 
    
    // set up a thread to take care of updating the display
    //
    
    // variables to reference the display and output panels
    //
    PzmapPanel pz_map;
    
    // flag to determine if a repaint is necessary
    //
    boolean update_flag = false;
    
    // create a variable to hold the design type
    //
    private int design_type = Constants.DEFAULT_DESIGN;
    
    // current type of object being constructed (pole or zero)
    //
    private int current_type = Constants.POLE_INDICATOR;
    
    // declare the default fonts
    //
    Font currentFont = getFont();
    Font newFont = new Font(currentFont.getName(), currentFont.getStyle(), 12);
    
    
    //*****************************************************************
    //
    // constructor methods
    //
    //*****************************************************************
    
    // method: DataInputPanel
    //
    // parameters: none
    // 
    // this method constructs a DataInputPanel of the default type
    //
    public DataInputPanel(PzmapPanel pz_map_a, int type) {
	
	// call the super-class's constructor
	//
	super();
	
	// assign the design type
	//
	design_type = type;
	
	// assign the display and output panels
	//
	pz_map = pz_map_a;
	
	// add the components to the panel
	//
	this.add_components();
	
	// cause manual repaint of the canvas
	//
	update_flag = true;
    }
    
    
    //***************************************************************
    //
    // private methods
    //
    //***************************************************************
    
    
    // method: add_components
    //
    // parameters: none
    // 
    // this method initializes the gui components of the class
    //
    // returns: boolean
    //  
    private boolean add_components() {
	
	// create text input areas
	//
	entry_text_0 = new JTextField(String.valueOf(DEFAULT_FREQ), 
				      TEXT_FIELD_LENGTH);
	entry_text_1 = new JTextField(String.valueOf(DEFAULT_FREQ), 
				      TEXT_FIELD_LENGTH);
	
	
	// add components and labels to this panel while creating constraints 
	// for each
	//
	band_label = new JLabel(band_string);
	band_label.setFont(newFont);
	band_label.setForeground(Color.black);
	
	freq_label = new JLabel(freq_string);
	freq_label.setFont(newFont);
	freq_label.setForeground(Color.black);
	
	dc_gain_label.setFont(newFont);
	dc_gain_label.setForeground(Color.black);
	
	if (design_type == Constants.DIGITAL) {
	    sf_range_label = new JLabel(sf_string);
	    sf_range_label.setFont(newFont);
	    sf_range_label.setForeground(Color.black);
	}
	else {
	    sf_range_label = new JLabel(range_string);
	    sf_range_label.setFont(newFont);
	    sf_range_label.setForeground(Color.black);
	}
	// add action listeners to textfields
	//
	dc_gain_area.addActionListener(this);
	dc_gain_area.setActionCommand("dc_gain");
	sf_area.addActionListener(this);
	sf_area.setActionCommand("sf_area");
	
	// exit gracefully
	//
	return true;
	
    }
    
    //****************************************************************
    //
    // public methods
    //
    //****************************************************************
    
    
    
    // method start
    //
    // parameters: none
    //
    // this method starts or restarts the threaded object
    //
    // returns: none
    //
    public void start () {
	
    }
    
    
    // method stop
    //
    // parameters: none
    //
    // this method stops the threaded object
    //
    // returns: none
    //
    public void stop () {
	
	
	
    }
    
    
    // method: set_design_type 
    //
    // parameters:
    //     integer new_type: (input) design type to change to
    //
    // this is a method to set the design type.  the design type will
    // be set to the value passed in to new_type
    //
    // returns: boolean
    //
    public boolean set_design_type(int new_type) {
	
	// make sure the new_type is valid, then set the design_type
	// variable, and return a boolean value which indicates whether
	// or not the new_type was valid.  note:  this assumes that 
	// the design_type is currently a valid value
	//
	// set the labels for input
	//
	if (new_type == Constants.DIGITAL) {
	    sf_range_label.setText(sf_string);
	}
	else {
	    sf_range_label.setText(range_string);
	}
	
	if (new_type == Constants.DIGITAL) {
	    design_type = new_type;
	    pz_map.set_map_type(design_type);
	    return true;
	}
	else if (new_type == Constants.ANALOG) {
	    update_flag = true;
	    design_type = new_type;
	    pz_map.set_map_type(design_type);
	    return true;
	}
	else {
	    return false;
	}
    }
    
    
    
    
    // method: add_pz
    //
    // parameters: (none)
    //
    // this is a method to add a pole or zero to the appropriate
    // portions of our frame.
    //
    // returns: boolean
    //
    public boolean add_pz() {
	
	// set local variables
	// value0 is either frequency or the real portion
	// and value1 is either bandwidth or the imaginary portion
	//
	double frequency = 0.0;
	double bandwidth = 0.0;
	
	// make sure the values are valid doubles
	//
	
	try {
	    frequency = Double.valueOf(entry_text_0.getText()).doubleValue();
	    bandwidth = Double.valueOf(entry_text_1.getText()).doubleValue();
	} 
	catch(java.lang.NumberFormatException e) {
	    return false;
	}
	
	// add the pole or zero to the pz_map
	// 
	if (current_type == Constants.POLE_INDICATOR) {
	    pz_map.add_pole(frequency, bandwidth);
	}
	else {
	    pz_map.add_zero(frequency, bandwidth);
	}
	
	// exit gracefully
	//
	return true;
    }
    
    public boolean add_pz(double freq , double band_width) {
	
	// set local variables
	// value0 is either frequency or the real portion
	// and value1 is either bandwidth or the imaginary portion
	//
	//double frequency = 0.0;
	//double bandwidth = 0.0;
	
	double frequency;
	double bandwidth;
	
	// make sure the values are valid doubles
	//
	
	frequency = freq;
	bandwidth = band_width;
	//try {
	//  frequency = Double.valueOf(entry_text_0.getText()).doubleValue();
	//  bandwidth = Double.valueOf(entry_text_1.getText()).doubleValue();
	//} 
	//catch(java.lang.NumberFormatException e) {
	//  return false;
	//}
	
	// add the pole or zero to the pz_map
	// 
	if (current_type == Constants.POLE_INDICATOR) {
	    pz_map.add_pole(frequency, bandwidth);
	}
	else {
	    pz_map.add_zero(frequency, bandwidth);
	}
	
	// exit gracefully
	//
	return true;
    }
    
    public void type_ret(int type_pole_zero)  {
	current_type = type_pole_zero;
    }
    
    
    //*****************************************************************
    //
    // event handler methods
    //
    //*****************************************************************
    
    // method: handleEvent
    //
    // parameters: 
    //     Event event: (input) an Event object indicating the event to handle
    //
    // this method handles the various interrupt events that may occur in this
    // component
    //
    // returns: boolean
    //
    //public boolean handleEvent(Event event) {
    
    public void actionPerformed(ActionEvent event) {
	
	// see if a carriage return was pressed in the sample_frequency area
	//
	if (event.getActionCommand().equals("sf_area")) {
	    sf_area.requestFocus();
	    // make sure the value is a valid double
	    //
	    double new_sf = 0.0;
	    try {
		new_sf = Double.valueOf(sf_area.getText()).doubleValue();
	    } 
	    catch(java.lang.NumberFormatException e) {
	    }
	    this.pz_map.set_sample_frequency(new_sf);
	    
	}
	//else if (event.getActionCommand().equals("dc")) {
	//
	else {
	    
	    // make sure the value is a valid double
	    //
	    double new_dc_gain = 0.0;
	    dc_gain_area.requestFocus();
	    try {
		new_dc_gain = Double.valueOf(dc_gain_area.getText()).doubleValue();
	    } 
	    catch(java.lang.NumberFormatException e) {
	    }
	    this.pz_map.set_dc_gain(new_dc_gain);
	    
	}
	
    } // end actionperformed
    
} // end class DataInput
