| 1 | // License: GPL. For details, see LICENSE file. |
| 2 | package org.openstreetmap.josm.gui.preferences; |
| 3 | |
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| 5 | |
| 6 | import java.awt.event.ActionEvent; |
| 7 | import java.awt.event.ActionListener; |
| 8 | |
| 9 | import javax.swing.Box; |
| 10 | import javax.swing.JCheckBox; |
| 11 | import javax.swing.JLabel; |
| 12 | import javax.swing.JPasswordField; |
| 13 | import javax.swing.JSeparator; |
| 14 | import javax.swing.JTextField; |
| 15 | import javax.swing.SwingConstants; |
| 16 | |
| 17 | import org.openstreetmap.josm.Main; |
| 18 | import org.openstreetmap.josm.tools.GBC; |
| 19 | |
| 20 | public 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 | } |