OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | 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.
 */
 
 /*
 * Créé le 23 avr. 2012
 */
package org.openconcerto.erp.core.customerrelationship.customer.report;

import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.JDate;
import org.openconcerto.utils.ExceptionHandler;

import java.awt.Desktop;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

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

public class ReportingCommercialPanel extends JPanel {

    public ReportingCommercialPanel(final DBRoot root) {
        super(new GridBagLayout());

        JLabel labelCom = new JLabel("Période du ");

        GridBagConstraints c = new DefaultGridBagConstraints();
        this.add(labelCom, c);
        c.gridx++;
        final JDate dateDeb = new JDate();
        this.add(dateDeb, c);
        c.gridx++;
        JLabel labelYear = new JLabel("au");
        final JDate dateFin = new JDate();

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        dateDeb.setValue(cal.getTime());

        this.add(labelYear, c);
        c.gridx++;
        this.add(dateFin, c);
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DAY_OF_MONTH, 31);
        dateFin.setValue(cal.getTime());

        final JButton buttonValid = new JButton(new AbstractAction("Valider") {

            @Override
            public void actionPerformed(ActionEvent e) {

                new Thread() {
                    public void run() {
                        ReportingCommercialCreator creator = new ReportingCommercialCreator(dateDeb.getValue(), dateFin.getValue(), root);

                        List<ReportingCommercial> list = new ArrayList<>(creator.getValues());
                        ReportingCommercialPDF reporting = new ReportingCommercialPDF(list);
                        File file;
                        try {
                            file = File.createTempFile("ReportingCommerical", ".pdf");
                            reporting.export(file);
                            Desktop.getDesktop().open(file);
                        } catch (IOException e) {
                            ExceptionHandler.handle("Erreur lors de la cration du fichier", e);
                        }
                        SwingUtilities.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                Frame frame = (Frame) SwingUtilities.getRoot(ReportingCommercialPanel.this);
                                if (frame != null) {
                                    frame.dispose();
                                }
                            }
                        });
                    };
                }.start();

            }

        });
        c.gridx++;
        // buttonValid.setEnabled(false);
        this.add(buttonValid, c);
        dateDeb.addValueListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                buttonValid.setEnabled(dateDeb.getValue() != null && dateFin.getValue() != null);
            }
        });
        dateFin.addValueListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                buttonValid.setEnabled(dateDeb.getValue() != null && dateFin.getValue() != null);
            }
        });
    }

}