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.
 */
 
 package org.openconcerto.erp.core.customerrelationship.mail;

import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import com.lowagie.text.Font;

public class EmailTemplate {
    private String name;
    private String title;
    private String text;
    private boolean isDefault;
    private String dateFormat;

    public EmailTemplate(String name, String title, String text, Boolean isDefault, String dateFormat) {
        this.name = name;
        this.title = title;
        this.text = text;
        this.isDefault = isDefault;
        this.dateFormat = dateFormat;
    }

    public static List<EmailTemplate> getAll(DBRoot root) {
        final List<EmailTemplate> result = new ArrayList<>();
        final SQLSelect selF = new SQLSelect();
        final SQLTable tFamilleArticle = root.getTable(EmailTemplateSQLElement.TABLE_NAME);
        selF.addSelect(tFamilleArticle.getKey());
        selF.addSelect(tFamilleArticle.getField("NOM"));
        selF.addSelect(tFamilleArticle.getField("TITRE"));
        selF.addSelect(tFamilleArticle.getField("TEXTE"));
        selF.addSelect(tFamilleArticle.getField("PAR_DEFAUT"));
        selF.addSelect(tFamilleArticle.getField("FORMAT_DATE"));
        final List<SQLRow> lF = SQLRowListRSH.execute(selF);
        for (SQLRow sqlRow : lF) {
            result.add(new EmailTemplate(sqlRow.getString("NOM"), sqlRow.getString("TITRE"), sqlRow.getString("TEXTE"), sqlRow.getBoolean("PAR_DEFAUT"), sqlRow.getString("FORMAT_DATE")));
        }
        return result;
    }

    public String getName() {
        return this.name;
    }

    public String getTitle() {
        return this.title;
    }

    public String getText() {
        return this.text;
    }

    public boolean isDefault() {
        return this.isDefault;
    }

    public String getDateFormat() {
        return this.dateFormat;
    }

    public static void askTemplate(JComponent parent, DBRoot root, ValueListener listener) {
        if (!SwingUtilities.isEventDispatchThread()) {
            throw new IllegalStateException("must be called from EDT");
        }
        List<EmailTemplate> templates = getAll(root);
        if (templates.isEmpty()) {
            listener.valueSelected(null);
            return;
        }
        if (templates.size() == 1) {
            listener.valueSelected(templates.get(0));
            return;
        }
        Collections.sort(templates, new Comparator<EmailTemplate>() {

            @Override
            public int compare(EmailTemplate o1, EmailTemplate o2) {
                return o1.getName().compareToIgnoreCase(o2.getName());
            }
        });
        final JDialog dialog = new JDialog((Frame) SwingUtilities.getWindowAncestor(parent), "Modèle à utiliser", true);
        final JList<EmailTemplate> list = new JList<>(templates.toArray(new EmailTemplate[0]));
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        for (int i = 0; i < templates.size(); i++) {
            final EmailTemplate t = templates.get(i);
            if (t.isDefault) {
                list.setSelectedIndex(i);
                break;
            }
        }
        list.setFixedCellHeight((int) (new JLabel("t").getPreferredSize().height * 1.5));

        list.setCellRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                EmailTemplate t = (EmailTemplate) value;

                final JLabel l = (JLabel) super.getListCellRendererComponent(list, t.getName(), index, isSelected, cellHasFocus);
                if (t.isDefault) {
                    l.setFont(l.getFont().deriveFont(Font.BOLD));
                } else {
                    l.setFont(l.getFont().deriveFont(Font.NORMAL));
                }
                return l;
            }
        });
        list.setMinimumSize(new Dimension(300, 200));
        list.setPreferredSize(new Dimension(300, 200));
        final Container contentPane = dialog.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(list, BorderLayout.CENTER);
        JPanel p = new JPanel();
        p.setLayout(new FlowLayout(FlowLayout.RIGHT));

        final JButton button = new JButton("Valider");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                listener.valueSelected(list.getSelectedValue());
                dialog.dispose();
            }
        });
        p.add(button);
        contentPane.add(p, BorderLayout.SOUTH);
        dialog.pack();
        dialog.setLocationRelativeTo(parent);
        dialog.setVisible(true);

    }

}