Ticket #682: proxy-via-preferences.patch

File proxy-via-preferences.patch, 7.8 KB (added by egore@…, 4 years ago)

Patch to add support to set proxy by using the preferences menu

  • src/org/openstreetmap/josm/gui/preferences/FilePreferences.java

     
    55 
    66import javax.swing.Box; 
    77import javax.swing.JCheckBox; 
     8import javax.swing.JSeparator; 
     9import javax.swing.SwingConstants; 
    810 
    911import org.openstreetmap.josm.Main; 
    1012import org.openstreetmap.josm.tools.GBC; 
     
    2022        private JCheckBox keepBackup = new JCheckBox(tr("Keep backup files")); 
    2123         
    2224        public void addGui(PreferenceDialog gui) { 
     25                gui.connection.add(new JSeparator(SwingConstants.HORIZONTAL), GBC.eol().fill(GBC.HORIZONTAL)); 
    2326                keepBackup.setSelected(Main.pref.getBoolean("save.keepbackup")); 
    2427                keepBackup.setToolTipText(tr("When saving, keep backup files ending with a ~")); 
    2528                gui.connection.add(keepBackup, GBC.eol().insets(20,0,0,0)); 
    26                 gui.connection.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL)); 
    2729    } 
    2830 
    2931        public void ok() { 
  • src/org/openstreetmap/josm/gui/preferences/ProxyPreferences.java

     
     1// License: GPL. For details, see LICENSE file. 
     2package org.openstreetmap.josm.gui.preferences; 
     3 
     4import static org.openstreetmap.josm.tools.I18n.tr; 
     5 
     6import java.awt.event.ActionEvent; 
     7import java.awt.event.ActionListener; 
     8 
     9import javax.swing.Box; 
     10import javax.swing.JCheckBox; 
     11import javax.swing.JLabel; 
     12import javax.swing.JPasswordField; 
     13import javax.swing.JSeparator; 
     14import javax.swing.JTextField; 
     15import javax.swing.SwingConstants; 
     16 
     17import org.openstreetmap.josm.Main; 
     18import org.openstreetmap.josm.tools.GBC; 
     19 
     20public class ProxyPreferences implements PreferenceSetting { 
     21 
     22        public static final String PROXY_ENABLE = "proxy.enable"; 
     23        public static final String PROXY_HOST = "proxy.host"; 
     24        public static final String PROXY_PORT = "proxy.port"; 
     25        public static final String PROXY_ANONYMOUS = "proxy.anonymous"; 
     26        public static final String PROXY_USER = "proxy.user"; 
     27        public static final String PROXY_PASS = "proxy.pass"; 
     28 
     29        private JCheckBox proxyEnable = new JCheckBox(tr("Enable proxy server")); 
     30        private JTextField proxyHost = new JTextField(50); 
     31        private JTextField proxyPort = new JTextField(5); 
     32        private JCheckBox proxyAnonymous = new JCheckBox(tr("Anonymous")); 
     33        private JTextField proxyUser = new JTextField(50); 
     34        private JPasswordField proxyPass = new JPasswordField(50); 
     35 
     36        @Override 
     37        public void addGui(PreferenceDialog gui) { 
     38                proxyEnable.setSelected(Main.pref.getBoolean(PROXY_ENABLE)); 
     39                proxyEnable.addActionListener(new ActionListener(){ 
     40                        public void actionPerformed(ActionEvent e) { 
     41                                proxyHost.setEnabled(proxyEnable.isSelected()); 
     42                                proxyPort.setEnabled(proxyEnable.isSelected()); 
     43                                proxyAnonymous.setEnabled(proxyEnable.isSelected()); 
     44                                proxyUser.setEnabled(proxyEnable.isSelected() && proxyAnonymous.isSelected()); 
     45                                proxyPass.setEnabled(proxyEnable.isSelected() && proxyAnonymous.isSelected()); 
     46                        } 
     47                }); 
     48                proxyHost.setEnabled(Main.pref.getBoolean(PROXY_ENABLE)); 
     49                proxyHost.setText(Main.pref.get(PROXY_HOST)); 
     50                proxyPort.setEnabled(Main.pref.getBoolean(PROXY_ENABLE)); 
     51                proxyPort.setText(Main.pref.get(PROXY_PORT)); 
     52                proxyAnonymous.setSelected(Main.pref.getBoolean(PROXY_ANONYMOUS)); 
     53                proxyAnonymous.addActionListener(new ActionListener(){ 
     54                        public void actionPerformed(ActionEvent e) { 
     55                                proxyUser.setEnabled(proxyEnable.isSelected() && proxyAnonymous.isSelected()); 
     56                                proxyPass.setEnabled(proxyEnable.isSelected() && proxyAnonymous.isSelected()); 
     57                        } 
     58                }); 
     59                proxyUser.setEnabled(Main.pref.getBoolean(PROXY_ENABLE) && (Main.pref.getBoolean(PROXY_ANONYMOUS))); 
     60                proxyUser.setText(Main.pref.get(PROXY_USER)); 
     61                proxyPass.setEnabled(Main.pref.getBoolean(PROXY_ENABLE) && (Main.pref.getBoolean(PROXY_ANONYMOUS))); 
     62                proxyPass.setText(Main.pref.get(PROXY_USER)); 
     63                 
     64                gui.connection.add(new JSeparator(SwingConstants.HORIZONTAL), GBC.eol().fill(GBC.HORIZONTAL)); 
     65                gui.connection.add(new JLabel(tr("Proxy Settings")), GBC.eol()); 
     66                gui.connection.add(proxyEnable, GBC.eol().insets(20, 0, 0, 0)); 
     67                gui.connection.add(new JLabel(tr("Proxy server host")), GBC.std()); 
     68                gui.connection.add(proxyHost, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
     69                gui.connection.add(new JLabel(tr("Proxy server port")), GBC.std()); 
     70                gui.connection.add(proxyPort, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
     71                gui.connection.add(proxyAnonymous, GBC.eop().insets(40, 0, 0, 0)); 
     72                gui.connection.add(new JLabel(tr("Proxy server username")), GBC.std()); 
     73                gui.connection.add(proxyUser, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
     74                gui.connection.add(new JLabel(tr("Proxy server password")), GBC.std()); 
     75                gui.connection.add(proxyPass, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
     76 
     77                gui.connection.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL)); 
     78        } 
     79 
     80        @Override 
     81        public void ok() { 
     82                Main.pref.put(PROXY_ENABLE, proxyEnable.isSelected()); 
     83                Main.pref.put(PROXY_HOST, proxyHost.getText()); 
     84                Main.pref.put(PROXY_PORT, proxyPort.getText()); 
     85                Main.pref.put(PROXY_ANONYMOUS, proxyAnonymous.isSelected()); 
     86                Main.pref.put(PROXY_USER, proxyUser.getText()); 
     87                Main.pref.put(PROXY_PASS, new String(proxyPass.getPassword())); 
     88        } 
     89 
     90} 
  • src/org/openstreetmap/josm/gui/preferences/PreferenceDialog.java

     
    104104                settings.add(new MapPaintPreference()); 
    105105                settings.add(new ServerAccessPreference()); 
    106106                settings.add(new FilePreferences()); 
     107                settings.add(new ProxyPreferences()); 
    107108                settings.add(new ProjectionPreference()); 
    108109                settings.add(new TaggingPresetPreference()); 
    109110                settings.add(new PluginPreference()); 
  • src/org/openstreetmap/josm/data/Preferences.java

     
    1212import java.util.Collection; 
    1313import java.util.LinkedList; 
    1414import java.util.Map; 
     15import java.util.Properties; 
    1516import java.util.SortedMap; 
    1617import java.util.StringTokenizer; 
    1718import java.util.TreeMap; 
    1819import java.util.Map.Entry; 
    1920 
    2021import org.openstreetmap.josm.Main; 
     22import org.openstreetmap.josm.gui.preferences.ProxyPreferences; 
    2123import org.openstreetmap.josm.tools.ColorHelper; 
    2224 
    2325/** 
     
    207209         */ 
    208210        public void save() { 
    209211                try { 
     212                        setSystemProperties(); 
    210213                        final PrintWriter out = new PrintWriter(new FileWriter(getPreferencesDir() + "preferences"), false); 
    211214                        for (final Entry<String, String> e : properties.entrySet()) { 
    212215                                if (!e.getValue().equals("")) 
     
    236239                if (!errLines.isEmpty()) { 
    237240                        throw new IOException("Malformed config file at lines " + errLines); 
    238241                } 
     242                setSystemProperties(); 
    239243        } 
    240244 
    241245        public final void resetToDefault() { 
     
    372376                } 
    373377                return def; 
    374378        } 
     379 
     380        private void setSystemProperties() { 
     381                Properties sysProp = System.getProperties(); 
     382                if (getBoolean(ProxyPreferences.PROXY_ENABLE)) { 
     383                        sysProp.put("proxySet", "true"); 
     384                        sysProp.put("http.proxyHost", get(ProxyPreferences.PROXY_HOST)); 
     385                        sysProp.put("proxyPort", get(ProxyPreferences.PROXY_PORT)); 
     386                        if (!getBoolean(ProxyPreferences.PROXY_ANONYMOUS)) { 
     387                                sysProp.put("proxyUser", get(ProxyPreferences.PROXY_USER)); 
     388                                sysProp.put("proxyPassword", get(ProxyPreferences.PROXY_PASS)); 
     389                        } 
     390                        System.setProperties(sysProp); 
     391                } 
     392        } 
    375393}