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

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

Adds support for addSimple
By default, addSimple will be used. It can be changed in the preferences dialog.

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