OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
174 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.customerrelationship.mail;
15
 
16
import org.openconcerto.sql.model.DBRoot;
17
import org.openconcerto.sql.model.SQLRow;
18
import org.openconcerto.sql.model.SQLRowListRSH;
19
import org.openconcerto.sql.model.SQLSelect;
20
import org.openconcerto.sql.model.SQLTable;
21
 
22
import java.awt.BorderLayout;
23
import java.awt.Component;
24
import java.awt.Container;
25
import java.awt.Dimension;
26
import java.awt.FlowLayout;
177 ilm 27
import java.awt.Font;
174 ilm 28
import java.awt.Frame;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.util.ArrayList;
32
import java.util.Collections;
33
import java.util.Comparator;
34
import java.util.List;
35
 
36
import javax.swing.DefaultListCellRenderer;
37
import javax.swing.JButton;
38
import javax.swing.JComponent;
39
import javax.swing.JDialog;
40
import javax.swing.JLabel;
41
import javax.swing.JList;
42
import javax.swing.JPanel;
43
import javax.swing.ListSelectionModel;
44
import javax.swing.SwingUtilities;
45
 
46
public class EmailTemplate {
47
    private String name;
48
    private String title;
49
    private String text;
50
    private boolean isDefault;
51
    private String dateFormat;
52
 
53
    public EmailTemplate(String name, String title, String text, Boolean isDefault, String dateFormat) {
54
        this.name = name;
55
        this.title = title;
56
        this.text = text;
57
        this.isDefault = isDefault;
58
        this.dateFormat = dateFormat;
59
    }
60
 
61
    public static List<EmailTemplate> getAll(DBRoot root) {
62
        final List<EmailTemplate> result = new ArrayList<>();
63
        final SQLSelect selF = new SQLSelect();
64
        final SQLTable tFamilleArticle = root.getTable(EmailTemplateSQLElement.TABLE_NAME);
65
        selF.addSelect(tFamilleArticle.getKey());
66
        selF.addSelect(tFamilleArticle.getField("NOM"));
67
        selF.addSelect(tFamilleArticle.getField("TITRE"));
68
        selF.addSelect(tFamilleArticle.getField("TEXTE"));
69
        selF.addSelect(tFamilleArticle.getField("PAR_DEFAUT"));
70
        selF.addSelect(tFamilleArticle.getField("FORMAT_DATE"));
71
        final List<SQLRow> lF = SQLRowListRSH.execute(selF);
72
        for (SQLRow sqlRow : lF) {
73
            result.add(new EmailTemplate(sqlRow.getString("NOM"), sqlRow.getString("TITRE"), sqlRow.getString("TEXTE"), sqlRow.getBoolean("PAR_DEFAUT"), sqlRow.getString("FORMAT_DATE")));
74
        }
75
        return result;
76
    }
77
 
78
    public String getName() {
79
        return this.name;
80
    }
81
 
82
    public String getTitle() {
83
        return this.title;
84
    }
85
 
86
    public String getText() {
87
        return this.text;
88
    }
89
 
90
    public boolean isDefault() {
91
        return this.isDefault;
92
    }
93
 
94
    public String getDateFormat() {
95
        return this.dateFormat;
96
    }
97
 
98
    public static void askTemplate(JComponent parent, DBRoot root, ValueListener listener) {
99
        if (!SwingUtilities.isEventDispatchThread()) {
100
            throw new IllegalStateException("must be called from EDT");
101
        }
102
        List<EmailTemplate> templates = getAll(root);
103
        if (templates.isEmpty()) {
104
            listener.valueSelected(null);
105
            return;
106
        }
107
        if (templates.size() == 1) {
108
            listener.valueSelected(templates.get(0));
109
            return;
110
        }
111
        Collections.sort(templates, new Comparator<EmailTemplate>() {
112
 
113
            @Override
114
            public int compare(EmailTemplate o1, EmailTemplate o2) {
115
                return o1.getName().compareToIgnoreCase(o2.getName());
116
            }
117
        });
118
        final JDialog dialog = new JDialog((Frame) SwingUtilities.getWindowAncestor(parent), "Modèle à utiliser", true);
119
        final JList<EmailTemplate> list = new JList<>(templates.toArray(new EmailTemplate[0]));
120
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
121
        for (int i = 0; i < templates.size(); i++) {
122
            final EmailTemplate t = templates.get(i);
123
            if (t.isDefault) {
124
                list.setSelectedIndex(i);
125
                break;
126
            }
127
        }
128
        list.setFixedCellHeight((int) (new JLabel("t").getPreferredSize().height * 1.5));
129
 
130
        list.setCellRenderer(new DefaultListCellRenderer() {
131
            @Override
132
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
133
                EmailTemplate t = (EmailTemplate) value;
134
 
135
                final JLabel l = (JLabel) super.getListCellRendererComponent(list, t.getName(), index, isSelected, cellHasFocus);
136
                if (t.isDefault) {
137
                    l.setFont(l.getFont().deriveFont(Font.BOLD));
138
                } else {
177 ilm 139
                    l.setFont(l.getFont().deriveFont(Font.PLAIN));
174 ilm 140
                }
141
                return l;
142
            }
143
        });
144
        list.setMinimumSize(new Dimension(300, 200));
145
        list.setPreferredSize(new Dimension(300, 200));
146
        final Container contentPane = dialog.getContentPane();
147
        contentPane.setLayout(new BorderLayout());
148
        contentPane.add(list, BorderLayout.CENTER);
149
        JPanel p = new JPanel();
150
        p.setLayout(new FlowLayout(FlowLayout.RIGHT));
151
 
152
        final JButton button = new JButton("Valider");
153
        button.addActionListener(new ActionListener() {
154
 
155
            @Override
156
            public void actionPerformed(ActionEvent e) {
157
                listener.valueSelected(list.getSelectedValue());
158
                dialog.dispose();
159
            }
160
        });
161
        p.add(button);
162
        contentPane.add(p, BorderLayout.SOUTH);
163
        dialog.pack();
164
        dialog.setLocationRelativeTo(parent);
165
        dialog.setVisible(true);
166
 
167
    }
168
 
169
}