source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/ConnexionDialog.java @ 9921

Last change on this file since 9921 was 9921, checked in by mlg, 13 years ago

Adds spring for the dependency injection
Deletion of jdom (not useful just for a class that manipulates a simple XML file)

File size: 13.0 KB
Line 
1package fr.mael.jiwigo.ui;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.FlowLayout;
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.Insets;
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
11import java.awt.event.ItemEvent;
12import java.awt.event.ItemListener;
13import java.awt.event.KeyEvent;
14import java.awt.event.KeyListener;
15import java.awt.event.WindowEvent;
16import java.awt.event.WindowListener;
17import java.text.SimpleDateFormat;
18import java.util.Arrays;
19import java.util.Locale;
20
21import javax.swing.AbstractListModel;
22import javax.swing.ComboBoxModel;
23import javax.swing.JButton;
24import javax.swing.JCheckBox;
25import javax.swing.JComboBox;
26import javax.swing.JDialog;
27import javax.swing.JLabel;
28import javax.swing.JOptionPane;
29import javax.swing.JPanel;
30import javax.swing.JPasswordField;
31import javax.swing.JTextField;
32
33import fr.mael.jiwigo.Main;
34import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum;
35import fr.mael.jiwigo.transverse.session.SessionManager;
36import fr.mael.jiwigo.transverse.util.Messages;
37import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
38import fr.mael.jiwigo.transverse.util.spring.SpringUtils;
39
40/**
41   Copyright (c) 2010, Mael
42   All rights reserved.
43
44   Redistribution and use in source and binary forms, with or without
45   modification, are permitted provided that the following conditions are met:
46    * Redistributions of source code must retain the above copyright
47      notice, this list of conditions and the following disclaimer.
48    * Redistributions in binary form must reproduce the above copyright
49      notice, this list of conditions and the following disclaimer in the
50      documentation and/or other materials provided with the distribution.
51    * Neither the name of jiwigo nor the
52      names of its contributors may be used to endorse or promote products
53      derived from this software without specific prior written permission.
54
55   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
56   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
57   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
58   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
59   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
60   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
61   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65
66 * @author mael
67 * Dialog de connexion au site
68 */
69public class ConnexionDialog extends JDialog implements ActionListener, ItemListener, KeyListener, WindowListener {
70
71    /**
72     * Logger
73     */
74    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
75            .getLog(ConnexionDialog.class);
76    /**
77     * field that contains the url
78     */
79    private JTextField fieldUrl;
80    /**
81     * field that contains the login
82     */
83    private JTextField loginField;
84    /**
85     * field that contains de password
86     */
87    private JPasswordField passwordField;
88    /**
89     * label of the url field
90     */
91    private JLabel labelUrl;
92    /**
93     * label of the login field
94     */
95    private JLabel labelLogin;
96    /**
97     *  label of the password field
98     */
99    private JLabel labelPass;
100    /**
101     * ok button for the connection
102     */
103    private JButton boutonOk;
104
105    /**
106     * Box that allows to save informations
107     */
108    private JCheckBox checkBoxRetenir;
109
110    /**
111     * Combo containing all locales
112     */
113    private JComboBox comboLocales;
114
115    /**
116     * Panel that allows to define a proxy to use
117     */
118    private ProxyPanel proxyPanel;
119
120    private SessionManager sessionManager;
121
122    /**
123     * Constructor
124     */
125    public ConnexionDialog() {
126        Locale defautLocale = Messages.usedLocale;
127        labelUrl = new JLabel(Messages.getMessage("connexionDialog_urlSite"));
128        labelLogin = new JLabel(Messages.getMessage("connexionDialog_login"));
129        labelPass = new JLabel(Messages.getMessage("connexionDialog_pass"));
130        proxyPanel = new ProxyPanel(this);
131        comboLocales = new JComboBox();
132        boutonOk = new JButton("Ok");
133        String url = PreferencesManagement.getValue(PreferencesEnum.URL_SITE.getLabel());
134        String login = PreferencesManagement.getValue(PreferencesEnum.LOGIN.getLabel());
135        String pass = PreferencesManagement.getValue(PreferencesEnum.PASSWORD.getLabel());
136
137        fieldUrl = new JTextField(url);
138        loginField = new JTextField(login);
139        passwordField = new JPasswordField(pass);
140        checkBoxRetenir = new JCheckBox(Messages.getMessage("connexionDialog_sauvegarder"));
141        if (!url.equals("") && !login.equals("") && !pass.equals("")) {
142            checkBoxRetenir.setSelected(true);
143        }
144        JPanel panelInformations = new JPanel(new GridBagLayout());
145        Dimension fieldDimensions = new Dimension(300, 30);
146        fieldUrl.setPreferredSize(fieldDimensions);
147        fieldUrl.addActionListener(this);
148        loginField.setPreferredSize(fieldDimensions);
149        loginField.addActionListener(this);
150        passwordField.setPreferredSize(fieldDimensions);
151        passwordField.addActionListener(this);
152        this.getContentPane().setLayout(new BorderLayout());
153        GridBagConstraints constraints = new GridBagConstraints();
154        constraints.gridx = 0;
155        constraints.gridy = 0;
156        constraints.insets = new Insets(3, 3, 3, 3);
157        panelInformations.add(labelUrl, constraints);
158        constraints.gridx++;
159        panelInformations.add(fieldUrl, constraints);
160        constraints.gridx = 0;
161        constraints.gridy++;
162        panelInformations.add(labelLogin, constraints);
163        constraints.gridx++;
164        panelInformations.add(loginField, constraints);
165        constraints.gridx = 0;
166        constraints.gridy++;
167        panelInformations.add(labelPass, constraints);
168        constraints.gridx++;
169        panelInformations.add(passwordField, constraints);
170        constraints.gridx = 0;
171        constraints.gridy++;
172        panelInformations.add(comboLocales, constraints);
173        constraints.gridx++;
174        panelInformations.add(checkBoxRetenir, constraints);
175        constraints.gridx = 0;
176        constraints.gridy++;//
177        //      for (Locale locale : SimpleDateFormat.getAvailableLocales()) {
178        //          comboLocales.addItem(locale.getDisplayName(Locale.ENGLISH));
179        //      }
180        comboLocales.setPreferredSize(new Dimension(130, 25));
181        comboLocales.addItemListener(this);
182        boutonOk.setPreferredSize(new Dimension(80, 30));
183        boutonOk.addActionListener(this);
184        JPanel panelBouton = new JPanel(new FlowLayout(FlowLayout.CENTER));
185        panelBouton.add(boutonOk);
186        localeModel model = new localeModel();
187        comboLocales.setModel(model);
188        model.setSelectedItem(defautLocale.getDisplayLanguage(Locale.ENGLISH));
189
190        this.sessionManager = SpringUtils.getSessionManager();
191
192        this.getContentPane().add(panelInformations, BorderLayout.NORTH);
193        this.getContentPane().add(proxyPanel, BorderLayout.CENTER);
194        this.getContentPane().add(panelBouton, BorderLayout.SOUTH);
195        this.addWindowListener(this);
196        this.setResizable(false);
197    }
198
199    public void showDialog() {
200        this.pack();
201        this.setLocationRelativeTo(null);
202        this.setVisible(true);
203    }
204
205    @Override
206    public void actionPerformed(ActionEvent paramActionEvent) {
207        //if one field is empty, an error is displayed
208        if (fieldUrl.getText().equals("") || loginField.getText().equals("") || passwordField.getText().equals("")) {
209            JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_emptyField"), Messages
210                    .getMessage("error"), JOptionPane.ERROR_MESSAGE);
211        } else {
212            if (!fieldUrl.getText().startsWith("http://")) {
213                fieldUrl.setText("http://" + fieldUrl.getText());
214            }
215            //instanciation of the session manager
216            sessionManager.setLogin(loginField.getText());
217            sessionManager.setPassword(passwordField.getText());
218            sessionManager.setUrl(fieldUrl.getText() + "/ws.php");
219            boolean echecProxy = false;
220            if (proxyPanel.getCheckBox().isSelected()) {
221                int portProxy = 0;
222                try {
223                    portProxy = Integer.valueOf(proxyPanel.getFieldPort().getText());
224                    sessionManager.setPortProxy(portProxy);
225                    sessionManager.setUrlProxy(proxyPanel.getFieldUrl().getText());
226                    sessionManager.setLoginProxy(proxyPanel.getFieldLogin().getText());
227                    sessionManager.setPassProxy(proxyPanel.getFieldPassword().getText());
228                    sessionManager.setUsesProxy(true);
229                } catch (Exception e) {
230                    echecProxy = true;
231                }
232
233            }
234
235            //save informations...
236            if (checkBoxRetenir.isSelected()) {
237                PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), loginField.getText());
238                PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), passwordField.getText());
239                PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), fieldUrl.getText());
240                PreferencesManagement
241                        .setValue(PreferencesEnum.URL_PROXY.getLabel(), proxyPanel.getFieldUrl().getText());
242                PreferencesManagement.setValue(PreferencesEnum.PORT_PROXY.getLabel(), proxyPanel.getFieldPort()
243                        .getText());
244                PreferencesManagement.setValue(PreferencesEnum.USER_PROXY.getLabel(), proxyPanel.getFieldLogin()
245                        .getText());
246                PreferencesManagement.setValue(PreferencesEnum.PASS_PROXY.getLabel(), proxyPanel.getFieldPassword()
247                        .getText());
248                PreferencesManagement.setValue(PreferencesEnum.USE_PROXY.getLabel(), String.valueOf(proxyPanel
249                        .getCheckBox().isSelected()));
250            }
251            //... or not
252            else {
253                PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), "");
254                PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), "");
255                PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), "");
256                PreferencesManagement
257                        .setValue(PreferencesEnum.URL_PROXY.getLabel(), proxyPanel.getFieldUrl().getText());
258                PreferencesManagement.setValue(PreferencesEnum.PORT_PROXY.getLabel(), proxyPanel.getFieldPort()
259                        .getText());
260                PreferencesManagement.setValue(PreferencesEnum.USER_PROXY.getLabel(), proxyPanel.getFieldLogin()
261                        .getText());
262                PreferencesManagement.setValue(PreferencesEnum.PASS_PROXY.getLabel(), proxyPanel.getFieldPassword()
263                        .getText());
264                PreferencesManagement.setValue(PreferencesEnum.USE_PROXY.getLabel(), "false");
265            }
266            if (echecProxy) {
267                JOptionPane.showMessageDialog(null, Messages.getMessage("proxyError"), Messages.getMessage("error"),
268                        JOptionPane.ERROR_MESSAGE);
269            } else {
270                int response = sessionManager.processLogin();
271                if (response == 0) {
272                    Main.showFrame();
273                    //hides the dialog
274                    this.dispose();
275                } else if (response == 1) {
276                    //if the login fails, an error is displayed
277                    if (!sessionManager.isProxyError()) {
278                        JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"),
279                                Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
280                    }
281                } else if (response == 2) {
282                    JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_proxyAuthError"), Messages
283                            .getMessage("error"), JOptionPane.ERROR_MESSAGE);
284                }
285            }
286
287        }
288    }
289
290    @Override
291    public void itemStateChanged(ItemEvent paramItemEvent) {
292        for (Locale locale : SimpleDateFormat.getAvailableLocales()) {
293            if (locale.getDisplayName(Locale.ENGLISH).equals(paramItemEvent.getItem())) {
294                Messages.usedLocale = locale;
295                dispose();
296                ConnexionDialog dial = new ConnexionDialog();
297                dial.showDialog();
298
299            }
300
301        }
302    }
303
304    @Override
305    public void keyPressed(KeyEvent arg0) {
306    }
307
308    @Override
309    public void keyReleased(KeyEvent arg0) {
310
311    }
312
313    @Override
314    public void keyTyped(KeyEvent arg0) {
315    }
316
317    @Override
318    public void windowActivated(WindowEvent e) {
319        // TODO Auto-generated method stub
320
321    }
322
323    @Override
324    public void windowClosed(WindowEvent e) {
325    }
326
327    @Override
328    public void windowClosing(WindowEvent e) {
329        System.exit(0);
330    }
331
332    @Override
333    public void windowDeactivated(WindowEvent e) {
334        // TODO Auto-generated method stub
335
336    }
337
338    @Override
339    public void windowDeiconified(WindowEvent e) {
340        // TODO Auto-generated method stub
341
342    }
343
344    @Override
345    public void windowIconified(WindowEvent e) {
346        // TODO Auto-generated method stub
347
348    }
349
350    @Override
351    public void windowOpened(WindowEvent e) {
352        // TODO Auto-generated method stub
353
354    }
355
356    /**
357     * @author mael
358     * Internal class that represents the model of the combobox that allows
359     * to choose a locale
360     */
361    class localeModel extends AbstractListModel implements ComboBoxModel {
362
363        Locale[] locales = SimpleDateFormat.getAvailableLocales();
364        //List<Locale> locales = Messages.getAvailableBundles();
365        String[] localesNames;
366
367        String selection = null;
368
369        public localeModel() {
370            localesNames = new String[locales.length];
371            int compteur = 0;
372            for (Locale locale : locales) {
373                localesNames[compteur] = locale.getDisplayName(Locale.ENGLISH);
374                compteur++;
375            }
376            Arrays.sort(localesNames);
377        }
378
379        public Object getElementAt(int index) {
380            return localesNames[index];
381        }
382
383        public int getSize() {
384            return locales.length;
385        }
386
387        public void setSelectedItem(Object anItem) {
388            selection = (String) anItem;
389        }
390
391        public Object getSelectedItem() {
392            if (selection != null)
393                return selection;
394            else
395                return null;
396        }
397    }
398}
Note: See TracBrowser for help on using the repository browser.