// file: TextTree.java
//

// import java libraries
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.tree.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.text.*;

/**
 * BugPanel as an extension of the JFrame class and implements many listeners
 * 
 */
public class BugPanel extends JFrame 
    implements ActionListener,ItemListener, KeyListener, MouseListener{

    // declare global variables
    //
    
    /**
     * the tree that will keep up with all the descriptions related to
     * the actions performed on the bug
     */
    public static TreeBox  treeBox = null;
    
    /**
     * this panel will hold all the major components of this panel
     * for later adding
     */
    private static JPanel mainPanel = null;

    /**
     * this panel will hold all the secondary components of this panel
     * for later adding
     */
    private static JPanel secondaryPanel = null;

    /**
     * panel that contains all of the combo boxes
     */
    public static ComboPanel comboPanel = null;

    /**
     * panel containing all of the buttons 
     */
    private static ButtonPanel buttonPanel = null;
    
    /**
     * string to hold the titles to the nodes that are created
     */
    private String newTitle = null;

    /**
     * box for editing the title
     */
    JTextField titleBox = null;

    /**
     * create an instance of the VarmintDB Class
     */
    VarmintDB database = null;

    /**
     * create a local copy of the font that is being used
     */
    Font newFont = null;

    /**
     * Instance of the enums vector
     */
    Vector enums = 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;
    
    private JLabel bugLabel = null;

    /**
     * create a main bug for use in this class
     */
    Bug bug = null;

    /**
     * create a global value that will tell the panel what mode it is in
     */
    int mode = 0;

    /**
     * allow the userName to be accessible all over the class
     */
    String userName;

    /**
     * this will hold the old title of the bug so cancels can occur
     */
    String oldTitle;


    /**
     * object giving access to the system clipboard
     */
    StringSelection stringSelection =  null;
    // RJDAPPLET   Clipboard clipboard = null; 
	
    // RJDAPPLET JFileChooser fileChooser = null;
    // RJDAPPLET File insertFile = null;

    /**
     * flag telling if the bug is currently in edit mode
     */
    boolean isBeingEdited;


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

    
    /**
     * default class constructor
     *
     * @param font Font variable to use
     * @param bugNumber String variable to keep track of the bugNumber
     * @param m int variable to be assigned to the class mode variable
     * @param b Bug variable to be assigned to the class bug variable
     * @param varmintdb VarmintDB variable to be assigned to the class
     *                  database variable
     * @param user String variable to be assigned to the userName variable
     */
    public BugPanel(Font font, String bugNumber, int m, Bug b, 
		    VarmintDB varmintdb, String user) {

	addKeyListener(this);

	bug = b;
	mode = m;

	userName = user;

	// get the database for this bug
	//
	String db = bug.getField("DBase");
	
	// instantiate the ClipBoard object
	//
	// RJDAPPLET clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 
	stringSelection = new StringSelection(""); 
	
	// get the passed in font
	//
	newFont = font;
	
	// instantiate the database connection
	//
	database = varmintdb;
	
	// instantiate the main panel
	//
	mainPanel = new JPanel();
	secondaryPanel = new JPanel();

	// set this main panel to use a border layout
	//
	mainPanel.setLayout(new BorderLayout());
	secondaryPanel.setLayout(new BorderLayout());

	// set the border of the main panel
	//
        secondaryPanel.setBorder(BorderFactory.createEtchedBorder());

	// create a tree to mount in our panel
	//
	treeBox  = new TreeBox(userName, newFont, mode, bug);
	
	// get the config values from the database
	//
	Vector configVector = null;
	if (db.equals("")){
	    //	    System.out.println("I'm not looking");
	    configVector = database.getConfig();
	    
	}
	else{
	    //System.out.println("I'm looking");
	    configVector = database.getConfig(db);
	}

	// create the comboPanel to mount in the main panel
	//

	Config config = new Config();
	Vector names = config.create_names();
	enums = config.create_enums(configVector);
	comboPanel = new ComboPanel(names, enums, newFont, mode, bug, 
				    database);
	if (mode == NEW_BUG) {
	    comboPanel.boxes[6].setEnabled(false);
	    comboPanel.boxes[7].setEnabled(false);
	    comboPanel.boxes[9].setEnabled(false);
	    comboPanel.boxes[11].setEnabled(false);

	    comboPanel.boxes[0].
		setSelectedItem(database.getDefault("Owner"));
	    comboPanel.boxes[1].
		setSelectedItem(database.getDefault("DBase"));
	    comboPanel.boxes[2].
		setSelectedItem(database.getDefault("Class"));
	    comboPanel.boxes[3].
		setSelectedItem(database.getDefault("Priority"));
	    comboPanel.boxes[4].
		setSelectedItem(database.getDefault("Severity"));
	    comboPanel.boxes[5].
		setSelectedItem(database.getDefault("Due"));
	    comboPanel.boxes[6].
		setSelectedItem(database.getDefault("Resolution"));
	    comboPanel.boxes[7].
		setSelectedItem(database.getDefault("Inspected"));
	    comboPanel.boxes[8].
		setSelectedItem(database.getDefault("Environment"));
	    comboPanel.boxes[9].
		setSelectedItem(database.getDefault("TestCase"));
	    comboPanel.boxes[11].
		setSelectedItem(database.getDefault("ReviewDate"));

	    
	    for (int i = 0; i < 12; i++) {
		if (!comboPanel.boxes[i].getSelectedItem().equals("unspecified")) {
		    comboPanel.boxes[i].removeItemAt(0);
		    comboPanel.blankRemoved[i] = true;
		}
	    }
	}
	
		
	for (int i = 0; i < 12; i++) {
	    
	    comboPanel.boxes[i].addItemListener(this);
	}
	

	// create the button panel to add to the main panel
	//
	buttonPanel = new ButtonPanel(this);

	if (mode == NEW_BUG) {
	    // change the refresh button over to a Cancel button
	    //
	    buttonPanel.buttons[4].setText("Cancel");
	    buttonPanel.buttons[4].setActionCommand("Cancel");
	    
	    // disable all the buttons that should not be used
	    //
	    buttonPanel.buttons[0].setEnabled(false);
	    buttonPanel.buttons[1].setEnabled(false); 
	    buttonPanel.buttons[2].setEnabled(false);
	    buttonPanel.buttons[3].setEnabled(false);
	} 
	
	else if ((mode == LOAD_BUG) && (bug.getField("Status").
					toLowerCase().equals("resolved"))) {

	    buttonPanel.buttons[1].setText("Open");
	    buttonPanel.buttons[1].setActionCommand("Open");
	    
	    buttonPanel.buttons[2].setEnabled(true);
	    buttonPanel.buttons[5].setEnabled(false);
	}

	else if ((mode == LOAD_BUG) && (bug.getField("Status").
					toLowerCase().equals("closed"))) {

	    buttonPanel.buttons[1].setText("Open");
	    buttonPanel.buttons[1].setActionCommand("Open");
	    
	    buttonPanel.buttons[2].setEnabled(false);
	    buttonPanel.buttons[5].setEnabled(false);
	}

	else if ((mode == LOAD_BUG) && (bug.getField("Status").
					toLowerCase().equals("open"))) {

	    buttonPanel.buttons[1].setText("Resolve");
	    buttonPanel.buttons[1].setActionCommand("Resolve");
	    
	    buttonPanel.buttons[2].setEnabled(false);
	    buttonPanel.buttons[5].setEnabled(false);
	}

	// create the menu 
	//
	BugMenu bugMenu = new BugMenu(this);
	bugMenu.setMinimumSize(new Dimension(100,35));

	// create the text box to hold the bug's title
	//
	titleBox = new JTextField(bug.getField("Title"));
	titleBox.setColumns(35);
	titleBox.setFont(newFont);
	
	if (mode == LOAD_BUG) 
	    titleBox.setEnabled(false);
	else
	    titleBox.setEnabled(true);
	
	
	bugLabel = new JLabel(bugNumber + " (" + 
				     bug.getField("Status") + ")");
	bugLabel.setFont(newFont);

	JLabel titleLabel = new JLabel("Title:");
	titleLabel.setFont(newFont);

	// add all of the components to the secondary panel
	// using a GridBagLayout
	//
	GridBagLayout gridbag = new GridBagLayout();
	this.getContentPane().setLayout(gridbag);	
	GridBagConstraints c = new GridBagConstraints();


	c.gridx = 0;
	c.gridy = 0;
	c.gridwidth = GridBagConstraints.REMAINDER;
	c.gridheight = 1;
	c.weightx = 1;
	c.weighty = 0;
	c.anchor = GridBagConstraints.NORTHWEST;
	c.fill = GridBagConstraints.HORIZONTAL;
	c.insets = new Insets(0,0,0,0);
	gridbag.setConstraints(bugMenu, c);
	this.getContentPane().add(bugMenu);


	c.gridx = 0;
	c.gridy = 1;
	c.gridwidth = 1;
	c.gridheight = 1;
	c.weightx = 0;
	c.weighty = 0;
	c.anchor = GridBagConstraints.NORTHWEST;
	c.fill = GridBagConstraints.NONE;
	c.insets = new Insets(2,5,2,0);
	gridbag.setConstraints(bugLabel, c);
	this.getContentPane().add(bugLabel);


	c.gridx = 1;
	c.gridy = 1;
	c.gridwidth = 1;
	c.gridheight = 1;
	c.weightx = 1;
	c.weighty = 0;
	c.anchor = GridBagConstraints.CENTER;
	c.fill = GridBagConstraints.NONE;
	c.insets = new Insets(2,35,2,0);
	gridbag.setConstraints(titleLabel, c);
	this.getContentPane().add(titleLabel);



	c.gridx = 2;
	c.gridy = 1;
	c.gridwidth = GridBagConstraints.REMAINDER;
	c.gridheight = 1;
	c.weightx = 1;
	c.weighty = 0;
	c.anchor = GridBagConstraints.CENTER;
	c.fill = GridBagConstraints.HORIZONTAL;
	c.insets = new Insets(2,5,2,5);
	gridbag.setConstraints(titleBox, c);
	this.getContentPane().add(titleBox);

	c.gridx = 0;
	c.gridy = 2;
	c.gridwidth = GridBagConstraints.REMAINDER;
	c.gridheight = 1;
	c.weightx = 1;
	c.weighty = 0;
	c.anchor = GridBagConstraints.NORTHWEST;
	c.fill = GridBagConstraints.HORIZONTAL;
	c.insets = new Insets(0,0,0,0);
	gridbag.setConstraints(comboPanel, c);
	this.getContentPane().add(comboPanel);

	c.gridx = 0;
	c.gridy = 3;
	c.gridwidth = GridBagConstraints.REMAINDER;
	c.gridheight = 1;
	c.weightx = 1;
	c.weighty = 0;
	c.anchor = GridBagConstraints.NORTHWEST;
	c.fill = GridBagConstraints.HORIZONTAL;
	c.insets = new Insets(0,0,0,0);
	gridbag.setConstraints(buttonPanel, c);
	this.getContentPane().add(buttonPanel);


	c.gridx = 0;
	c.gridy = 4;
	c.gridwidth = GridBagConstraints.REMAINDER;
	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(treeBox, c);
	this.getContentPane().add(treeBox);


	newTitle = new String("");

	treeBox.treeUpdate.description_entry.addMouseListener(this);
	treeBox.description_entry.addMouseListener(this);
	titleBox.addMouseListener(this);

	// RJDAPPLET fileChooser = new JFileChooser();

    }


    //-----------------------------------------------------------------
    //
    //   Utitility Members
    //
    //-----------------------------------------------------------------

    /*    private void importFile(JTextComponent target) {

	// create the string that will fill the approve button
	//
	String loadApprove = new String("Insert File");
	fileChooser.setApproveButtonText(loadApprove);
	
	// make the File Chooser rescan the current directory
	//
	fileChooser.rescanCurrentDirectory();
	
	// display the file chooser object
	//
	int returnVal = fileChooser.showOpenDialog(BugPanel.this);
	
	// check to see if a file was selected by the user
	//
	if (returnVal == JFileChooser.APPROVE_OPTION) {
	    
	    // retrieve the selected file
	    //
	    insertFile = fileChooser.getSelectedFile();
	    
	    // read the query file
	    //
	    target.setText(target.getText() + readFile());
	    
	} 
    }

    private String readFile() {
	
	String returnValue = new String("");
	String line = new String("");
	
	// open the selected file for reading
	//
	try {
	    
	    // initialize the file output stream
	    //
	    FileReader fin = new FileReader(insertFile);
	    BufferedReader file = new BufferedReader(fin);
	    
	    // read the lines form the file
	    //
	    while ((line = file.readLine()) != null) {
		returnValue = returnValue + line + "\n";
	    }
	}

	// catch and deal with any file I/O exceptions
	//
	catch(IOException e1) {}

	return returnValue;
    }
    */
    /**
     * Adds a node to the tree
     *
     * @param treebox TreeBox variable to be added to tree
     * @param line String to be used as text for the node
     * @param mode int that tells what mode to use
     */
    private void addNode(TreeBox treebox, String line, int mode) {
	
	// create nodes to be used for the descriptor (category) and for 
	// the full text(leaf).
	//
	DefaultMutableTreeNode category = null;
	DefaultMutableTreeNode leaf = null;

	DefaultMutableTreeNode category2 = null;
	DefaultMutableTreeNode leaf2 = null;

	
	// tokenizes the string using \n as the delimiter, but keeping the
	// newlines so that any blank lines can be detected
	//
	StringTokenizer tokenizer = new StringTokenizer(line, "\n", true);
	
	// tells the numbers of leafs that have been added
	//
	int count = 0;

	// used to tell if a blank line in necessary if the current line is 
	// a new line character
	//
	boolean blank = false;
	boolean previous = true;

	// make the title the first line of the edit pane
	//
	category = new DefaultMutableTreeNode(newTitle);
	category2 = new DefaultMutableTreeNode(newTitle);
	
	// add the text that is supposed to be added
	//
	treeBox.treeUpdate.treeModel.insertNodeInto
	    (category, treeBox.treeUpdate.top, 
	     treeBox.treeUpdate.total_entries);
	
	treeBox.treePanel.treeModel.insertNodeInto
	    (category2, treeBox.treePanel.top, 
	     treeBox.treePanel.total_entries); 
	
	// increment the total number of entries
	//
	treeBox.treeUpdate.total_entries++;
	treeBox.treePanel.total_entries++;
			

	// run through the string until the tokenizer runs out of tokens
	//
	while (tokenizer.hasMoreTokens()) {
	   
	    // read the next token and assign it to line
	    //
	    line = tokenizer.nextToken();
	    

	    // if the lines is a newline character and the previosu lines
	    // was also a new line, allow the \n character to be added
	    // to the tree by setting blank to true
	    //
	    if ( (line.equals("\n")) && 
		 (previous == true)) {
		blank = true;
	    }

	    // otherwise, do not allow a blank line to be added
      	    else {
		blank = false;
	    }

	    // sets a mark so on the next term, the program will now if this
	    // one was a new line
	    //
	    if (line.equals("\n")) {
		previous = true;
	    }
	    else {
		previous = false;
	    }

	    // if the line is not a newline character or if it is, but the 
	    // blank flag has been set to true, then add the line as 
	    // a leaf node
	    //
	    if ( !(line.equals("\n")) || (blank == true)) {
		// creates the node based on the line of text sent in
		//
		leaf = new DefaultMutableTreeNode(line);
		leaf2 = new DefaultMutableTreeNode(line);
		
		// adds the node as a leaf at the point specified by
		// count
		//
		treeBox.treePanel.treeModel.
		    insertNodeInto(leaf2, category2, count);
		treeBox.treeUpdate.treeModel.
		    insertNodeInto(leaf, category, count);
		
		// increment count so the next line will occur after this one
		//
		count++;
	    }
	}
    }



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

	if (e.getActionCommand().equals("Modify")) {
	
	    if (
		database.lockBug(bug.getField("DBase"),
				 bug.getField("BugNumber"),
				 userName)) {
		isBeingEdited = true;
		titleBox.setEnabled(true);
		actionRefresh();
		actionModify();	
	    }
	}
	
	else if (e.getActionCommand().equals("Refresh")) {

	    titleBox.setEnabled(true);
	    actionRefresh();
	}

	else if (e.getActionCommand().equals("Resolve")) {

	
	    if (
		database.lockBug(bug.getField("DBase"),
				 bug.getField("BugNumber"),
				 userName)) {
		isBeingEdited = true;
		titleBox.setEnabled(true);
		actionRefresh();
		actionResolve();
	    }
	    
	}
	
    
	else if (e.getActionCommand().equals("Close")) {

	    

	    if (
		database.lockBug(bug.getField("DBase"),
				 bug.getField("BugNumber"),
				 userName)) {
		isBeingEdited = true;
		titleBox.setEnabled(true);
		actionRefresh();
		actionClose();
	    }

	}
	

	else if (e.getActionCommand().equals("Assign")) {
	    if (
		database.lockBug(bug.getField("DBase"),
				 bug.getField("BugNumber"),
				 userName)) {
		isBeingEdited = true;
		titleBox.setEnabled(true);
		actionRefresh();
		actionAssign();
	    }
	}
	

	else if (e.getActionCommand().equals("Open")) {

	    if (
		database.lockBug(bug.getField("DBase"),
				 bug.getField("BugNumber"),
				 userName)) {
		isBeingEdited = true;
		titleBox.setEnabled(true);
		actionRefresh();
		actionOpen();
	    }
	}
	
	else if (e.getActionCommand().equals("Cancel")) {
	    
	    titleBox.setEnabled(false);
	    actionCancel();
	}
	
	else if (e.getActionCommand().equals("Save")) {


	    actionSave();
    
	}
    
    
	// if the user chooses to close the bug window
	//
	else if (e.getActionCommand().equals("Close Bug Window")) {

	    if (isBeingEdited) {

		JFrame frame = new JFrame("Warning");
		int response = 
		    JOptionPane.
		    showConfirmDialog(frame,
				      "Save Changes?",
				      "Save Confirmation",
				      JOptionPane.YES_NO_CANCEL_OPTION, 
				      JOptionPane.WARNING_MESSAGE);
		
		if (response == 1) {
		    actionCancel();
		    this.dispose();
		    
		}
		
		else if (response == 0) {
		    actionSave();
		    this.dispose();
		} 
	    }
	    else 
		this.dispose();
	}
    }

    //--------------------------------------------------------------------
    //
    //  Item Change Listeners
    //
    //--------------------------------------------------------------------

    /**
     * Item change listener
     * @param e ItemEvent that occured
     */
    public void itemStateChanged(ItemEvent e) {
	//System.out.println(e.getItemSelectable());
	JComboBox temp = (JComboBox)e.getItemSelectable();
		
	for(int i = 0; i < 12; i++) {
	    
	    
	    if(comboPanel.boxes[i].getName().equals(temp.getName())) {
		if (i == 1)
		    comboPanel.blankRemoved[10] = true;
		if ((comboPanel.blankRemoved[i] == false) &&
		    (comboPanel.blankHidden[i]  == false) &&
		    (!((String)comboPanel.boxes[i].getSelectedItem()).equals("unspecified"))) {
		    
		    System.out.println((String)comboPanel.boxes[i].getItemAt(0));
		    temp.removeItem("unspecified");	
		    comboPanel.blankHidden[i] = true;
		}
	    }
	}

	
	if(comboPanel.boxes[1].getName().equals(temp.getName())) {
	    String components = database.
		getComponent((String)comboPanel.boxes[1].getSelectedItem());
	    
	    // 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");
	    
	    // 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 = comboPanel.boxes[10].getItemCount() -1;
		 i > 0; i--) {
		comboPanel.boxes[10].removeItemAt(i);
	    }
	    
	    for (int i=0; i < componentValues.size(); i++) {
		comboPanel.boxes[10].addItem(componentValues.get(i));
	    }
	    
	    comboPanel.blankRemoved[10] = false;

	    comboPanel.boxes[10].removeItemAt(0);
	    
	}
	
    }


    //---------------------------------------------------------------------
    //
    //  Keyboard Listeners
    //
    //---------------------------------------------------------------------

    /**
     * Blank implementation 
     */
    public void keyTyped(KeyEvent e) {
    }

    /**
     * Blank implementation 
     */ 
    public void keyReleased(KeyEvent e) {
	//	System.out.println("The key pressed event was fired");
    }

    /**
     * Handler for a key press
     * @param evt KeyEvent action that occured
     */
    public void keyPressed(KeyEvent evt) {
	if (evt.getModifiers() == java.awt.event.ActionEvent.CTRL_MASK) { 
	    
	    switch (evt.getKeyCode()) { 
	    case java.awt.event.KeyEvent.VK_M: // Modify Bug
		actionModify();
		break; 
		
	    case java.awt.event.KeyEvent.VK_R: // Resolve Bug 
		if(bug.getField("Status").toLowerCase().equals("open"))
		    actionResolve();
		break; 
		
	    case java.awt.event.KeyEvent.VK_C: // Close Bug 
		if(bug.getField("Status").toLowerCase().equals("resolved"))
		    actionClose();
		break; 		
		
	    case java.awt.event.KeyEvent.VK_O: // Open Query 
		if(!bug.getField("Status").toLowerCase().equals("open"))
		actionOpen();
		break; 
	    
	    case java.awt.event.KeyEvent.VK_A: // Assign Bug 
		actionAssign();
		break; 

	    case java.awt.event.KeyEvent.VK_G: // Cancel 
		actionCancel();
		break; 
		
	    case java.awt.event.KeyEvent.VK_X: // Close Window 
		this.dispose();
		break; 
	    
	    }
	}
    }

    //-------------------------------------------------------------------
    //
    //  Mouse Handler Methods
    //
    //-------------------------------------------------------------------
    

    /**
     * Blank implementation 
     */    
    public void mouseClicked(MouseEvent l) {

	/*
	if(javax.swing.SwingUtilities.isMiddleMouseButton(l)) {
	    
	importFile((JTextComponent)l.getComponent());

	    }*/
    }

    /**
     * Blank implementation 
     */    
    public void mouseEntered(MouseEvent l) {
    }

    /**
     * Blank implementation 
     */    
    public void mousePressed(MouseEvent l) {
    }

    /**
     * Blank implementation 
     */    
    public void mouseReleased(MouseEvent l) {
    }

    /**
     * Blank implementation 
     */    
    public void mouseExited(MouseEvent l) {
    }


    //----------------------------------------------------------------------
    //
    //  Save Functions
    //  All are private, so no Javadoc comments needed
    //
    //----------------------------------------------------------------------

   
    private void saveModify() {
	// update the tree in the update panel
	//
	Date date = new Date();
	
	newTitle = "#### " + date.toString() +
	    " ### Modified by " + userName;
	
	addNode(treeBox, treeBox.treeUpdate.
		description_entry.getText(), mode);
	
	
	String newDescription = bug.getField("Description") + "\n";
	if( treeBox.treeUpdate.
	    description_entry.getText().equals(""))
	    newDescription += newTitle + "\n";
	else
	    newDescription += newTitle +  "\n" + 
		treeBox.treeUpdate.
		description_entry.getText();
	
	bug.setField("Description", 
		     newDescription);
	
	// set the text input areas to nothing
	//
	newTitle = new String("");
	treeBox.treeUpdate.description_entry.setText("");
	
	
	// capture the values of all the fields that are allowed
	//to be updated
	//
	bug.setField("Title", titleBox.getText());
	bug.setField("Class", (String)comboPanel.
		     boxes[2].getSelectedItem());
	bug.setField("Priority", (String)comboPanel.
		     boxes[3].getSelectedItem());
	bug.setField("Severity", (String)comboPanel.
		     boxes[4].getSelectedItem());
	bug.setField("Due", (String)comboPanel.
		     boxes[5].getSelectedItem());
	bug.setField("Resolution", (String)comboPanel.
		     boxes[6].getSelectedItem());
	bug.setField("Inspected", (String)comboPanel.
		     boxes[7].getSelectedItem());
	bug.setField("Environment", (String)comboPanel.
		     boxes[8].getSelectedItem());
	bug.setField("TestCase", (String)comboPanel.
		     boxes[9].getSelectedItem());
	bug.setField("Component", (String)comboPanel.
		     boxes[10].getSelectedItem());
	bug.setField("ReviewDate", (String)comboPanel.
		     boxes[11].getSelectedItem());
	bug.setField("DBase", (String)comboPanel.boxes[1].getSelectedItem());

	//		    System.out.
	//	println("The setting of the bug data is complete");
	
	if(!(database.updateBug("modify", bug)))
	    //	    System.out.println("The bug creation failed");
	mode = LOAD_BUG;


    }

    private void saveModifyDB() {
	
	// save the modifications
	//
	saveModify();
	
	database.deleteBug(bug);
	
	int bugNumber = database.
	    newBug(bug, (String)comboPanel.boxes[1].getSelectedItem());
	
	bug.setField("BugNumber", "" + bugNumber);
	bug.setField("DBase", (String)comboPanel.boxes[1].getSelectedItem());

	//		    if( bugNumber < 0 )
	//	System.out.println("The bug creation failed");
	
	bugLabel.setText(bugNumber + " (" + 
			 bug.getField("Status") + ")");
	
    }

    private boolean saveIsReady() {
	
	boolean notReady = false;

	if (titleBox.getText().trim().equals("")) {

	    JFrame frame = new JFrame("Warning");
	    JOptionPane.showMessageDialog(frame, 
					  "The title may not be blank.");
	    notReady = true;
	    return notReady;
	}



	if ((mode == NEW_BUG) && 
	    (treeBox.description_entry.getText().equals(""))) {
	    
	    JFrame frame = new JFrame("Warning");
	    JOptionPane.showMessageDialog(frame, 
					  "The description may not be blank.");
	    notReady = true;
	} 

	if ((mode == OPEN) && 
	    (treeBox.treeUpdate.description_entry.getText().equals(""))) {
	    
	    JFrame frame = new JFrame("Warning");
	    JOptionPane.showMessageDialog(frame, 
					  "The description may not be blank.");
	    notReady = true;
	} 

	
	/*
	  if (mode == NEW_BUG) {
	  if (comboPanel.boxes[0].getSelectedItem() == "unspecified")
	  notReady = true;
	  else if (comboPanel.boxes[1].getSelectedItem() == "unspecified")
	  notReady = true;
	  else if (comboPanel.boxes[2].getSelectedItem() == "unspecified")
	  notReady = true;
	  else if (comboPanel.boxes[3].getSelectedItem() == "unspecified")
	  notReady = true;
	  else if (comboPanel.boxes[4].getSelectedItem() == "unspecified")
	  notReady = true;
	  else if (comboPanel.boxes[5].getSelectedItem() == "unspecified")
	  notReady = true;
	  else if (comboPanel.boxes[8].getSelectedItem() == "unspecified")
	  notReady = true;
	  else if (comboPanel.boxes[10].getSelectedItem() == "unspecified")
	  notReady = true;
	  
	  if (notReady) {
	  JFrame frame = new JFrame("Warning");
	  JOptionPane.showMessageDialog(frame, "All editable values "
	  + "must be changed before "
	  + "creating a new bug");
	  
	  }
	  }
	*/
	
	if (mode == RESOLVE) {
	      
	    if (comboPanel.boxes[6].getSelectedItem() == "unspecified") {
		
		notReady = true;
		
		String warningString =
		    new String("Please set value for Resolution.");
		
		JFrame frame = new JFrame("Warning");
		JOptionPane.showMessageDialog(frame, warningString); 
	    }

	    else if (userName.equals(
				     ((String)comboPanel.
				      boxes[7].getSelectedItem()))){
		notReady = true;
	    
		String warningString =
		    new String("Inspected By cannot be the current user.");
		
		JFrame frame = new JFrame("Warning");
		JOptionPane.showMessageDialog(frame, warningString);  
	    }
	    
	}
	
	
	if (notReady == false)
	    titleBox.setEnabled(false);
	return notReady;
    }
    
    private void saveGeneric() {
	
	// if this runs, all the combo boxes should be disabled
	//
	for (int i=0; i < 12; i++ ) {
	    comboPanel.boxes[i].setEnabled(false);
	}
	
	// change the refresh button over to a Cancel button
	//
	buttonPanel.buttons[4].setText("Refresh");
	buttonPanel.buttons[4].setActionCommand("Refresh");
	
	// disable all the buttons that should not be used
	//
	for (int i=0; i < 6; i++ ) {
	    buttonPanel.buttons[i].setEnabled(true);
	}
	
	// change the tree back to read only
	//
	CardLayout cl = (CardLayout)(treeBox.getLayout());
	cl.show(treeBox, "Tree");

	if (mode != NEW_BUG)
	    database.unLockBug(bug.getField("DBase"), 
			       bug.getField("BugNumber"),
			       userName);
	
    }
    

    private void saveNewBug() {
	
	// update the tree in the update panel
	//
	Date date = new Date();
	
	newTitle = "#### " + date.toString() +
	    " ### Item opened by " + userName;
	addNode(treeBox, treeBox.description_entry.getText(), mode);
	
	String newDescription = bug.getField("Description");
	if( treeBox.description_entry.getText().equals(""))
	    newDescription += newTitle + "\n";
	else
	    newDescription += newTitle +  "\n" + 
		treeBox.description_entry.getText();
	
	bug.setField("Description", 
		     newDescription);
	
	treeBox.description_entry.setText("");
	
	
	bug.setField("Title", titleBox.getText());
	bug.setField("Owner", (String)comboPanel.
		     boxes[0].getSelectedItem());
	bug.setField("DBase", (String)comboPanel.
		     boxes[1].getSelectedItem());
	bug.setField("Class", (String)comboPanel.
		     boxes[2].getSelectedItem());
	bug.setField("Priority", (String)comboPanel.
		     boxes[3].getSelectedItem());
	bug.setField("Severity", (String)comboPanel.
		     boxes[4].getSelectedItem());
	bug.setField("Due", (String)comboPanel.
		     boxes[5].getSelectedItem());
	bug.setField("Resolution", (String)comboPanel.
		     boxes[6].getSelectedItem());
	bug.setField("Inspected", (String)comboPanel.
		     boxes[7].getSelectedItem());
	bug.setField("Environment", (String)comboPanel.
		     boxes[8].getSelectedItem());
	bug.setField("TestCase", (String)comboPanel.
		     boxes[9].getSelectedItem());
	bug.setField("Component", (String)comboPanel.
		     boxes[10].getSelectedItem());
	bug.setField("ReviewDate", (String)comboPanel.
		     boxes[11].getSelectedItem());
		    		    
	bug.setField("Status", "Open");
	bug.setField("OpenedBy", userName);
	bug.setField("OpenDate", date.toString());
	
	int bugNumber = database.
	    newBug(bug, bug.getField("DBase"));
	
	bug.setField("BugNumber", "" + bugNumber);
	
	//		    if( bugNumber < 0 )
//	System.out.println("The bug creation failed");
	
	bugLabel.setText(bugNumber + " (" + 
			 bug.getField("Status") + ")");
	mode = LOAD_BUG;
    }

    private void saveClose() {
	
	// update the tree in the update panel
	//
	Date date = new Date();
	
	newTitle = "#### " + date.toString() +
	    " ### Item closed by " + userName;
	
	addNode(treeBox, treeBox.treeUpdate.
		description_entry.getText(), mode);
	
	String newDescription = bug.getField("Description") + "\n";
	if( treeBox.treeUpdate.
	    description_entry.getText().equals(""))
	    newDescription += newTitle + "\n";
	else
	    newDescription += newTitle +  "\n" + 
		treeBox.treeUpdate.
		description_entry.getText();
	
	bug.setField("Description", 
		     newDescription);
	
	
	// clear out the description entries
	//
	newTitle = new String("");
	treeBox.treeUpdate.description_entry.setText("");
	
	
	bug.setField("Title", titleBox.getText());
	bug.setField("TestCase", (String)comboPanel.
		     boxes[9].getSelectedItem());
	bug.setField("Status", "Closed");
	
	if(!(database.updateBug("close", bug)))
	    System.out.println("The bug close failed");
	
	String bugNumber = bug.getField("BugNumber");
	bugLabel.setText(bugNumber + " (" + 
			 bug.getField("Status") + ")");
	
	mode = LOAD_BUG;
    }

    private void saveOpen() {
	// update the tree in the update panel
	//
	Date date = new Date();
	
	newTitle = "#### " + date.toString() +
	    " ### Item opened  by " + userName;
	
	addNode(treeBox, treeBox.treeUpdate.
		description_entry.getText(), mode);
	
		
	bug.setField("Owner", (String)comboPanel.
		     boxes[0].getSelectedItem());
	bug.setField("DBase", (String)comboPanel.
		     boxes[1].getSelectedItem());
	bug.setField("Class", (String)comboPanel.
		     boxes[2].getSelectedItem());
	bug.setField("Priority", (String)comboPanel.
		     boxes[3].getSelectedItem());
	bug.setField("Severity", (String)comboPanel.
		     boxes[4].getSelectedItem());
	bug.setField("Due", (String)comboPanel.
		     boxes[5].getSelectedItem());
	bug.setField("Resolution", (String)comboPanel.
		     boxes[6].getSelectedItem());
	bug.setField("Inspected", (String)comboPanel.
		     boxes[7].getSelectedItem());
	bug.setField("Environment", (String)comboPanel.
		     boxes[8].getSelectedItem());
	bug.setField("TestCase", (String)comboPanel.
		     boxes[9].getSelectedItem());
	bug.setField("Component", (String)comboPanel.
		     boxes[10].getSelectedItem());
	bug.setField("ReviewDate", (String)comboPanel.
		     boxes[11].getSelectedItem());
	
	String newDescription = bug.getField("Description") + "\n";
	if( treeBox.treeUpdate.
	    description_entry.getText().equals(""))
	    newDescription += newTitle + "\n";
	else
	    newDescription += newTitle +  "\n" + 
		treeBox.treeUpdate.
		description_entry.getText();
	
	bug.setField("Description", 
		     newDescription);
	
	// clear out the description entries
	//
	newTitle = new String("");
	treeBox.treeUpdate.description_entry.setText("");
	
	bug.setField("Title", titleBox.getText());
	bug.setField("Status", "Open");
	
	if(!(database.updateBug("open", bug)))
	    System.out.println("The bug open failed");
	
	String bugNumber = bug.getField("BugNumber");
	bugLabel.setText(bugNumber + " (" + 
			 bug.getField("Status") + ")");
	
	bug.setField("ResolvedBy", "");
	bug.setField("ResolvedDate", "");

	buttonPanel.buttons[1].setText("Resolve");
	buttonPanel.buttons[1].setActionCommand("Resolve");
	
	mode = LOAD_BUG;
	
	
    }

    private void saveResolve() {

	// update the tree in the update panel
	//
	Date date = new Date();
	
	newTitle = "#### " + date.toString() +
	    " ### Item resolved by " + userName + 
	    " as " + (String)comboPanel.boxes[6].getSelectedItem();
	
	addNode(treeBox, treeBox.treeUpdate.
		description_entry.getText(), mode);
	
	
	String newDescription = bug.getField("Description") + "\n";
	if( treeBox.treeUpdate.
	    description_entry.getText().equals(""))
	    newDescription += newTitle + "\n";
	else
	    newDescription += newTitle +  "\n" + 
		treeBox.treeUpdate.
		description_entry.getText();
	
	bug.setField("Description", 
		     newDescription);
	
	newTitle = new String("");
	
	treeBox.treeUpdate.description_entry.setText("");
	
	
	bug.setField("Title", titleBox.getText());
	bug.setField("Owner", (String)comboPanel.
		     boxes[0].getSelectedItem());
	bug.setField("Resolution", (String)comboPanel.
		     boxes[6].getSelectedItem());
	bug.setField("Inspected", (String)comboPanel.
		     boxes[7].getSelectedItem());
	bug.setField("TestCase", (String)comboPanel.
		     boxes[9].getSelectedItem());
	bug.setField("Status", "Resolved");
	bug.setField("ResolvedBy", userName);
	bug.setField("ResolvedDate", date.toString());
	
	
	if(!(database.updateBug("resolve", bug)))
	    System.out.println("The bug resolve failed");
		    
	
	buttonPanel.buttons[1].setText("Open");
	buttonPanel.buttons[1].setActionCommand("Open");
		    
	String bugNumber = bug.getField("BugNumber");
	bugLabel.setText(bugNumber + " (" + 
				     bug.getField("Status") + ")");
	
	mode = LOAD_BUG;
    }    

    private void saveAssign() {

	// update the tree in the update panel
	//
	Date date = new Date();
	
	String oldUser = (String)comboPanel.boxes[0].
	    getItemAt(comboPanel.oldValues[0]);
	String newUser =  (String)comboPanel.boxes[0].
	    getItemAt(comboPanel.boxes[0].getSelectedIndex());
	
	
	newTitle = "#### " + date.toString() +
	    " ### Item assigned by " + userName + 
	    "; " + oldUser + " --> " + newUser;
	
	addNode(treeBox, treeBox.treeUpdate.
		description_entry.getText(), mode);
	
	String newDescription = bug.getField("Description") + "\n";
	if( treeBox.treeUpdate.
	    description_entry.getText().equals(""))
	    newDescription += newTitle + "\n";
	else
	    newDescription += newTitle +  "\n" + 
		treeBox.treeUpdate.
		description_entry.getText();
	
	bug.setField("Description", 
		     newDescription);
	
	newTitle = new String("");
	
	treeBox.treeUpdate.description_entry.setText("");
	
	bug.setField("Title", titleBox.getText());
	bug.setField("Owner", (String)comboPanel.
		     boxes[0].getSelectedItem());
	bug.setField("DBase", (String)comboPanel.
		     boxes[1].getSelectedItem());
	bug.setField("Class", (String)comboPanel.
		     boxes[2].getSelectedItem());
	bug.setField("Priority", (String)comboPanel.
		     boxes[3].getSelectedItem());
	bug.setField("Severity", (String)comboPanel.
		     boxes[4].getSelectedItem());
	bug.setField("Due", (String)comboPanel.
		     boxes[5].getSelectedItem());
	bug.setField("Environment", (String)comboPanel.
		     boxes[8].getSelectedItem());
	bug.setField("TestCase", (String)comboPanel.
		     boxes[9].getSelectedItem());
	bug.setField("Component", (String)comboPanel.
		     boxes[10].getSelectedItem());
	bug.setField("ReviewDate", (String)comboPanel.
		     boxes[11].getSelectedItem());
	
	if(!(database.updateBug("assign", bug)))
	    System.out.println("The bug resolve failed");
	
	mode = LOAD_BUG;
    }

    //----------------------------------------------------------------------
    //
    //   Action Functions
    //
    //----------------------------------------------------------------------

    private void actionCancel() {


	if (mode == NEW_BUG) {
	    this.dispose();
	    return;
	}
	
	// if this runs, all the combo boxes should be disabled except
	// for the first one (owner)
	//
	for (int i=0; i < 12; i++ ) {
	    if ( (comboPanel.blankHidden[i] == true)  && 
		 (comboPanel.oldValues[i] == 0)) {
		comboPanel.boxes[i].insertItemAt("unspecified", 0);
		comboPanel.blankHidden[i] = false;
	    }
	    
	    comboPanel.boxes[i].setSelectedIndex(comboPanel.oldValues[i]);
	    comboPanel.boxes[i].setEnabled(false);
	}
	
	titleBox.setText(oldTitle);
	
	// change the refresh button over to a Cancel button
	//
	buttonPanel.buttons[4].setText("Refresh");
	buttonPanel.buttons[4].setActionCommand("Refresh");

	// disable all the buttons that should not be used
	//
	for (int i=0; i < 6; i++ ) {
	    buttonPanel.buttons[i].setEnabled(true);
	}
	
	// change the tree back to read only
	//
	CardLayout cl = (CardLayout)(treeBox.getLayout());
	cl.show(treeBox, "Tree");
	
	treeBox.treeUpdate.description_entry.setText("");
	
	mode = LOAD_BUG;
	
	if (!bug.getField("Status").toLowerCase().
				       equals("resolved")) {
	    
	    buttonPanel.buttons[2].setEnabled(false);
	    buttonPanel.buttons[5].setEnabled(false);
	    
	}

	database.unLockBug(bug.getField("DBase"), 
			   bug.getField("BugNumber"),
			   userName);
	isBeingEdited = false;

    }

    private void actionOpen() {

	oldTitle = titleBox.getText();
	
	for (int i = 0; i < 12; i++) {
	    comboPanel.oldValues[i] = 
		comboPanel.boxes[i].getSelectedIndex();
	}
	
	// change the refresh button over to a Cancel button
	//
	buttonPanel.buttons[4].setText("Cancel");
	buttonPanel.buttons[4].setActionCommand("Cancel");
	
	// disable all the buttons that should not be used
	//
	buttonPanel.buttons[0].setEnabled(false);
	buttonPanel.buttons[1].setEnabled(false); 
	buttonPanel.buttons[2].setEnabled(false);
	buttonPanel.buttons[3].setEnabled(false);
	buttonPanel.buttons[5].setEnabled(true);


	// enable the owner field and allow it to be changed by the user
	//
	comboPanel.boxes[0].setEnabled(true);

	// enable the database, class, pri, sev, due, env, test case, and 
	// component fields for editing
	//
	comboPanel.boxes[1].setEnabled(true);
	comboPanel.boxes[2].setEnabled(true);
	comboPanel.boxes[3].setEnabled(true);
	comboPanel.boxes[4].setEnabled(true);
	comboPanel.boxes[5].setEnabled(true);
	comboPanel.boxes[8].setEnabled(true);
	comboPanel.boxes[9].setEnabled(true);
	comboPanel.boxes[10].setEnabled(true);

	// set the owner back to the user that resolved the bug
	//
	comboPanel.boxes[0].setSelectedItem(bug.getField("ResolvedBy"));

	
	// if necessary, insert a blank at the 
	// front of the resolution field and set the
	// selected index to this
	//

	String value = ((String)comboPanel.boxes[6].getSelectedItem());

	if((value != null) && (!value.equals("unspecified"))) {
	    comboPanel.boxes[6].insertItemAt("unspecified", 0);
	    comboPanel.boxes[6].setSelectedIndex(0);
	}
	

	// take care of inspected by in the same manner
	//
	value = ((String)comboPanel.boxes[7].getSelectedItem());

	if((value != null) && (!value.equals("unspecified"))) {
	    comboPanel.boxes[7].insertItemAt("unspecified", 0);
	    comboPanel.boxes[7].setSelectedIndex(0);
	}
	
	// and also take care of the review data field
	//
	value = ((String)comboPanel.boxes[11].getSelectedItem());

	if((value != null) && (!value.equals("unspecified"))) {
	    comboPanel.boxes[11].insertItemAt("unspecified", 0);
	    comboPanel.boxes[11].setSelectedIndex(0);
	}
	    
	// allow the user to make the modifications to the tree
	//
	CardLayout cl = (CardLayout)(treeBox.getLayout());
	cl.show(treeBox, "Update");
	
	mode = OPEN;

	// have the edit box get the mosue focus
	//
	treeBox.treeUpdate.description_entry.requestFocus();
    }

    private void actionAssign() {

	oldTitle = titleBox.getText();
	
	// if this runs, Owner, TestCase, Resolution, InspectedBy, and
	// Description will be editable
	//
	comboPanel.boxes[0].setEnabled(true); // owner
	
	for (int i = 0; i < 12; i++) {
	    comboPanel.oldValues[i] = 
		comboPanel.boxes[i].getSelectedIndex();
	}
	
	// change the refresh button over to a Cancel button
	//
	buttonPanel.buttons[4].setText("Cancel");
	buttonPanel.buttons[4].setActionCommand("Cancel");
	
	// disable all the buttons that should not be used
	//

	buttonPanel.buttons[0].setEnabled(false);
	buttonPanel.buttons[1].setEnabled(false); 
	buttonPanel.buttons[2].setEnabled(false);
	buttonPanel.buttons[3].setEnabled(false);
	buttonPanel.buttons[5].setEnabled(true);

	// allow the user to make the modifications to the tree
	//
	CardLayout cl = (CardLayout)(treeBox.getLayout());
	cl.show(treeBox, "Update");
	
	mode = ASSIGN;


	// have the edit box get the mosue focus
	//
	treeBox.treeUpdate.description_entry.requestFocus();
    }
    
    private void actionClose() {

	oldTitle = titleBox.getText();
	
	for (int i = 0; i < 12; i++) {
	    comboPanel.oldValues[i] = 
		comboPanel.boxes[i].getSelectedIndex();
	}

	// if this runs, Owner, TestCase, Resolution, InspectedBy, and
	// Description will be editable
	//
	comboPanel.boxes[9].setEnabled(true); // testcase
	
	// change the refresh button over to a Cancel button
	//
	buttonPanel.buttons[4].setText("Cancel");
	buttonPanel.buttons[4].setActionCommand("Cancel");
	
	// disable all the buttons that should not be used
	//
	buttonPanel.buttons[0].setEnabled(false);
	buttonPanel.buttons[1].setEnabled(false); 
	buttonPanel.buttons[2].setEnabled(false);
	buttonPanel.buttons[3].setEnabled(false);
	buttonPanel.buttons[5].setEnabled(true);
	
	// allow the user to make the modifications to the tree
	//
	CardLayout cl = (CardLayout)(treeBox.getLayout());
	cl.show(treeBox, "Update");
	
	mode = CLOSE;

	// have the edit box get the mosue focus
	//
	treeBox.treeUpdate.description_entry.requestFocus();
    }
    
    private void actionResolve() {
	oldTitle = titleBox.getText();
	
	for (int i = 0; i < 12; i++) {
	    comboPanel.oldValues[i] = 
		comboPanel.boxes[i].getSelectedIndex();
	}
	
	// if this runs, Owner, TestCase, Resolution, InspectedBy, and
	// Description will be editable
	//
	comboPanel.boxes[0].setEnabled(true); // owner
	System.out.println(bug.getField("OpenedBy"));
	comboPanel.boxes[0].setSelectedItem(bug.getField("OpenedBy"));

	comboPanel.boxes[6].setEnabled(true); // resolution
	comboPanel.boxes[7].setEnabled(true); // inspectedby
	comboPanel.boxes[9].setEnabled(true); // testcase
	
	// change the refresh button over to a Cancel button
	//
	buttonPanel.buttons[4].setText("Cancel");
	buttonPanel.buttons[4].setActionCommand("Cancel");
	
	// disable all the buttons that should not be used
	//
	buttonPanel.buttons[0].setEnabled(false);
	buttonPanel.buttons[1].setEnabled(false); 
	buttonPanel.buttons[2].setEnabled(false);
	buttonPanel.buttons[3].setEnabled(false);
	buttonPanel.buttons[5].setEnabled(true);
	
	

	// allow the user to make the modifications to the tree
	//
	CardLayout cl = (CardLayout)(treeBox.getLayout());
	cl.show(treeBox, "Update");
	
	mode = RESOLVE;

	// have the edit box get the mosue focus
	//
	treeBox.treeUpdate.description_entry.requestFocus();	
    }

    private void actionModify() {

	oldTitle = titleBox.getText();
	
	for (int i = 0; i < 12; i++) {
	    comboPanel.oldValues[i] = 
		comboPanel.boxes[i].getSelectedIndex();
	}
	
	// if this runs, all the combo boxes should be enabled except
	// for the first one (owner)
	//
	for (int i = 1; i < 12; i++ ) {
	    comboPanel.boxes[i].setEnabled(true);
	}

	// disable the resolution combo box
	//
	if(!bug.getField("Status").equals("Resolved"))
	    comboPanel.boxes[6].setEnabled(false);

	// change the refresh button over to a Cancel button
	//
	buttonPanel.buttons[4].setText("Cancel");
	buttonPanel.buttons[4].setActionCommand("Cancel");
	
	// disable all the buttons that should not be used
	//
	buttonPanel.buttons[0].setEnabled(false);
	buttonPanel.buttons[1].setEnabled(false); 
	buttonPanel.buttons[2].setEnabled(false);
	buttonPanel.buttons[3].setEnabled(false);
	buttonPanel.buttons[5].setEnabled(true);
	
	// allow the user to make the modifications to the tree
	//
	CardLayout cl = (CardLayout)(treeBox.getLayout());
	cl.show(treeBox, "Update");
	
	mode = MODIFY;

	// have the edit box get the mosue focus
	//
	treeBox.treeUpdate.description_entry.requestFocus();
    }

    private void actionRefresh() {
	
	Bug newBug = database.
	    getBug(bug.getField("DBase"), 
		   Integer.parseInt(bug.getField("BugNumber")));
	
	//	System.out.println(bug.getField("DBase") + bug.getField("BugNumber"));
	comboPanel.configureBoxes(newBug);
	
	treeBox.treeUpdate.rebuildTree(newBug.getField("Description"));
	treeBox.treePanel.rebuildTree(newBug.getField("Description"));
	
	//	System.out.println("It got the bug on the refresh");
    }

    /**
     * Depending on the state of the mode variable many actions can be saved
     */
    public void actionSave() {


	// make sure that all of the options that all the combo
	// boxes have been changed off of blank.
	//
	
	boolean notReady = saveIsReady();
	
	if(notReady == false) {
	    
	    saveGeneric();
	    
	    if (mode == NEW_BUG) { 
		
		saveNewBug();
	    }
	    
	    if (mode == CLOSE) { 
		
		saveClose();
	    }
	    
	    if (mode == OPEN) { 
		
		saveOpen();
	    }
	    
	    if (mode == RESOLVE) { 
		
		saveResolve();
	    }
	    
	    if (mode == ASSIGN) {
		
		saveAssign();
	    }
	    
	    if (mode == MODIFY) {
		
		//if (bug.getField("DBase").
		//    equals((String)comboPanel.
		//	   boxes[1].getSelectedItem())){
		    
		saveModify();
		    //}
		    //else {
		    //saveModifyDB();
		    //}
		
	    }
	    
	    // keep the user from closing the bug unless the bug has
	    // already been resolved.  Needs to be run after save
	    // completed
	    //
	    if (!bug.getField("Status").equals("Resolved"))
		buttonPanel.buttons[2].setEnabled(false);
	    
	    buttonPanel.buttons[5].setEnabled(false);
	}
	
	
	isBeingEdited = false;
	
	actionRefresh();
	
    }

    public static void main(String[] args) {

	JFrame mainFrame = new JFrame();

	Font newFont = mainFrame.getFont();
	
	Bug newbug = new Bug();

	VarmintDB database = new VarmintDB();
	final BugPanel bugPanel  = new BugPanel(newFont, "new", 1, newbug, 
						database, "duncan");

	// listen for window closing events
	//
	bugPanel.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) 
		{bugPanel.dispose();}
	    });
	
	bugPanel.setFont(newFont);
	
	// resize and set the frame dimensions
	//
	bugPanel.pack();
	
	// center the frame to the screen
	//
	Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 
	Rectangle frameDim = bugPanel.getBounds(); 
	bugPanel.setLocation((screenDim.width - frameDim.width) / 2 + 50,
			     (screenDim.height - 
			      frameDim.height) / 2 + 50); 

	
	//	mainFrame.getContentPane().add(bugPanel, BorderLayout.CENTER);
	bugPanel.setVisible(true);
    }
}

