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

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

Modifications to use the last version of jiwigo-ws-api which manages proxy errors.

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