/*
 * Created on Feb 1, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.nasa.jsc.plum;

import gov.nasa.jsc.plum.Configuration;
import gov.nasa.jsc.plum.PlumUtil.DatabaseWindow;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author randrews
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ProgramPanel extends JPanel implements ActionListener{

    private DatabaseWindow parent;
    
    private JTextField calc;
    private JTextField single;
    private JTextField combogen;
    private JTextField output;
    private JTextField programs;
    private JTextField incgraph;
    private JTextField simulgraph;
    private JTextField selsuccess;
    private JTextField topology;
    private JTextField avgruntime;
    private JComboBox delimiter;
    
    private JButton apply;
    private JButton revert;
    private JButton defaults;
    
    public ProgramPanel(DatabaseWindow parent){
        this.parent=parent;
        
        setLayout(new GridBagLayout());
        
        calc=new JTextField(parent.getCfg().getCalc());calc.setColumns(50);
        single=new JTextField(parent.getCfg().getSingle());single.setColumns(50);
        combogen=new JTextField(parent.getCfg().getCombogen());combogen.setColumns(50);
        incgraph=new JTextField(parent.getCfg().getIncrementGraph());incgraph.setColumns(50);
        simulgraph=new JTextField(parent.getCfg().getSimulGraph());simulgraph.setColumns(50);
        avgruntime=new JTextField(parent.getCfg().getAvgRuntime());avgruntime.setColumns(50);
        selsuccess=new JTextField(parent.getCfg().getSelSuccess());selsuccess.setColumns(50);
        topology=new JTextField(parent.getCfg().getTopology());topology.setColumns(50);
        output=new JTextField(parent.getCfg().getOutput());output.setColumns(50);
        programs=new JTextField(parent.getCfg().getPrograms());programs.setColumns(50);
    
        delimiter=new JComboBox();
        delimiter.addItem("slash");
        delimiter.addItem("backslash");
        delimiter.setSelectedItem(parent.getCfg().getDelimiter());
        
        revert=new JButton("Revert");
        apply=new JButton("Apply");
        defaults=new JButton("Defaults");
        apply.addActionListener(this);
        revert.addActionListener(this);
        defaults.addActionListener(this);

        GridBagConstraints c=new GridBagConstraints();
        
        c.gridx=0;c.gridy=0;c.gridheight=1;c.gridwidth=1;c.ipadx=10;
        c.anchor=GridBagConstraints.EAST;
        
        c.weightx=0;
        add(new JLabel("Combo generator command:"),c);
        c.gridy++;
        add(new JLabel("Calc command:"),c);
        c.gridy++;
        add(new JLabel("Single combination command:"),c);
        c.gridy++;
        add(new JLabel("Increment graph command:"),c);
        c.gridy++;
        add(new JLabel("Simultaneous graph command:"),c);
        c.gridy++;
        add(new JLabel("Average runtime graph command:"),c);
        c.gridy++;
        add(new JLabel("Selection success command:"),c);
        c.gridy++;
        add(new JLabel("Topology command:"),c);
        c.gridy++;
        add(new JLabel("Output path:"),c);
        c.gridy++;
        add(new JLabel("Path to programs:"),c);
        c.gridy++;
        add(new JLabel("Path delimiter:"),c);
        
        c.weightx=1;
        c.gridx=1;c.gridy=0;c.gridwidth=2;
        c.anchor=GridBagConstraints.WEST;
        c.fill=GridBagConstraints.HORIZONTAL;
        add(combogen,c);
        c.gridy++;
        add(calc,c);
        c.gridy++;
        add(single,c);
        c.gridy++;
        add(incgraph,c);
        c.gridy++;
        add(simulgraph,c);
        c.gridy++;
        add(avgruntime,c);
        c.gridy++;
        add(selsuccess,c);
        c.gridy++;
        add(topology,c);
        c.gridy++;
        add(output,c);
        c.gridy++;
        add(programs,c);
        c.gridy++;
        add(delimiter,c);

        c.weightx=0;
        c.gridy++;
        c.anchor=GridBagConstraints.CENTER;
        c.fill=GridBagConstraints.BOTH;
        c.gridx=0;
        c.gridwidth=3;
        add(revert,c);
        c.gridy++;
        add(defaults,c);
        c.gridy++;
        add(apply,c);
        
    }
    
    public void actionPerformed(ActionEvent arg0) {
        if(arg0.getSource()==revert)
        {
            delimiter.setSelectedItem(parent.getCfg().getDelimiter());

            calc.setText(parent.getCfg().getCalc());
            single.setText(parent.getCfg().getSingle());
            combogen.setText(parent.getCfg().getCombogen());
            output.setText(parent.getCfg().getOutput());
            programs.setText(parent.getCfg().getPrograms());
            incgraph.setText(parent.getCfg().getIncrementGraph());
            simulgraph.setText(parent.getCfg().getSimulGraph());
            avgruntime.setText(parent.getCfg().getAvgRuntime());
            selsuccess.setText(parent.getCfg().getSelSuccess());
            topology.setText(parent.getCfg().getTopology());            
        }
        else if(arg0.getSource()==apply)
        {
            Configuration cfg=parent.getCfg();

            cfg.setDelimiter(delimiter.getSelectedItem().toString());

            cfg.setCalc(calc.getText());
            cfg.setSingle(single.getText());
            cfg.setCombogen(combogen.getText());
            cfg.setOutput(output.getText());
            cfg.setPrograms(programs.getText());
            cfg.setIncrementGraph(incgraph.getText());
            cfg.setSimulGraph(simulgraph.getText());
            cfg.setAvgRuntime(avgruntime.getText());
            cfg.setSelSuccess(selsuccess.getText());
            cfg.setTopology(topology.getText());
        }
        else if(arg0.getSource()==defaults)
        {
            Configuration cfg=parent.getCfg();
            cfg.setDefault();
            delimiter.setSelectedItem(parent.getCfg().getDelimiter());
            calc.setText(parent.getCfg().getCalc());
            single.setText(parent.getCfg().getSingle());
            combogen.setText(parent.getCfg().getCombogen());
            output.setText(parent.getCfg().getOutput());
            programs.setText(parent.getCfg().getPrograms());
            incgraph.setText(parent.getCfg().getIncrementGraph());
            simulgraph.setText(parent.getCfg().getSimulGraph());
            avgruntime.setText(parent.getCfg().getAvgRuntime());
            selsuccess.setText(parent.getCfg().getSelSuccess());
            topology.setText(parent.getCfg().getTopology());            
        }
    }
}
