1 | package fr.mael.jiwigo.ui; |
---|
2 | |
---|
3 | import java.awt.Dimension; |
---|
4 | import java.awt.GridBagConstraints; |
---|
5 | import java.awt.GridBagLayout; |
---|
6 | import java.awt.Insets; |
---|
7 | import java.awt.event.ActionEvent; |
---|
8 | import java.awt.event.ActionListener; |
---|
9 | import java.awt.event.ItemEvent; |
---|
10 | import java.awt.event.ItemListener; |
---|
11 | import java.awt.event.KeyEvent; |
---|
12 | import java.awt.event.KeyListener; |
---|
13 | import java.awt.event.WindowEvent; |
---|
14 | import java.awt.event.WindowListener; |
---|
15 | import java.text.SimpleDateFormat; |
---|
16 | import java.util.Arrays; |
---|
17 | import java.util.Locale; |
---|
18 | import javax.swing.AbstractListModel; |
---|
19 | import javax.swing.ComboBoxModel; |
---|
20 | import javax.swing.JButton; |
---|
21 | import javax.swing.JCheckBox; |
---|
22 | import javax.swing.JComboBox; |
---|
23 | import javax.swing.JDialog; |
---|
24 | import javax.swing.JLabel; |
---|
25 | import javax.swing.JOptionPane; |
---|
26 | import javax.swing.JPasswordField; |
---|
27 | import javax.swing.JTextField; |
---|
28 | import fr.mael.jiwigo.Main; |
---|
29 | import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; |
---|
30 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
31 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
32 | import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; |
---|
33 | |
---|
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 | */ |
---|
64 | public class ConnexionDialog extends JDialog implements ActionListener, ItemListener, KeyListener, WindowListener { |
---|
65 | |
---|
66 | /** |
---|
67 | * Logger |
---|
68 | */ |
---|
69 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.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 | /** |
---|
111 | * Constructor |
---|
112 | */ |
---|
113 | public ConnexionDialog() { |
---|
114 | Locale defautLocale = Messages.usedLocale; |
---|
115 | labelUrl = new JLabel(Messages.getMessage("connexionDialog_urlSite")); |
---|
116 | labelLogin = new JLabel(Messages.getMessage("connexionDialog_login")); |
---|
117 | labelPass = new JLabel(Messages.getMessage("connexionDialog_pass")); |
---|
118 | comboLocales = new JComboBox(); |
---|
119 | boutonOk = new JButton("Ok"); |
---|
120 | String url = PreferencesManagement.getValue(PreferencesEnum.URL_SITE.getLabel()); |
---|
121 | String login = PreferencesManagement.getValue(PreferencesEnum.LOGIN.getLabel()); |
---|
122 | String pass = PreferencesManagement.getValue(PreferencesEnum.PASSWORD.getLabel()); |
---|
123 | fieldUrl = new JTextField(url); |
---|
124 | loginField = new JTextField(login); |
---|
125 | passwordField = new JPasswordField(pass); |
---|
126 | checkBoxRetenir = new JCheckBox(Messages.getMessage("connexionDialog_sauvegarder")); |
---|
127 | if (!url.equals("") && !login.equals("") && !pass.equals("")) { |
---|
128 | checkBoxRetenir.setSelected(true); |
---|
129 | } |
---|
130 | Dimension fieldDimensions = new Dimension(300, 30); |
---|
131 | fieldUrl.setPreferredSize(fieldDimensions); |
---|
132 | fieldUrl.addActionListener(this); |
---|
133 | loginField.setPreferredSize(fieldDimensions); |
---|
134 | loginField.addActionListener(this); |
---|
135 | passwordField.setPreferredSize(fieldDimensions); |
---|
136 | passwordField.addActionListener(this); |
---|
137 | this.getContentPane().setLayout(new GridBagLayout()); |
---|
138 | GridBagConstraints constraints = new GridBagConstraints(); |
---|
139 | constraints.gridx = 0; |
---|
140 | constraints.gridy = 0; |
---|
141 | constraints.insets = new Insets(3, 3, 3, 3); |
---|
142 | this.getContentPane().add(labelUrl, constraints); |
---|
143 | constraints.gridx++; |
---|
144 | this.getContentPane().add(fieldUrl, constraints); |
---|
145 | constraints.gridx = 0; |
---|
146 | constraints.gridy++; |
---|
147 | this.getContentPane().add(labelLogin, constraints); |
---|
148 | constraints.gridx++; |
---|
149 | this.getContentPane().add(loginField, constraints); |
---|
150 | constraints.gridx = 0; |
---|
151 | constraints.gridy++; |
---|
152 | this.getContentPane().add(labelPass, constraints); |
---|
153 | constraints.gridx++; |
---|
154 | this.getContentPane().add(passwordField, constraints); |
---|
155 | constraints.gridx = 0; |
---|
156 | constraints.gridy++; |
---|
157 | this.getContentPane().add(comboLocales, constraints); |
---|
158 | constraints.gridx++; |
---|
159 | this.getContentPane().add(checkBoxRetenir, constraints); |
---|
160 | constraints.gridx = 0; |
---|
161 | constraints.gridy++;// |
---|
162 | // for (Locale locale : SimpleDateFormat.getAvailableLocales()) { |
---|
163 | // comboLocales.addItem(locale.getDisplayName(Locale.ENGLISH)); |
---|
164 | // } |
---|
165 | comboLocales.setPreferredSize(new Dimension(130, 25)); |
---|
166 | comboLocales.addItemListener(this); |
---|
167 | localeModel model = new localeModel(); |
---|
168 | comboLocales.setModel(model); |
---|
169 | model.setSelectedItem(defautLocale.getDisplayLanguage(Locale.ENGLISH)); |
---|
170 | this.getContentPane().add(boutonOk, constraints); |
---|
171 | boutonOk.setPreferredSize(new Dimension(80, 30)); |
---|
172 | boutonOk.addActionListener(this); |
---|
173 | this.addWindowListener(this); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | public void showDialog() { |
---|
178 | this.pack(); |
---|
179 | this.setLocationRelativeTo(null); |
---|
180 | this.setVisible(true); |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | @Override |
---|
185 | public void actionPerformed(ActionEvent paramActionEvent) { |
---|
186 | //if one field is empty, an error is displayed |
---|
187 | if (fieldUrl.getText().equals("") || loginField.getText().equals("") || passwordField.getText().equals("")) { |
---|
188 | JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_emptyField"), |
---|
189 | Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
190 | } else { |
---|
191 | if (!fieldUrl.getText().startsWith("http://")) { |
---|
192 | fieldUrl.setText("http://" + fieldUrl.getText()); |
---|
193 | } |
---|
194 | //instanciation of the session manager |
---|
195 | Main.sessionManager = new SessionManager(loginField.getText(), passwordField.getText(), fieldUrl.getText()); |
---|
196 | if (checkBoxRetenir.isSelected()) { |
---|
197 | PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), loginField.getText()); |
---|
198 | PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), passwordField.getText()); |
---|
199 | PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), fieldUrl.getText()); |
---|
200 | } else { |
---|
201 | PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), ""); |
---|
202 | PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), ""); |
---|
203 | PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), ""); |
---|
204 | } |
---|
205 | if (!Main.sessionManager.processLogin()) { |
---|
206 | //if the login fails, an error is displayed |
---|
207 | JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), |
---|
208 | Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
209 | } else { |
---|
210 | // Locale.setDefault((Locale) comboLocales.getSelectedItem()); |
---|
211 | Main.showFrame(); |
---|
212 | //hides the dialog |
---|
213 | this.dispose(); |
---|
214 | } |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * @author mael |
---|
220 | * Internal class that represents the model of the combobox that allows |
---|
221 | * to choose a locale |
---|
222 | */ |
---|
223 | class localeModel extends AbstractListModel implements ComboBoxModel { |
---|
224 | |
---|
225 | Locale[] locales = SimpleDateFormat.getAvailableLocales(); |
---|
226 | //List<Locale> locales = Messages.getAvailableBundles(); |
---|
227 | String[] localesNames; |
---|
228 | |
---|
229 | String selection = null; |
---|
230 | |
---|
231 | |
---|
232 | public localeModel() { |
---|
233 | localesNames = new String[locales.length]; |
---|
234 | int compteur = 0; |
---|
235 | for (Locale locale : locales) { |
---|
236 | localesNames[compteur] = locale.getDisplayName(Locale.ENGLISH); |
---|
237 | compteur++; |
---|
238 | } |
---|
239 | Arrays.sort(localesNames); |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | public Object getElementAt(int index) { |
---|
244 | return localesNames[index]; |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | public int getSize() { |
---|
249 | return locales.length; |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | public void setSelectedItem(Object anItem) { |
---|
254 | selection = (String) anItem; |
---|
255 | } |
---|
256 | |
---|
257 | |
---|
258 | public Object getSelectedItem() { |
---|
259 | if (selection != null) |
---|
260 | return selection; |
---|
261 | else |
---|
262 | return null; |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | @Override |
---|
268 | public void itemStateChanged(ItemEvent paramItemEvent) { |
---|
269 | for (Locale locale : SimpleDateFormat.getAvailableLocales()) { |
---|
270 | if (locale.getDisplayName(Locale.ENGLISH).equals(paramItemEvent.getItem())) { |
---|
271 | Messages.usedLocale = locale; |
---|
272 | dispose(); |
---|
273 | ConnexionDialog dial = new ConnexionDialog(); |
---|
274 | dial.showDialog(); |
---|
275 | |
---|
276 | } |
---|
277 | |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | |
---|
282 | @Override |
---|
283 | public void keyPressed(KeyEvent arg0) { |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | @Override |
---|
288 | public void keyReleased(KeyEvent arg0) { |
---|
289 | |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | @Override |
---|
294 | public void keyTyped(KeyEvent arg0) { |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | @Override |
---|
299 | public void windowActivated(WindowEvent e) { |
---|
300 | // TODO Auto-generated method stub |
---|
301 | |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | @Override |
---|
306 | public void windowClosed(WindowEvent e) { |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | @Override |
---|
311 | public void windowClosing(WindowEvent e) { |
---|
312 | System.exit(0); |
---|
313 | } |
---|
314 | |
---|
315 | |
---|
316 | @Override |
---|
317 | public void windowDeactivated(WindowEvent e) { |
---|
318 | // TODO Auto-generated method stub |
---|
319 | |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | @Override |
---|
324 | public void windowDeiconified(WindowEvent e) { |
---|
325 | // TODO Auto-generated method stub |
---|
326 | |
---|
327 | } |
---|
328 | |
---|
329 | |
---|
330 | @Override |
---|
331 | public void windowIconified(WindowEvent e) { |
---|
332 | // TODO Auto-generated method stub |
---|
333 | |
---|
334 | } |
---|
335 | |
---|
336 | |
---|
337 | @Override |
---|
338 | public void windowOpened(WindowEvent e) { |
---|
339 | // TODO Auto-generated method stub |
---|
340 | |
---|
341 | } |
---|
342 | } |
---|