import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;



public class JFrameConfig extends JFrame implements ActionListener {

	
	private static final long serialVersionUID = 1L;

	public int temps = 1000; 
	public boolean souris, clavier, boucle, aleatoire, delai, manuel;

	private JRadioButton JRBaleatoire;
	private JRadioButton JRBboucle;
	private ButtonGroup lesradios1;
	
	public JButton JBok;
	public JButton JBAnnuler;
	public JTextField JTFtemps;
	
	private JRadioButton JRBdelai;
	private JRadioButton JRBclicksouris;
	private JRadioButton JRBtoucheclavier;
	private JRadioButton JRBmanuel;
	private ButtonGroup lesradios2;

	private JFrameDiaporama pere;
	
	public JFrameConfig(JFrameDiaporama papa){
		
		super("Diaporama Java - Config");
		this.setResizable(false);
		this.pere = papa;
		//this.pere = (JFrameDiaporama) this.getParent();
		
		// les JCheckBox
		this.JRBaleatoire = new JRadioButton("Aléatoire"); 
		this.JRBboucle = new JRadioButton("Boucle");
		this.JRBboucle.setSelected(true);
		this.boucle = true;
		this.aleatoire = false;
		
	    // les JRadio et le JTextfField
		// toucheclavier et clicksouris sont désactivés
	    this.JRBdelai = new JRadioButton("Délai (en ms)");
	    this.JRBdelai.setSelected(false);
	    this.JRBmanuel = new JRadioButton("Manuel");
	    this.JRBmanuel.setSelected(true);
	    this.JRBclicksouris = new JRadioButton("Click souris");
	    //this.JRBclicksouris.setEnabled(false);
	    this.JRBtoucheclavier = new JRadioButton("Touche clavier");
	    //this.JRBtoucheclavier.setEnabled(false);
	    this.delai = false;
	    this.manuel = true;
	    this.clavier = false;
	    this.souris = false;
	    
	    this.JTFtemps = new JTextField(5);
	    this.JTFtemps.setText("1000");
	    JPanel pan1 = new JPanel();
	    pan1.add(this.JRBdelai);
	    pan1.add(this.JTFtemps);
	    
	    // Groupage des radios buttons
	    this.lesradios1 = new ButtonGroup();
	    this.lesradios1.add(this.JRBaleatoire);
	    this.lesradios1.add(this.JRBboucle);
	    
	    this.lesradios2 = new ButtonGroup();
	    this.lesradios2.add(this.JRBdelai);
	    this.lesradios2.add(this.JRBmanuel);
	    this.lesradios2.add(this.JRBclicksouris);
	    this.lesradios2.add(this.JRBtoucheclavier);
	    
	    
	    JPanel pan2 = new JPanel(new GridLayout(4, 1));
	    pan2.add(pan1);
	    pan2.add(this.JRBmanuel);
	    pan2.add(this.JRBclicksouris);
	    pan2.add(this.JRBtoucheclavier);
	    
	    JPanel pan3 = new JPanel(new GridLayout(2, 1));
	    pan3.add(this.JRBaleatoire);
	    pan3.add(this.JRBboucle);
	    
	    JPanel pan4 = new JPanel(new GridLayout(1, 2));
	    pan4.add(pan2);
	    pan4.add(pan3);
	    
	    this.JBok = new JButton("Ok");
	    this.JBok.addActionListener(this);
	    this.JBAnnuler = new JButton("Annuler");
	    this.JBAnnuler.addActionListener(this);
	    JPanel pan5 = new JPanel();
	    pan5.add(JBok);
	    pan5.add(JBAnnuler);
	    
	    this.setLayout(new BorderLayout());
	    this.add(pan4, BorderLayout.CENTER);
	    this.add(pan5, BorderLayout.SOUTH);    
	
		this.pack();
	}
	
    public void actionPerformed(ActionEvent e) {
    	
    	// bouton Annuler
        if (e.getSource() == JBAnnuler) {
        	this.setVisible(false);
        }
        // bouton OK
        if (e.getSource() == JBok) {
    		// delai OU click Souris OU touche clavier est sélectionné
        	delai = this.JRBdelai.isSelected();
   			souris = this.JRBclicksouris.isSelected();
    		clavier = this.JRBtoucheclavier.isSelected();
    		manuel = this.JRBmanuel.isSelected();
    		// Boucle ET/OU aleatoire est sélectionné
    		boucle = this.JRBboucle.isSelected();
    		aleatoire = this.JRBaleatoire.isSelected();
    		
        	// Diaporama
        	if (delai){
        		try
        		{
        			temps = Integer.parseInt(this.JTFtemps.getText());
        		}catch (Exception ee){temps = 1000;}
        	}

        	this.pere.charge_config();
            this.setVisible(false);
        }
}
    
    // Test de cette seule classe
    public static void main(String[] argv){
    	JFrameConfig monJPanelConfig = new JFrameConfig(new JFrameDiaporama());
    	
    	monJPanelConfig.pack();
    	monJPanelConfig.setVisible(true);
    }
}
