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

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

Adds a wait cursor for time consuming operations.

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