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