Ticket #682: proxy-via-preferences.3.patch

File proxy-via-preferences.3.patch, 8.0 KB (added by egore@…, 17 years ago)

Patch without java 6 stuff

  • 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        public void addGui(PreferenceDialog gui) {
     37                proxyEnable.setSelected(Main.pref.getBoolean(PROXY_ENABLE));
     38                proxyEnable.addActionListener(new ActionListener(){
     39                        public void actionPerformed(ActionEvent e) {
     40                                proxyHost.setEnabled(proxyEnable.isSelected());
     41                                proxyPort.setEnabled(proxyEnable.isSelected());
     42                                proxyAnonymous.setEnabled(proxyEnable.isSelected());
     43                                proxyUser.setEnabled(proxyEnable.isSelected() && proxyAnonymous.isSelected());
     44                                proxyPass.setEnabled(proxyEnable.isSelected() && proxyAnonymous.isSelected());
     45                        }
     46                });
     47                proxyHost.setEnabled(Main.pref.getBoolean(PROXY_ENABLE));
     48                proxyHost.setText(Main.pref.get(PROXY_HOST));
     49                proxyPort.setEnabled(Main.pref.getBoolean(PROXY_ENABLE));
     50                proxyPort.setText(Main.pref.get(PROXY_PORT));
     51                proxyAnonymous.setSelected(Main.pref.getBoolean(PROXY_ANONYMOUS));
     52                proxyAnonymous.addActionListener(new ActionListener(){
     53                        public void actionPerformed(ActionEvent e) {
     54                                proxyUser.setEnabled(proxyEnable.isSelected() && proxyAnonymous.isSelected());
     55                                proxyPass.setEnabled(proxyEnable.isSelected() && proxyAnonymous.isSelected());
     56                        }
     57                });
     58                proxyUser.setEnabled(Main.pref.getBoolean(PROXY_ENABLE) && (Main.pref.getBoolean(PROXY_ANONYMOUS)));
     59                proxyUser.setText(Main.pref.get(PROXY_USER));
     60                proxyPass.setEnabled(Main.pref.getBoolean(PROXY_ENABLE) && (Main.pref.getBoolean(PROXY_ANONYMOUS)));
     61                proxyPass.setText(Main.pref.get(PROXY_USER));
     62               
     63                gui.connection.add(new JSeparator(SwingConstants.HORIZONTAL), GBC.eol().fill(GBC.HORIZONTAL));
     64                gui.connection.add(new JLabel(tr("Proxy Settings")), GBC.eol());
     65                gui.connection.add(proxyEnable, GBC.eol().insets(20, 0, 0, 0));
     66                gui.connection.add(new JLabel(tr("Proxy server host")), GBC.std());
     67                gui.connection.add(proxyHost, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
     68                gui.connection.add(new JLabel(tr("Proxy server port")), GBC.std());
     69                gui.connection.add(proxyPort, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
     70                gui.connection.add(proxyAnonymous, GBC.eop().insets(40, 0, 0, 0));
     71                gui.connection.add(new JLabel(tr("Proxy server username")), GBC.std());
     72                gui.connection.add(proxyUser, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
     73                gui.connection.add(new JLabel(tr("Proxy server password")), GBC.std());
     74                gui.connection.add(proxyPass, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
     75
     76                gui.connection.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
     77        }
     78
     79        public void ok() {
     80                Main.pref.put(PROXY_ENABLE, proxyEnable.isSelected());
     81                Main.pref.put(PROXY_HOST, proxyHost.getText());
     82                Main.pref.put(PROXY_PORT, proxyPort.getText());
     83                Main.pref.put(PROXY_ANONYMOUS, proxyAnonymous.isSelected());
     84                Main.pref.put(PROXY_USER, proxyUser.getText());
     85                Main.pref.put(PROXY_PASS, new String(proxyPass.getPassword()));
     86        }
     87
     88}
  • 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/**
     
    212214         */
    213215        public void save() {
    214216                try {
     217                        setSystemProperties();
    215218                        final PrintWriter out = new PrintWriter(new FileWriter(getPreferencesDir() + "preferences"), false);
    216219                        for (final Entry<String, String> e : properties.entrySet()) {
    217220                                out.println(e.getKey() + "=" + e.getValue());
     
    240243                if (!errLines.isEmpty()) {
    241244                        throw new IOException("Malformed config file at lines " + errLines);
    242245                }
     246                setSystemProperties();
    243247        }
    244248
    245249        public final void resetToDefault() {
     
    376380                }
    377381                return def;
    378382        }
     383
     384        private void setSystemProperties() {
     385                Properties sysProp = System.getProperties();
     386                if (getBoolean(ProxyPreferences.PROXY_ENABLE)) {
     387                        sysProp.put("proxySet", "true");
     388                        sysProp.put("http.proxyHost", get(ProxyPreferences.PROXY_HOST));
     389                        sysProp.put("proxyPort", get(ProxyPreferences.PROXY_PORT));
     390                        if (!getBoolean(ProxyPreferences.PROXY_ANONYMOUS)) {
     391                                sysProp.put("proxyUser", get(ProxyPreferences.PROXY_USER));
     392                                sysProp.put("proxyPassword", get(ProxyPreferences.PROXY_PASS));
     393                        }
     394                } else {
     395                        sysProp.put("proxySet", "false");
     396                        sysProp.remove("http.proxyHost");
     397                        sysProp.remove("proxyPort");
     398                        sysProp.remove("proxyUser");
     399                        sysProp.remove("proxyPassword");
     400                }
     401                System.setProperties(sysProp);
     402        }
    379403}