/**
 * 
         -------- Copyright (C) [2012]  [Kristofer SCHWEIGER & Niklas FLORIAN1]---------

    This program is free software; you can redistribute it and/or modify it under the terms of the
 *  GNU General Public License as published by the Free Software Foundation; either version 2 of the License,
 *  or (at your option) any later version.
    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 * 
    You should have received a copy of the GNU General Public License along with this program; if not, 
 *  write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA

 * 
 * For any licensing issues please contact us under following adress: Mosaik.Projekt.2012@gmail.com
 */

package gui;

import java.awt.BorderLayout;
import java.io.File;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;



public class ChoosePicture extends javax.swing.JDialog {

    {
        // Set Look & Feel
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private javax.swing.JFileChooser jFileChooser1;
    private MosaikApp mosaik;
 

    public ChoosePicture(java.awt.Frame parent, boolean modal) {

        super(parent, modal);
        mosaik = (MosaikApp) parent;
        
        initGUI();
        jFileChooser1.removeChoosableFileFilter(jFileChooser1.getFileFilter());
        jFileChooser1.addChoosableFileFilter(new ImportFilefilter());
       
    }


    private void initGUI() {
        try {
            {
                this.setTitle("Bild wählen");
                jFileChooser1 = new JFileChooser();
                jFileChooser1.setFileSelectionMode(JFileChooser.FILES_ONLY);
                jFileChooser1.setMultiSelectionEnabled(false);
                getContentPane().add(jFileChooser1, BorderLayout.CENTER);

                jFileChooser1.removeChoosableFileFilter(jFileChooser1.getFileFilter());
                jFileChooser1.addChoosableFileFilter(new ImportFilefilter());

                jFileChooser1.addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(
                            java.awt.event.ActionEvent evt) {
                        try {
                            jFileChooser1ActionPerformed(evt);
                        } catch (IOException ex) {
                            Logger.getLogger(ChoosePicture.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                });

            }
            this.setSize(676, 423);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) throws IOException {// GEN-FIRST:event_jFileChooser1ActionPerformed
        // SchlieÃƒÂŸt das Fenster
        if (evt.getActionCommand().equals("CancelSelection")) {
            this.setVisible(false);
            this.dispose();
        }
        // FÃƒÂ¼hrt den Inmport durch
        if (evt.getActionCommand().equals("ApproveSelection")) {
           mosaik.picturePathofTheOrinignalFile = jFileChooser1.getSelectedFile();
            System.out.print(mosaik.picturePathofTheOrinignalFile);
                            
                    
            mosaik.setPictureofJLabelPic();
            mosaik.enableComponents1(true);
            mosaik.jButton5.setEnabled(false);
            this.setVisible(false);
            this.dispose();

         
        }
    }// GEN-LAST:event_jFileChooser1ActionPerformed
}
/**
 * Filter fÃƒÂ¼r den Import
 *
 * @author Niklas F.
 */
class ImportFilefilter extends javax.swing.filechooser.FileFilter {

    /**
     * @param f
     */
    @Override
    public boolean accept(File f) {
        if (f.isDirectory()) {
            return true;
        }
        String name = f.getName();
        int i = name.lastIndexOf('.');

        if (i > 0 && i < name.length() - 1) {
            String type = name.substring(i + 1).toLowerCase();		// shorter ;)

            for (String format : FORMATS) {
                if (type.equals(format)) {
                    return true;
                }
            }

        }

        return false;

    }
    static final String[] FORMATS = ImageIO.getReaderFormatNames(); // all formats ;)

    /**
     * @return
     */
    @Override
    public String getDescription() {

        String ret = "";
        for (String format : FORMATS) {
            ret += "*." + format.toLowerCase() + " ";
        }

        return ret;
      
    }
}
