- Timestamp:
- Oct 16, 2010, 1:08:28 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/ConnexionDialog.java
r7070 r7221 16 16 import java.util.Arrays; 17 17 import java.util.Locale; 18 19 18 import javax.swing.AbstractListModel; 20 19 import javax.swing.ComboBoxModel; … … 27 26 import javax.swing.JPasswordField; 28 27 import javax.swing.JTextField; 29 30 28 import fr.mael.jiwigo.Main; 31 29 import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; … … 33 31 import fr.mael.jiwigo.transverse.util.Messages; 34 32 import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; 33 35 34 36 35 /** … … 64 63 */ 65 64 public class ConnexionDialog extends JDialog implements ActionListener, ItemListener, KeyListener, WindowListener { 66 /** 67 * Logger 68 */ 69 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 70 .getLog(ConnexionDialog.class); 71 /** 72 * field that contains the url 73 */ 74 private JTextField fieldUrl; 75 /** 76 * field that contains the login 77 */ 78 private JTextField loginField; 79 /** 80 * field that contains de password 81 */ 82 private JPasswordField passwordField; 83 /** 84 * label of the url field 85 */ 86 private JLabel labelUrl; 87 /** 88 * label of the login field 89 */ 90 private JLabel labelLogin; 91 /** 92 * label of the password field 93 */ 94 private JLabel labelPass; 95 /** 96 * ok button for the connection 97 */ 98 private JButton boutonOk; 99 100 /** 101 * Box that allows to save informations 102 */ 103 private JCheckBox checkBoxRetenir; 104 105 /** 106 * Combo containing all locales 107 */ 108 private JComboBox comboLocales; 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 public void showDialog() { 177 this.pack(); 178 this.setLocationRelativeTo(null); 179 this.setVisible(true); 180 } 181 182 @Override 183 public void actionPerformed(ActionEvent paramActionEvent) { 184 //if one field is empty, an error is displayed 185 if (fieldUrl.getText().equals("") || loginField.getText().equals("") || passwordField.getText().equals("")) { 186 JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_emptyField"), Messages 187 .getMessage("error"), JOptionPane.ERROR_MESSAGE); 188 } else { 189 if (!fieldUrl.getText().startsWith("http://")) { 190 fieldUrl.setText("http://" + fieldUrl.getText()); 191 } 192 //instanciation of the session manager 193 Main.sessionManager = new SessionManager(loginField.getText(), passwordField.getText(), fieldUrl.getText()); 194 if (checkBoxRetenir.isSelected()) { 195 PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), loginField.getText()); 196 PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), passwordField.getText()); 197 PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), fieldUrl.getText()); 198 } else { 199 PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), ""); 200 PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), ""); 201 PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), ""); 202 } 203 if (!Main.sessionManager.processLogin()) { 204 //if the login fails, an error is displayed 205 JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), Messages 206 .getMessage("error"), JOptionPane.ERROR_MESSAGE); 207 } else { 208 // Locale.setDefault((Locale) comboLocales.getSelectedItem()); 209 Main.showFrame(); 210 //hides the dialog 211 this.dispose(); 212 } 213 } 214 } 215 216 /** 217 * @author mael 218 * Internal class that represents the model of the combobox that allows 219 * to choose a locale 220 */ 221 class localeModel extends AbstractListModel implements ComboBoxModel { 222 Locale[] locales = SimpleDateFormat.getAvailableLocales(); 223 //List<Locale> locales = Messages.getAvailableBundles(); 224 String[] localesNames; 225 226 String selection = null; 227 228 public localeModel() { 229 localesNames = new String[locales.length]; 230 int compteur = 0; 231 for (Locale locale : locales) { 232 localesNames[compteur] = locale.getDisplayName(Locale.ENGLISH); 233 compteur++; 234 } 235 Arrays.sort(localesNames); 236 } 237 238 public Object getElementAt(int index) { 239 return localesNames[index]; 240 } 241 242 public int getSize() { 243 return locales.length; 244 } 245 246 public void setSelectedItem(Object anItem) { 247 selection = (String) anItem; 248 } 249 250 public Object getSelectedItem() { 251 if (selection != null) 252 return selection; 253 else 254 return null; 255 } 256 } 257 258 @Override 259 public void itemStateChanged(ItemEvent paramItemEvent) { 260 for (Locale locale : SimpleDateFormat.getAvailableLocales()) { 261 if (locale.getDisplayName(Locale.ENGLISH).equals(paramItemEvent.getItem())) { 262 Messages.usedLocale = locale; 263 dispose(); 264 ConnexionDialog dial = new ConnexionDialog(); 265 dial.showDialog(); 266 267 } 268 269 } 270 } 271 272 @Override 273 public void keyPressed(KeyEvent arg0) { 274 } 275 276 @Override 277 public void keyReleased(KeyEvent arg0) { 278 279 } 280 281 @Override 282 public void keyTyped(KeyEvent arg0) { 283 } 284 285 @Override 286 public void windowActivated(WindowEvent e) { 287 // TODO Auto-generated method stub 288 289 } 290 291 @Override 292 public void windowClosed(WindowEvent e) { 293 System.out.println("closed"); 294 } 295 296 @Override 297 public void windowClosing(WindowEvent e) { 298 System.exit(0); 299 } 300 301 @Override 302 public void windowDeactivated(WindowEvent e) { 303 // TODO Auto-generated method stub 304 305 } 306 307 @Override 308 public void windowDeiconified(WindowEvent e) { 309 // TODO Auto-generated method stub 310 311 } 312 313 @Override 314 public void windowIconified(WindowEvent e) { 315 // TODO Auto-generated method stub 316 317 } 318 319 @Override 320 public void windowOpened(WindowEvent e) { 321 // TODO Auto-generated method stub 322 323 } 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 } 324 342 }
Note: See TracChangeset
for help on using the changeset viewer.