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

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

Translation of the comments
French -> English

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