OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.core.finance.accounting.ui;

import org.openconcerto.erp.panel.compta.ImportFEC;
import org.openconcerto.sql.element.SQLElementDirectory;
import org.openconcerto.sql.users.UserManager;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.ReloadPanel;
import org.openconcerto.ui.SwingThreadUtils;

import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ImportEcritureFECPanel extends JPanel {

    private final Map<String, Integer> mapJournal = new HashMap<>();
    private final Map<String, Integer> mapCompte = new HashMap<>();

    public ImportEcritureFECPanel(final SQLElementDirectory dir) {
        super(new GridBagLayout());

        JLabel label = new JLabel("Import depuis un fichier au format FEC.");
        final JButton button = new JButton("Sélectionner le ficher");
        GridBagConstraints c = new DefaultGridBagConstraints();
        c.gridwidth = 2;
        this.add(label, c);
        c.gridy++;
        c.gridwidth = 1;
        c.weightx = 1;
        final ReloadPanel rlPanel = new ReloadPanel();
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.NONE;
        this.add(rlPanel, c);
        c.gridx++;
        c.weightx = 0;
        this.add(button, c);

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                button.setEnabled(false);
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, ImportEcritureFECPanel.this);
                final FileDialog fd = new FileDialog(frame, "Import d'écritures", FileDialog.LOAD);
                fd.setVisible(true);
                rlPanel.setMode(ReloadPanel.MODE_ROTATE);
                if (fd.getFile() != null) {

                    new Thread() {
                        @Override
                        public void run() {
                            final File fileToImport = new File(fd.getDirectory(), fd.getFile());
                            ImportFEC fec = new ImportFEC();
                            try {
                                fec.loadFrom(fileToImport);
                                fec.importTo(dir, dir.getElement("ECRITURE").getTable().getDBRoot(), UserManager.getUser());
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }finally {
                                SwingUtilities.invokeLater(new Runnable() {

                                    @Override
                                    public void run() {
                                        if (fd != null) {
                                            rlPanel.setMode(ReloadPanel.MODE_EMPTY);
                                        }
                                        JOptionPane.showMessageDialog(null, "Import terminé!");
                                    }
                                });

                            }
                        }
                    }.start();
                }
            }
        });
    }

}