package fr.mael.jiwigo.ui; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Locale; import javax.swing.AbstractListModel; import javax.swing.ComboBoxModel; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import fr.mael.jiwigo.Main; import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; import fr.mael.jiwigo.transverse.session.SessionManager; import fr.mael.jiwigo.transverse.util.Messages; import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; /** Copyright (c) 2010, Mael All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of jiwigo nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @author mael * Dialog de connexion au site */ public class ConnexionDialog extends JDialog implements ActionListener, ItemListener, KeyListener, WindowListener { /** * Logger */ public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory .getLog(ConnexionDialog.class); /** * field that contains the url */ private JTextField fieldUrl; /** * field that contains the login */ private JTextField loginField; /** * field that contains de password */ private JPasswordField passwordField; /** * label of the url field */ private JLabel labelUrl; /** * label of the login field */ private JLabel labelLogin; /** * label of the password field */ private JLabel labelPass; /** * ok button for the connection */ private JButton boutonOk; /** * Box that allows to save informations */ private JCheckBox checkBoxRetenir; /** * Combo containing all locales */ private JComboBox comboLocales; /** * Panel that allows to define a proxy to use */ private ProxyPanel proxyPanel; /** * Constructor */ public ConnexionDialog() { Locale defautLocale = Messages.usedLocale; labelUrl = new JLabel(Messages.getMessage("connexionDialog_urlSite")); labelLogin = new JLabel(Messages.getMessage("connexionDialog_login")); labelPass = new JLabel(Messages.getMessage("connexionDialog_pass")); proxyPanel = new ProxyPanel(this); comboLocales = new JComboBox(); boutonOk = new JButton("Ok"); String url = PreferencesManagement.getValue(PreferencesEnum.URL_SITE.getLabel()); String login = PreferencesManagement.getValue(PreferencesEnum.LOGIN.getLabel()); String pass = PreferencesManagement.getValue(PreferencesEnum.PASSWORD.getLabel()); fieldUrl = new JTextField(url); loginField = new JTextField(login); passwordField = new JPasswordField(pass); checkBoxRetenir = new JCheckBox(Messages.getMessage("connexionDialog_sauvegarder")); if (!url.equals("") && !login.equals("") && !pass.equals("")) { checkBoxRetenir.setSelected(true); } JPanel panelInformations = new JPanel(new GridBagLayout()); Dimension fieldDimensions = new Dimension(300, 30); fieldUrl.setPreferredSize(fieldDimensions); fieldUrl.addActionListener(this); loginField.setPreferredSize(fieldDimensions); loginField.addActionListener(this); passwordField.setPreferredSize(fieldDimensions); passwordField.addActionListener(this); this.getContentPane().setLayout(new BorderLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 0; constraints.gridy = 0; constraints.insets = new Insets(3, 3, 3, 3); panelInformations.add(labelUrl, constraints); constraints.gridx++; panelInformations.add(fieldUrl, constraints); constraints.gridx = 0; constraints.gridy++; panelInformations.add(labelLogin, constraints); constraints.gridx++; panelInformations.add(loginField, constraints); constraints.gridx = 0; constraints.gridy++; panelInformations.add(labelPass, constraints); constraints.gridx++; panelInformations.add(passwordField, constraints); constraints.gridx = 0; constraints.gridy++; panelInformations.add(comboLocales, constraints); constraints.gridx++; panelInformations.add(checkBoxRetenir, constraints); constraints.gridx = 0; constraints.gridy++;// // for (Locale locale : SimpleDateFormat.getAvailableLocales()) { // comboLocales.addItem(locale.getDisplayName(Locale.ENGLISH)); // } comboLocales.setPreferredSize(new Dimension(130, 25)); comboLocales.addItemListener(this); boutonOk.setPreferredSize(new Dimension(80, 30)); boutonOk.addActionListener(this); JPanel panelBouton = new JPanel(new FlowLayout(FlowLayout.CENTER)); panelBouton.add(boutonOk); localeModel model = new localeModel(); comboLocales.setModel(model); model.setSelectedItem(defautLocale.getDisplayLanguage(Locale.ENGLISH)); this.getContentPane().add(panelInformations, BorderLayout.NORTH); this.getContentPane().add(proxyPanel, BorderLayout.CENTER); this.getContentPane().add(panelBouton, BorderLayout.SOUTH); this.addWindowListener(this); this.setResizable(false); } public void showDialog() { this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } @Override public void actionPerformed(ActionEvent paramActionEvent) { //if one field is empty, an error is displayed if (fieldUrl.getText().equals("") || loginField.getText().equals("") || passwordField.getText().equals("")) { JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_emptyField"), Messages .getMessage("error"), JOptionPane.ERROR_MESSAGE); } else { if (!fieldUrl.getText().startsWith("http://")) { fieldUrl.setText("http://" + fieldUrl.getText()); } //instanciation of the session manager Main.sessionManager = new SessionManager(loginField.getText(), passwordField.getText(), fieldUrl.getText()); boolean echecProxy = false; if (proxyPanel.getCheckBox().isSelected()) { int portProxy = 0; try { portProxy = Integer.valueOf(proxyPanel.getFieldPort().getText()); Main.sessionManager.setPortProxy(portProxy); Main.sessionManager.setUrlProxy(proxyPanel.getFieldUrl().getText()); Main.sessionManager.setLoginProxy(proxyPanel.getFieldLogin().getText()); Main.sessionManager.setPassProxy(proxyPanel.getFieldPassword().getText()); Main.sessionManager.setUsesProxy(true); } catch (Exception e) { echecProxy = true; } } //save informations... if (checkBoxRetenir.isSelected()) { PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), loginField.getText()); PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), passwordField.getText()); PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), fieldUrl.getText()); PreferencesManagement .setValue(PreferencesEnum.URL_PROXY.getLabel(), proxyPanel.getFieldUrl().getText()); PreferencesManagement.setValue(PreferencesEnum.PORT_PROXY.getLabel(), proxyPanel.getFieldPort() .getText()); PreferencesManagement.setValue(PreferencesEnum.USER_PROXY.getLabel(), proxyPanel.getFieldLogin() .getText()); PreferencesManagement.setValue(PreferencesEnum.PASS_PROXY.getLabel(), proxyPanel.getFieldPassword() .getText()); PreferencesManagement.setValue(PreferencesEnum.USE_PROXY.getLabel(), String.valueOf(proxyPanel .getCheckBox().isSelected())); } //... or not else { PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), ""); PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), ""); PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), ""); PreferencesManagement .setValue(PreferencesEnum.URL_PROXY.getLabel(), proxyPanel.getFieldUrl().getText()); PreferencesManagement.setValue(PreferencesEnum.PORT_PROXY.getLabel(), proxyPanel.getFieldPort() .getText()); PreferencesManagement.setValue(PreferencesEnum.USER_PROXY.getLabel(), proxyPanel.getFieldLogin() .getText()); PreferencesManagement.setValue(PreferencesEnum.PASS_PROXY.getLabel(), proxyPanel.getFieldPassword() .getText()); PreferencesManagement.setValue(PreferencesEnum.USE_PROXY.getLabel(), "false"); } if (echecProxy) { JOptionPane.showMessageDialog(null, Messages.getMessage("proxyError"), Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); } else { if (!Main.sessionManager.processLogin()) { //if the login fails, an error is displayed if (!Main.sessionManager.isProxyError()) { JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); } } else { // Locale.setDefault((Locale) comboLocales.getSelectedItem()); Main.showFrame(); //hides the dialog this.dispose(); } } } } @Override public void itemStateChanged(ItemEvent paramItemEvent) { for (Locale locale : SimpleDateFormat.getAvailableLocales()) { if (locale.getDisplayName(Locale.ENGLISH).equals(paramItemEvent.getItem())) { Messages.usedLocale = locale; dispose(); ConnexionDialog dial = new ConnexionDialog(); dial.showDialog(); } } } @Override public void keyPressed(KeyEvent arg0) { } @Override public void keyReleased(KeyEvent arg0) { } @Override public void keyTyped(KeyEvent arg0) { } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosed(WindowEvent e) { } @Override public void windowClosing(WindowEvent e) { System.exit(0); } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } /** * @author mael * Internal class that represents the model of the combobox that allows * to choose a locale */ class localeModel extends AbstractListModel implements ComboBoxModel { Locale[] locales = SimpleDateFormat.getAvailableLocales(); //List locales = Messages.getAvailableBundles(); String[] localesNames; String selection = null; public localeModel() { localesNames = new String[locales.length]; int compteur = 0; for (Locale locale : locales) { localesNames[compteur] = locale.getDisplayName(Locale.ENGLISH); compteur++; } Arrays.sort(localesNames); } public Object getElementAt(int index) { return localesNames[index]; } public int getSize() { return locales.length; } public void setSelectedItem(Object anItem) { selection = (String) anItem; } public Object getSelectedItem() { if (selection != null) return selection; else return null; } } }