OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 142 Rev 177
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.sql.ui;
14
 package org.openconcerto.sql.ui;
15
 
15
 
16
import static org.openconcerto.sql.TM.getTM;
16
import static org.openconcerto.sql.TM.getTM;
17
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.Configuration;
-
 
18
import org.openconcerto.sql.PropsConfiguration;
18
import org.openconcerto.sql.TM;
19
import org.openconcerto.sql.TM;
19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.sql.preferences.UserProps;
22
import org.openconcerto.sql.preferences.UserProps;
22
import org.openconcerto.sql.request.ComboSQLRequest;
23
import org.openconcerto.sql.request.ComboSQLRequest;
23
import org.openconcerto.sql.sqlobject.IComboModel;
24
import org.openconcerto.sql.sqlobject.IComboModel;
24
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
25
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
25
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
26
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
26
import org.openconcerto.sql.users.UserManager;
27
import org.openconcerto.sql.users.UserManager;
27
import org.openconcerto.sql.users.rights.UserRightsManager;
28
import org.openconcerto.sql.users.rights.UserRightsManager;
28
import org.openconcerto.ui.DefaultGridBagConstraints;
29
import org.openconcerto.ui.DefaultGridBagConstraints;
29
import org.openconcerto.ui.ReloadPanel;
30
import org.openconcerto.ui.ReloadPanel;
30
import org.openconcerto.ui.valuewrapper.EmptyValueWrapper;
31
import org.openconcerto.ui.valuewrapper.EmptyValueWrapper;
31
import org.openconcerto.ui.valuewrapper.ValueWrapperFactory;
32
import org.openconcerto.ui.valuewrapper.ValueWrapperFactory;
32
import org.openconcerto.utils.CollectionUtils;
33
import org.openconcerto.utils.CollectionUtils;
33
import org.openconcerto.utils.ExceptionUtils;
34
import org.openconcerto.utils.ExceptionUtils;
34
import org.openconcerto.utils.JImage;
35
import org.openconcerto.utils.JImage;
35
import org.openconcerto.utils.Tuple2;
36
import org.openconcerto.utils.Tuple2;
36
import org.openconcerto.utils.cc.ITransformer;
37
import org.openconcerto.utils.cc.ITransformer;
37
import org.openconcerto.utils.checks.EmptyListener;
38
import org.openconcerto.utils.checks.EmptyListener;
38
import org.openconcerto.utils.checks.EmptyObj;
39
import org.openconcerto.utils.checks.EmptyObj;
39
import org.openconcerto.utils.i18n.TranslationManager;
40
import org.openconcerto.utils.i18n.TranslationManager;
40
import org.openconcerto.utils.text.SimpleDocumentListener;
41
import org.openconcerto.utils.text.SimpleDocumentListener;
41
 
42
 
42
import java.awt.GridBagConstraints;
43
import java.awt.GridBagConstraints;
43
import java.awt.GridBagLayout;
44
import java.awt.GridBagLayout;
44
import java.awt.Insets;
45
import java.awt.Insets;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.awt.event.ActionListener;
47
import java.beans.PropertyChangeEvent;
48
import java.beans.PropertyChangeEvent;
48
import java.beans.PropertyChangeListener;
49
import java.beans.PropertyChangeListener;
49
import java.util.ArrayList;
50
import java.util.ArrayList;
50
import java.util.Arrays;
51
import java.util.Arrays;
51
import java.util.List;
52
import java.util.List;
52
import java.util.Locale;
53
import java.util.Locale;
53
import java.util.ResourceBundle;
54
import java.util.ResourceBundle;
54
 
55
 
55
import javax.swing.BorderFactory;
56
import javax.swing.BorderFactory;
56
import javax.swing.JButton;
57
import javax.swing.JButton;
57
import javax.swing.JCheckBox;
58
import javax.swing.JCheckBox;
58
import javax.swing.JCheckBoxMenuItem;
59
import javax.swing.JCheckBoxMenuItem;
-
 
60
import javax.swing.JComponent;
59
import javax.swing.JLabel;
61
import javax.swing.JLabel;
60
import javax.swing.JOptionPane;
62
import javax.swing.JOptionPane;
61
import javax.swing.JPanel;
63
import javax.swing.JPanel;
62
import javax.swing.JPasswordField;
64
import javax.swing.JPasswordField;
63
import javax.swing.JPopupMenu;
65
import javax.swing.JPopupMenu;
64
import javax.swing.JSeparator;
66
import javax.swing.JSeparator;
65
import javax.swing.JTextField;
67
import javax.swing.JTextField;
66
import javax.swing.SwingConstants;
68
import javax.swing.SwingConstants;
67
import javax.swing.SwingUtilities;
69
import javax.swing.SwingUtilities;
68
import javax.swing.event.DocumentEvent;
70
import javax.swing.event.DocumentEvent;
69
 
71
 
70
/**
72
/**
71
 * Panel de connexion de l'utilisateur au démarrage avec choix de la société
73
 * Panel de connexion de l'utilisateur au démarrage avec choix de la société
72
 */
74
 */
73
public class ConnexionPanel extends JPanel implements ActionListener {
75
public class ConnexionPanel extends JPanel implements ActionListener {
74
 
76
 
75
    public static final String QUICK_LOGIN = "org.openconcerto.quickLogin";
77
    public static final String QUICK_LOGIN = "org.openconcerto.quickLogin";
76
 
78
 
77
    static private boolean quickLogin(final Runnable r, final boolean societeSelector) {
79
    static private boolean quickLogin(final Runnable r, final boolean societeSelector) {
78
        final String lastLogin = UserProps.getInstance().getLastLoginName();
80
        final String lastLogin = UserProps.getInstance().getLastLoginName();
79
        final String pass = UserProps.getInstance().getStoredPassword();
81
        final String pass = UserProps.getInstance().getStoredPassword();
80
        if (Boolean.getBoolean(QUICK_LOGIN) && lastLogin.length() > 0 && pass != null && (!societeSelector || UserProps.getInstance().getLastSocieteID() >= SQLRow.MIN_VALID_ID)) {
82
        if (Boolean.getBoolean(QUICK_LOGIN) && lastLogin.length() > 0 && pass != null && (!societeSelector || UserProps.getInstance().getLastSocieteID() >= SQLRow.MIN_VALID_ID)) {
81
            final Tuple2<String, String> res = new Login(Configuration.getInstance().getRoot()).connectEnc(lastLogin, pass);
83
            final Tuple2<String, String> res = new Login(Configuration.getInstance().getRoot()).connectEnc(lastLogin, pass);
82
            if (res.get0() == null) {
84
            if (res.get0() == null) {
83
                // ConnexionPanel normally executes r outside of the EDT, do the same
85
                // ConnexionPanel normally executes r outside of the EDT, do the same
84
                execute(r);
86
                execute(r);
85
                // no need to display a panel
87
                // no need to display a panel
86
                return true;
88
                return true;
87
            }
89
            }
88
        }
90
        }
89
        return false;
91
        return false;
90
    }
92
    }
91
 
93
 
92
    /**
94
    /**
93
     * Create a panel to log with a user/pass if quick login fails. For quick login to succeed the
95
     * Create a panel to log with a user/pass if quick login fails. For quick login to succeed the
94
     * system property {@value #QUICK_LOGIN} must be <code>true</code> and the user must have stored
96
     * system property {@value #QUICK_LOGIN} must be <code>true</code> and the user must have stored
95
     * his credential.
97
     * his credential.
96
     * 
98
     * 
97
     * @param r what to do once authenticated, *not* run in the EDT.
99
     * @param r what to do once authenticated, *not* run in the EDT.
98
     * @param imageLogo the picture put above the text fields.
100
     * @param imageLogo the picture put above the text fields.
99
     * @return a panel to log in, <code>null</code> if quick logged.
101
     * @return a panel to log in, <code>null</code> if quick logged.
100
     */
102
     */
101
    static public ConnexionPanel create(final Runnable r, final JImage imageLogo) {
103
    static public ConnexionPanel create(final Runnable r, final JImage imageLogo) {
102
        return create(r, imageLogo, false);
104
        return create(r, imageLogo, false);
103
 
105
 
104
    }
106
    }
105
 
107
 
106
    static public ConnexionPanel create(final Runnable r, final JImage imageLogo, final boolean societeSelector) {
108
    static public ConnexionPanel create(final Runnable r, final JImage imageLogo, final boolean societeSelector) {
107
        if (quickLogin(r, societeSelector)) {
109
        if (quickLogin(r, societeSelector)) {
108
            return null;
110
            return null;
109
        } else {
111
        } else {
110
            return new ConnexionPanel(r, imageLogo, societeSelector);
112
            return new ConnexionPanel(r, imageLogo, societeSelector);
111
        }
113
        }
112
    }
114
    }
113
 
115
 
114
    private final Login login;
116
    private final Login login;
115
 
117
 
116
    private final JPasswordField textPassWord;
118
    private final JPasswordField textPassWord;
117
    private final EmptyValueWrapper<String> textLogin;
119
    private final EmptyValueWrapper<String> textLogin;
118
    private SQLRequestComboBox comboSociete;
120
    private SQLRequestComboBox comboSociete;
119
    private String encryptedPassword;
121
    private String encryptedPassword;
120
    protected String clearPassword;
122
    protected String clearPassword;
121
    private final Runnable r;
123
    private final Runnable r;
122
    private final boolean societeSelector;
124
    private final boolean societeSelector;
123
    private final ReloadPanel reloadPanel;
125
    private final ReloadPanel reloadPanel;
124
    private boolean isConnecting = false;
126
    private boolean isConnecting = false;
125
    private String connectionAllowed;
127
    private String connectionAllowed;
126
    private final JCheckBox saveCheckBox = new JCheckBox(getTM().translate("loginPanel.storePass"));
128
    private final JCheckBox saveCheckBox = new JCheckBox(getTM().translate("loginPanel.storePass"));
127
    private final JButton buttonConnect = new JButton(getTM().translate("loginPanel.loginAction"));
129
    private final JButton buttonConnect = new JButton(getTM().translate("loginPanel.loginAction"));
128
    private String adminLogin = getTM().translate("loginPanel.adminLogin");
130
    private String adminLogin = getTM().translate("loginPanel.adminLogin");
129
    private final JLabel loginLabel = new JLabel(getTM().translate("loginPanel.loginLabel"));
131
    private final JLabel loginLabel = new JLabel(getTM().translate("loginPanel.loginLabel"));
130
    private final JLabel passwordLabel = new JLabel(getTM().translate("loginPanel.passLabel"));
132
    private final JLabel passwordLabel = new JLabel(getTM().translate("loginPanel.passLabel"));
131
    private final JLabel companyLabel = new JLabel(getTM().translate("loginPanel.companyLabel"));
133
    private final JLabel companyLabel = new JLabel(getTM().translate("loginPanel.companyLabel"));
132
    private String localeBaseName = null;
134
    private String localeBaseName = null;
133
    private final List<Locale> localesToDisplay = new ArrayList<Locale>();
135
    private final List<Locale> localesToDisplay = new ArrayList<Locale>();
134
    private final JButton langButton = new JButton(Locale.ROOT.getLanguage());
136
    private final JButton langButton = new JButton(Locale.ROOT.getLanguage());
135
 
137
 
136
    /**
138
    /**
137
     * Create a panel to log with a user/pass.
139
     * Create a panel to log with a user/pass.
138
     * 
140
     * 
139
     * @param r what to do once authenticated, *not* run in the EDT.
141
     * @param r what to do once authenticated, *not* run in the EDT.
140
     * @param imageLogo the picture put above the text fields.
142
     * @param imageLogo the picture put above the text fields.
141
     */
143
     */
142
    public ConnexionPanel(final Runnable r, final JImage imageLogo) {
144
    public ConnexionPanel(final Runnable r, final JImage imageLogo) {
143
        this(r, imageLogo, false);
145
        this(r, imageLogo, false);
144
    }
146
    }
145
 
147
 
146
    public ConnexionPanel(final Runnable r, final JImage imageLogo, final boolean societeSelector) {
148
    public ConnexionPanel(final Runnable r, final JImage imageLogo, final boolean societeSelector) {
147
        this(r, imageLogo, societeSelector, true);
149
        this(r, imageLogo, societeSelector, true);
148
    }
150
    }
149
 
151
 
150
    public ConnexionPanel(final Runnable r, final JImage imageLogo, final boolean societeSelector, final boolean allowStoredPass) {
152
    public ConnexionPanel(final Runnable r, final JImage imageLogo, final boolean societeSelector, final boolean allowStoredPass) {
151
        super();
153
        super();
152
        if (r == null) {
154
        if (r == null) {
153
            throw new IllegalArgumentException("null runnable");
155
            throw new IllegalArgumentException("null runnable");
154
        }
156
        }
155
        this.login = new Login(Configuration.getInstance().getRoot());
157
        this.login = new Login(Configuration.getInstance().getRoot());
156
 
158
 
157
        this.societeSelector = societeSelector;
159
        this.societeSelector = societeSelector;
158
        this.r = r;
160
        this.r = r;
159
        String lastLoginName = UserProps.getInstance().getLastLoginName();
161
        String lastLoginName = UserProps.getInstance().getLastLoginName();
160
        if (lastLoginName == null || lastLoginName.trim().length() == 0) {
162
        if (lastLoginName == null || lastLoginName.trim().length() == 0) {
161
            lastLoginName = this.adminLogin;
163
            lastLoginName = this.adminLogin;
162
        }
164
        }
163
        final String storedPassword = allowStoredPass ? UserProps.getInstance().getStoredPassword() : null;
165
        final String storedPassword = allowStoredPass ? UserProps.getInstance().getStoredPassword() : null;
164
        this.encryptedPassword = storedPassword;
166
        this.encryptedPassword = storedPassword;
165
        this.connectionAllowed = null;
167
        this.connectionAllowed = null;
166
 
168
 
167
        this.setLayout(new GridBagLayout());
169
        this.setLayout(new GridBagLayout());
168
 
170
 
169
        final GridBagConstraints c = new GridBagConstraints();
171
        final GridBagConstraints c = new GridBagConstraints();
170
        c.gridheight = 1;
172
        c.gridheight = 1;
171
        c.gridwidth = 2;
173
        c.gridwidth = 2;
172
        c.gridx = 0;
174
        c.gridx = 0;
173
        c.gridy = 0;
175
        c.gridy = 0;
174
        c.weightx = 1;
176
        c.weightx = 1;
175
        c.weighty = 0;
177
        c.weighty = 0;
176
        c.insets = new Insets(0, 0, 0, 0);
178
        c.insets = new Insets(0, 0, 0, 0);
177
        c.fill = GridBagConstraints.HORIZONTAL;
179
        c.fill = GridBagConstraints.HORIZONTAL;
178
 
180
 
179
        // Logo
181
        // Logo
180
        imageLogo.check();
182
        imageLogo.check();
181
        this.add(imageLogo, c);
183
        this.add(imageLogo, c);
182
        c.gridy++;
184
        c.gridy++;
183
        c.gridwidth = GridBagConstraints.REMAINDER;
185
        c.gridwidth = GridBagConstraints.REMAINDER;
184
        this.add(new JSeparator(SwingConstants.HORIZONTAL), c);
186
        this.add(new JSeparator(SwingConstants.HORIZONTAL), c);
185
 
187
 
186
        // Login
188
        // Login
187
        c.insets = new Insets(2, 2, 1, 2);
189
        c.insets = new Insets(2, 2, 1, 2);
188
        c.gridy++;
190
        c.gridy++;
189
        c.gridwidth = 1;
191
        c.gridwidth = 1;
190
        c.weightx = 0;
192
        c.weightx = 0;
191
        this.loginLabel.setHorizontalAlignment(SwingConstants.RIGHT);
193
        this.loginLabel.setHorizontalAlignment(SwingConstants.RIGHT);
192
        this.add(this.loginLabel, c);
194
        this.add(this.loginLabel, c);
193
 
195
 
194
        this.textLogin = new EmptyValueWrapper<String>(ValueWrapperFactory.create(new JTextField(), String.class));
196
        this.textLogin = new EmptyValueWrapper<String>(ValueWrapperFactory.create(new JTextField(), String.class));
195
        this.textLogin.setValue(lastLoginName);
197
        this.textLogin.setValue(lastLoginName);
196
        c.gridx++;
198
        c.gridx++;
197
        c.weightx = 1;
199
        c.weightx = 1;
198
        this.add(this.textLogin.getComp(), c);
200
        this.add(this.textLogin.getComp(), c);
199
        ((JTextField) this.textLogin.getComp()).addActionListener(this);
201
        ((JTextField) this.textLogin.getComp()).addActionListener(this);
200
        this.textLogin.addEmptyListener(new EmptyListener() {
202
        this.textLogin.addEmptyListener(new EmptyListener() {
201
            @Override
203
            @Override
202
            public void emptyChange(final EmptyObj src, final boolean newValue) {
204
            public void emptyChange(final EmptyObj src, final boolean newValue) {
203
                checkValidity();
205
                checkValidity();
204
            }
206
            }
205
        });
207
        });
206
 
208
 
207
        // Password
209
        // Password
208
        c.gridy++;
210
        c.gridy++;
209
        c.gridx = 0;
211
        c.gridx = 0;
210
        c.weightx = 0;
212
        c.weightx = 0;
211
 
213
 
212
        this.passwordLabel.setHorizontalAlignment(SwingConstants.RIGHT);
214
        this.passwordLabel.setHorizontalAlignment(SwingConstants.RIGHT);
213
        this.add(this.passwordLabel, c);
215
        this.add(this.passwordLabel, c);
214
 
216
 
215
        this.textPassWord = new JPasswordField();
217
        this.textPassWord = new JPasswordField();
216
        // to show the user its password has been retrieved
218
        // to show the user its password has been retrieved
217
        if (storedPassword != null) {
219
        if (storedPassword != null) {
218
            final char[] s = new char[8];
220
            final char[] s = new char[8];
219
            Arrays.fill(s, ' ');
221
            Arrays.fill(s, ' ');
220
            this.textPassWord.setText(new String(s));
222
            this.textPassWord.setText(new String(s));
221
            this.clearPassword = null;
223
            this.clearPassword = null;
222
        } else
224
        } else
223
            this.clearPassword = "";
225
            this.clearPassword = "";
224
        c.gridx++;
226
        c.gridx++;
225
        c.weightx = 1;
227
        c.weightx = 1;
226
        this.add(this.textPassWord, c);
228
        this.add(this.textPassWord, c);
227
        this.textPassWord.getDocument().addDocumentListener(new SimpleDocumentListener() {
229
        this.textPassWord.getDocument().addDocumentListener(new SimpleDocumentListener() {
228
            @Override
230
            @Override
229
            public void update(final DocumentEvent e) {
231
            public void update(final DocumentEvent e) {
230
                ConnexionPanel.this.clearPassword = String.valueOf(ConnexionPanel.this.textPassWord.getPassword());
232
                ConnexionPanel.this.clearPassword = String.valueOf(ConnexionPanel.this.textPassWord.getPassword());
231
                checkValidity();
233
                checkValidity();
232
            }
234
            }
233
        });
235
        });
234
        this.textPassWord.addActionListener(this);
236
        this.textPassWord.addActionListener(this);
235
 
237
 
236
        if (societeSelector) {
238
        if (societeSelector) {
237
            // Societe
239
            // Societe
238
 
240
 
239
            c.gridy++;
241
            c.gridy++;
240
            c.gridx = 0;
242
            c.gridx = 0;
241
            c.weightx = 0;
243
            c.weightx = 0;
242
 
244
 
243
            this.companyLabel.setHorizontalAlignment(SwingConstants.RIGHT);
245
            this.companyLabel.setHorizontalAlignment(SwingConstants.RIGHT);
244
            this.add(this.companyLabel, c);
246
            this.add(this.companyLabel, c);
245
 
247
 
246
            final SQLTable tableSociete = this.login.getUserTable().getDBRoot().findTable("SOCIETE_COMMON");
248
            final SQLTable tableSociete = this.login.getUserTable().getDBRoot().findTable("SOCIETE_COMMON");
247
 
249
 
248
            if (tableSociete == null) {
250
            if (tableSociete == null) {
249
                throw ExceptionUtils.createExn(IllegalStateException.class, "Missing table : SOCIETE_COMMON", null);
251
                throw ExceptionUtils.createExn(IllegalStateException.class, "Missing table : SOCIETE_COMMON", null);
250
            }
252
            }
251
 
253
 
252
            this.comboSociete = new SQLRequestComboBox(false, 25);
254
            this.comboSociete = new SQLRequestComboBox(false, 25);
253
            final ComboSQLRequest req = Configuration.getInstance().getDirectory().getElement(tableSociete).getComboRequest(true);
255
            final ComboSQLRequest req = Configuration.getInstance().getDirectory().getElement(tableSociete).getComboRequest(true);
254
            // keep rows for checkValidity()
256
            // keep rows for checkValidity()
255
            req.keepRows(true);
257
            req.keepRows(true);
256
            final IComboModel model = new IComboModel(req);
258
            final IComboModel model = new IComboModel(req);
257
            final int lastSociete = UserProps.getInstance().getLastSocieteID();
259
            final int lastSociete = UserProps.getInstance().getLastSocieteID();
258
            if (lastSociete >= SQLRow.MIN_VALID_ID) {
260
            if (lastSociete >= SQLRow.MIN_VALID_ID) {
259
                model.setValue(lastSociete);
261
                model.setValue(lastSociete);
260
            } else {
262
            } else {
261
                model.setFirstFillSelection(new ITransformer<List<IComboSelectionItem>, IComboSelectionItem>() {
263
                model.setFirstFillSelection(new ITransformer<List<IComboSelectionItem>, IComboSelectionItem>() {
262
                    @Override
264
                    @Override
263
                    public IComboSelectionItem transformChecked(List<IComboSelectionItem> input) {
265
                    public IComboSelectionItem transformChecked(List<IComboSelectionItem> input) {
264
                        // Guillaume 25/06/2010
266
                        // Guillaume 25/06/2010
265
                        return CollectionUtils.getFirst(input);
267
                        return CollectionUtils.getFirst(input);
266
                    }
268
                    }
267
                });
269
                });
268
            }
270
            }
269
            this.comboSociete.uiInit(model);
271
            this.comboSociete.uiInit(model);
270
 
272
 
271
            this.comboSociete.addEmptyListener(new EmptyListener() {
273
            this.comboSociete.addEmptyListener(new EmptyListener() {
272
                @Override
274
                @Override
273
                public void emptyChange(final EmptyObj src, final boolean newValue) {
275
                public void emptyChange(final EmptyObj src, final boolean newValue) {
274
                    checkValidity();
276
                    checkValidity();
275
                }
277
                }
276
            });
278
            });
277
            this.comboSociete.addValueListener(new PropertyChangeListener() {
279
            this.comboSociete.addValueListener(new PropertyChangeListener() {
278
                @Override
280
                @Override
279
                public void propertyChange(final PropertyChangeEvent evt) {
281
                public void propertyChange(final PropertyChangeEvent evt) {
280
                    checkValidity();
282
                    checkValidity();
281
                }
283
                }
282
            });
284
            });
283
            c.gridx++;
285
            c.gridx++;
284
            this.add(this.comboSociete, c);
286
            this.add(this.comboSociete, c);
285
        }
287
        }
286
 
288
 
287
        // Button
289
        // Button
288
        final JPanel panelButton = new JPanel();
290
        final JPanel panelButton = new JPanel();
289
        panelButton.setOpaque(false);
291
        panelButton.setOpaque(false);
290
        panelButton.setLayout(new GridBagLayout());
292
        panelButton.setLayout(new GridBagLayout());
291
        final GridBagConstraints c2 = new DefaultGridBagConstraints();
293
        final GridBagConstraints c2 = new DefaultGridBagConstraints();
292
        c2.weightx = 1;
294
        c2.weightx = 1;
293
        if (allowStoredPass) {
295
        if (allowStoredPass) {
294
            this.saveCheckBox.setOpaque(false);
296
            this.saveCheckBox.setOpaque(false);
295
            panelButton.add(this.saveCheckBox, c2);
297
            panelButton.add(this.saveCheckBox, c2);
296
            c2.weightx = 0;
298
            c2.weightx = 0;
297
            if (storedPassword != null && storedPassword.length() > 0) {
299
            if (storedPassword != null && storedPassword.length() > 0) {
298
                this.saveCheckBox.setSelected(true);
300
                this.saveCheckBox.setSelected(true);
299
            }
301
            }
300
        }
302
        }
301
        c2.gridx++;
303
        c2.gridx++;
302
        this.reloadPanel = new ReloadPanel();
304
        this.reloadPanel = new ReloadPanel();
303
        this.reloadPanel.setOpaque(false);
305
        this.reloadPanel.setOpaque(false);
304
        /*
306
        /*
305
         * reloadPanel.setPreferredSize(new Dimension(20, 20)); reloadPanel.setMinimumSize(new
307
         * reloadPanel.setPreferredSize(new Dimension(20, 20)); reloadPanel.setMinimumSize(new
306
         * Dimension(20, 20)); reloadPanel.setMaximumSize(new Dimension(20, 20));
308
         * Dimension(20, 20)); reloadPanel.setMaximumSize(new Dimension(20, 20));
307
         */
309
         */
308
        this.reloadPanel.setMode(ReloadPanel.MODE_EMPTY);
310
        this.reloadPanel.setMode(ReloadPanel.MODE_EMPTY);
309
        panelButton.add(this.reloadPanel, c2);
311
        panelButton.add(this.reloadPanel, c2);
310
        c2.gridx++;
312
        c2.gridx++;
311
        c2.weightx = 0;
313
        c2.weightx = 0;
312
 
314
 
313
        this.langButton.addActionListener(new ActionListener() {
315
        this.langButton.addActionListener(new ActionListener() {
314
 
316
 
315
            @Override
317
            @Override
316
            public void actionPerformed(ActionEvent e) {
318
            public void actionPerformed(ActionEvent e) {
317
                final JPopupMenu menu = new JPopupMenu();
319
                final JPopupMenu menu = new JPopupMenu();
318
                final Locale locale = UserProps.getInstance().getLocale();
320
                final Locale locale = UserProps.getInstance().getLocale();
319
                for (final Locale l : ConnexionPanel.this.localesToDisplay) {
321
                for (final Locale l : ConnexionPanel.this.localesToDisplay) {
320
                    final JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(l.getDisplayName(l));
322
                    final JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(l.getDisplayName(l));
321
                    if (l.equals(locale)) {
323
                    if (l.equals(locale)) {
322
                        menuItem.setSelected(true);
324
                        menuItem.setSelected(true);
323
                    }
325
                    }
324
                    menu.add(menuItem);
326
                    menu.add(menuItem);
325
                    menuItem.addActionListener(new ActionListener() {
327
                    menuItem.addActionListener(new ActionListener() {
326
                        @Override
328
                        @Override
327
                        public void actionPerformed(ActionEvent e) {
329
                        public void actionPerformed(ActionEvent e) {
328
                            setUILanguage(l);
330
                            setUILanguage(l);
329
                        }
331
                        }
330
                    });
332
                    });
331
                }
333
                }
332
 
334
 
333
                menu.show(ConnexionPanel.this.langButton, 0, 0);
335
                menu.show(ConnexionPanel.this.langButton, 0, 0);
334
 
336
 
335
            }
337
            }
336
        });
338
        });
337
        this.langButton.setOpaque(false);
339
        this.langButton.setOpaque(false);
338
        this.langButton.setBorderPainted(false);
340
        this.langButton.setBorderPainted(false);
339
        this.langButton.setContentAreaFilled(false);
341
        this.langButton.setContentAreaFilled(false);
340
        this.langButton.setBorder(BorderFactory.createEmptyBorder());
342
        this.langButton.setBorder(BorderFactory.createEmptyBorder());
341
        this.langButton.setFocusable(false);
343
        this.langButton.setFocusable(false);
342
        this.langButton.setVisible(false);
344
        this.langButton.setVisible(false);
343
        panelButton.add(this.langButton, c2);
345
        panelButton.add(this.langButton, c2);
344
        c2.gridx++;
346
        c2.gridx++;
345
        this.buttonConnect.setOpaque(false);
347
        this.buttonConnect.setOpaque(false);
346
        panelButton.add(this.buttonConnect, c2);
348
        panelButton.add(this.buttonConnect, c2);
347
 
349
 
348
        c.gridy++;
350
        c.gridy++;
349
        c.gridx = 0;
351
        c.gridx = 0;
350
        c.weightx = 0;
352
        c.weightx = 0;
351
        c.gridwidth = GridBagConstraints.REMAINDER;
353
        c.gridwidth = GridBagConstraints.REMAINDER;
352
        c.weighty = 1;
354
        c.weighty = 1;
353
        this.add(panelButton, c);
355
        this.add(panelButton, c);
354
 
356
 
355
        this.buttonConnect.addActionListener(this);
357
        this.buttonConnect.addActionListener(this);
356
 
358
 
357
        checkValidity();
359
        checkValidity();
358
    }
360
    }
359
 
361
 
360
    public final void initLocalization(final String baseName, final List<Locale> toDisplay) {
362
    public final void initLocalization(final String baseName, final List<Locale> toDisplay) {
361
        if (baseName == null)
363
        if (baseName == null)
362
            throw new NullPointerException("Null baseName");
364
            throw new NullPointerException("Null baseName");
363
        if (this.localeBaseName != null)
365
        if (this.localeBaseName != null)
364
            throw new IllegalStateException("Already inited to " + this.localeBaseName);
366
            throw new IllegalStateException("Already inited to " + this.localeBaseName);
365
        this.localeBaseName = baseName;
367
        this.localeBaseName = baseName;
366
        this.localesToDisplay.addAll(toDisplay);
368
        this.localesToDisplay.addAll(toDisplay);
367
        this.setUILanguage(UserProps.getInstance().getLocale());
369
        this.setUILanguage(UserProps.getInstance().getLocale());
368
        TM.getInstance();
370
        TM.getInstance();
369
    }
371
    }
370
 
372
 
371
    private void checkValidity() {
373
    private void checkValidity() {
372
        this.buttonConnect.setEnabled(this.connectionAllowed == null && this.areFieldsValidated());
374
        this.buttonConnect.setEnabled(this.connectionAllowed == null && this.areFieldsValidated());
373
        this.buttonConnect.setToolTipText(this.connectionAllowed);
375
        this.buttonConnect.setToolTipText(this.connectionAllowed);
374
    }
376
    }
375
 
377
 
376
    private boolean areFieldsValidated() {
378
    private boolean areFieldsValidated() {
377
        if (this.societeSelector) {
379
        if (this.societeSelector) {
378
            if (this.comboSociete.isEmpty()) {
380
            if (this.comboSociete.isEmpty()) {
379
                return false;
381
                return false;
380
            }
382
            }
381
        }
383
        }
382
        if (this.textLogin == null || this.textLogin.isEmpty() || this.textPassWord == null) {
384
        if (this.textLogin == null || this.textLogin.isEmpty() || this.textPassWord == null) {
383
            return false;
385
            return false;
384
        } else {
386
        } else {
385
            return this.textLogin.getValidState().isValid();
387
            return this.textLogin.getValidState().isValid();
386
        }
388
        }
387
    }
389
    }
388
 
390
 
389
    @Override
391
    @Override
390
    public void actionPerformed(final ActionEvent e) {
392
    public void actionPerformed(final ActionEvent e) {
391
        if (isConnecting())
393
        if (isConnecting())
392
            return;
394
            return;
393
 
395
 
394
        if (this.textLogin.getValue().length() < 1) {
396
        if (this.textLogin.getValue().length() < 1) {
395
            return;
397
            return;
396
        }
398
        }
397
 
399
 
398
        setConnecting(true);
400
        setConnecting(true);
399
 
401
 
400
        execute(new Runnable() {
402
        execute(new Runnable() {
401
            @Override
403
            @Override
402
            public void run() {
404
            public void run() {
403
                connect();
405
                connect();
404
            }
406
            }
405
        });
407
        });
406
    }
408
    }
407
 
409
 
408
    static private void execute(Runnable runnable) {
410
    static private void execute(Runnable runnable) {
409
        final Thread t = new Thread(runnable, "ConnexionPanel Login");
411
        final Thread t = new Thread(runnable, "ConnexionPanel Login");
410
        t.setPriority(Thread.MIN_PRIORITY);
412
        t.setPriority(Thread.MIN_PRIORITY);
411
        t.start();
413
        t.start();
412
    }
414
    }
413
 
415
 
414
    public void setConnectionAllowed(String connectionAllowed) {
416
    public void setConnectionAllowed(String connectionAllowed) {
415
        this.connectionAllowed = connectionAllowed;
417
        this.connectionAllowed = connectionAllowed;
416
        this.checkValidity();
418
        this.checkValidity();
417
    }
419
    }
418
 
420
 
419
    private synchronized boolean isConnecting() {
421
    private synchronized boolean isConnecting() {
420
        return this.isConnecting;
422
        return this.isConnecting;
421
    }
423
    }
422
 
424
 
423
    private synchronized void setConnecting(final boolean b) {
425
    private synchronized void setConnecting(final boolean b) {
424
 
426
 
425
        if (b) {
427
        if (b) {
426
            this.reloadPanel.setMode(ReloadPanel.MODE_ROTATE);
428
            this.reloadPanel.setMode(ReloadPanel.MODE_ROTATE);
427
            this.buttonConnect.setEnabled(false);
429
            this.buttonConnect.setEnabled(false);
428
            this.saveCheckBox.setEnabled(false);
430
            this.saveCheckBox.setEnabled(false);
429
            if (this.comboSociete != null) {
431
            if (this.comboSociete != null) {
430
                this.comboSociete.setEnabled(false);
432
                this.comboSociete.setEnabled(false);
431
            }
433
            }
432
            ((JTextField) this.textLogin.getComp()).setEditable(false);
434
            ((JTextField) this.textLogin.getComp()).setEditable(false);
433
            this.textPassWord.setEditable(false);
435
            this.textPassWord.setEditable(false);
434
        } else {
436
        } else {
435
            this.buttonConnect.setEnabled(true);
437
            this.buttonConnect.setEnabled(true);
436
            this.saveCheckBox.setEnabled(true);
438
            this.saveCheckBox.setEnabled(true);
437
            if (this.comboSociete != null) {
439
            if (this.comboSociete != null) {
438
                this.comboSociete.setEnabled(true);
440
                this.comboSociete.setEnabled(true);
439
            }
441
            }
440
            ((JTextField) this.textLogin.getComp()).setEditable(true);
442
            ((JTextField) this.textLogin.getComp()).setEditable(true);
441
            this.textPassWord.setEditable(true);
443
            this.textPassWord.setEditable(true);
442
            this.reloadPanel.setMode(ReloadPanel.MODE_EMPTY);
444
            this.reloadPanel.setMode(ReloadPanel.MODE_EMPTY);
443
 
445
 
444
        }
446
        }
445
        this.isConnecting = b;
447
        this.isConnecting = b;
446
    }
448
    }
447
 
449
 
448
    private void connect() {
450
    private void connect() {
449
        final String userName = this.textLogin.getValue();
451
        final String userName = this.textLogin.getValue();
450
        final Tuple2<String, String> loginRes;
452
        final Tuple2<String, String> loginRes;
451
        // if the user has not typed anything and there was a stored pass
453
        // if the user has not typed anything and there was a stored pass
452
        if (this.clearPassword == null)
454
        if (this.clearPassword == null)
453
            loginRes = this.login.connectEnc(userName, this.encryptedPassword);
455
            loginRes = this.login.connectEnc(userName, this.encryptedPassword);
454
        else
456
        else
455
            // handle legacy passwords
457
            // handle legacy passwords
456
            loginRes = this.login.connectClear(userName, this.clearPassword, "\"" + this.clearPassword + "\"");
458
            loginRes = this.login.connectClear(userName, this.clearPassword, "\"" + this.clearPassword + "\"");
457
 
459
 
458
        if (loginRes.get0() == null) {
460
        if (loginRes.get0() == null) {
459
            // --->Connexion
461
            // --->Connexion
460
            UserProps.getInstance().setLastLoginName(userName);
462
            UserProps.getInstance().setLastLoginName(userName);
461
            if (this.societeSelector) {
463
            if (this.societeSelector) {
462
                UserProps.getInstance().setLastSocieteID(this.comboSociete.getSelectedId());
464
                UserProps.getInstance().setLastSocieteID(this.comboSociete.getSelectedId());
463
            }
465
            }
464
            if (this.saveCheckBox.isSelected()) {
466
            if (this.saveCheckBox.isSelected()) {
465
                UserProps.getInstance().setEncryptedStoredPassword(loginRes.get1());
467
                UserProps.getInstance().setEncryptedStoredPassword(loginRes.get1());
466
            } else
468
            } else
467
                UserProps.getInstance().setEncryptedStoredPassword(null);
469
                UserProps.getInstance().setEncryptedStoredPassword(null);
468
            UserProps.getInstance().store();
470
            UserProps.getInstance().store();
469
 
471
 
470
            // Preload right to avoid request in AWT later
472
            // Preload right to avoid request in AWT later
471
            if (UserRightsManager.getInstance() != null)
473
            if (UserRightsManager.getInstance() != null)
472
                UserRightsManager.getInstance().preloadRightsForUserId(UserManager.getUserID());
474
                UserRightsManager.getInstance().preloadRightsForUserId(UserManager.getUserID());
473
 
475
 
474
            // Fermeture des frames et execution du Runnable
476
            // Fermeture des frames et execution du Runnable
475
            this.r.run();
477
            this.r.run();
476
            // only dispose the panel after r has run so that there's always something on screen for
478
            // only dispose the panel after r has run so that there's always something on screen for
477
            // the user to see
479
            // the user to see
478
            SwingUtilities.getWindowAncestor(this).dispose();
480
            SwingUtilities.getWindowAncestor(this).dispose();
479
        } else {
481
        } else {
480
            unlockUIOnError(loginRes.get0(), userName);
482
            unlockUIOnError(loginRes.get0(), userName);
481
        }
483
        }
482
    }
484
    }
483
 
485
 
484
    private void unlockUIOnError(final String error, final String userName) {
486
    private void unlockUIOnError(final String error, final String userName) {
485
        SwingUtilities.invokeLater(new Runnable() {
487
        SwingUtilities.invokeLater(new Runnable() {
486
            @Override
488
            @Override
487
            public void run() {
489
            public void run() {
488
                try {
490
                try {
489
                    ConnexionPanel.this.reloadPanel.setMode(ReloadPanel.MODE_BLINK);
491
                    ConnexionPanel.this.reloadPanel.setMode(ReloadPanel.MODE_BLINK);
490
                    JOptionPane.showMessageDialog(ConnexionPanel.this, TM.getTM().translate("loginPanel." + error, userName));
492
                    JOptionPane.showMessageDialog(ConnexionPanel.this, TM.getTM().translate("loginPanel." + error, userName));
491
                    // Guillaume wants this for the Nego
493
                    // Guillaume wants this for the Nego
492
                    if (Login.UNKNOWN_USER.equals(error))
494
                    if (Login.UNKNOWN_USER.equals(error))
493
                        ConnexionPanel.this.textLogin.setValue(ConnexionPanel.this.adminLogin);
495
                        ConnexionPanel.this.textLogin.setValue(ConnexionPanel.this.adminLogin);
494
                    setConnecting(false);
496
                    setConnecting(false);
495
                } catch (Exception e) {
497
                } catch (Exception e) {
496
                    e.printStackTrace();
498
                    e.printStackTrace();
497
                }
499
                }
498
            }
500
            }
499
        });
501
        });
500
    }
502
    }
501
 
503
 
502
    public int getSelectedSociete() {
504
    public int getSelectedSociete() {
503
        return this.comboSociete.getSelectedId();
505
        return this.comboSociete.getSelectedId();
504
    }
506
    }
505
 
507
 
506
    protected void setUILanguage(Locale locale) {
508
    protected void setUILanguage(Locale locale) {
507
        final ResourceBundle bundle = ResourceBundle.getBundle(this.localeBaseName, locale, TranslationManager.getControl());
509
        final ResourceBundle bundle = ResourceBundle.getBundle(this.localeBaseName, locale, TranslationManager.getControl());
508
        this.adminLogin = bundle.getString("adminLogin");
510
        this.adminLogin = bundle.getString("adminLogin");
509
        this.loginLabel.setText(bundle.getString("loginLabel"));
511
        this.loginLabel.setText(bundle.getString("loginLabel"));
510
        this.passwordLabel.setText(bundle.getString("passwordLabel"));
512
        this.passwordLabel.setText(bundle.getString("passwordLabel"));
511
        this.companyLabel.setText(bundle.getString("companyLabel"));
513
        this.companyLabel.setText(bundle.getString("companyLabel"));
512
        this.saveCheckBox.setText(bundle.getString("saveCheckBox"));
514
        this.saveCheckBox.setText(bundle.getString("saveCheckBox"));
513
        this.buttonConnect.setText(bundle.getString("buttonConnect"));
515
        this.buttonConnect.setText(bundle.getString("buttonConnect"));
514
        this.langButton.setText(locale.getLanguage());
516
        this.langButton.setText(locale.getLanguage());
515
        this.langButton.setVisible(true);
517
        this.langButton.setVisible(true);
-
 
518
        // for next launch
516
        UserProps.getInstance().setLocale(locale);
519
        UserProps.getInstance().setLocale(locale);
-
 
520
        // for current run
-
 
521
        ((PropsConfiguration) Configuration.getInstance()).setLocale(locale);
-
 
522
        // for code that will never need more than one Locale concurrently
-
 
523
        // (e.g. used by TM singletons in various packages)
-
 
524
        Locale.setDefault(locale);
-
 
525
        // as explained in Locale.setDefault() javadoc : "be prepared to reinitialize
-
 
526
        // locale-sensitive code"
-
 
527
        JComponent.setDefaultLocale(locale);
-
 
528
        // throw RuntimeException like ResourceBundle.getBundle() at the beginning of this
-
 
529
        // method
-
 
530
        TranslationManager.createDefaultInstance();
517
    }
531
    }
518
}
532
}