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

Last change on this file since 6992 was 6992, checked in by mlg, 14 years ago

Bug fixing :
Java was not killed when exiting the connection dialog. It's fixed by adding a window listener to the dialog, and execute System.exit(0) on closing.

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