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

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

Integrates last jiwigo-ws-api modifications

File size: 13.3 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(passwordField.getText());
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(proxyPanel.getFieldPassword().getText());
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(), passwordField.getText());
241                PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), fieldUrl.getText());
242                PreferencesManagement
243                        .setValue(PreferencesEnum.URL_PROXY.getLabel(), proxyPanel.getFieldUrl().getText());
244                PreferencesManagement.setValue(PreferencesEnum.PORT_PROXY.getLabel(), proxyPanel.getFieldPort()
245                        .getText());
246                PreferencesManagement.setValue(PreferencesEnum.USER_PROXY.getLabel(), proxyPanel.getFieldLogin()
247                        .getText());
248                PreferencesManagement.setValue(PreferencesEnum.PASS_PROXY.getLabel(), proxyPanel.getFieldPassword()
249                        .getSelectedText());
250                PreferencesManagement.setValue(PreferencesEnum.USE_PROXY.getLabel(),
251                        String.valueOf(proxyPanel.getCheckBox().isSelected()));
252            }
253            //... or not
254            else {
255                PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), "");
256                PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), "");
257                PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), "");
258                PreferencesManagement
259                        .setValue(PreferencesEnum.URL_PROXY.getLabel(), proxyPanel.getFieldUrl().getText());
260                PreferencesManagement.setValue(PreferencesEnum.PORT_PROXY.getLabel(), proxyPanel.getFieldPort()
261                        .getText());
262                PreferencesManagement.setValue(PreferencesEnum.USER_PROXY.getLabel(), proxyPanel.getFieldLogin()
263                        .getText());
264                PreferencesManagement.setValue(PreferencesEnum.PASS_PROXY.getLabel(), proxyPanel.getFieldPassword()
265                        .getSelectedText());
266                PreferencesManagement.setValue(PreferencesEnum.USE_PROXY.getLabel(), "false");
267            }
268            if (echecProxy) {
269                JOptionPane.showMessageDialog(null, Messages.getMessage("proxyError"), Messages.getMessage("error"),
270                        JOptionPane.ERROR_MESSAGE);
271            } else {
272                int response = 0;
273                try {
274                    response = sessionManager.processLogin();
275                } catch (JiwigoException e) {
276                    if (e.getCause() != null && ProxyAuthenticationException.class.equals(e.getCause().getClass())) {
277                        response = 2;
278                    } else {
279                        response = 1;
280                    }
281                }
282                if (response == 0) {
283                    Main.showFrame();
284                    //hides the dialog
285                    this.dispose();
286                } else if (response == 1) {
287                    //if the login fails, an error is displayed
288                    JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"),
289                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
290                } else if (response == 2) {
291                    JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_proxyAuthError"),
292                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
293                }
294            }
295
296        }
297    }
298
299    @Override
300    public void itemStateChanged(ItemEvent paramItemEvent) {
301        for (Locale locale : SimpleDateFormat.getAvailableLocales()) {
302            if (locale.getDisplayName(Locale.ENGLISH).equals(paramItemEvent.getItem())) {
303                Messages.usedLocale = locale;
304                dispose();
305                ConnexionDialog dial = new ConnexionDialog();
306                dial.showDialog();
307
308            }
309
310        }
311    }
312
313    @Override
314    public void keyPressed(KeyEvent arg0) {
315    }
316
317    @Override
318    public void keyReleased(KeyEvent arg0) {
319
320    }
321
322    @Override
323    public void keyTyped(KeyEvent arg0) {
324    }
325
326    @Override
327    public void windowActivated(WindowEvent e) {
328        // TODO Auto-generated method stub
329
330    }
331
332    @Override
333    public void windowClosed(WindowEvent e) {
334    }
335
336    @Override
337    public void windowClosing(WindowEvent e) {
338        System.exit(0);
339    }
340
341    @Override
342    public void windowDeactivated(WindowEvent e) {
343        // TODO Auto-generated method stub
344
345    }
346
347    @Override
348    public void windowDeiconified(WindowEvent e) {
349        // TODO Auto-generated method stub
350
351    }
352
353    @Override
354    public void windowIconified(WindowEvent e) {
355        // TODO Auto-generated method stub
356
357    }
358
359    @Override
360    public void windowOpened(WindowEvent e) {
361        // TODO Auto-generated method stub
362
363    }
364
365    /**
366     * @author mael
367     * Internal class that represents the model of the combobox that allows
368     * to choose a locale
369     */
370    class localeModel extends AbstractListModel implements ComboBoxModel {
371
372        Locale[] locales = SimpleDateFormat.getAvailableLocales();
373        //List<Locale> locales = Messages.getAvailableBundles();
374        String[] localesNames;
375
376        String selection = null;
377
378        public localeModel() {
379            localesNames = new String[locales.length];
380            int compteur = 0;
381            for (Locale locale : locales) {
382                localesNames[compteur] = locale.getDisplayName(Locale.ENGLISH);
383                compteur++;
384            }
385            Arrays.sort(localesNames);
386        }
387
388        public Object getElementAt(int index) {
389            return localesNames[index];
390        }
391
392        public int getSize() {
393            return locales.length;
394        }
395
396        public void setSelectedItem(Object anItem) {
397            selection = (String) anItem;
398        }
399
400        public Object getSelectedItem() {
401            if (selection != null)
402                return selection;
403            else
404                return null;
405        }
406    }
407}
Note: See TracBrowser for help on using the repository browser.