Ignore:
Timestamp:
Jan 21, 2011, 7:20:05 PM (13 years ago)
Author:
mlg
Message:

Adds proxy support for the connection.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/ConnexionDialog.java

    r7221 r8830  
    11package fr.mael.jiwigo.ui;
    22
     3import java.awt.BorderLayout;
    34import java.awt.Dimension;
     5import java.awt.FlowLayout;
    46import java.awt.GridBagConstraints;
    57import java.awt.GridBagLayout;
     
    1618import java.util.Arrays;
    1719import java.util.Locale;
     20
    1821import javax.swing.AbstractListModel;
    1922import javax.swing.ComboBoxModel;
     
    2427import javax.swing.JLabel;
    2528import javax.swing.JOptionPane;
     29import javax.swing.JPanel;
    2630import javax.swing.JPasswordField;
    2731import javax.swing.JTextField;
     32
    2833import fr.mael.jiwigo.Main;
    2934import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum;
     
    3136import fr.mael.jiwigo.transverse.util.Messages;
    3237import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
    33 
    3438
    3539/**
     
    6468public class ConnexionDialog extends JDialog implements ActionListener, ItemListener, KeyListener, WindowListener {
    6569
    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);
     70    /**
     71     * Logger
     72     */
     73    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
     74            .getLog(ConnexionDialog.class);
     75    /**
     76     * field that contains the url
     77     */
     78    private JTextField fieldUrl;
     79    /**
     80     * field that contains the login
     81     */
     82    private JTextField loginField;
     83    /**
     84     * field that contains de password
     85     */
     86    private JPasswordField passwordField;
     87    /**
     88     * label of the url field
     89     */
     90    private JLabel labelUrl;
     91    /**
     92     * label of the login field
     93     */
     94    private JLabel labelLogin;
     95    /**
     96     *  label of the password field
     97     */
     98    private JLabel labelPass;
     99    /**
     100     * ok button for the connection
     101     */
     102    private JButton boutonOk;
     103
     104    /**
     105     * Box that allows to save informations
     106     */
     107    private JCheckBox checkBoxRetenir;
     108
     109    /**
     110     * Combo containing all locales
     111     */
     112    private JComboBox comboLocales;
     113
     114    /**
     115     * Panel that allows to define a proxy to use
     116     */
     117    private ProxyPanel proxyPanel;
     118
     119    /**
     120     * Constructor
     121     */
     122    public ConnexionDialog() {
     123        Locale defautLocale = Messages.usedLocale;
     124        labelUrl = new JLabel(Messages.getMessage("connexionDialog_urlSite"));
     125        labelLogin = new JLabel(Messages.getMessage("connexionDialog_login"));
     126        labelPass = new JLabel(Messages.getMessage("connexionDialog_pass"));
     127        proxyPanel = new ProxyPanel(this);
     128        comboLocales = new JComboBox();
     129        boutonOk = new JButton("Ok");
     130        String url = PreferencesManagement.getValue(PreferencesEnum.URL_SITE.getLabel());
     131        String login = PreferencesManagement.getValue(PreferencesEnum.LOGIN.getLabel());
     132        String pass = PreferencesManagement.getValue(PreferencesEnum.PASSWORD.getLabel());
     133
     134        fieldUrl = new JTextField(url);
     135        loginField = new JTextField(login);
     136        passwordField = new JPasswordField(pass);
     137        checkBoxRetenir = new JCheckBox(Messages.getMessage("connexionDialog_sauvegarder"));
     138        if (!url.equals("") && !login.equals("") && !pass.equals("")) {
     139            checkBoxRetenir.setSelected(true);
     140        }
     141        JPanel panelInformations = new JPanel(new GridBagLayout());
     142        Dimension fieldDimensions = new Dimension(300, 30);
     143        fieldUrl.setPreferredSize(fieldDimensions);
     144        fieldUrl.addActionListener(this);
     145        loginField.setPreferredSize(fieldDimensions);
     146        loginField.addActionListener(this);
     147        passwordField.setPreferredSize(fieldDimensions);
     148        passwordField.addActionListener(this);
     149        this.getContentPane().setLayout(new BorderLayout());
     150        GridBagConstraints constraints = new GridBagConstraints();
     151        constraints.gridx = 0;
     152        constraints.gridy = 0;
     153        constraints.insets = new Insets(3, 3, 3, 3);
     154        panelInformations.add(labelUrl, constraints);
     155        constraints.gridx++;
     156        panelInformations.add(fieldUrl, constraints);
     157        constraints.gridx = 0;
     158        constraints.gridy++;
     159        panelInformations.add(labelLogin, constraints);
     160        constraints.gridx++;
     161        panelInformations.add(loginField, constraints);
     162        constraints.gridx = 0;
     163        constraints.gridy++;
     164        panelInformations.add(labelPass, constraints);
     165        constraints.gridx++;
     166        panelInformations.add(passwordField, constraints);
     167        constraints.gridx = 0;
     168        constraints.gridy++;
     169        panelInformations.add(comboLocales, constraints);
     170        constraints.gridx++;
     171        panelInformations.add(checkBoxRetenir, constraints);
     172        constraints.gridx = 0;
     173        constraints.gridy++;// 
     174        //      for (Locale locale : SimpleDateFormat.getAvailableLocales()) {
     175        //          comboLocales.addItem(locale.getDisplayName(Locale.ENGLISH));
     176        //      }
     177        comboLocales.setPreferredSize(new Dimension(130, 25));
     178        comboLocales.addItemListener(this);
     179        boutonOk.setPreferredSize(new Dimension(80, 30));
     180        boutonOk.addActionListener(this);
     181        JPanel panelBouton = new JPanel(new FlowLayout(FlowLayout.CENTER));
     182        panelBouton.add(boutonOk);
     183        localeModel model = new localeModel();
     184        comboLocales.setModel(model);
     185        model.setSelectedItem(defautLocale.getDisplayLanguage(Locale.ENGLISH));
     186        this.getContentPane().add(panelInformations, BorderLayout.NORTH);
     187        this.getContentPane().add(proxyPanel, BorderLayout.CENTER);
     188        this.getContentPane().add(panelBouton, BorderLayout.SOUTH);
     189        this.addWindowListener(this);
     190        this.setResizable(false);
     191    }
     192
     193    public void showDialog() {
     194        this.pack();
     195        this.setLocationRelativeTo(null);
     196        this.setVisible(true);
     197    }
     198
     199    @Override
     200    public void actionPerformed(ActionEvent paramActionEvent) {
     201        //if one field is empty, an error is displayed
     202        if (fieldUrl.getText().equals("") || loginField.getText().equals("") || passwordField.getText().equals("")) {
     203            JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_emptyField"), Messages
     204                    .getMessage("error"), JOptionPane.ERROR_MESSAGE);
     205        } else {
     206            if (!fieldUrl.getText().startsWith("http://")) {
     207                fieldUrl.setText("http://" + fieldUrl.getText());
     208            }
     209            //instanciation of the session manager
     210            Main.sessionManager = new SessionManager(loginField.getText(), passwordField.getText(), fieldUrl.getText());
     211            boolean echecProxy = false;
     212            if (proxyPanel.getCheckBox().isSelected()) {
     213                int portProxy = 0;
     214                try {
     215                    portProxy = Integer.valueOf(proxyPanel.getFieldPort().getText());
     216                    Main.sessionManager.setPortProxy(portProxy);
     217                    Main.sessionManager.setUrlProxy(proxyPanel.getFieldUrl().getText());
     218                    Main.sessionManager.setLoginProxy(proxyPanel.getFieldLogin().getText());
     219                    Main.sessionManager.setPassProxy(proxyPanel.getFieldPassword().getText());
     220                    Main.sessionManager.setUsesProxy(true);
     221                } catch (Exception e) {
     222                    echecProxy = true;
    129223                }
    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);
     224
     225            }
     226
     227            //save informations...
     228            if (checkBoxRetenir.isSelected()) {
     229                PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), loginField.getText());
     230                PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), passwordField.getText());
     231                PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), fieldUrl.getText());
     232                PreferencesManagement
     233                        .setValue(PreferencesEnum.URL_PROXY.getLabel(), proxyPanel.getFieldUrl().getText());
     234                PreferencesManagement.setValue(PreferencesEnum.PORT_PROXY.getLabel(), proxyPanel.getFieldPort()
     235                        .getText());
     236                PreferencesManagement.setValue(PreferencesEnum.USER_PROXY.getLabel(), proxyPanel.getFieldLogin()
     237                        .getText());
     238                PreferencesManagement.setValue(PreferencesEnum.PASS_PROXY.getLabel(), proxyPanel.getFieldPassword()
     239                        .getText());
     240                PreferencesManagement.setValue(PreferencesEnum.USE_PROXY.getLabel(), String.valueOf(proxyPanel
     241                        .getCheckBox().isSelected()));
     242            }
     243            //... or not
     244            else {
     245                PreferencesManagement.setValue(PreferencesEnum.LOGIN.getLabel(), "");
     246                PreferencesManagement.setValue(PreferencesEnum.PASSWORD.getLabel(), "");
     247                PreferencesManagement.setValue(PreferencesEnum.URL_SITE.getLabel(), "");
     248                PreferencesManagement
     249                        .setValue(PreferencesEnum.URL_PROXY.getLabel(), proxyPanel.getFieldUrl().getText());
     250                PreferencesManagement.setValue(PreferencesEnum.PORT_PROXY.getLabel(), proxyPanel.getFieldPort()
     251                        .getText());
     252                PreferencesManagement.setValue(PreferencesEnum.USER_PROXY.getLabel(), proxyPanel.getFieldLogin()
     253                        .getText());
     254                PreferencesManagement.setValue(PreferencesEnum.PASS_PROXY.getLabel(), proxyPanel.getFieldPassword()
     255                        .getText());
     256                PreferencesManagement.setValue(PreferencesEnum.USE_PROXY.getLabel(), "false");
     257            }
     258            if (echecProxy) {
     259                JOptionPane.showMessageDialog(null, Messages.getMessage("proxyError"), Messages.getMessage("error"),
     260                        JOptionPane.ERROR_MESSAGE);
     261            } else {
     262                if (!Main.sessionManager.processLogin()) {
     263                    //if the login fails, an error is displayed
     264                    if (!Main.sessionManager.isErreurProxy()) {
     265                        JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"),
     266                                Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
     267                    }
    190268                } 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                         }
     269                    //          Locale.setDefault((Locale) comboLocales.getSelectedItem());
     270                    Main.showFrame();
     271                    //hides the dialog
     272                    this.dispose();
    215273                }
    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         }
     274            }
     275
     276        }
     277    }
     278
     279    @Override
     280    public void itemStateChanged(ItemEvent paramItemEvent) {
     281        for (Locale locale : SimpleDateFormat.getAvailableLocales()) {
     282            if (locale.getDisplayName(Locale.ENGLISH).equals(paramItemEvent.getItem())) {
     283                Messages.usedLocale = locale;
     284                dispose();
     285                ConnexionDialog dial = new ConnexionDialog();
     286                dial.showDialog();
     287
     288            }
     289
     290        }
     291    }
     292
     293    @Override
     294    public void keyPressed(KeyEvent arg0) {
     295    }
     296
     297    @Override
     298    public void keyReleased(KeyEvent arg0) {
     299
     300    }
     301
     302    @Override
     303    public void keyTyped(KeyEvent arg0) {
     304    }
     305
     306    @Override
     307    public void windowActivated(WindowEvent e) {
     308        // TODO Auto-generated method stub
     309
     310    }
     311
     312    @Override
     313    public void windowClosed(WindowEvent e) {
     314    }
     315
     316    @Override
     317    public void windowClosing(WindowEvent e) {
     318        System.exit(0);
     319    }
     320
     321    @Override
     322    public void windowDeactivated(WindowEvent e) {
     323        // TODO Auto-generated method stub
     324
     325    }
     326
     327    @Override
     328    public void windowDeiconified(WindowEvent e) {
     329        // TODO Auto-generated method stub
     330
     331    }
     332
     333    @Override
     334    public void windowIconified(WindowEvent e) {
     335        // TODO Auto-generated method stub
     336
     337    }
     338
     339    @Override
     340    public void windowOpened(WindowEvent e) {
     341        // TODO Auto-generated method stub
     342
     343    }
     344
     345    /**
     346     * @author mael
     347     * Internal class that represents the model of the combobox that allows
     348     * to choose a locale
     349     */
     350    class localeModel extends AbstractListModel implements ComboBoxModel {
     351
     352        Locale[] locales = SimpleDateFormat.getAvailableLocales();
     353        //List<Locale> locales = Messages.getAvailableBundles();
     354        String[] localesNames;
     355
     356        String selection = null;
     357
     358        public localeModel() {
     359            localesNames = new String[locales.length];
     360            int compteur = 0;
     361            for (Locale locale : locales) {
     362                localesNames[compteur] = locale.getDisplayName(Locale.ENGLISH);
     363                compteur++;
     364            }
     365            Arrays.sort(localesNames);
     366        }
     367
     368        public Object getElementAt(int index) {
     369            return localesNames[index];
     370        }
     371
     372        public int getSize() {
     373            return locales.length;
     374        }
     375
     376        public void setSelectedItem(Object anItem) {
     377            selection = (String) anItem;
     378        }
     379
     380        public Object getSelectedItem() {
     381            if (selection != null)
     382                return selection;
     383            else
     384                return null;
     385        }
     386    }
    342387}
Note: See TracChangeset for help on using the changeset viewer.