/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS 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.
 *
 * XEMELIOS 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 XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.ui.admin;

import fr.gouv.finances.cp.utils.PropertiesExpansion;
import fr.gouv.finances.dgfip.xemelios.common.Constants;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JComboBox;
import org.apache.log4j.Logger;

/**
 *
 * @author  chm
 */
public class PnlExport extends javax.swing.JPanel implements ParamSaver {
    private static Logger logger = Logger.getLogger(PnlExport.class);
    
    private static String PANEL_DESCRIPTION = "Permet de configurer la façon dont les données sont exportées\npar le menu Fichier / Export, ou par le menu Exports / Executer";
    
    private static ExporterConfig EXPORT_CSV = new ExporterConfig("fr.gouv.finances.cp.xemelios.export.CsvExporter","Exports CSV","Permet de réaliser des exports au format CSV.");
    private static ExporterConfig EXPORT_XLS_2000 = new ExporterConfig("fr.gouv.finances.cp.xemelios.export.HtmlTableExporter","Exports Excel 2000/2003/2007","Permet de réaliser des exports de gros volumes pour Excel.\nNécessite qu'Excel 2000 / 2003 / 2007 soit installé sur le poste.");
    private static ExporterConfig EXPORT_XLS_97 = new ExporterConfig("fr.gouv.finances.cp.xemelios.export.SylkExporter","Exports Excel 97","Permet de réaliser des exports de gros volumes pour Excel.\nNécessite qu'Excel 97 soit installé sur le poste.\nNe convient pas à Excel 97SR1 et 97SR2.");
    private static ExporterConfig EXPORT_XLS_97SR1 = new ExporterConfig("fr.gouv.finances.cp.xemelios.export.HtmlTableExporterXls97SR1","Exports Excel 97 SR1 / SR2b","Permet de réaliser des exports de gros volumes pour Excel.\nNécessite qu'Excel 97 SR1 ou SR2b soit installé sur le poste.");
    private static ExporterConfig EXPORT_XLS_JXL = new ExporterConfig("fr.gouv.finances.cp.xemelios.export.JxlExporter","Exports Excel simples","Permet de réaliser des exports pour toutes versions d'Excel,\nmais limités à de petits fichiers.");
    
    private static ExporterConfig[] exporters = {EXPORT_CSV, EXPORT_XLS_2000, EXPORT_XLS_97, EXPORT_XLS_97SR1, EXPORT_XLS_JXL};
    /** Creates new form PnlExport */
    public PnlExport() {
        initComponents();
        initializeData();
    }
    
    protected void initializeData() {
        String exporterClassName = System.getProperty(Constants.SYS_PROP_XEMELIOS_EXPORTER_CLASS);
        for(ExporterConfig ec:exporters) {
            if(ec.getClassName().equals(exporterClassName)) {
                cbxExporter.setSelectedItem(ec);
                break;
            }
        }
    }
    
    /** 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() {

        jLabel1 = new javax.swing.JLabel();
        cbxExporter = new JComboBox(exporters);
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        dfDesc = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea(PANEL_DESCRIPTION);

        jLabel1.setText("Méthode d'export Excel");

        cbxExporter.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxExporterItemStateChanged(evt);
            }
        });

        jLabel2.setText("Description");

        dfDesc.setColumns(20);
        dfDesc.setEditable(false);
        dfDesc.setRows(5);
        jScrollPane1.setViewportView(dfDesc);

        jScrollPane2.setBorder(null);

        jTextArea1.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setBorder(null);
        jScrollPane2.setViewportView(jTextArea1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                            .add(cbxExporter, 0, 238, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 24, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(cbxExporter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(79, 79, 79))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cbxExporterItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxExporterItemStateChanged
        if(cbxExporter.getSelectedItem()!=null) {
            dfDesc.setText(((ExporterConfig)cbxExporter.getSelectedItem()).getDescription());
        }
    }//GEN-LAST:event_cbxExporterItemStateChanged

    public void saveConfiguration() {
        PropertiesExpansion props = new PropertiesExpansion(System.getProperties());
        Properties xemProps = new Properties();
        
        File xemPropsFile = new File(props.replace("${xemelios.prg}/root/xemelios.properties"));
        if(!xemPropsFile.exists()) {
        } else {
            try {
                xemProps.load(new FileInputStream(xemPropsFile));
                String className = ((ExporterConfig)cbxExporter.getSelectedItem()).getClassName();
                xemProps.put(Constants.SYS_PROP_XEMELIOS_EXPORTER_CLASS, className);
                System.setProperty(Constants.SYS_PROP_XEMELIOS_EXPORTER_CLASS, className);
                xemProps.store(new FileOutputStream(xemPropsFile),"");
            } catch(IOException ioEx) {
                logger.error("while saving exporter configuration: ",ioEx);
            }
        }
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbxExporter;
    private javax.swing.JTextArea dfDesc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

    private static class ExporterConfig {
        private String className;
        private String title;
        private String description;
        
        public ExporterConfig(String className, String title, String description) {
            super();
            this.className=className;
            this.title=title;
            this.description=description;
        }
        @Override
        public String toString() { return title; }
        public String getClassName() { return className; }
        public String getTitle() { return title; }
        public String getDescription() { return description; }
    }
}
