import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class KlasorIcindekiResimDosyalari {

	 public static JButton butonKlasorSec;
	 public static JPanel infoPanel, butonPaneli;
	 public static JFileChooser fileChooser;
	
	KlasorIcindekiResimDosyalari() {
		
		JFrame frame = new JFrame("JFileChooser ile Klasördeki Dosya Bilgileri | Java Blackboard [101]");
		
		butonPaneli = new JPanel();
		butonPaneli.setLayout(null);
		butonPaneli.setBackground(new Color(64, 64, 64));
		butonPaneli.setBounds(0,0,800,75);
		
		infoPanel = new JPanel();
        infoPanel.setLayout(null);
        infoPanel.setPreferredSize(new Dimension(800,800));
        
        JScrollPane infoPane = new JScrollPane(infoPanel);
        infoPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        infoPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        infoPane.setBackground(new Color(115, 95, 108));
        infoPane.setBounds(0,80,800,475);  // JScrollPane'in gorulebilir alan ölçüsü.
        infoPane.setPreferredSize(new Dimension(800,1000));  // ScrollBar'in kaydirilacagi alan öçüsü
        
        fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Klasör Seç");
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
       
		butonKlasorSec = new JButton("KLASÖR SEÇ");
		butonKlasorSec.setBounds(325,10,150,30);
        
		butonKlasorSec.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent event) {
        	
        		 int kullanicinSecimi = fileChooser.showOpenDialog(null);
        		 
        		 int x = 10;
                 int y = 5;
                 int w = 600;
                 int h = 25;
                 int f = 20;
        		 
                 if (kullanicinSecimi == JFileChooser.APPROVE_OPTION) {
                 
                	 File klasor = fileChooser.getSelectedFile();
                     
                	 File[] dosyalar = klasor.listFiles();
                     
                	 infoPanel.removeAll();
                     
                	 int dosyaSayisi = 0;
                	 
                	 for (File dosya : dosyalar) {
                		 
                		 // Sadece Klasorleri verir.
                	//   if (dosya.isDirectory()) {  
                                
                		// Sadece dosyalari verir
                	//	 if (dosya.isFile()) {  
                		 
                		// Sadece JPEG JPG PNG dosyalarini verir
                		 if (dosya.isFile() && (dosya.getName().endsWith(".jpg") || dosya.getName().endsWith(".jpeg") || dosya.getName().endsWith(".png"))) {
                             	 
                			 JPanel dosyaPaneli = new JPanel();
                			 dosyaPaneli.setBackground(new Color(191, 164, 187));
                			 dosyaPaneli.setLayout(null);
                			 dosyaPaneli.setBounds(10, 10+(dosyaSayisi*150), 760, 135);
                             
                             // Bu satir dosya yolunu console'a yazdirma islemi yapar.
                       //   System.out.println(dosya.getAbsolutePath());
                             
                             ImageIcon icon = new ImageIcon(dosya.getAbsolutePath());
                             
                             JLabel resimLabel = new JLabel(icon);
                             resimLabel.setBounds(10,10,115, 115);
                             
                             dosyaPaneli.add(resimLabel);
                             
                             JPanel dosyaInfoPaneli = new JPanel();
                             dosyaInfoPaneli.setLayout(null);
                             dosyaInfoPaneli.setBackground(new Color(217, 193, 215));
                             dosyaInfoPaneli.setBounds(135,10,610,115);
                            
                             JLabel dosyaIsim = new JLabel((dosyaSayisi+1) + ". Dosya ismi: " + dosya.getName());
         					 JLabel dosyaBoyutu = new JLabel("Dosya boyutu: " + icon.getIconWidth() + " x " + icon.getIconHeight());
                             JLabel dosyaOlcu = new JLabel("Dosya olcusu: " + dosya.length() + " bytes");
         					 JLabel dosyaYol = new JLabel("Dosya yolu: " + dosya.getAbsolutePath());
         					 JLabel dosyaTarih = new JLabel("Dosya tarihi: " + new Date(dosya.lastModified()).toString());
                             
         					 dosyaIsim.setBounds(x,y,w,h); y = y + f;
         					 dosyaBoyutu.setBounds(x,y,w,h); y = y + f;
         					 dosyaOlcu.setBounds(x,y,w,h); y = y + f;
         					 dosyaYol.setBounds(x,y,w,h); y = y + f;
         					 dosyaTarih.setBounds(x,y,w,h); y = 5;
                             
         					 dosyaInfoPaneli.add(dosyaIsim);
         					 dosyaInfoPaneli.add(dosyaBoyutu);
         					 dosyaInfoPaneli.add(dosyaOlcu);
         					 dosyaInfoPaneli.add(dosyaYol);
         					 dosyaInfoPaneli.add(dosyaTarih);
         					 
         					 dosyaPaneli.add(dosyaInfoPaneli);
         					 
                             infoPanel.add(dosyaPaneli);
                             
                    		 dosyaSayisi++;
                         }
                	        
                		 infoPanel.setPreferredSize(new Dimension(800,(150*dosyaSayisi)+10));
                         infoPanel.revalidate();
                         infoPanel.repaint();
                     }
                 }
        		
        	}
        	
        });
        
		butonPaneli.add(butonKlasorSec);
        
		frame.add(butonPaneli);
		frame.add(infoPane);
		
		frame.setSize(800,600);
		frame.setLayout(null);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
}