package turgen;

import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.io.*;
import javax.swing.*;
import turgen.playlist.AbstractPlayListItem;
import turgen.plugin.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import turgen.config.StringConfigEntry;
import turgen.dtb.StatisticsDialog;
import turgen.generator.PulseCrate;


/**
 * Dialog for editing and creating playlist items
 *
 */
public class PlayListItemDialog extends javax.swing.JDialog implements InOutProvider,UIPersistor{
    
    
    private StringConfigEntry cfgFavoriteWaveDirectory;
    private ArrayList auxFileChoosers;
    private int persistenceSelectedIndex;
    private boolean werePersistenceDefaultsUsed;
    

    /**
     *New instance
     * @param owner Owner of this dialog
     * 
     */
    public PlayListItemDialog(JFrame owner) {
        super(owner);
        initComponents();
        this.getRootPane().setDefaultButton(btnOK);
        /*Favortie wave directory*/
        cfgFavoriteWaveDirectory=new StringConfigEntry("favoriteOutputDirectory","Favorite output directory", null);
        Turgen.cfcTurgen.addEntry(cfgFavoriteWaveDirectory);
        auxFileChoosers = new ArrayList();
        werePersistenceDefaultsUsed = false;
                
    }
    
    public PlayListItemDialog() {
        this.setModal(true);
        initComponents();
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btgrTypes = new javax.swing.ButtonGroup();
        jfcInputFile = new javax.swing.JFileChooser();
        jfcOutputFile = new javax.swing.JFileChooser();
        jfcAltInputFile = new javax.swing.JFileChooser();
        jfcAltOutputFile = new javax.swing.JFileChooser();
        pMainPart = new javax.swing.JPanel();
        pCommonPart = new javax.swing.JPanel();
        pPlugin = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jcoPlugins = new javax.swing.JComboBox();
        PMain = new javax.swing.JPanel();
        jlbInFile = new javax.swing.JLabel();
        jtfInFile = new javax.swing.JTextField();
        btnBrowseIn = new javax.swing.JButton();
        jlbOutFile = new javax.swing.JLabel();
        jtfOutFile = new javax.swing.JTextField();
        btnBrowseOut = new javax.swing.JButton();
        pItemSettings = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        pPulsePart = new javax.swing.JPanel();
        lblPulses = new javax.swing.JLabel();
        jcoPulses = new javax.swing.JComboBox();
        pControls = new javax.swing.JPanel();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Playlist item");
        setModal(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                dialogClosing(evt);
            }
        });

        pMainPart.setMinimumSize(new java.awt.Dimension(0, 0));
        pMainPart.setLayout(new java.awt.BorderLayout());

        pCommonPart.setLayout(new java.awt.BorderLayout());

        pPlugin.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pPlugin.setToolTipText("Select plugin here");
        pPlugin.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 4, 4));

        jLabel1.setText("PLUGIN:");
        pPlugin.add(jLabel1);

        jcoPlugins.setMaximumRowCount(12);
        jcoPlugins.setToolTipText("Choose plugin that you want to use");
        jcoPlugins.setRenderer(new PluginCellRenderer());
        jcoPlugins.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                on_plugin_changed(evt);
            }
        });
        pPlugin.add(jcoPlugins);

        pCommonPart.add(pPlugin, java.awt.BorderLayout.NORTH);

        PMain.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        PMain.setToolTipText("");
        PMain.setLayout(new java.awt.GridBagLayout());

        jlbInFile.setForeground(java.awt.Color.blue);
        jlbInFile.setText("Input file:");
        jlbInFile.setToolTipText("Click to obtain information about input file");
        jlbInFile.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                onInputFileLabelClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                on_inFile_mouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                on_inFile_mouseExited(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        PMain.add(jlbInFile, gridBagConstraints);

        jtfInFile.setColumns(24);
        jtfInFile.setToolTipText("File that will be converted");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        PMain.add(jtfInFile, gridBagConstraints);

        btnBrowseIn.setMnemonic('b');
        btnBrowseIn.setText("Browse");
        btnBrowseIn.setToolTipText("Specify input file using file chooser");
        btnBrowseIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseInon_input_browse(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 4);
        PMain.add(btnBrowseIn, gridBagConstraints);

        jlbOutFile.setForeground(java.awt.Color.blue);
        jlbOutFile.setText("Output file:");
        jlbOutFile.setToolTipText("Click to automatically set output file");
        jlbOutFile.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                output_file_label_clicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                on_outputFile_mouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                on_outFile_mouseExited(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        PMain.add(jlbOutFile, gridBagConstraints);

        jtfOutFile.setColumns(24);
        jtfOutFile.setToolTipText("Output file (WAVE file or tape image)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        PMain.add(jtfOutFile, gridBagConstraints);

        btnBrowseOut.setMnemonic('o');
        btnBrowseOut.setText("Browse");
        btnBrowseOut.setToolTipText("Specify output file using file chooser");
        btnBrowseOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseOuton_output_browse(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 4);
        PMain.add(btnBrowseOut, gridBagConstraints);

        pCommonPart.add(PMain, java.awt.BorderLayout.CENTER);

        pMainPart.add(pCommonPart, java.awt.BorderLayout.NORTH);

        pItemSettings.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pItemSettings.setLayout(new java.awt.CardLayout());

        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jLabel2.setText("<html>\n0<br> \n0<br> \n0<br> \n0<br> \n0<br> \n0<br> \n0<br> \n0<br> \n0<br> \n0<br> \n0<br> \n</html>");
        jPanel1.add(jLabel2);

        pItemSettings.add(jPanel1, "card3");

        pMainPart.add(pItemSettings, java.awt.BorderLayout.CENTER);

        pPulsePart.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pPulsePart.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        lblPulses.setText("Pulses:");
        pPulsePart.add(lblPulses);

        pPulsePart.add(jcoPulses);

        pMainPart.add(pPulsePart, java.awt.BorderLayout.SOUTH);

        getContentPane().add(pMainPart, java.awt.BorderLayout.CENTER);

        btnOK.setForeground(java.awt.Color.blue);
        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });
        pControls.add(btnOK);

        btnCancel.setForeground(java.awt.Color.red);
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        pControls.add(btnCancel);

        btnClear.setForeground(new java.awt.Color(0, 157, 0));
        btnClear.setText("Clear");
        btnClear.setToolTipText("Clear this dialog");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });
        pControls.add(btnClear);

        getContentPane().add(pControls, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBrowseOuton_output_browse(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseOuton_output_browse
       
        /*Primary or alternate ?*/
        JFileChooser chooser = jfcOutputFile;
        if (getSelectedPlugin().isUsingAlternateFileChooser()==true) {
            chooser = jfcAltOutputFile;
        }
        
        /*Shift - setup the dialog*/
        if ( (evt.getModifiers() & ActionEvent.SHIFT_MASK)!=0) {
            File sf = new File(jtfOutFile.getText());
            if (sf.isFile()) chooser.setSelectedFile(sf);
        }
        
        int r = chooser.showSaveDialog(this);
        if (r==JFileChooser.APPROVE_OPTION) {
            File f = chooser.getSelectedFile();
            if (f!=null) {
                jtfOutFile.setText(f.getAbsolutePath());
            }
        }
    }//GEN-LAST:event_btnBrowseOuton_output_browse

    private void btnBrowseInon_input_browse(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseInon_input_browse
        
        /*Primary or alternate*/
        JFileChooser chooser = jfcInputFile;
        if (getSelectedPlugin().isUsingAlternateFileChooser()==true) {
            chooser = jfcAltInputFile;
        }
        
        /*Shift - setup the dialog*/
        if ( (evt.getModifiers() & ActionEvent.SHIFT_MASK)!=0) {
            File sf = new File(jtfInFile.getText());
            if (sf.isFile()) chooser.setSelectedFile(sf);
        }
        int r = chooser.showOpenDialog(this);
        if (r==JFileChooser.APPROVE_OPTION) {

            File f = chooser.getSelectedFile();
            if (f==null) return;
            
            jtfInFile.setText(f.getAbsolutePath());

            if (!f.isFile()==true) {
                warn("Selected input file is not a regular file.");
            }
            
        }
    }//GEN-LAST:event_btnBrowseInon_input_browse

    private void on_plugin_changed(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_on_plugin_changed
        if (evt.getStateChange()==ItemEvent.SELECTED) {

            /*Change appropriate card*/
            CardLayout c = (CardLayout)this.pItemSettings.getLayout();
            String s = Turgen.pluginManager.getPluginClassName((Plugin)jcoPlugins.getSelectedItem());
            if (s!=null) {
                c.show(pItemSettings,s);
                
            }
            /*Setup pulses panel*/
            jcoPulses.removeAllItems();
            PulseCrate[] pulses = Turgen.pulseCache.getPulsesForPlugin(s);
            for(int i=0;i<pulses.length;i++) {
                jcoPulses.addItem(pulses[i]);
            }
            
        }
    }//GEN-LAST:event_on_plugin_changed
        
    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
       clear(false);
    }//GEN-LAST:event_btnClearActionPerformed
            
    private void dialogClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_dialogClosing
        tmpRecord=null; 
        setVisible(false);
    }//GEN-LAST:event_dialogClosing
    
    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        tmpRecord=null;
        setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed
    
    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        
        /*Input file verfication*/
        File f = new File(this.getInFile());
        if (!f.exists()) {
            warn("Input file does not exist, this may result in further errors.");
        }
        
        /*Other data verification*/
        Plugin pgn = getSelectedPlugin();
        if (pgn==null) return;
        String vdi=pgn.validateItemOnPanel();
        if (vdi==null) {
            tmpRecord=pgn.getItemFromPanel();
            setVisible(false);
        }
        else {
            warn("<HTML>The playlist item is invalid for the following reason(s):<BR>"+vdi+"<BR>Correct the data entered.</HTML>");
            
        }
        
        
    }//GEN-LAST:event_btnOKActionPerformed

    private void output_file_label_clicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_output_file_label_clicked
        if (evt.getClickCount()==1) this.synchronizeOutputFile();
    }//GEN-LAST:event_output_file_label_clicked

    private void onInputFileLabelClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_onInputFileLabelClicked
        if (evt.getClickCount()!=1) return;
        showStatistics();
    }//GEN-LAST:event_onInputFileLabelClicked

    private void on_inFile_mouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_on_inFile_mouseEntered
        ((JComponent)evt.getSource()).setForeground(Color.red);
    }//GEN-LAST:event_on_inFile_mouseEntered

    private void on_outputFile_mouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_on_outputFile_mouseEntered
        ((JComponent)evt.getSource()).setForeground(Color.red);
    }//GEN-LAST:event_on_outputFile_mouseEntered

    private void on_inFile_mouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_on_inFile_mouseExited
        ((JComponent)evt.getSource()).setForeground(Color.blue);
}//GEN-LAST:event_on_inFile_mouseExited

    private void on_outFile_mouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_on_outFile_mouseExited
        ((JComponent)evt.getSource()).setForeground(Color.blue);
}//GEN-LAST:event_on_outFile_mouseExited
                
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PlayListItemDialog().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PMain;
    private javax.swing.ButtonGroup btgrTypes;
    private javax.swing.JButton btnBrowseIn;
    private javax.swing.JButton btnBrowseOut;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    public javax.swing.JComboBox jcoPlugins;
    private javax.swing.JComboBox jcoPulses;
    private javax.swing.JFileChooser jfcAltInputFile;
    private javax.swing.JFileChooser jfcAltOutputFile;
    private javax.swing.JFileChooser jfcInputFile;
    private javax.swing.JFileChooser jfcOutputFile;
    private javax.swing.JLabel jlbInFile;
    private javax.swing.JLabel jlbOutFile;
    private javax.swing.JTextField jtfInFile;
    private javax.swing.JTextField jtfOutFile;
    private javax.swing.JLabel lblPulses;
    private javax.swing.JPanel pCommonPart;
    private javax.swing.JPanel pControls;
    private javax.swing.JPanel pItemSettings;
    private javax.swing.JPanel pMainPart;
    private javax.swing.JPanel pPlugin;
    private javax.swing.JPanel pPulsePart;
    // End of variables declaration//GEN-END:variables
    
   
    AbstractPlayListItem tmpRecord;
    
    public void setupPlugin(Plugin pgn) {
        jcoPlugins.addItem(pgn);
        String clsn =Turgen.pluginManager.getPluginClassName(pgn);
        pItemSettings.add(clsn,pgn.getItemPanel());
    }
    
    public void selectInitialPlugin() {
    
        CardLayout c = (CardLayout)this.pItemSettings.getLayout();
        int count = jcoPlugins.getItemCount();
        
        if (count>persistenceSelectedIndex) {
            jcoPlugins.setSelectedIndex(persistenceSelectedIndex);
        }
        else {
           jcoPlugins.setSelectedIndex(0);
        }
        c.show(pItemSettings, Turgen.pluginManager.getPluginClassName((Plugin)jcoPlugins.getSelectedItem()));
        
    }
    
    private Plugin getSelectedPlugin() {
        return (Plugin)jcoPlugins.getSelectedItem();
    }
    
    public void warn(String s) {
        JOptionPane.showMessageDialog(this,s,"Warning",JOptionPane.WARNING_MESSAGE);
    }
    
    public AbstractPlayListItem getLastItem() {
        return tmpRecord;
    }
    
    public boolean prepareEdit(AbstractPlayListItem apr) {

        /*Select appropriate pluginClass*/
        Plugin pgn = Turgen.pluginManager.getPlugin(apr.pluginClass);
        if (pgn==null) return false;
        CardLayout c = (CardLayout)this.pItemSettings.getLayout();
        jcoPlugins.setSelectedItem(pgn);

        /*Show appropriate panel for editing item*/
        pgn.fillPanel(apr);

        /*Fill input file and output file text fields*/
        jtfInFile.setText(apr.inFile);
        jtfOutFile.setText(apr.outFile);

        /*Setup pulses*/
        jcoPulses.setSelectedItem(apr.pulses);

        return true;
    }

    public String getInFile() {
        return this.jtfInFile.getText();
    }

    public String getOutFile() {
        return this.jtfOutFile.getText();
    }
    
    public void clear(boolean all) {
        if (all==false) {
            Plugin pgn = getSelectedPlugin();
            if (pgn!=null) pgn.resetPanel();
        }
        else {
          int l=jcoPlugins.getItemCount();
          for(int i=0;i<l;i++) {
              Plugin pgn = (Plugin)jcoPlugins.getItemAt(i);
              pgn.resetPanel();
          }
        }
        jtfInFile.setText("");
        jtfOutFile.setText("");
    }
    
    void updateFileDialogUI() {
        SwingUtilities.updateComponentTreeUI(jfcInputFile);
        SwingUtilities.updateComponentTreeUI(jfcOutputFile);
        SwingUtilities.updateComponentTreeUI(jfcAltInputFile);
        SwingUtilities.updateComponentTreeUI(jfcAltOutputFile);
        
        Iterator it = auxFileChoosers.iterator();
        while(it.hasNext()) {
            SwingUtilities.updateComponentTreeUI((JFileChooser)it.next());
        }
        
    }

    private void showStatistics() {
        
        String fn = jtfInFile.getText().trim();

        if (StatisticsDialog.fillStatisticsDialog(fn, Turgen.dlm.dlgStatistics)==true) {
            Turgen.dlm.dlgStatistics.setVisible(true);
        }
    }

    /**Automatically create output file specifier*/
    private void synchronizeOutputFile() {
        
        String outFileName=jtfInFile.getText().trim();

        File ef = new File(outFileName);
        if (!ef.exists() || !ef.isFile()) return;


        int j = outFileName.lastIndexOf('.');
        String r = (j<0)?outFileName:outFileName.substring(0,j);
        r=r.concat(".wav");
        
        if (cfgFavoriteWaveDirectory.isBlank()==true) {
            jtfOutFile.setText(r);
        }
        else {
            File f= new File(r);
            String nm = f.getName();
            String fwd = cfgFavoriteWaveDirectory.getAsString();
            
            if (fwd.endsWith(Turgen.sp)) {
                fwd=fwd.substring(0,fwd.length()-1);
            }
            fwd=fwd+Turgen.sp+nm;
            
            jtfOutFile.setText(fwd);
        }
        
        
    }

    public PulseCrate getPulses() {
        return (PulseCrate)jcoPulses.getSelectedItem();
    }

    public Object[] getPersistenceData() {

        Object[] pData = new Object[6];

        /*Bounds*/
        pData[0]=getBounds();

        /*File chooser directories*/
        pData[1]=jfcInputFile.getCurrentDirectory().getAbsolutePath();
        pData[2]=jfcOutputFile.getCurrentDirectory().getAbsolutePath();
        
        pData[3]=jfcAltInputFile.getCurrentDirectory().getAbsolutePath();
        pData[4]=jfcAltOutputFile.getCurrentDirectory().getAbsolutePath();
        
        pData[5]=new Integer(jcoPlugins.getSelectedIndex());

        return pData;

    }

    public void setPersistenceData(Object[] data) throws Exception {

        /*Bounds*/
        setBounds((Rectangle)data[0]);

        /*File chooser directories*/
        jfcInputFile.setCurrentDirectory(new File((String)data[1]));
        jfcOutputFile.setCurrentDirectory(new File((String)data[2]));
        jfcAltInputFile.setCurrentDirectory(new File((String)data[3]));
        jfcAltOutputFile.setCurrentDirectory(new File((String)data[4]));
        persistenceSelectedIndex = ((Integer)data[5]).intValue();
        
    }

    public String getPersistenceId() {
        return getClass().getName();
    }

    public void setPersistenceDefaults() {
        werePersistenceDefaultsUsed=true;
        pack();
        DialogManager.centerContainer(this);
        persistenceSelectedIndex=0;
    }

    public void registerFileChooser(JFileChooser fc) {
        auxFileChoosers.add(fc);
    }


    private static class PluginCellRenderer extends JPanel implements ListCellRenderer {

        private Insets insets = new Insets(1,2,1,2);
        private JLabel lblText;
        private JLabel lblIcon;

        private ImageIcon csFlag;
        private ImageIcon plFlag;
        private ImageIcon unFlag;


        PluginCellRenderer() {
            super();
            setOpaque(true);

            lblText=new JLabel();
            lblIcon=new JLabel();

            setLayout(new BorderLayout(0,0));

            add("East",lblIcon);
            add("Center",lblText);

            try {

                csFlag=new javax.swing.ImageIcon(getClass().getResource("/turgen/resources/flag_cs.png"));
                plFlag=new javax.swing.ImageIcon(getClass().getResource("/turgen/resources/flag_pl.png"));
                unFlag=new javax.swing.ImageIcon(getClass().getResource("/turgen/resources/flag_un.png"));
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

            if (isSelected==true && index!=-1) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
                lblText.setBackground(list.getSelectionBackground());
                lblText.setForeground(list.getSelectionForeground());

            }
            else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
                lblText.setBackground(list.getBackground());
                lblText.setForeground(list.getForeground());
            }
            
            /*Fallback*/
            if (lblText.getForeground().equals(lblText.getBackground())) {
                lblText.setForeground(Color.black);
                lblText.setBackground(Color.white);
            }
            
            if (value==null) return this;
            
            Plugin p = (Plugin)value;
            lblText.setText(p.getName());

            lblIcon.setIcon(unFlag);
            if (p.getCountry().equals("CS")) lblIcon.setIcon(csFlag);
            if (p.getCountry().equals("PL")) lblIcon.setIcon(plFlag);

            return this;

        }

        public Insets getInsets() {
            return insets;
        }
        
        

    }
    
    public boolean werePersistenceDefaultsUsed() {
         return werePersistenceDefaultsUsed;
    }

    
}
