// 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.*;

public class VConfigPanel extends JFrame implements ActionListener {

    VarmintDB database = null;
    
    Hashtable values = new Hashtable();
    Hashtable defs = new Hashtable();

    Font newFont = null;

    public VConfigPanel(Font font, VarmintDB dbase) {
	
	database = dbase;

	newFont = font;
	
	JPanel main_panel = new JPanel();
	
	main_panel.setLayout(new BoxLayout(main_panel, BoxLayout.Y_AXIS));
	
	addField("Owner", main_panel);
	addField("Priority", main_panel);
	addField("Severity", main_panel);
	addField("DBase", main_panel);
	addField("Class", main_panel);
	addField("Resolution", main_panel);
	addField("Due", main_panel);
	main_panel.add(Box.createRigidArea(new Dimension(0, 5)));
	JButton dismiss = new JButton("Dismiss");
	dismiss.setAlignmentX(Component.CENTER_ALIGNMENT);
	main_panel.add(dismiss);
	
	dismiss.addActionListener(this);
	dismiss.setActionCommand("__dismiss");
	
	main_panel.add(Box.createRigidArea(new Dimension(0, 5)));
	getContentPane().add(main_panel);
    }

    private void addField(String field, JPanel main_panel) {

	main_panel.add(Box.createRigidArea(new Dimension(0, 5)));

	JPanel line = new JPanel();

	line.setLayout(new BoxLayout(line, BoxLayout.X_AXIS));
	line.setAlignmentX(Component.CENTER_ALIGNMENT);
	

	String[] str = database.getConfigVal(field);

	JLabel lab = new JLabel(str[0]);

	lab.setPreferredSize(new Dimension(90, 10));
	lab.setMinimumSize(new Dimension(90, 10));

	line.add(Box.createRigidArea(new Dimension(10, 0)));
	line.add(lab);
	
	line.add(Box.createRigidArea(new Dimension(10, 0)));
	
	JTextField tfield = new JTextField();
	line.add(tfield);
	
	values.put(field, tfield);

	tfield.setPreferredSize(new Dimension(400, 10));
	tfield.setMinimumSize(new Dimension(400, 10));
	tfield.setText(str[1]);
	
	line.add(Box.createRigidArea(new Dimension(10, 0)));

	JTextField def = new JTextField();
	line.add(def);
	
	defs.put(field, def);

	def.setPreferredSize(new Dimension(100, 10));
	def.setMinimumSize(new Dimension(100, 10));
	def.setText(str[2]);
	
	line.add(Box.createRigidArea(new Dimension(10, 0)));

	JButton b = new JButton("Update");
	b.addActionListener(this);
	b.setActionCommand(field);
	
	line.add(b);
	line.add(Box.createRigidArea(new Dimension(10, 0)));
	main_panel.add(line);
    }
	
    public void actionPerformed(ActionEvent e) {

	if (e.getActionCommand().equals("__dismiss")) {
	    this.dispose();
	}
	    
	else {
	    String field = e.getActionCommand();
	    
	    String val = ((JTextField)values.get(field)).getText();
	    String def = ((JTextField)defs.get(field)).getText();
	    
	    database.setConfigVal(field, val, def);
	}
    }
   
}
