package be.msyben.vinylManager.gui;

import be.msyben.vinylManager.gui.icon.Icons;
import be.msyben.vinylManager.properties.Language;
import be.msyben.vinylManager.properties.Preferences;
import be.msyben.vinylManager.tools.FontTool;
import be.msyben.vinylManager.tools.internalEntities.CompilationMode;
import be.msyben.vinylManager.tools.internalEntities.TreeSortColumnEnum;
import be.msyben.vinylManager.tools.swing.MyJComboBox;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

/**
 * Fenêtre pour ajuster les préférences.
 *
 * @author Mathieu Syben
 */
public class PrefWindow extends JFrame {

    /**
     * uid
     */
    private static final long serialVersionUID = 1l;
    /**
     * Nombre de choix pour les compilations.
     */
    private static final int NBR_CHOIX_COMPILATION = 2;
    /**
     * checkbox pour ouvrir le dernier fichier au démarrage
     */
    private JCheckBox ouvrirDernier = new JCheckBox(Language.getInstance().get("prefs.open.last"));
    /**
     * checkbox pour redimensionner les images
     */
    private JCheckBox redimImage = new JCheckBox(Language.getInstance().get("prefs.images.resize"));
    /**
     * checkbox pour vérifier la dernière version
     */
    private JCheckBox lastVersionCheck = new JCheckBox(Language.getInstance().get("prefs.last.version.check"));
    /**
     * checkbox pour minimiser dans le system tray
     */
    private JCheckBox minimizeToTray = new JCheckBox(Language.getInstance().get("prefs.last.minimize.to.tray"));
    /**
     * checkbox pour afficher le nombre d'albums dans la collection dans le
     * titre
     */
    private JCheckBox showNbrAlbums = new JCheckBox(Language.getInstance().get("prefs.show.nbr.albums"));
    /**
     * checkbox pour ne pas tenir compte des 'le' 'la' 'les' 'the'
     */
    private JCheckBox removePrefix = new JCheckBox(Language.getInstance().get("prefs.remove.prefix"));
    /**
     * selection langue
     */
    private MyJComboBox<String> choixLangue = new MyJComboBox<>();
    /**
     * selection export html
     */
    private MyJComboBox<String> choixExportHtml = new MyJComboBox<>();
    /**
     * selection mode fonctionnement compilation
     */
    private MyJComboBox<CompilationMode> choixCompilation = new MyJComboBox<>();
    private JRadioButton affichageStandard = new JRadioButton(Language.getInstance().get("prefs.display.standard"));
    /**
     * checkbox pour ne pas tenir compte des 'le' 'la' 'les' 'the'
     */
    private JCheckBox artistInUpperCase = new JCheckBox(Language.getInstance().get("prefs.name.upper.case"));
    private JRadioButton affichageArbre = new JRadioButton(Language.getInstance().get("prefs.display.tree"));
    private ButtonGroup affichage = new ButtonGroup();
    private MyJComboBox<TreeSortColumnEnum> selectionnableCritere = new MyJComboBox<>();
    private MyJComboBox<TreeSortColumnEnum> critere1 = new MyJComboBox<>();
    private MyJComboBox<TreeSortColumnEnum> critere2 = new MyJComboBox<>();
    private MyJComboBox<TreeSortColumnEnum> critere3 = new MyJComboBox<>();
    private JCheckBox displaySubNodeAlbums = new JCheckBox(Language.getInstance().get("prefs.tree.display.sub.nodes.album"));
    private JCheckBox displayRootAlbums = new JCheckBox(Language.getInstance().get("prefs.tree.display.root.album"));
    private JLabel policeL = new JLabel(Language.getInstance().get("prefs.police"));
    private MyJComboBox<String> police = new MyJComboBox<>();
    private MyJComboBox<String> taille = new MyJComboBox<>();
    private JLabel dateFormatL = new JLabel(Language.getInstance().get("prefs.date.format"));
    private MyJComboBox<String> dateFormat = new MyJComboBox<>();
    
    
    private JLabel choixLangueLabel = new JLabel(Language.getInstance().get("prefs.language.choice"));
    private JLabel choixExportHtmlLabel = new JLabel(Language.getInstance().get("prefs.export.html.choice"));
    private JLabel choixCompilationLabel = new JLabel(Language.getInstance().get("prefs.compilation"));
    private JLabel asterisque = new JLabel(Language.getInstance().get("prefs.asterisque"));
    
    private JLabel displayDuration = new JLabel(Language.getInstance().get("prefs.display.duration"));
    private JCheckBox displayDurationAlbum = new JCheckBox(Language.getInstance().get("name.album"));
    private JCheckBox displayDurationDisc = new JCheckBox(Language.getInstance().get("name.disque"));
    private JCheckBox displayDurationSide = new JCheckBox(Language.getInstance().get("name.face"));
    
    private JLabel discogsConsumerSecretLabel = new JLabel(Language.getInstance().get("prefs.discogs.consumer.secret"));
    private JTextField discogsConsumerSecret = new JTextField();
    private JLabel discogsConsumerKeyLabel = new JLabel(Language.getInstance().get("prefs.discogs.consumer.key"));
    private JTextField discogsConsumerKey = new JTextField();
    private JLabel discogsTokenLabel = new JLabel(Language.getInstance().get("prefs.discogs.token"));
    private JTextField discogsToken = new JTextField();
    
    private JLabel proxyHttpUrlLabel = new JLabel(Language.getInstance().get("prefs.proxy.http.url"));
    private JTextField proxyHttpUrl = new JTextField();
    private JLabel proxyHttpPortLabel = new JLabel(Language.getInstance().get("prefs.proxy.http.port"));
    private JTextField proxyHttpPort = new JTextField();
    private JLabel proxyHttpsUrlLabel = new JLabel(Language.getInstance().get("prefs.proxy.https.url"));
    private JTextField proxyHttpsUrl = new JTextField();
    private JLabel proxyHttpsPortLabel = new JLabel(Language.getInstance().get("prefs.proxy.https.port"));
    private JTextField proxyHttpsPort = new JTextField();
    
    /**
     * Tabbed Panel
     */
    private JTabbedPane tab = new JTabbedPane();

    /**
     * Creates a new instance of PrefWindow
     */
    public PrefWindow() {
        super(Language.getInstance().get("prefs.window.title"));
        setIconImage(Icons.getInstance().icon.getImage());
        MainWindow.getInstance().setEnabled(false);
        this.setSize(515, 410); // On donne une taille à notre fenêtre
        this.setLocationRelativeTo(null); // On centre la fenêtre sur l'ecran
        this.setResizable(false); // On interdit la redimensionnement de la fenêtre
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.getContentPane().setLayout(null);
        putElements();
        setVisible(true);
    }

    /**
     * Place les éléments dans la fenêtre
     */
    private void putElements() {
        this.getContentPane().setLayout(null);

        JPanel general = getGeneral();
        tab.addTab(Language.getInstance().get("prefs.tab.general"), general);


        JPanel affichagePanel = getAffichagePanel();
        tab.addTab(Language.getInstance().get("prefs.tab.display"), affichagePanel);
        
        JPanel discogsPanel = getDiscogsPanel();
        tab.addTab(Language.getInstance().get("prefs.tab.discogs"), discogsPanel);
        
        JPanel proxyPanel = getProxyPanel();
        tab.addTab(Language.getInstance().get("prefs.tab.proxy"), proxyPanel);
        

        tab.setBounds(0, 0, 500, 340);
        this.getContentPane().add(tab);
        JButton sauver = new JButton(Language.getInstance().get("button.sauver"));
        JButton annuler = new JButton(Language.getInstance().get("button.annuler"));
        JButton appliquer = new JButton(Language.getInstance().get("button.appliquer"));

        sauver.setBounds(75, 345, 110, 25);
        annuler.setBounds(200, 345, 110, 25);
        appliquer.setBounds(325, 345, 110, 25);

        this.getContentPane().add(sauver);
        this.getContentPane().add(annuler);
        this.getContentPane().add(appliquer);

        sauver.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                sauverPrefs();
                MainWindow.getInstance().setEnabled(true);
                MainWindow.getInstance().toFront();
                MainWindow.getInstance().refreshFromPreferences();
                dispose();
            }
        });
        annuler.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                MainWindow.getInstance().setEnabled(true);
                MainWindow.getInstance().toFront();
                dispose();
            }
        });
        appliquer.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                sauverPrefs();
                MainWindow.getInstance().refreshFromPreferences();
                MainWindow.getInstance().setEnabled(false);
            }
        });

        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                MainWindow.getInstance().setEnabled(true);
                MainWindow.getInstance().toFront();
                MainWindow.getInstance().refreshFromPreferences();
                dispose();
            }
        });
    }
    
    private JPanel getProxyPanel() {
        JPanel proxyPanel = new JPanel();
        proxyPanel.setLayout(null);
        proxyPanel.setSize(500, 340);
        
        proxyHttpUrlLabel.setBounds(5,5,150,20);
        proxyPanel.add(proxyHttpUrlLabel);        
        proxyHttpUrl.setBounds(160,2,300,25);
        proxyHttpUrl.setText(Preferences.getInstance().get(Preferences.HTTP_PROXY_URL, ""));
        proxyPanel.add(proxyHttpUrl);
        
        proxyHttpPortLabel.setBounds(5,35,150,20);
        proxyPanel.add(proxyHttpPortLabel);        
        proxyHttpPort.setBounds(160,32,150,25);
        proxyHttpPort.setText(Preferences.getInstance().get(Preferences.HTTP_PROXY_PORT, ""));
        proxyPanel.add(proxyHttpPort);
        
        proxyHttpsUrlLabel.setBounds(5,65,150,20);
        proxyPanel.add(proxyHttpsUrlLabel);        
        proxyHttpsUrl.setBounds(160,62,300,25);
        proxyHttpsUrl.setText(Preferences.getInstance().get(Preferences.HTTPS_PROXY_URL, ""));
        proxyPanel.add(proxyHttpsUrl);
        
        proxyHttpsPortLabel.setBounds(5,95,150,20);
        proxyPanel.add(proxyHttpsPortLabel);        
        proxyHttpsPort.setBounds(160,92,150,25);
        proxyHttpsPort.setText(Preferences.getInstance().get(Preferences.HTTPS_PROXY_PORT, ""));
        proxyPanel.add(proxyHttpsPort);
        
        
        return proxyPanel;
    }

    /**
     * Crée et renvoie le panel 'Discogs'.
     *
     * @return le panel Discogs.
     */
    private JPanel getDiscogsPanel() {
        JPanel discogsPanel = new JPanel();
        discogsPanel.setLayout(null);
        discogsPanel.setSize(500, 340);
        
        discogsConsumerKeyLabel.setBounds(5,65,150,20);
        discogsPanel.add(discogsConsumerKeyLabel);
        
        discogsConsumerKey.setBounds(160,62,300,25);
        discogsConsumerKey.setText(Preferences.getInstance().get(Preferences.DISCOGS_CONSUMER_TOKEN_KEY, ""));
        discogsConsumerKey.setBackground(Color.LIGHT_GRAY);
        discogsPanel.add(discogsConsumerKey);
        
        discogsConsumerSecretLabel.setBounds(5,95,150,20);
        discogsPanel.add(discogsConsumerSecretLabel);
        
        discogsConsumerSecret.setBounds(160,92,300,25);
        discogsConsumerSecret.setText(Preferences.getInstance().get(Preferences.DISCOGS_CONSUMER_TOKEN_SECRET, ""));
        discogsConsumerSecret.setBackground(Color.LIGHT_GRAY);
        discogsPanel.add(discogsConsumerSecret);
        
        
        
        discogsTokenLabel.setBounds(5,5,150,20);
        discogsPanel.add(discogsTokenLabel);
        
        discogsToken.setBounds(160,2,300,25);
        discogsToken.setText(Preferences.getInstance().get(Preferences.DISCOGS_TOKEN, ""));
        discogsPanel.add(discogsToken);
        
        return discogsPanel;
    }
    
    /**
     * Crée et renvoie le panel 'affichage'.
     *
     * @return le panel affichage.
     */
    private JPanel getAffichagePanel() {
        JPanel affichagePanel = new JPanel();
        affichagePanel.setLayout(null);
        affichagePanel.setSize(500, 340);
        affichage.add(affichageStandard);
        affichage.add(affichageArbre);


        affichageStandard.setBounds(5, 5, 170, 20);
        affichagePanel.add(affichageStandard);
        
        selectionnableCritere.setBounds(175, 2, 150, 25);
        affichagePanel.add(selectionnableCritere);
        
        artistInUpperCase.setBounds(20, 30, 400, 25);
        artistInUpperCase.setSelected(Preferences.getInstance().get(Preferences.STANDARD_NAME_UPPER_CASE, "Y").equals("Y"));
        affichagePanel.add(artistInUpperCase);

        affichageArbre.setBounds(5, 60, 200, 20);
        affichagePanel.add(affichageArbre);


        ActionListener affichageListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (affichageStandard.isSelected()) {
                    selectionnableCritere.setEnabled(true);
                    critere1.setEnabled(false);
                    critere2.setEnabled(false);
                    critere3.setEnabled(false);
                    displayRootAlbums.setEnabled(false);
                    displaySubNodeAlbums.setEnabled(false);
                    artistInUpperCase.setEnabled(true);
                }
                if (affichageArbre.isSelected()) {
                    selectionnableCritere.setEnabled(false);
                    artistInUpperCase.setEnabled(false);
                    critere1.setEnabled(true);
                    critere2.setEnabled(critere1.getSelectedIndex() > 0);
                    critere3.setEnabled(critere2.getSelectedIndex() > 0);
                    displaySubNodeAlbums.setEnabled(true);
                    displayRootAlbums.setEnabled(displaySubNodeAlbums.isSelected());
                }
            }
        };

        affichageArbre.addActionListener(affichageListener);
        affichageStandard.addActionListener(affichageListener);

        for (TreeSortColumnEnum s : TreeSortColumnEnum.values()) {
            selectionnableCritere.addItem(s);
            critere1.addItem(s);
            critere2.addItem(s);
            critere3.addItem(s);
        }
        critere3.setSelectedItem(TreeSortColumnEnum.getTreeSortColumnEnum(Integer.parseInt(Preferences.getInstance().get(Preferences.TREE_CRITERE_3, "0"))));
        critere2.setSelectedItem(TreeSortColumnEnum.getTreeSortColumnEnum(Integer.parseInt(Preferences.getInstance().get(Preferences.TREE_CRITERE_2, "0"))));
        critere1.setSelectedItem(TreeSortColumnEnum.getTreeSortColumnEnum(Integer.parseInt(Preferences.getInstance().get(Preferences.TREE_CRITERE_1, "0"))));
        selectionnableCritere.setSelectedItem(TreeSortColumnEnum.getTreeSortColumnEnum(Integer.parseInt(Preferences.getInstance().get(Preferences.SELECTIONNABLE_CRITERE, "0"))));

        
        displaySubNodeAlbums.setSelected(Preferences.getInstance().get(Preferences.TREE_DISPLAY_SUB, "N").equals("Y"));
        displayRootAlbums.setSelected(Preferences.getInstance().get(Preferences.TREE_DISPLAY_ROOT, "N").equals("Y"));
        displaySubNodeAlbums.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                displayRootAlbums.setEnabled(displaySubNodeAlbums.isSelected());
                if (!displaySubNodeAlbums.isSelected()) {
                    displayRootAlbums.setSelected(false);
                }
            }
        });

        critere1.setBounds(20, 85, 150, 25);
        critere2.setBounds(175, 85, 150, 25);
        critere3.setBounds(330, 85, 150, 25);
        displaySubNodeAlbums.setBounds(20, 120, 300, 25);
        displayRootAlbums.setBounds(35, 150, 300, 25);

        int affichageChoisi = Integer.parseInt(Preferences.getInstance().get(Preferences.AFFICHAGE, "1"));
        switch (affichageChoisi) {
            case 1:
                affichageStandard.setSelected(true);
                selectionnableCritere.setEnabled(true);
                critere1.setEnabled(false);
                critere2.setEnabled(false);
                critere3.setEnabled(false);
                artistInUpperCase.setEnabled(true);
                displaySubNodeAlbums.setEnabled(false);
                displayRootAlbums.setEnabled(false);
                break;
            case 2:
                selectionnableCritere.setEnabled(false);
                affichageArbre.setSelected(true);
                critere1.setEnabled(true);
                critere2.setEnabled(critere1.getSelectedIndex() > 0);
                critere3.setEnabled(critere2.getSelectedIndex() > 0);
                artistInUpperCase.setEnabled(false);
                displaySubNodeAlbums.setEnabled(true);
                displayRootAlbums.setEnabled(displaySubNodeAlbums.isSelected());
                break;
            default:
                affichageStandard.setSelected(true);
                selectionnableCritere.setEnabled(true);
                critere1.setEnabled(false);
                critere2.setEnabled(false);
                critere3.setEnabled(false);
                artistInUpperCase.setEnabled(true);
                displaySubNodeAlbums.setEnabled(false);
                displayRootAlbums.setEnabled(false);
                break;
        }



        critere1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (critere1.getSelectedIndex() <= 0) {
                    critere2.setSelectedIndex(0);
                    critere2.setEnabled(false);
                } else {
                    critere2.setEnabled(true);
                }
            }
        });
        critere2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (critere2.getSelectedIndex() <= 0) {
                    critere3.setSelectedIndex(0);
                    critere3.setEnabled(false);
                } else {
                    critere3.setEnabled(true);
                }
            }
        });

        affichagePanel.add(critere1);
        affichagePanel.add(critere2);
        affichagePanel.add(critere3);
        affichagePanel.add(displaySubNodeAlbums);
        affichagePanel.add(displayRootAlbums);

        removePrefix.setSelected(Preferences.getInstance().get(Preferences.REMOVE_PREFIX, "N").equals("Y"));
        removePrefix.setBounds(5, 180, 300, 20);
        affichagePanel.add(removePrefix);

        displayDuration.setBounds(5, 208, 150, 20);
        affichagePanel.add(displayDuration);
        displayDurationAlbum.setSelected(Preferences.getInstance().get(Preferences.DISPLAY_DURATION_ALBUM, "N").equals("Y"));
        displayDurationDisc.setSelected(Preferences.getInstance().get(Preferences.DISPLAY_DURATION_DISC, "Y").equals("Y"));
        displayDurationSide.setSelected(Preferences.getInstance().get(Preferences.DISPLAY_DURATION_SIDE, "Y").equals("Y"));
        displayDurationAlbum.setBounds(160, 205, 75, 25);
        displayDurationDisc.setBounds(240, 205, 75, 25);
        displayDurationSide.setBounds(320, 205, 75, 25);
        affichagePanel.add(displayDurationAlbum);
        affichagePanel.add(displayDurationDisc);
        affichagePanel.add(displayDurationSide);

        policeL.setBounds(5, 248, 100, 20);
        affichagePanel.add(policeL);

        GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
        police.addItem("");
        String selectedPolice = Preferences.getInstance().get(Preferences.POLICE, "");
        for (Font f : e.getAllFonts()) {
            police.addItem(f.getFontName());
        }
        police.setSelectedItem(selectedPolice);
        police.setBounds(55, 245, 200, 25);
        affichagePanel.add(police);

        taille.addItem("");
        String selectedPoliceSize = Preferences.getInstance().get(Preferences.POLICE_SIZE, "");
        for (int i = 1; i < 25; i++) {
            taille.addItem("" + i);
        }
        taille.setSelectedItem(selectedPoliceSize);

        taille.setBounds(270, 245, 100, 25);
        affichagePanel.add(taille);

        
        dateFormatL.setBounds(5, 278, 100, 20);
        affichagePanel.add(dateFormatL);
        
        dateFormat.setBounds(175, 275, 200, 25);
        dateFormat.addItem("dd/MM/yyyy");
        dateFormat.addItem("yyyy/MM/yy");
        dateFormat.addItem("dd MMM yyyy");
        dateFormat.addItem("dd MMMM yyyy");
        dateFormat.setSelectedItem(Preferences.getInstance().get(Preferences.DATE_FORMAT, "dd/MM/yyyy"));
        affichagePanel.add(dateFormat);
        
        
        return affichagePanel;
    }

    /**
     * Ajout les possibilités dans la combo box de compilation.
     */
    private void setChoixCompilation() {
        int currentSelection = 1;
        try {
            currentSelection = Integer.parseInt(Preferences.getInstance().get(Preferences.COMPILATION_MODE, "1"));
        } catch (NumberFormatException nfe) {
            currentSelection = 1;
        }
        for (int i = 1; i <= NBR_CHOIX_COMPILATION; i++) {
            choixCompilation.addItem(new CompilationMode(i));
            if (i == currentSelection) {
                choixCompilation.setSelectedIndex(i - 1);
            }
        }
    }

    /**
     * Ajoute les langues possible dans la combobox
     */
    private void setChoixExportHtml() {
        File f = new File("export_html");
        choixExportHtml.addItem(Language.getInstance().get("prefs.export.html.defaut"));
        if (f.exists() && f.isDirectory()) {
            for (String filename : f.list()) {
                if (filename.toLowerCase().endsWith(".zip")) {
                    choixExportHtml.addItem(filename.substring(0, filename.length() - 4));
                }
            }
        }

        String lastSelect = Preferences.getInstance().get(Preferences.EXPORT_HTML_FILE);
        if (lastSelect != null && !lastSelect.equals("")) {
            choixExportHtml.setSelectedItem(lastSelect);
        }
        if (choixExportHtml.getSelectedItem() == null) {
            choixExportHtml.setSelectedIndex(0);
        }
    }

    /**
     * Ajoute les langues possible dans la combobox
     */
    private void setChoixLangues() {
        File f = new File("languages");

        choixLangue.addItem("Français (default)");
        if (f.exists() && f.isDirectory()) {
            for (String filename : f.list()) {
                if (filename.toLowerCase().endsWith(".lng")) {
                    choixLangue.addItem(filename.substring(0, filename.length() - 4));
                }
            }
        }

        String lastSelect = Preferences.getInstance().get(Preferences.LANGUAGE_FILE);
        if (lastSelect != null && !lastSelect.equals("")) {
            choixLangue.setSelectedItem(lastSelect);
        }
        if (choixLangue.getSelectedItem() == null) {
            choixLangue.setSelectedIndex(0);
        }
    }

    /**
     * Crée et renvoie le panel 'general'.
     *
     * @return le panel general.
     */
    private JPanel getGeneral() {
        JPanel general = new JPanel();
        general.setLayout(null);
        general.setSize(500, 340);

        ouvrirDernier.setSelected(Preferences.getInstance().get(Preferences.OPEN_LAST_FILE, "N").equals("Y"));
        ouvrirDernier.setBounds(5, 5, 300, 20);
        general.add(ouvrirDernier);

        choixLangueLabel.setBounds(5, 35, 140, 20);
        general.add(choixLangueLabel);

        setChoixLangues();
        choixLangue.setBounds(145, 35, 340, 25);
        general.add(choixLangue);


        choixExportHtmlLabel.setBounds(5, 65, 240, 20);
        general.add(choixExportHtmlLabel);

        setChoixExportHtml();
        choixExportHtml.setBounds(245, 65, 240, 25);
        general.add(choixExportHtml);

        choixCompilationLabel.setBounds(5, 95, 240, 20);
        general.add(choixCompilationLabel);
        setChoixCompilation();
        choixCompilation.setBounds(245, 95, 240, 25);
        general.add(choixCompilation);

        redimImage.setSelected(Preferences.getInstance().get(Preferences.RESIZE_PICTURES, "Y").equals("Y"));
        redimImage.setBounds(5, 125, 400, 20);
        general.add(redimImage);

        lastVersionCheck.setSelected(Preferences.getInstance().get(Preferences.LAST_VERSION_CHECK, "Y").equals("Y"));
        lastVersionCheck.setBounds(5, 155, 400, 20);
        general.add(lastVersionCheck);

        minimizeToTray.setSelected(Preferences.getInstance().get(Preferences.MINIMIZE_TO_TRAY, "N").equals("Y"));
        minimizeToTray.setBounds(5, 185, 300, 20);
        general.add(minimizeToTray);

        showNbrAlbums.setSelected(Preferences.getInstance().get(Preferences.SHOW_NBR_ALBUMS, "N").equals("Y"));
        showNbrAlbums.setBounds(5, 215, 300, 20);
        general.add(showNbrAlbums);
        asterisque.setBounds(5, 255, 380, 20);
        //general.add(asterisque);
        return general;
    }

    /**
     * Sauve les préférences
     */
    private void sauverPrefs() {
        Preferences.getInstance().set(Preferences.OPEN_LAST_FILE, ouvrirDernier.isSelected() ? "Y" : "N");
        Preferences.getInstance().set(Preferences.RESIZE_PICTURES, redimImage.isSelected() ? "Y" : "N");
        Preferences.getInstance().set(Preferences.LAST_VERSION_CHECK, lastVersionCheck.isSelected() ? "Y" : "N");
        Preferences.getInstance().set(Preferences.MINIMIZE_TO_TRAY, minimizeToTray.isSelected() ? "Y" : "N");
        Preferences.getInstance().set(Preferences.REMOVE_PREFIX, removePrefix.isSelected() ? "Y" : "N");
        Preferences.getInstance().set(Preferences.SHOW_NBR_ALBUMS, showNbrAlbums.isSelected() ? "Y" : "N");
        Preferences.getInstance().set(Preferences.COMPILATION_MODE, ((CompilationMode) choixCompilation.getSelectedItem()).getId() + "");
        if (choixLangue.getSelectedIndex() > 0) {
            Preferences.getInstance().set(Preferences.LANGUAGE_FILE, (String) choixLangue.getSelectedItem());
        } else {
            Preferences.getInstance().set(Preferences.LANGUAGE_FILE, "");
        }
        if (choixExportHtml.getSelectedIndex() > 0) {
            Preferences.getInstance().set(Preferences.EXPORT_HTML_FILE, (String) choixExportHtml.getSelectedItem());
        } else {
            Preferences.getInstance().set(Preferences.EXPORT_HTML_FILE, "");
        }

        if (affichageStandard.isSelected()) {
            Preferences.getInstance().set(Preferences.AFFICHAGE, "1");
        } else if (affichageArbre.isSelected()) {
            Preferences.getInstance().set(Preferences.AFFICHAGE, "2");
        }

        Preferences.getInstance().set(Preferences.STANDARD_NAME_UPPER_CASE, artistInUpperCase.isSelected() ? "Y" : "N");

        Preferences.getInstance().set(Preferences.TREE_DISPLAY_SUB, displaySubNodeAlbums.isSelected() ? "Y" : "N");
        Preferences.getInstance().set(Preferences.TREE_DISPLAY_ROOT, displayRootAlbums.isSelected() ? "Y" : "N");


        Preferences.getInstance().set(Preferences.TREE_CRITERE_1, ((TreeSortColumnEnum) critere1.getSelectedItem()).getId() + "");
        Preferences.getInstance().set(Preferences.TREE_CRITERE_2, ((TreeSortColumnEnum) critere2.getSelectedItem()).getId() + "");
        Preferences.getInstance().set(Preferences.TREE_CRITERE_3, ((TreeSortColumnEnum) critere3.getSelectedItem()).getId() + "");


        Preferences.getInstance().set(Preferences.SELECTIONNABLE_CRITERE, ((TreeSortColumnEnum) selectionnableCritere.getSelectedItem()).getId() + "");

        Preferences.getInstance().set(Preferences.DISPLAY_DURATION_ALBUM, displayDurationAlbum.isSelected() ? "Y" : "N");
        Preferences.getInstance().set(Preferences.DISPLAY_DURATION_DISC, displayDurationDisc.isSelected() ? "Y" : "N");
        Preferences.getInstance().set(Preferences.DISPLAY_DURATION_SIDE, displayDurationSide.isSelected() ? "Y" : "N");        

        Preferences.getInstance().set(Preferences.POLICE_SIZE, taille.getSelectedItem().toString());
        Preferences.getInstance().set(Preferences.POLICE, police.getSelectedItem().toString());
        Preferences.getInstance().set(Preferences.DATE_FORMAT, dateFormat.getSelectedItem().toString());

        Preferences.getInstance().set(Preferences.DISCOGS_CONSUMER_TOKEN_KEY, discogsConsumerKey.getText());
        Preferences.getInstance().set(Preferences.DISCOGS_CONSUMER_TOKEN_SECRET, discogsConsumerSecret.getText());
        Preferences.getInstance().set(Preferences.DISCOGS_TOKEN, discogsToken.getText());
        
        Preferences.getInstance().set(Preferences.HTTP_PROXY_URL, proxyHttpUrl.getText());
        if (proxyHttpPort.getText() != null) {
            try {
                int port = Integer.parseInt(proxyHttpPort.getText());
                if (port > 0 && port < 65535) {
                    Preferences.getInstance().set(Preferences.HTTP_PROXY_PORT, ""+port);
                } else {
                    Preferences.getInstance().set(Preferences.HTTP_PROXY_PORT, "");
                }
            } catch (NumberFormatException ex) {
                Preferences.getInstance().set(Preferences.HTTP_PROXY_PORT, "");
            }
        } else {
            Preferences.getInstance().set(Preferences.HTTP_PROXY_PORT, "");
        }
        
        Preferences.getInstance().set(Preferences.HTTPS_PROXY_URL, proxyHttpsUrl.getText());
        if (proxyHttpsPort.getText() != null) {
            try {
                int port = Integer.parseInt(proxyHttpsPort.getText());
                if (port > 0 && port < 65535) {
                    Preferences.getInstance().set(Preferences.HTTPS_PROXY_PORT, ""+port);
                } else {
                    Preferences.getInstance().set(Preferences.HTTPS_PROXY_PORT, "");
                }
            } catch (NumberFormatException ex) {
                Preferences.getInstance().set(Preferences.HTTPS_PROXY_PORT, "");
            }
        } else {
            Preferences.getInstance().set(Preferences.HTTPS_PROXY_PORT, "");
        }
        
        Preferences.getInstance().save();
        if (FontTool.setFont()) {
            FontTool.refreshFont(PrefWindow.this);
        }


    }
}
