// file: ComboPanel.java
//

// import java libraries
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
 * Holds labels and combo boxes, an extension of JPanel, an implementation
 * of ActionListener
 */
public class ComboPanel extends JPanel implements ActionListener{


    // ---------------------------------------------------
    //
    // declare class member data
    //
    // ---------------------------------------------------
   
    /**
     * panel for holding the labels and combo boxes
     */
    private static JPanel leftPanel = null;

    /**
     * panel for holding the labels and combo boxes
     */
    private static JPanel rightPanel = null; 

    /**
     * 12 element array of JLabel type to hold the JLabels
     */
    private JLabel labels[] = null;

    /**
     * 12 element array of JComboBox type to hold the combo boxes
     */
    public JComboBox boxes[] = null;
    
    /**
     * NEW_BUG = 1 Used to be compared to mode variable
     */
    public static final int NEW_BUG = 1;

    /**
     * LOAD_BUG = 2 Used to be compared to mode variable
     */
    public static final int LOAD_BUG = 2;

    /**
     * MODIFY = 3 Used to be compared to mode variable
     */
    public static final int MODIFY = 3;

    /**
     * RESOLVE = 4 Used to be compared to mode variable
     */
    public static final int RESOLVE = 4;

    /**
     * CLOSE = 5 Used to be compared to mode variable
     */
    public static final int CLOSE = 5;

    /**
     * ASSIGN = 6 Used to be compared to mode variable
     */
    public static final int ASSIGN = 6;

    /**
     * OPEN = 7 Used to be compared to mode variable
     */
    public static final int OPEN = 7;

    /**
     * vector to hold the values of all the blocks so that the cancel option
     * as well as part of the assign method can work correctly
     */
    int[] oldValues;

    /**
     * variable to hold wether or not the blank line has been removed 
     * from the item
     */
    public boolean[] blankRemoved;

    /**
     * variable to hold wether or not the blank line has been hidden 
     * from the item
     */
    public boolean[] blankHidden;

    /**
     * variable necessary for the configBoxes function
     */
    Vector names = null;

    /**
     * variable necessary for the configBoxes function
     */
    Vector enums = null; 

    /**
     * variable necessary for the configBoxes function
     */
    int mode = 0;
    

    // ---------------------------------------------------
    //
    // declare class constructors
    //
    // ---------------------------------------------------

    /**
     * default class constructor
     *
     * @param name Vector assigned to class Vector names
     * @param enumm Vector assigned to the class Vector enums
     * @param newFont Font to be used
     * @param m int value assigned to the mode variable
     * @param bug Bug value to be initialized
     * @param database VarmintDB database variable used
     */
    public ComboPanel(Vector name, Vector enumm, Font newFont, 
		      int m, Bug bug, VarmintDB database) {
	
	// note that the variable enum was changed to enumm to comply
	// with java 1.5
	//
	names = name;
	enums = enumm;
	mode = m;


	this.setFont(newFont);

	// instantiate the two panels for use
	//
	leftPanel = new JPanel();
	rightPanel = new JPanel();

	// create a border around the two panels
        //
        leftPanel.setBorder(BorderFactory.createEtchedBorder());
	rightPanel.setBorder(BorderFactory.createEtchedBorder());

	// instantiate the labels and combo boxes
	//
	labels = new JLabel[12];
	boxes = new JComboBox[12];
	blankRemoved = new boolean[12];
	blankHidden = new boolean[12];


	// make the panels use grid bag layout
	//
	leftPanel.setLayout(new GridBagLayout());
	leftPanel.setMinimumSize(new Dimension(300, 180));
	leftPanel.setPreferredSize(new Dimension(300, 180));
	leftPanel.setFont(newFont);
	rightPanel.setLayout(new GridBagLayout());
	rightPanel.setMinimumSize(new Dimension(300, 180));
	rightPanel.setPreferredSize(new Dimension(300, 180));
	rightPanel.setFont(newFont);
	
	
	// create all of the labels and boxes
	//
	for (int i=0; i < 12; i++) {
	    blankRemoved[i] = false;
	    blankHidden[i] = false;
   
	    labels[i] = new JLabel((String)(names.get(i)));
	    labels[i].setFont(newFont);
	    boxes[i] = new JComboBox((Vector)(enums.get(i)));
	    
	    boxes[i].setName((String)(names.get(i)));
	    
	    if (mode == LOAD_BUG)
		boxes[i].setEnabled(false);
	    else 
		boxes[i].setEnabled(true);
	    
	    boxes[i].setFont(newFont);

	    if ( i < 6 ) {
		leftPanel.add(labels[i], new 
		    GridBagConstraints(0, i, 1, 1, 1, 0.0,
				       GridBagConstraints.WEST,
				       GridBagConstraints.HORIZONTAL, 
				       new Insets(0, 5, 0, -5),
				       7, 10));


		leftPanel.add(boxes[i], new 
		    GridBagConstraints(1, i, 1, 1, 1, 0.0 ,
				       GridBagConstraints.EAST,
				       GridBagConstraints.HORIZONTAL, 
				       new Insets(0, -5, 0, 5),
				       7,0));
	    }
	    else {
		rightPanel.add(labels[i], new 
		    GridBagConstraints(0, i, 1, 1, 1, 0.0,
				       GridBagConstraints.WEST,
				       GridBagConstraints.HORIZONTAL, 
				       new Insets(0, 5, 0, -5),
				       7, 10));


		rightPanel.add(boxes[i], new 
		    GridBagConstraints(1, i, 1, 1, 1, 0.0 ,
				       GridBagConstraints.EAST,
				       GridBagConstraints.HORIZONTAL, 
				       new Insets(0, -5, 0, 5),
				       7, 0));
	    }
	}

	oldValues = new int[12];

	labels[0].setToolTipText(new String("This is a test.\nWill it include the newline?"));


	String temp = new String("");

	String components = database.
	    getComponent(bug.getField("DBase"));
	
	// create the vector to hold each of the enum values
	//
	Vector componentValues =  new Vector(5,5);
	
	// create a blanks value to be the default value
	//
	componentValues.add("unspecified");
	componentValues.add("software");
	componentValues.add("documentation");
		    
	// tokenize the string containing the values with comma as the
	// delimiter
	//
	StringTokenizer tokenizer = new StringTokenizer(components, ",");
	
	// run through and add each token to the enum_items vector
	//
	while (tokenizer.hasMoreTokens()) {
	    
	    String line = (String)tokenizer.nextToken();
	    componentValues.add(line.trim());
	}
	
	for (int i = boxes[10].getItemCount() -1;
	     i > 0; i--) {
	    boxes[10].removeItemAt(i);
	}
	
	for (int i=0; i < componentValues.size(); i++) {
	    boxes[10].addItem(componentValues.get(i));
	}
	
	blankRemoved[10] = false;

	boxes[10].removeItemAt(0);
	    
	configureBoxes(bug);

	for(int i = 0; i < 12; i++) {
	    if(boxes[i].getSelectedIndex() != 0) {
		boxes[i].removeItem("unspecified");
		blankRemoved[i] = true;
	    }
	}

	
	// set the layout of this panel
	//
	GridBagLayout gridbag = new GridBagLayout();
	this.setLayout(gridbag);	
	GridBagConstraints c = new GridBagConstraints();

	// add the left panel into the ComboPanel
	//
	c.gridx = 0;
	c.gridy = 0;
	c.gridwidth = 1;
	c.gridheight = GridBagConstraints.REMAINDER;
	c.weightx = 1;
	c.weighty = 1;
	c.anchor = GridBagConstraints.NORTHWEST;
	c.fill = GridBagConstraints.BOTH;
	c.insets = new Insets(0,0,0,0);
	gridbag.setConstraints(leftPanel, c);
	this.add(leftPanel);

	// add the right panel into the ComboPanel
	//
	c.gridx = 1;
	c.gridy = 0;
	c.gridwidth = 1;
	c.gridheight = GridBagConstraints.REMAINDER;
	c.weightx = 1;
	c.weighty = 1;
	c.anchor = GridBagConstraints.NORTHWEST;
	c.fill = GridBagConstraints.BOTH;
	c.insets = new Insets(0,0,0,0);
	gridbag.setConstraints(rightPanel, c);
	this.add(rightPanel);

    }

    /**
     * listens for actions taking place on the components,
     * implementation is blank
     * @param e event that was fired
     */
    public void actionPerformed(ActionEvent e) {

    }

    /**
     * Configures boxes with the Bug value passed
     * @param bug Bug value to be configured
     */
    public void configureBoxes(Bug bug) {
	oldValues[0] = (((Vector)enums.get(0)).indexOf(bug.getField("Owner")));
	if (oldValues[0] == -1)
	    oldValues[0] = 0;
	boxes[0].setSelectedIndex(oldValues[0]);
	
	oldValues[1] = (((Vector)enums.get(1)).indexOf(bug.getField("DBase")));
	if (oldValues[1] == -1)
	    oldValues[1] = 0;
	boxes[1].setSelectedIndex(oldValues[1]);

	oldValues[2] = (((Vector)enums.get(2)).indexOf(bug.getField("Class")));
	if (oldValues[2] == -1)
	    oldValues[2] = 0;
	boxes[2].setSelectedIndex(oldValues[2]);

	oldValues[3] = (((Vector)enums.get(3)).
			indexOf(bug.getField("Priority")));
	if (oldValues[3] == -1)
	    oldValues[3] = 0;
	boxes[3].setSelectedIndex(oldValues[3]);


	oldValues[4] = (((Vector)enums.get(4)).
			indexOf(bug.getField("Severity")));
	if (oldValues[4] == -1)
	    oldValues[4] = 0;
	boxes[4].setSelectedIndex(oldValues[4]);


	oldValues[5] = (((Vector)enums.get(5)).
			indexOf(bug.getField("Due")));
	if (oldValues[5] == -1)
	    oldValues[5] = 0;
	boxes[5].setSelectedIndex(oldValues[5]);

	oldValues[6] = (((Vector)enums.get(6)).
			indexOf(bug.getField("Resolution")));
	if (oldValues[6] == -1)
	    oldValues[6] = 0;
	boxes[6].setSelectedIndex(oldValues[6]);

	oldValues[7] = (((Vector)enums.get(7)).
			indexOf(bug.getField("Inspected")));
	if (oldValues[7] == -1)
	    oldValues[7] = 0;
	boxes[7].setSelectedIndex(oldValues[7]);

	oldValues[8] = (((Vector)enums.get(8)).
			indexOf(bug.getField("Environment")));
	if (oldValues[8] == -1)
	    oldValues[8] = 0;
	boxes[8].setSelectedIndex(oldValues[8]);

	oldValues[9] = (((Vector)enums.get(9)).
			indexOf(bug.getField("TestCase")));
	if (oldValues[9] == -1)
	    oldValues[9] = 0;
	boxes[9].setSelectedIndex(oldValues[9]);

	oldValues[10] = (((Vector)enums.get(10)).
			indexOf(bug.getField("Component")));
	if (oldValues[10] == -1)
	    oldValues[10] = 0;
	boxes[10].setSelectedIndex(oldValues[10]);

	oldValues[11] = (((Vector)enums.get(11)).
			indexOf(bug.getField("ReviewDate")));
	if (oldValues[11] == -1)
	    oldValues[11] = 0;
	boxes[11].setSelectedIndex(oldValues[11]);

    }
}







