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 | |
---|
10 | import javax.swing.JButton; |
---|
11 | import javax.swing.JCheckBox; |
---|
12 | import javax.swing.JDialog; |
---|
13 | import javax.swing.JLabel; |
---|
14 | import javax.swing.JOptionPane; |
---|
15 | import javax.swing.JPasswordField; |
---|
16 | import javax.swing.JTextField; |
---|
17 | |
---|
18 | import fr.mael.jiwigo.Main; |
---|
19 | import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; |
---|
20 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
21 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
22 | import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; |
---|
23 | |
---|
24 | /** |
---|
25 | Copyright (c) 2010, Mael |
---|
26 | All rights reserved. |
---|
27 | |
---|
28 | Redistribution and use in source and binary forms, with or without |
---|
29 | modification, are permitted provided that the following conditions are met: |
---|
30 | * Redistributions of source code must retain the above copyright |
---|
31 | notice, this list of conditions and the following disclaimer. |
---|
32 | * Redistributions in binary form must reproduce the above copyright |
---|
33 | notice, this list of conditions and the following disclaimer in the |
---|
34 | documentation and/or other materials provided with the distribution. |
---|
35 | * Neither the name of jiwigo nor the |
---|
36 | names of its contributors may be used to endorse or promote products |
---|
37 | derived from this software without specific prior written permission. |
---|
38 | |
---|
39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
40 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
41 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
42 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
43 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
44 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
45 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
46 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
47 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
48 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
49 | |
---|
50 | * @author mael |
---|
51 | * Dialog de connexion au site |
---|
52 | */ |
---|
53 | public class ConnexionDialog extends JDialog implements ActionListener { |
---|
54 | /** |
---|
55 | * Logger |
---|
56 | */ |
---|
57 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
58 | .getLog(ConnexionDialog.class); |
---|
59 | /** |
---|
60 | * field contenant l'url |
---|
61 | */ |
---|
62 | private JTextField fieldUrl; |
---|
63 | /** |
---|
64 | * field contenant le login |
---|
65 | */ |
---|
66 | private JTextField loginField; |
---|
67 | /** |
---|
68 | * field contenant le mot de passe |
---|
69 | */ |
---|
70 | private JPasswordField passwordField; |
---|
71 | /** |
---|
72 | * label de l'url |
---|
73 | */ |
---|
74 | private JLabel labelUrl; |
---|
75 | /** |
---|
76 | * label du login |
---|
77 | */ |
---|
78 | private JLabel labelLogin; |
---|
79 | /** |
---|
80 | * label du mot de passe |
---|
81 | */ |
---|
82 | private JLabel labelPass; |
---|
83 | /** |
---|
84 | * bouton ok, pour la connexion |
---|
85 | */ |
---|
86 | private JButton boutonOk; |
---|
87 | |
---|
88 | /** |
---|
89 | * Box qui permet de retenir les informations |
---|
90 | */ |
---|
91 | private JCheckBox checkBoxRetenir; |
---|
92 | |
---|
93 | /** |
---|
94 | * Constructeur |
---|
95 | */ |
---|
96 | public ConnexionDialog() { |
---|
97 | labelUrl = new JLabel(Messages.getMessage("connexionDialog_urlSite")); |
---|
98 | labelLogin = new JLabel(Messages.getMessage("connexionDialog_login")); |
---|
99 | labelPass = new JLabel(Messages.getMessage("connexionDialog_pass")); |
---|
100 | boutonOk = new JButton("Ok"); |
---|
101 | String url = PreferencesManagement.getValue(PreferencesEnum.URL_SITE.getLabel()); |
---|
102 | String login = PreferencesManagement.getValue(PreferencesEnum.LOGIN.getLabel()); |
---|
103 | String pass = PreferencesManagement.getValue(PreferencesEnum.PASSWORD.getLabel()); |
---|
104 | fieldUrl = new JTextField(url); |
---|
105 | loginField = new JTextField(login); |
---|
106 | passwordField = new JPasswordField(pass); |
---|
107 | checkBoxRetenir = new JCheckBox(Messages.getMessage("connexionDialog_sauvegarder")); |
---|
108 | if (!url.equals("") && !login.equals("") && !pass.equals("")) { |
---|
109 | checkBoxRetenir.setSelected(true); |
---|
110 | } |
---|
111 | Dimension fieldDimensions = new Dimension(300, 30); |
---|
112 | fieldUrl.setPreferredSize(fieldDimensions); |
---|
113 | loginField.setPreferredSize(fieldDimensions); |
---|
114 | passwordField.setPreferredSize(fieldDimensions); |
---|
115 | this.getContentPane().setLayout(new GridBagLayout()); |
---|
116 | GridBagConstraints constraints = new GridBagConstraints(); |
---|
117 | constraints.gridx = 0; |
---|
118 | constraints.gridy = 0; |
---|
119 | constraints.insets = new Insets(3, 3, 3, 3); |
---|
120 | this.getContentPane().add(labelUrl, constraints); |
---|
121 | constraints.gridx++; |
---|
122 | this.getContentPane().add(fieldUrl, constraints); |
---|
123 | constraints.gridx = 0; |
---|
124 | constraints.gridy++; |
---|
125 | this.getContentPane().add(labelLogin, constraints); |
---|
126 | constraints.gridx++; |
---|
127 | this.getContentPane().add(loginField, constraints); |
---|
128 | constraints.gridx = 0; |
---|
129 | constraints.gridy++; |
---|
130 | this.getContentPane().add(labelPass, constraints); |
---|
131 | constraints.gridx++; |
---|
132 | this.getContentPane().add(passwordField, constraints); |
---|
133 | constraints.gridx = 0; |
---|
134 | constraints.gridy++; |
---|
135 | this.getContentPane().add(boutonOk, constraints); |
---|
136 | constraints.gridx++; |
---|
137 | this.getContentPane().add(checkBoxRetenir, constraints); |
---|
138 | boutonOk.setPreferredSize(new Dimension(80, 30)); |
---|
139 | boutonOk.addActionListener(this); |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | public void showDialog() { |
---|
144 | this.pack(); |
---|
145 | this.setLocationRelativeTo(null); |
---|
146 | this.setVisible(true); |
---|
147 | } |
---|
148 | |
---|
149 | @Override |
---|
150 | public void actionPerformed(ActionEvent paramActionEvent) { |
---|
151 | //si un des champs est vide, on affiche une erreur |
---|
152 | if (fieldUrl.getText().equals("") || loginField.getText().equals("") || passwordField.getText().equals("")) { |
---|
153 | JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_emptyField"), Messages |
---|
154 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
155 | } else { |
---|
156 | if (!fieldUrl.getText().startsWith("http://")) { |
---|
157 | fieldUrl.setText("http://" + fieldUrl.getText()); |
---|
158 | } |
---|
159 | //sinon, on instancie le session manager de la classe principale |
---|
160 | Main.sessionManager = new SessionManager(loginField.getText(), passwordField.getText(), fieldUrl.getText()); |
---|
161 | if (checkBoxRetenir.isSelected()) { |
---|
162 | PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), loginField.getText()); |
---|
163 | PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), passwordField.getText()); |
---|
164 | PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), fieldUrl.getText()); |
---|
165 | } else { |
---|
166 | PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), ""); |
---|
167 | PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), ""); |
---|
168 | PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), ""); |
---|
169 | } |
---|
170 | if (!Main.sessionManager.processLogin()) { |
---|
171 | //si le login a échoué, on affiche une erreur |
---|
172 | JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), Messages |
---|
173 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
174 | } else { |
---|
175 | Main.showFrame(); |
---|
176 | //on cache le dialog de connexion |
---|
177 | this.dispose(); |
---|
178 | } |
---|
179 | } |
---|
180 | } |
---|
181 | } |
---|