Changeset 8830


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

Adds proxy support for the connection.

Location:
extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse/enumeration/PreferencesEnum.java

    r6821 r8830  
    3636
    3737    CHUNK_SIZE("chunk_size"), WIDTH_ORIGINALE("width_original"), HEIGHT_ORIGINAL("height_original"), PASSWORD(
    38             "password"), LOGIN("login"), URL_SITE("url");
     38            "password"), LOGIN("login"), URL_SITE("url"), USE_PROXY("use_proxy"), URL_PROXY("url_proxy"), PORT_PROXY(
     39            "port_proxy"), USER_PROXY("user_proxy"), PASS_PROXY("password_proxy");
    3940
    4041    protected String label;
  • extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse/session/SessionManager.java

    r7219 r8830  
    44import java.io.InputStream;
    55import java.io.UnsupportedEncodingException;
     6
    67import javax.swing.JOptionPane;
     8
     9import org.apache.commons.httpclient.Credentials;
     10import org.apache.commons.httpclient.HostConfiguration;
    711import org.apache.commons.httpclient.HttpClient;
    812import org.apache.commons.httpclient.HttpException;
     13import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
     14import org.apache.commons.httpclient.UsernamePasswordCredentials;
     15import org.apache.commons.httpclient.auth.AuthScope;
    916import org.apache.commons.httpclient.methods.PostMethod;
     17import org.apache.commons.lang.StringUtils;
    1018import org.jdom.Document;
    1119import org.jdom.JDOMException;
     20
    1221import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
    1322import fr.mael.jiwigo.transverse.util.Messages;
    1423import fr.mael.jiwigo.transverse.util.Outil;
    15 
    1624
    1725/**
     
    4654public class SessionManager {
    4755
    48         /**
    49          * Logger
    50          */
    51         public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(SessionManager.class);
    52         /**
    53          * the entered login
    54          */
    55         private String login;
    56         /**
    57          * the entered password
    58          */
    59         private String motDePasse;
    60         /**
    61          * the url of the site
    62          */
    63         private String url;
    64         /**
    65          * the http client
    66          */
    67         private HttpClient client;
    68 
    69 
    70         /**
    71          * Constructor
    72          * @param login the login
    73          * @param motDePasse the password
    74          * @param url the url of the site
    75          */
    76         public SessionManager(String login, String motDePasse, String url) {
    77                 this.login = login;
    78                 this.motDePasse = motDePasse;
    79                 this.url = url + "/ws.php";
    80                 client = new HttpClient();
    81                 //Using of a Linux user agent. cause... it's better 8)
    82                 client.getParams().setParameter("http.useragent",
    83                                 "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1");
    84         }
    85 
    86 
    87         /**
    88          * Connection method
    89          * @return true if successful
    90          */
    91         public boolean processLogin() {
    92                 Document doc;
    93                 try {
    94                         doc = executerReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", motDePasse);
    95                         return Outil.checkOk(doc);
    96                 } catch (Exception e) {
    97                         LOG.error(Outil.getStackTrace(e));
    98                 }
    99                 return false;
    100 
    101         }
    102 
    103 
    104         /**
    105          * Executes a method on the webservice and returns the result as a string
    106          * @param methode the method to execute
    107          * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value
    108          * @return the result
    109          * @throws UnsupportedEncodingException
    110          */
    111         public String executerReturnString(String methode, String... parametres) throws UnsupportedEncodingException {
    112                 if (parametres.length % 2 != 0 && !(parametres == null)) {
    113                         try {
    114                                 throw new Exception("Le nombre de parametres doit etre pair");
    115                         } catch (Exception e) {
    116                                 LOG.error(Outil.getStackTrace(e));
    117                                 return null;
    118                         }
    119                 }
    120                 PostMethod method = new PostMethod(url);
    121                 method.addParameter("method", methode);
    122                 for (int i = 0; i < parametres.length; i += 2) {
    123                         method.addParameter(parametres[i], parametres[i + 1]);
    124 
    125                 }
    126                 //begin bug:0001833
    127                 method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    128                 //end
    129 
    130                 try {
    131                         client.executeMethod(method);
    132                         InputStream streamResponse = method.getResponseBodyAsStream();
    133                         //          System.out.println(Outil.readInputStreamAsString(streamResponse));
    134                         //          String toReturn = method.getResponseBodyAsString();
    135                         String toReturn = Outil.readInputStreamAsString(streamResponse);
    136                         LOG.debug(toReturn);
    137                         return toReturn;
    138                 } catch (HttpException e) {
    139                         // TODO Auto-generated catch block
    140                         LOG.error(Outil.getStackTrace(e));
    141                 } catch (IOException e) {
    142                         // TODO Auto-generated catch block
    143                         LOG.error(Outil.getStackTrace(e));
    144                 } catch (IllegalArgumentException e) {
    145                         LOG.error(Outil.getStackTrace(e));
    146                         JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"),
    147                                         Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
    148                 } finally {
    149                         method.releaseConnection();
    150                 }
     56    /**
     57     * Logger
     58     */
     59    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
     60            .getLog(SessionManager.class);
     61    /**
     62     * the entered login
     63     */
     64    private String login;
     65    /**
     66     * the entered password
     67     */
     68    private String motDePasse;
     69    /**
     70     * the url of the site
     71     */
     72    private String url;
     73    /**
     74     * the http client
     75     */
     76    private HttpClient client;
     77
     78    /**
     79     * defines if the user uses a proxy
     80     */
     81    private boolean usesProxy;
     82
     83    /**
     84     * url of the proxy
     85     */
     86    private String urlProxy;
     87
     88    /**
     89     * port of the proxy
     90     */
     91    private int portProxy;
     92
     93    /**
     94     * login for the proxy
     95     */
     96    private String loginProxy;
     97
     98    /**
     99     * pass for the proxy
     100     */
     101    private String passProxy;
     102
     103    /**
     104     * true : an error was found for the proxy
     105     */
     106    private boolean erreurProxy;
     107
     108    /**
     109     * Constructor
     110     * @param login the login
     111     * @param motDePasse the password
     112     * @param url the url of the site
     113     */
     114    public SessionManager(String login, String motDePasse, String url) {
     115        this.login = login;
     116        this.motDePasse = motDePasse;
     117        this.url = url + "/ws.php";
     118        MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
     119        client = new HttpClient(connectionManager);
     120        //Using of a Linux user agent. cause... it's better 8)
     121        client.getParams().setParameter("http.useragent",
     122                "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1");
     123
     124    }
     125
     126    /**
     127     * Connection method
     128     * @return true if successful
     129     */
     130    public boolean processLogin() {
     131        Document doc;
     132        //configures the proxy
     133        if (usesProxy) {
     134            HostConfiguration config = client.getHostConfiguration();
     135            config.setProxy(urlProxy, portProxy);
     136            if (!StringUtils.isEmpty(loginProxy) && !StringUtils.isEmpty(passProxy)) {
     137                Credentials credentials = new UsernamePasswordCredentials(loginProxy, passProxy);
     138                AuthScope authScope = new AuthScope(urlProxy, portProxy);
     139                client.getState().setProxyCredentials(authScope, credentials);
     140            }
     141        }
     142        try {
     143            doc = executerReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", motDePasse);
     144            return Outil.checkOk(doc);
     145        } catch (Exception e) {
     146            LOG.error(Outil.getStackTrace(e));
     147        }
     148        return false;
     149    }
     150
     151    /**
     152     * Executes a method on the webservice and returns the result as a string
     153     * @param methode the method to execute
     154     * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value
     155     * @return the result
     156     * @throws UnsupportedEncodingException
     157     */
     158    public String executerReturnString(String methode, String... parametres) throws UnsupportedEncodingException {
     159        if (parametres.length % 2 != 0 && !(parametres == null)) {
     160            try {
     161                throw new Exception("Le nombre de parametres doit etre pair");
     162            } catch (Exception e) {
     163                LOG.error(Outil.getStackTrace(e));
    151164                return null;
    152 
    153         }
    154 
    155 
    156         /**
    157          * Executes a method on the webservice and returns the result as a Dom document
    158          * @param methode the method to execute
    159          * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value
    160          * @return the result
    161          * @throws IOException
    162          */
    163         public Document executerReturnDocument(String methode, String... parametres) throws IOException {
    164                 try {
    165                         return Outil.stringToDocument(executerReturnString(methode, parametres));
    166                 } catch (JDOMException e) {
    167                         // TODO Auto-generated catch block
    168                         LOG.error(Outil.getStackTrace(e));
    169                 }
    170                 return null;
    171 
    172         }
    173 
    174 
    175         /**
    176          * Executes a method on the webservice and returns the result as a Dom document
    177          * @param methode the method to execute
    178          * @return the result
    179          */
    180         public Document executerReturnDocument(String methode) {
    181                 try {
    182                         return Outil.stringToDocument(executerReturnString(methode));
    183                 } catch (JDOMException e) {
    184                         // TODO Auto-generated catch block
    185                         LOG.error(Outil.getStackTrace(e));
    186                 } catch (IOException e) {
    187                         // TODO Auto-generated catch block
    188                         LOG.error(Outil.getStackTrace(e));
    189                 }
    190                 return null;
    191 
    192         }
    193 
    194 
    195         /**
    196          * @return the login
    197          */
    198         public String getLogin() {
    199                 return login;
    200         }
    201 
    202 
    203         /**
    204          * @param login the login to set
    205          */
    206         public void setLogin(String login) {
    207                 this.login = login;
    208         }
    209 
    210 
    211         /**
    212          * @return the motDePasse
    213          */
    214         public String getMotDePasse() {
    215                 return motDePasse;
    216         }
    217 
    218 
    219         /**
    220          * @param motDePasse the motDePasse to set
    221          */
    222         public void setMotDePasse(String motDePasse) {
    223                 this.motDePasse = motDePasse;
    224         }
    225 
    226 
    227         /**
    228          * @return the url
    229          */
    230         public String getUrl() {
    231                 return url;
    232         }
    233 
    234 
    235         /**
    236          * @param url the url to set
    237          */
    238         public void setUrl(String url) {
    239                 this.url = url;
    240         }
     165            }
     166        }
     167        PostMethod method = new PostMethod(url);
     168        method.addParameter("method", methode);
     169        for (int i = 0; i < parametres.length; i += 2) {
     170            method.addParameter(parametres[i], parametres[i + 1]);
     171
     172        }
     173        //begin bug:0001833
     174        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
     175        //end
     176
     177        try {
     178            client.executeMethod(method);
     179            InputStream streamResponse = method.getResponseBodyAsStream();
     180            //      System.out.println(Outil.readInputStreamAsString(streamResponse));
     181            //      String toReturn = method.getResponseBodyAsString();
     182            String toReturn = Outil.readInputStreamAsString(streamResponse);
     183            LOG.debug(toReturn);
     184            return toReturn;
     185        } catch (HttpException e) {
     186            // TODO Auto-generated catch block
     187            LOG.error(Outil.getStackTrace(e));
     188        } catch (IOException e) {
     189            // TODO Auto-generated catch block
     190            LOG.error(Outil.getStackTrace(e));
     191        } catch (IllegalArgumentException e) {
     192            LOG.error(Outil.getStackTrace(e));
     193            JOptionPane.showMessageDialog(null, Messages.getMessage("connexionDialog_connexionError"), Messages
     194                    .getMessage("error"), JOptionPane.ERROR_MESSAGE);
     195        } finally {
     196            method.releaseConnection();
     197        }
     198        return null;
     199
     200    }
     201
     202    /**
     203     * Executes a method on the webservice and returns the result as a Dom document
     204     * @param methode the method to execute
     205     * @param parametres the parameters of the method. Must be even : the name of the parameter followed by its value
     206     * @return the result
     207     * @throws IOException
     208     */
     209    public Document executerReturnDocument(String methode, String... parametres) throws IOException {
     210        try {
     211            return Outil.stringToDocument(executerReturnString(methode, parametres));
     212        } catch (JDOMException e) {
     213            // TODO Auto-generated catch block
     214            LOG.error(Outil.getStackTrace(e));
     215        }
     216        return null;
     217
     218    }
     219
     220    /**
     221     * Executes a method on the webservice and returns the result as a Dom document
     222     * @param methode the method to execute
     223     * @return the result
     224     */
     225    public Document executerReturnDocument(String methode) {
     226        try {
     227            return Outil.stringToDocument(executerReturnString(methode));
     228        } catch (JDOMException e) {
     229            // TODO Auto-generated catch block
     230            LOG.error(Outil.getStackTrace(e));
     231        } catch (IOException e) {
     232            // TODO Auto-generated catch block
     233            LOG.error(Outil.getStackTrace(e));
     234        }
     235        return null;
     236
     237    }
     238
     239    /**
     240     * @return the login
     241     */
     242    public String getLogin() {
     243        return login;
     244    }
     245
     246    /**
     247     * @param login the login to set
     248     */
     249    public void setLogin(String login) {
     250        this.login = login;
     251    }
     252
     253    /**
     254     * @return the motDePasse
     255     */
     256    public String getMotDePasse() {
     257        return motDePasse;
     258    }
     259
     260    /**
     261     * @param motDePasse the motDePasse to set
     262     */
     263    public void setMotDePasse(String motDePasse) {
     264        this.motDePasse = motDePasse;
     265    }
     266
     267    /**
     268     * @return the url
     269     */
     270    public String getUrl() {
     271        return url;
     272    }
     273
     274    /**
     275     * @param url the url to set
     276     */
     277    public void setUrl(String url) {
     278        this.url = url;
     279    }
     280
     281    /**
     282     * @param usesProxy the usesProxy to set
     283     */
     284    public void setUsesProxy(boolean usesProxy) {
     285        this.usesProxy = usesProxy;
     286    }
     287
     288    /**
     289     * @param urlProxy the urlProxy to set
     290     */
     291    public void setUrlProxy(String urlProxy) {
     292        this.urlProxy = urlProxy;
     293    }
     294
     295    /**
     296     * @param portProxy the portProxy to set
     297     */
     298    public void setPortProxy(int portProxy) {
     299        this.portProxy = portProxy;
     300    }
     301
     302    /**
     303     * @param loginProxy the loginProxy to set
     304     */
     305    public void setLoginProxy(String loginProxy) {
     306        this.loginProxy = loginProxy;
     307    }
     308
     309    /**
     310     * @param passProxy the passProxy to set
     311     */
     312    public void setPassProxy(String passProxy) {
     313        this.passProxy = passProxy;
     314    }
     315
     316    /**
     317     * @return the erreurProxy
     318     */
     319    public boolean isErreurProxy() {
     320        return erreurProxy;
     321    }
    241322
    242323}
  • extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/transverse/util/preferences/PreferencesManagement.java

    r6980 r8830  
    66
    77import org.jdom.Document;
     8import org.jdom.Element;
    89import org.jdom.input.SAXBuilder;
     10import org.jdom.output.XMLOutputter;
    911
    1012import fr.mael.jiwigo.transverse.util.Outil;
     
    7375            xml.append("        <login></login>\n");
    7476            xml.append("        <url></url>\n");
     77            xml.append("        <use_proxy>false</use_proxy>\n");
     78            xml.append("        <url_proxy></url_proxy>\n");
     79            xml.append("        <port_proxy></port_proxy>\n");
     80            xml.append("        <user_proxy></user_proxy>\n");
     81            xml.append("        <password_proxy></password_proxy>\n");
    7582            xml.append("</config>");
    76 
    7783            FileWriter out = new FileWriter(outFile);
    7884            out.write(xml.toString());
     
    106112        try {
    107113            Document document = sxb.build(new File(getConfigFilePath()));
    108             document.getRootElement().getChild(key).setText(text);
    109             FileWriter out = new FileWriter(getConfigFilePath());
    110             out.write(Outil.documentToString(document));
    111             out.close();
     114            try {
     115                document.getRootElement().getChild(key).setText(text);
     116            } catch (NullPointerException e) {
     117                Element element = new Element(key);
     118                element.addContent(text);
     119                document.getRootElement().addContent(element);
     120            }
     121            XMLOutputter out = new XMLOutputter();
     122            FileWriter writer = new FileWriter(getConfigFilePath());
     123            out.output(document, writer);
     124            writer.flush();
     125            writer.close();
    112126        } catch (Exception e) {
    113127            LOG.error(Outil.getStackTrace(e));
  • 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.