import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import java.awt.*;
import java.awt.event.*;
import java.io.File;

public class JFrameDiaporama extends JFrame implements ActionListener, MouseListener, KeyListener {

         
		private static final long serialVersionUID = 1L;
		private JButton avant, apres, debut, fin, pause, config, play, open;
		// UN panneau avec Re-dimensionnement ou non
		//public JPanelDiapo monJPanelDiapo; 		// public obligatoire sinon impossible gérer évenements
		//public JPanelDiapoResize monJPanelDiapo; 	// public obligatoire sinon impossible gérer évenements
		public JPanelMosaic monJPanelDiapo; 	// public obligatoire sinon impossible gérer évenements
		public JFrameConfig monJFrameConfig;
		private JFileChooser fc;
		public Timer timer;
		
		 
public JFrameDiaporama() {
	 
	super("Diaporama Java");
	
	// Le JPanel avec les images
    //this.monJPanelDiapo = new JPanelDiapo();
    //this.monJPanelDiapo = new JPanelDiapoResize();
	this.monJPanelDiapo = new JPanelMosaic(50,50);

    // Le JFrame de configuration
	this.monJFrameConfig = new JFrameConfig(this);
	
	// Le Timer (1000 par défaut)
	this.timer = new Timer(1000, this);
	 
//	Le JFileChooser
	this.fc = new JFileChooser();
	//this.fc.setCurrentDirectory(new File("U:"));
	//this.fc.setCurrentDirectory(new File("U:\\cours\\java\\Image_diaporama"));
	this.fc.setCurrentDirectory(new File("F:\\java\\Image_diaporama\\icons"));
	// Choix d'un simple dossier
	//this.fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	//fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
	// Utilisation d'un filtre avec uniquement les images
	this.fc.addChoosableFileFilter(new ImageFilter());
	this.fc.setMultiSelectionEnabled(true);

	
     // Les Bouttons, le JFrame écoute les boutons
	 this.avant = new JButton("Avant", new ImageIcon("icons/Prev_enabled.PNG"));
	 this.apres = new JButton("Après", new ImageIcon("icons/Next_enabled.PNG"));
	 this.pause = new JButton("Pause", new ImageIcon("icons/Pause_enabled.PNG"));
	 this.play  = new JButton("Relance", new ImageIcon("icons/Play_enabled.PNG"));
	 this.debut = new JButton("Début", new ImageIcon("icons/Begin_enabled.PNG"));
	 this.fin   = new JButton("Fin", new ImageIcon("icons/End_enabled.PNG"));
	 this.config = new JButton("Config ...", new ImageIcon("icons/Configure_enabled.PNG"));
	 this.open = new JButton("Ouvrir dossier ...", new ImageIcon("icons/Folder_open.png"));
	 
     this.avant.addActionListener(this.monJPanelDiapo);
     this.apres.addActionListener(this.monJPanelDiapo);
     this.pause.addActionListener(this);
     this.play.addActionListener(this);
     this.debut.addActionListener(this.monJPanelDiapo);
     this.fin.addActionListener(this.monJPanelDiapo);
     this.config.addActionListener(this);
	 this.open.addActionListener(this);
	
	 this.avant.setActionCommand("avant");
     this.apres.setActionCommand("apres");
     this.pause.setActionCommand("pause");
     this.play.setActionCommand("play");
     this.debut.setActionCommand("debut");
     this.fin.setActionCommand("fin");
     this.config.setActionCommand("config");
	 this.open.setActionCommand("open");
 	 
	 //Positionnement des éléments
	 this.setLayout(new BorderLayout());
	 JPanel Boutton1JPanel = new JPanel();
	 
	 Boutton1JPanel.add(debut);
	 Boutton1JPanel.add(avant);
	 Boutton1JPanel.add(open);
	 Boutton1JPanel.add(apres);
	 Boutton1JPanel.add(fin);
	 
	 
	 JPanel Boutton2JPanel = new JPanel();
	 Boutton2JPanel.add(pause);
	 Boutton2JPanel.add(play);
	 Boutton2JPanel.add(config);
	 
	 JPanel BouttonsJPanel = new JPanel(new BorderLayout());
	 BouttonsJPanel.add(Boutton1JPanel, BorderLayout.NORTH);
	 BouttonsJPanel.add(Boutton2JPanel, BorderLayout.SOUTH);
	 
	 this.add(BouttonsJPanel, BorderLayout.SOUTH);
	 this.add(monJPanelDiapo, BorderLayout.CENTER);
	 active_bouttons(false);
	 
	 // pour click souris dans le JFrame
	 this.addMouseListener(this);
	 // pour click souris dans le JPanel affichant l'image
	 this.monJPanelDiapo.addMouseListener(this);
	 // pour touche clavier appuyée si boutton "apres" sélectionné 
	 this.apres.addKeyListener(this);
}
		 
// La méthode de l'interface
public void actionPerformed(ActionEvent e) {

	// Le timer déclenche un évenement
    if (e.getSource() == timer)
	{
    	this.apres.doClick();
    	repaint();
	}

	// Le boutton de choix du dossier d'images
    if (e.getSource() == open) {
        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
        	// Si choix d'un dossier 
        	if (fc.isDirectorySelectionEnabled())
        		this.monJPanelDiapo.chargeImages(fc.getSelectedFile());
        	else
        		this.monJPanelDiapo.chargeImages(fc.getSelectedFiles());
        	
            active_bouttons(true);
            charge_config();
        } else {
        	System.out.println("Annulation de l'utilisateur");
        }
    }
    // Le bouton de configuration
    if (e.getSource() == config) {
    	this.monJFrameConfig.setVisible(true);
    	charge_config();
    }
	// Le boutton de pause
    if (e.getSource() == pause) {
    	charge_config();
    	this.timer.stop();
    	this.pause.setEnabled(false);
    	this.play.setEnabled(true);
    }
   	// Le boutton de reprise
    if (e.getSource() == play) {
    	//this.timer.restart();
    	charge_config();
    	this.pause.setEnabled(true);
    	this.play.setEnabled(false);
    }
} 

private void active_bouttons(boolean bool){
	 this.avant.setEnabled(bool);
	 this.apres.setEnabled(bool);
	 this.pause.setEnabled(bool);
	 this.play.setEnabled(bool);
	 this.debut.setEnabled(bool);
	 this.fin.setEnabled(bool);
}

public void charge_config(){ 

	if (this.monJFrameConfig.delai) 
	{
		this.timer.setDelay(this.monJFrameConfig.temps);
		this.timer.start();
		this.pause.setEnabled(true);
    	this.play.setEnabled(false);
	}
	if (this.monJFrameConfig.souris)
	{
		
	}
	if (this.monJFrameConfig.clavier)
	{
		this.apres.setFocusable(true); // utile ?
		this.apres.setFocusPainted(true);
	}
	if (this.monJFrameConfig.manuel)
	{
		this.timer.stop();
		this.play.setEnabled(false);
    	this.pause.setEnabled(false);
	}

	this.monJPanelDiapo.boucle = this.monJFrameConfig.boucle;
	this.monJPanelDiapo.aleatoire = this.monJFrameConfig.aleatoire;
	
}

// Classe de test des composants
public static void main(String[] argv){
	JFrameDiaporama monJFrameDiaporama = new JFrameDiaporama();
	
	monJFrameDiaporama.pack();
	monJFrameDiaporama.setVisible(true);
	
}

//Interface MouseListener
public void mouseClicked(MouseEvent arg0) {
	if (this.monJFrameConfig.souris) 
		this.apres.doClick();
}

public void mouseEntered(MouseEvent arg0) {
	// TODO Auto-generated method stub	
}

public void mouseExited(MouseEvent arg0) {
	// TODO Auto-generated method stub	
}

public void mousePressed(MouseEvent arg0) {
	// TODO Auto-generated method stub	
}

public void mouseReleased(MouseEvent arg0) {
	// TODO Auto-generated method stub
	
}

// Interface KeyListener 
public void keyPressed(KeyEvent arg0) {
	// TODO Auto-generated method stub
}

public void keyReleased(KeyEvent arg0) {
	// TODO Auto-generated method stub
}

public void keyTyped(KeyEvent arg0) {
	if (this.monJFrameConfig.clavier) this.apres.doClick();
	
}
}
