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