/*
 * Wind2Config.java
 *
 * Created on October 19, 2006, 12:51 AM
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;


/**
 *
 * @author  davarus
 */
public class Wind2Config extends javax.swing.JFrame 
		implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IniFile inifile;

    /** Creates new form Wind2Config */
    public Wind2Config(IniFile ini) {
    	inifile = ini;
        initComponents();
	setTitle("Configuration");
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents

    	cAnemo = new Wind2ConfigAnemometer[3];
	cAnemo[0] = new Wind2ConfigAnemometer(0, inifile);
	cAnemo[1] = new Wind2ConfigAnemometer(1, inifile);
	cAnemo[2] = new Wind2ConfigAnemometer(2, inifile);
	cUser = new Wind2ConfigUser[4];
	cUser[0] = new Wind2ConfigUser(0, inifile);
	cUser[1] = new Wind2ConfigUser(1, inifile);
	cUser[2] = new Wind2ConfigUser(2, inifile);
	cUser[3] = new Wind2ConfigUser(3, inifile);
	cShow = new Wind2ConfigShow(inifile);
	cMain = new Wind2ConfigMain(inifile);
	cHTTP = new Wind2ConfigHTTP(inifile);
	cMySQL = new Wind2ConfigMySQL(inifile);
	pButtons = new JPanel();

	pAnemo = new JPanel();
	pAnemo.setLayout(new java.awt.GridLayout(0, 1));
	pUser = new JPanel();
	pUser.setLayout(new java.awt.GridLayout(0, 2));
	pShow = new JPanel();
	pBasic = new JPanel();
	pTab = new JTabbedPane();
	pTab.addTab("Main", pBasic);
	pTab.addTab("Anemometers", pAnemo);
	pTab.addTab("User Inputs", pUser);
	pTab.addTab("Show Inputs", pShow);
	pTab.addTab("HTTP Log", cHTTP);
	pTab.addTab("MySQL", cMySQL);

	pBasic.add(cMain);

	pAnemo.add(cAnemo[0]);
	pAnemo.add(cAnemo[1]);
	pAnemo.add(cAnemo[2]);

	pUser.add(cUser[0]);
	pUser.add(cUser[1]);
	pUser.add(cUser[2]);
	pUser.add(cUser[3]);

	pShow.add(cShow);

	java.awt.GridBagLayout grid = new java.awt.GridBagLayout();
	java.awt.GridBagConstraints cons = new java.awt.GridBagConstraints();
	pMain = new JPanel();
	pMain.setLayout(grid);
	cons.gridx = 0;
	cons.gridy = 0;
	cons.gridwidth = 3;
	pMain.add(pTab, cons);


	bSave = new JButton("Save");
	bSave.setActionCommand("save");
	bSave.addActionListener(this);
	bCancel = new JButton("Cancel");
	bCancel.setActionCommand("cancel");
	bCancel.addActionListener(this);
	
	pButtons.setLayout(new java.awt.GridLayout(0,2));
	pButtons.add(bSave);
	pButtons.add(bCancel);

	cons.gridx = 1;
	cons.gridy = 1;
	pMain.add(pButtons, cons);

	getContentPane().add(pMain);

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        pack();

	setLocationRelativeTo(null);
    }//GEN-END:initComponents

    public void actionPerformed(ActionEvent e) {
       	if ("save".equals(e.getActionCommand())) {
		boolean valid = true;
		for (int i = 0; i < 4; i++) {
			if (!cUser[i].verifyInput())
				valid = false;
		}

		if (!valid) {
			JOptionPane.showMessageDialog(this,
				"Invalid configuration data!",
				"Invalid data",
				JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		cMain.saveIni(inifile);
		cShow.saveIni(inifile);
		cUser[0].saveIni(inifile, 0);
		cUser[1].saveIni(inifile, 1);
		cUser[2].saveIni(inifile, 2);
		cUser[3].saveIni(inifile, 3);
		cAnemo[0].saveIni(inifile, 0);
		cAnemo[1].saveIni(inifile, 1);
		cAnemo[2].saveIni(inifile, 2);
		cHTTP.saveIni(inifile);
		inifile.saveFile();
		new Wind2Select().setVisible(true);
		dispose();
	} else if ("cancel".equals(e.getActionCommand())) {
		new Wind2Select().setVisible(true);
		dispose();
	}
    }
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
     //   System.exit(0);
		new Wind2Select().setVisible(true);
		dispose();
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new Wind2Config(new IniFile("wind2db.ini")).setVisible(true);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane pTab;
    private JPanel		pMain;
    private javax.swing.JPanel	pAnemo;
    private javax.swing.JPanel	pUser;
    private javax.swing.JPanel	pShow;
    private javax.swing.JPanel  pBasic;
    private Wind2ConfigMain	cMain;
    private Wind2ConfigAnemometer[] cAnemo;
    private Wind2ConfigUser[]	  cUser;
    private Wind2ConfigShow	  cShow;
    private Wind2ConfigHTTP	cHTTP;
    private Wind2ConfigMySQL	cMySQL;
    private JPanel		pButtons;
    private javax.swing.JButton	  bSave;
    private javax.swing.JButton	  bCancel;
    // End of variables declaration//GEN-END:variables
}
