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

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

/**
 * A panel to config users input through scroll bar, input text field,
 * checkbox or combobox.
 */
public class ConfigPanel extends JPanel {

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

    /**
     * The constant for initialization value
     */
    public static final int INIT = 100;
    
    /**
     * The constant for incremental value
     */
    public static final int INC = 50;

    /**
     * The default width value for text field size
     */
    public static final int WIDTH = 200;

    /**
     * The default height value for text field size
     */
    public static final int HEIGHT = 26;


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

    /**
     * TextField holder.
     */
    protected Vector<Object> text_field_d = new Vector<Object>(INIT, INC);

    /**
     * Data labels.
     */
    protected Vector<Object> labels_d = new Vector<Object>(INIT, INC);

    /**
     * Data types.
     */
    protected Vector<Object> types_d = new Vector<Object>(INIT, INC);

    /**
     * Data values.
     */
    protected Vector<Object> values_d = new Vector<Object>(INIT, INC);

    /**
     * Action commands related to the data.
     */
    protected Vector<Object> commands_d = new Vector<Object>(INIT, INC);

    /**
     * Maximum values for the data.
     */
    protected Vector<Object> max_values_d = new Vector<Object>(INIT, INC);

    /**
     * Minimum values for the data.
     */
    protected Vector<Object> min_values_d = new Vector<Object>(INIT, INC);

    /**
     * Default values for the data.
     */
    protected Vector<Object> def_values_d = new Vector<Object>(INIT, INC);

    /**
     * Inputed data from users.
     */
    protected Vector<Object> inputed_data_d = new Vector<Object>(INIT, INC);

    /**
     * Scale values.
     */
    protected Vector<Object> scale_values_d = new Vector<Object>(INIT, INC);

    /**
     * Byte order values.
     */
    protected Vector<Object> byte_order_values_d = new Vector<Object>(INIT, INC);

    /**
     * Encoding values.
     */
    protected Vector<Object> encoding_values_d = new Vector<Object>(INIT, INC);

    /**
     * Encoding values.
     */
    protected Vector<Object> file_format_d = new Vector<Object>(INIT, INC);

    /**
     * Encoding values.
     */
    protected Vector<Object> playback_mode_d = new Vector<Object>(INIT, INC);

    /**
     * Entered data by users.
     */
    protected Vector<Object> entered_data_d = new Vector<Object>(INIT, INC);

    /**
     * Block type for the data.
     */
    protected Vector<Object> block_type_d = new Vector<Object>(INIT, INC);

    /**
     * Data field to hold the inputed data
     */
    protected ConfigData cfg_data_d = new ConfigData();

    /**
     * Association of this data.
     */
    protected String association_d;

    //-------------------------------------------------------------------------
    //
    // public methods
    //
    //-------------------------------------------------------------------------
       
    /**
     * Constructor, use the default data format.
     */     
    public ConfigPanel() {

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

    /**
     * Paint methods.
     *
     */
    public void paintComponent(Graphics g) {
	super.paintComponent(g);
	
    }
    
    /**
     * Method to update the data.
     *
     * @return  a boolean value indicating status.
     */
    public boolean update() {
	
	cfg_data_d.init();

	for (int i = 0; i < labels_d.size(); i++) {
	    
	    cfg_data_d.getAssociation().add(association_d);
	    
	    cfg_data_d.getName().add(commands_d.get(i));

	    if (block_type_d.get(i).equals("boolean")) {
		
		boolean status = ((JCheckBox)entered_data_d.get(i)).isSelected();
		String value = new String();
		if (status) {
		    value = "true";
		}
		else {
		    value = "false";
		}
		
		cfg_data_d.getValue().add(value);
	    }

	    
	    // code to use if the data is an enumerated type
	    //
	    else if (block_type_d.get(i).equals("enum")) {
		
		cfg_data_d.getValue().add((String)((JComboBox)entered_data_d.get(i)).getSelectedItem());
	    }
	    
	    else if (block_type_d.get(i).equals("double")) {
		
		cfg_data_d.getValue().add(String.valueOf(((FloatTextField)text_field_d.get(i)).getValue()));
		
	    }
	    
	    else if (block_type_d.get(i).equals("integer")) {
		
		cfg_data_d.getValue().add(String.valueOf(((FloatTextField)text_field_d.get(i)).getValue()));
		
	    }
	    
	    cfg_data_d.getType().add(block_type_d.get(i));
	}

	// exit gracefully
	//
	return true;

    }


    /**
     * Method to reset the data to default value.
     *
     * @return  a boolean value indicating status.
     */
    public boolean defaultSetting() {
	
	for (int i = 0; i < labels_d.size(); i++) {
	    
	    if (block_type_d.get(i).equals("boolean")) {

		// get the value for the check box
		//
		String value = (String)(values_d.get(i));
		
		boolean value2;
		if (value.equals("true"))
		    value2 = true;
		else
		    value2 = false;

		// actually make the change to the check box
		//
		JCheckBox checkBox = 
		    (JCheckBox)entered_data_d.get(i); 
		checkBox.setSelected(value2);
	    }

	    
	    // code to use if the data is an enumerated type
	    //
	    else if (block_type_d.get(i).equals("enum")) {

		Vector enum_values = (Vector)values_d.get(i);
		    
		// actually change the enum to the default setting
		//
		JComboBox select = (JComboBox)entered_data_d.get(i);
		select.setSelectedItem((String)enum_values.get(0));
	    }
	    
	    else if (block_type_d.get(i).equals("double")) {

		((FloatTextField)text_field_d.get(i)).setText((String)inputed_data_d.get(i));
		
	    }
	    
	    else if (block_type_d.get(i).equals("integer")) {

		((FloatTextField)text_field_d.get(i)).setText((String)inputed_data_d.get(i));
	    }

	    update();
	    repaint();
	}
	printValue();

	// exit gracefully
	//
	return true;
    }


    /**
     * Print the current values for parameters
     *
     * @return  a boolean value indicating status.
     */
    public boolean printValue() {

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

	    System.out.println("ith item=" + i);
	    System.out.println(cfg_data_d.getAssociation().get(i));
	    System.out.println(cfg_data_d.getName().get(i));
	    System.out.println(cfg_data_d.getType().get(i));
	    System.out.println(cfg_data_d.getValue().get(i));
	}

	return true;
    }

    /**
     * Get the data
     *
     * @return a ConfigData object representing the input data by
     * users.
     */
    public ConfigData getData() {
	return cfg_data_d;
    }

    /**
     * Places all the proper input items onto the config panel for
     * configuring any item.
     *
     * @param container_a a panel being added to.
     * @param button_a what is being added to the panel.
     * @param gridbag_a gridbag layout for config panel.
     * @param gridx_a the x grid position of the constraint.
     * @param gridy_a the y grid position of the constraint.
     * @param gridwidth_a the number of gridspaces wide to constrain.
     * @param gridheight_a the number of gridspaces tall to constrain.
     * @param weightx_a the horizontal weighting of the constraint.
     * @param weighty_a the vertical weighting of the constraint.
     * @param anchor_a which side of the constraint to anchor to.
     *
     * @return  a boolean value indicating status.
     *
     */
    public boolean constrain(JPanel container_a, Component button_a, 
			  GridBagLayout gridbag_a, 
			  int gridx_a, int gridy_a, 
			  int gridwidth_a, int gridheight_a,
			  int weightx_a, int weighty_a, 
			  int anchor_a) {

	GridBagConstraints c = new GridBagConstraints();
	c.gridx = gridx_a;
	c.gridy = gridy_a;
	c.gridwidth = gridwidth_a;
	c.gridheight = gridheight_a;
	c.weightx = weightx_a;
	c.weighty = weighty_a;
	c.anchor = anchor_a;
	gridbag_a.setConstraints(button_a, c);
	container_a.add(button_a);

	// exit gracefully
	//
	return true;
    }
    
}

