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

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

Modifications to use the last version of jiwigo-ws-api

File size: 12.6 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                if (!Main.sessionManager.processLogin()) {
263                    //if the login fails, an error is displayed
264                    if (!Main.sessionManager.isProxyError()) {
265                        JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"),
266                                Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
267                    }
268                } else {
269                    //          Locale.setDefault((Locale) comboLocales.getSelectedItem());
270                    Main.showFrame();
271                    //hides the dialog
272                    this.dispose();
273                }
274            }
275
276        }
277    }
278
279    @Override
280    public void itemStateChanged(ItemEvent paramItemEvent) {
281        for (Locale locale : SimpleDateFormat.getAvailableLocales()) {
282            if (locale.getDisplayName(Locale.ENGLISH).equals(paramItemEvent.getItem())) {
283                Messages.usedLocale = locale;
284                dispose();
285                ConnexionDialog dial = new ConnexionDialog();
286                dial.showDialog();
287
288            }
289
290        }
291    }
292
293    @Override
294    public void keyPressed(KeyEvent arg0) {
295    }
296
297    @Override
298    public void keyReleased(KeyEvent arg0) {
299
300    }
301
302    @Override
303    public void keyTyped(KeyEvent arg0) {
304    }
305
306    @Override
307    public void windowActivated(WindowEvent e) {
308        // TODO Auto-generated method stub
309
310    }
311
312    @Override
313    public void windowClosed(WindowEvent e) {
314    }
315
316    @Override
317    public void windowClosing(WindowEvent e) {
318        System.exit(0);
319    }
320
321    @Override
322    public void windowDeactivated(WindowEvent e) {
323        // TODO Auto-generated method stub
324
325    }
326
327    @Override
328    public void windowDeiconified(WindowEvent e) {
329        // TODO Auto-generated method stub
330
331    }
332
333    @Override
334    public void windowIconified(WindowEvent e) {
335        // TODO Auto-generated method stub
336
337    }
338
339    @Override
340    public void windowOpened(WindowEvent e) {
341        // TODO Auto-generated method stub
342
343    }
344
345    /**
346     * @author mael
347     * Internal class that represents the model of the combobox that allows
348     * to choose a locale
349     */
350    class localeModel extends AbstractListModel implements ComboBoxModel {
351
352        Locale[] locales = SimpleDateFormat.getAvailableLocales();
353        //List<Locale> locales = Messages.getAvailableBundles();
354        String[] localesNames;
355
356        String selection = null;
357
358        public localeModel() {
359            localesNames = new String[locales.length];
360            int compteur = 0;
361            for (Locale locale : locales) {
362                localesNames[compteur] = locale.getDisplayName(Locale.ENGLISH);
363                compteur++;
364            }
365            Arrays.sort(localesNames);
366        }
367
368        public Object getElementAt(int index) {
369            return localesNames[index];
370        }
371
372        public int getSize() {
373            return locales.length;
374        }
375
376        public void setSelectedItem(Object anItem) {
377            selection = (String) anItem;
378        }
379
380        public Object getSelectedItem() {
381            if (selection != null)
382                return selection;
383            else
384                return null;
385        }
386    }
387}
Note: See TracBrowser for help on using the repository browser.