[2801] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.preferences.server;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
| 6 |
|
---|
| 7 | import java.awt.Component;
|
---|
| 8 | import java.awt.Dimension;
|
---|
| 9 | import java.awt.GridBagConstraints;
|
---|
| 10 | import java.awt.GridBagLayout;
|
---|
| 11 | import java.awt.Insets;
|
---|
| 12 | import java.awt.event.ItemEvent;
|
---|
| 13 | import java.awt.event.ItemListener;
|
---|
[5886] | 14 | import java.net.Authenticator.RequestorType;
|
---|
[2801] | 15 | import java.net.PasswordAuthentication;
|
---|
| 16 | import java.net.ProxySelector;
|
---|
| 17 | import java.util.HashMap;
|
---|
| 18 | import java.util.Map;
|
---|
| 19 |
|
---|
| 20 | import javax.swing.BorderFactory;
|
---|
| 21 | import javax.swing.ButtonGroup;
|
---|
| 22 | import javax.swing.JLabel;
|
---|
| 23 | import javax.swing.JPanel;
|
---|
| 24 | import javax.swing.JRadioButton;
|
---|
| 25 |
|
---|
| 26 | import org.openstreetmap.josm.Main;
|
---|
| 27 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
[6340] | 28 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
[5752] | 29 | import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
|
---|
[5886] | 30 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
[2801] | 31 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
---|
| 32 | import org.openstreetmap.josm.io.DefaultProxySelector;
|
---|
[4245] | 33 | import org.openstreetmap.josm.io.auth.CredentialsAgent;
|
---|
| 34 | import org.openstreetmap.josm.io.auth.CredentialsAgentException;
|
---|
[2801] | 35 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
| 36 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 37 |
|
---|
[6529] | 38 | /**
|
---|
| 39 | * Component allowing input of proxy settings.
|
---|
| 40 | */
|
---|
[2801] | 41 | public class ProxyPreferencesPanel extends VerticallyScrollablePanel {
|
---|
| 42 |
|
---|
[6529] | 43 | /**
|
---|
| 44 | * The proxy policy is how JOSM will use proxy information.
|
---|
| 45 | */
|
---|
[2801] | 46 | public enum ProxyPolicy {
|
---|
[6529] | 47 | /** No proxy: JOSM will access Internet resources directly */
|
---|
[2801] | 48 | NO_PROXY("no-proxy"),
|
---|
[6529] | 49 | /** Use system settings: JOSM will use system proxy settings */
|
---|
[2801] | 50 | USE_SYSTEM_SETTINGS("use-system-settings"),
|
---|
[6529] | 51 | /** Use HTTP proxy: JOSM will use the given HTTP proxy, configured manually */
|
---|
[2801] | 52 | USE_HTTP_PROXY("use-http-proxy"),
|
---|
[6529] | 53 | /** Use HTTP proxy: JOSM will use the given SOCKS proxy */
|
---|
[2801] | 54 | USE_SOCKS_PROXY("use-socks-proxy");
|
---|
| 55 |
|
---|
| 56 | private String policyName;
|
---|
| 57 | ProxyPolicy(String policyName) {
|
---|
| 58 | this.policyName = policyName;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[6529] | 61 | /**
|
---|
| 62 | * Replies the policy name, to be stored in proxy preferences.
|
---|
| 63 | * @return the policy unique name
|
---|
| 64 | */
|
---|
[2801] | 65 | public String getName() {
|
---|
| 66 | return policyName;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[6529] | 69 | /**
|
---|
| 70 | * Retrieves a proxy policy from its name found in preferences.
|
---|
| 71 | * @param policyName The policy name
|
---|
| 72 | * @return The proxy policy matching the given name, or {@code null}
|
---|
| 73 | */
|
---|
[6883] | 74 | public static ProxyPolicy fromName(String policyName) {
|
---|
[2801] | 75 | if (policyName == null) return null;
|
---|
| 76 | policyName = policyName.trim().toLowerCase();
|
---|
| 77 | for(ProxyPolicy pp: values()) {
|
---|
| 78 | if (pp.getName().equals(policyName))
|
---|
| 79 | return pp;
|
---|
| 80 | }
|
---|
| 81 | return null;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[6529] | 85 | /** Property key for proxy policy */
|
---|
[2801] | 86 | public static final String PROXY_POLICY = "proxy.policy";
|
---|
[6529] | 87 | /** Property key for HTTP proxy host */
|
---|
[2801] | 88 | public static final String PROXY_HTTP_HOST = "proxy.http.host";
|
---|
[6529] | 89 | /** Property key for HTTP proxy port */
|
---|
[2801] | 90 | public static final String PROXY_HTTP_PORT = "proxy.http.port";
|
---|
[6529] | 91 | /** Property key for SOCKS proxy host */
|
---|
[2801] | 92 | public static final String PROXY_SOCKS_HOST = "proxy.socks.host";
|
---|
[6529] | 93 | /** Property key for SOCKS proxy port */
|
---|
[2801] | 94 | public static final String PROXY_SOCKS_PORT = "proxy.socks.port";
|
---|
[6529] | 95 | /** Property key for proxy username */
|
---|
[2801] | 96 | public static final String PROXY_USER = "proxy.user";
|
---|
[6529] | 97 | /** Property key for proxy password */
|
---|
[2801] | 98 | public static final String PROXY_PASS = "proxy.pass";
|
---|
| 99 |
|
---|
| 100 | private Map<ProxyPolicy, JRadioButton> rbProxyPolicy;
|
---|
[5886] | 101 | private JosmTextField tfProxyHttpHost;
|
---|
| 102 | private JosmTextField tfProxyHttpPort;
|
---|
| 103 | private JosmTextField tfProxySocksHost;
|
---|
| 104 | private JosmTextField tfProxySocksPort;
|
---|
| 105 | private JosmTextField tfProxyHttpUser;
|
---|
| 106 | private JosmPasswordField tfProxyHttpPassword;
|
---|
[2801] | 107 |
|
---|
| 108 | private JPanel pnlHttpProxyConfigurationPanel;
|
---|
| 109 | private JPanel pnlSocksProxyConfigurationPanel;
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Builds the panel for the HTTP proxy configuration
|
---|
[3530] | 113 | *
|
---|
[5899] | 114 | * @return panel with HTTP proxy configuration
|
---|
[2801] | 115 | */
|
---|
[6890] | 116 | protected final JPanel buildHttpProxyConfigurationPanel() {
|
---|
[2801] | 117 | JPanel pnl = new JPanel(new GridBagLayout()) {
|
---|
| 118 | @Override
|
---|
| 119 | public Dimension getMinimumSize() {
|
---|
| 120 | return getPreferredSize();
|
---|
| 121 | }
|
---|
| 122 | };
|
---|
| 123 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 124 |
|
---|
| 125 | gc.anchor = GridBagConstraints.WEST;
|
---|
| 126 | gc.insets = new Insets(5,5,0,0);
|
---|
[2823] | 127 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 128 | gc.weightx = 0.0;
|
---|
[2801] | 129 | pnl.add(new JLabel(tr("Host:")), gc);
|
---|
| 130 |
|
---|
| 131 | gc.gridx = 1;
|
---|
[2823] | 132 | gc.weightx = 1.0;
|
---|
[5886] | 133 | pnl.add(tfProxyHttpHost = new JosmTextField(),gc);
|
---|
[2801] | 134 |
|
---|
| 135 | gc.gridy = 1;
|
---|
| 136 | gc.gridx = 0;
|
---|
[2823] | 137 | gc.fill = GridBagConstraints.NONE;
|
---|
| 138 | gc.weightx = 0.0;
|
---|
[2801] | 139 | pnl.add(new JLabel(trc("server", "Port:")), gc);
|
---|
| 140 |
|
---|
| 141 | gc.gridx = 1;
|
---|
[2823] | 142 | gc.weightx = 1.0;
|
---|
[5886] | 143 | pnl.add(tfProxyHttpPort = new JosmTextField(5),gc);
|
---|
[2823] | 144 | tfProxyHttpPort.setMinimumSize(tfProxyHttpPort.getPreferredSize());
|
---|
[2801] | 145 |
|
---|
| 146 | gc.gridy = 2;
|
---|
| 147 | gc.gridx = 0;
|
---|
| 148 | gc.gridwidth = 2;
|
---|
[2823] | 149 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
[2801] | 150 | gc.weightx = 1.0;
|
---|
[2823] | 151 | pnl.add(new JMultilineLabel(tr("Please enter a username and a password if your proxy requires authentication.")), gc);
|
---|
[2801] | 152 |
|
---|
| 153 | gc.gridy = 3;
|
---|
| 154 | gc.gridx = 0;
|
---|
| 155 | gc.gridwidth = 1;
|
---|
[2823] | 156 | gc.fill = GridBagConstraints.NONE;
|
---|
[2801] | 157 | gc.weightx = 0.0;
|
---|
| 158 | pnl.add(new JLabel(tr("User:")), gc);
|
---|
| 159 |
|
---|
| 160 | gc.gridy = 3;
|
---|
| 161 | gc.gridx = 1;
|
---|
[2823] | 162 | gc.weightx = 1.0;
|
---|
[5886] | 163 | pnl.add(tfProxyHttpUser = new JosmTextField(20),gc);
|
---|
[2823] | 164 | tfProxyHttpUser.setMinimumSize(tfProxyHttpUser.getPreferredSize());
|
---|
[2801] | 165 |
|
---|
| 166 | gc.gridy = 4;
|
---|
| 167 | gc.gridx = 0;
|
---|
[2823] | 168 | gc.weightx = 0.0;
|
---|
[2801] | 169 | pnl.add(new JLabel(tr("Password:")), gc);
|
---|
| 170 |
|
---|
| 171 | gc.gridx = 1;
|
---|
[2823] | 172 | gc.weightx = 1.0;
|
---|
[5752] | 173 | pnl.add(tfProxyHttpPassword = new JosmPasswordField(20),gc);
|
---|
[2823] | 174 | tfProxyHttpPassword.setMinimumSize(tfProxyHttpPassword.getPreferredSize());
|
---|
| 175 |
|
---|
| 176 | // add an extra spacer, otherwise the layout is broken
|
---|
| 177 | gc.gridy = 5;
|
---|
| 178 | gc.gridx = 0;
|
---|
| 179 | gc.gridwidth = 2;
|
---|
| 180 | gc.fill = GridBagConstraints.BOTH;
|
---|
| 181 | gc.weightx = 1.0;
|
---|
| 182 | gc.weighty = 1.0;
|
---|
| 183 | pnl.add(new JPanel(), gc);
|
---|
[2801] | 184 | return pnl;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
| 188 | * Builds the panel for the SOCKS proxy configuration
|
---|
[3530] | 189 | *
|
---|
[5899] | 190 | * @return panel with SOCKS proxy configuration
|
---|
[2801] | 191 | */
|
---|
[6890] | 192 | protected final JPanel buildSocksProxyConfigurationPanel() {
|
---|
[2801] | 193 | JPanel pnl = new JPanel(new GridBagLayout()) {
|
---|
| 194 | @Override
|
---|
| 195 | public Dimension getMinimumSize() {
|
---|
| 196 | return getPreferredSize();
|
---|
| 197 | }
|
---|
| 198 | };
|
---|
| 199 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 200 | gc.anchor = GridBagConstraints.WEST;
|
---|
| 201 | gc.insets = new Insets(5,5,0,0);
|
---|
[2823] | 202 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 203 | gc.weightx = 0.0;
|
---|
[2801] | 204 | pnl.add(new JLabel(tr("Host:")), gc);
|
---|
| 205 |
|
---|
| 206 | gc.gridx = 1;
|
---|
[2823] | 207 | gc.weightx = 1.0;
|
---|
[5886] | 208 | pnl.add(tfProxySocksHost = new JosmTextField(20),gc);
|
---|
[2801] | 209 |
|
---|
| 210 | gc.gridy = 1;
|
---|
| 211 | gc.gridx = 0;
|
---|
[2823] | 212 | gc.weightx = 0.0;
|
---|
| 213 | gc.fill = GridBagConstraints.NONE;
|
---|
[2801] | 214 | pnl.add(new JLabel(trc("server", "Port:")), gc);
|
---|
| 215 |
|
---|
| 216 | gc.gridx = 1;
|
---|
[2823] | 217 | gc.weightx = 1.0;
|
---|
[5886] | 218 | pnl.add(tfProxySocksPort = new JosmTextField(5), gc);
|
---|
[2823] | 219 | tfProxySocksPort.setMinimumSize(tfProxySocksPort.getPreferredSize());
|
---|
[2801] | 220 |
|
---|
| 221 | // add an extra spacer, otherwise the layout is broken
|
---|
| 222 | gc.gridy = 2;
|
---|
| 223 | gc.gridx = 0;
|
---|
| 224 | gc.gridwidth = 2;
|
---|
| 225 | gc.fill = GridBagConstraints.BOTH;
|
---|
| 226 | gc.weightx = 1.0;
|
---|
| 227 | gc.weighty = 1.0;
|
---|
| 228 | pnl.add(new JPanel(), gc);
|
---|
| 229 | return pnl;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[6890] | 232 | protected final JPanel buildProxySettingsPanel() {
|
---|
[2801] | 233 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
| 234 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 235 |
|
---|
[6267] | 236 | ButtonGroup bgProxyPolicy = new ButtonGroup();
|
---|
[2801] | 237 | rbProxyPolicy = new HashMap<ProxyPolicy, JRadioButton>();
|
---|
| 238 | ProxyPolicyChangeListener policyChangeListener = new ProxyPolicyChangeListener();
|
---|
| 239 | for (ProxyPolicy pp: ProxyPolicy.values()) {
|
---|
| 240 | rbProxyPolicy.put(pp, new JRadioButton());
|
---|
| 241 | bgProxyPolicy.add(rbProxyPolicy.get(pp));
|
---|
| 242 | rbProxyPolicy.get(pp).addItemListener(policyChangeListener);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | // radio button "No proxy"
|
---|
| 246 | gc.gridx = 0;
|
---|
| 247 | gc.gridy = 0;
|
---|
| 248 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 249 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
| 250 | gc.weightx = 0.0;
|
---|
| 251 | pnl.add(rbProxyPolicy.get(ProxyPolicy.NO_PROXY),gc);
|
---|
| 252 |
|
---|
| 253 | gc.gridx = 1;
|
---|
| 254 | gc.weightx = 1.0;
|
---|
| 255 | pnl.add(new JLabel(tr("No proxy")), gc);
|
---|
| 256 |
|
---|
| 257 | // radio button "System settings"
|
---|
| 258 | gc.gridx = 0;
|
---|
| 259 | gc.gridy = 1;
|
---|
| 260 | gc.weightx = 0.0;
|
---|
| 261 | pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS),gc);
|
---|
| 262 |
|
---|
| 263 | gc.gridx = 1;
|
---|
| 264 | gc.weightx = 1.0;
|
---|
| 265 | String msg;
|
---|
| 266 | if (DefaultProxySelector.willJvmRetrieveSystemProxies()) {
|
---|
| 267 | msg = tr("Use standard system settings");
|
---|
| 268 | } else {
|
---|
| 269 | msg = tr("Use standard system settings (disabled. Start JOSM with <tt>-Djava.net.useSystemProxies=true</tt> to enable)");
|
---|
| 270 | }
|
---|
| 271 | pnl.add(new JMultilineLabel("<html>" + msg + "</html>"), gc);
|
---|
| 272 |
|
---|
| 273 | // radio button http proxy
|
---|
| 274 | gc.gridx = 0;
|
---|
| 275 | gc.gridy = 2;
|
---|
| 276 | gc.weightx = 0.0;
|
---|
| 277 | pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY),gc);
|
---|
| 278 |
|
---|
| 279 | gc.gridx = 1;
|
---|
| 280 | gc.weightx = 1.0;
|
---|
| 281 | pnl.add(new JLabel(tr("Manually configure a HTTP proxy")),gc);
|
---|
| 282 |
|
---|
| 283 | // the panel with the http proxy configuration parameters
|
---|
| 284 | gc.gridx = 1;
|
---|
| 285 | gc.gridy = 3;
|
---|
| 286 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 287 | gc.weightx = 1.0;
|
---|
| 288 | gc.weighty = 0.0;
|
---|
| 289 | pnl.add(pnlHttpProxyConfigurationPanel = buildHttpProxyConfigurationPanel(),gc);
|
---|
| 290 |
|
---|
| 291 | // radio button SOCKS proxy
|
---|
| 292 | gc.gridx = 0;
|
---|
| 293 | gc.gridy = 4;
|
---|
| 294 | gc.weightx = 0.0;
|
---|
| 295 | pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_SOCKS_PROXY),gc);
|
---|
| 296 |
|
---|
| 297 | gc.gridx = 1;
|
---|
| 298 | gc.weightx = 1.0;
|
---|
| 299 | pnl.add(new JLabel(tr("Use a SOCKS proxy")),gc);
|
---|
| 300 |
|
---|
| 301 | // the panel with the SOCKS configuration parameters
|
---|
| 302 | gc.gridx = 1;
|
---|
| 303 | gc.gridy = 5;
|
---|
| 304 | gc.fill = GridBagConstraints.BOTH;
|
---|
| 305 | gc.anchor = GridBagConstraints.WEST;
|
---|
| 306 | gc.weightx = 1.0;
|
---|
| 307 | gc.weighty = 0.0;
|
---|
| 308 | pnl.add(pnlSocksProxyConfigurationPanel = buildSocksProxyConfigurationPanel(),gc);
|
---|
| 309 |
|
---|
| 310 | return pnl;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /**
|
---|
| 314 | * Initializes the panel with the values from the preferences
|
---|
| 315 | */
|
---|
[6890] | 316 | public final void initFromPreferences() {
|
---|
[2801] | 317 | String policy = Main.pref.get(PROXY_POLICY, null);
|
---|
| 318 | ProxyPolicy pp = ProxyPolicy.fromName(policy);
|
---|
[4246] | 319 | if (pp == null) {
|
---|
| 320 | pp = ProxyPolicy.NO_PROXY;
|
---|
| 321 | }
|
---|
[2801] | 322 | rbProxyPolicy.get(pp).setSelected(true);
|
---|
| 323 | String value = Main.pref.get("proxy.host", null);
|
---|
| 324 | if (value != null) {
|
---|
| 325 | // legacy support
|
---|
| 326 | tfProxyHttpHost.setText(value);
|
---|
| 327 | Main.pref.put("proxy.host", null);
|
---|
| 328 | } else {
|
---|
| 329 | tfProxyHttpHost.setText(Main.pref.get(PROXY_HTTP_HOST, ""));
|
---|
| 330 | }
|
---|
| 331 | value = Main.pref.get("proxy.port", null);
|
---|
| 332 | if (value != null) {
|
---|
| 333 | // legacy support
|
---|
| 334 | tfProxyHttpPort.setText(value);
|
---|
| 335 | Main.pref.put("proxy.port", null);
|
---|
| 336 | } else {
|
---|
| 337 | tfProxyHttpPort.setText(Main.pref.get(PROXY_HTTP_PORT, ""));
|
---|
| 338 | }
|
---|
| 339 | tfProxySocksHost.setText(Main.pref.get(PROXY_SOCKS_HOST, ""));
|
---|
| 340 | tfProxySocksPort.setText(Main.pref.get(PROXY_SOCKS_PORT, ""));
|
---|
| 341 |
|
---|
| 342 | if (pp.equals(ProxyPolicy.USE_SYSTEM_SETTINGS) && ! DefaultProxySelector.willJvmRetrieveSystemProxies()) {
|
---|
[6248] | 343 | Main.warn(tr("JOSM is configured to use proxies from the system setting, but the JVM is not configured to retrieve them. Resetting preferences to ''No proxy''"));
|
---|
[2801] | 344 | pp = ProxyPolicy.NO_PROXY;
|
---|
| 345 | rbProxyPolicy.get(pp).setSelected(true);
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | // save the proxy user and the proxy password to a credentials store managed by
|
---|
| 349 | // the credentials manager
|
---|
[4245] | 350 | CredentialsAgent cm = CredentialsManager.getInstance();
|
---|
[2801] | 351 | try {
|
---|
[4690] | 352 | PasswordAuthentication pa = cm.lookup(RequestorType.PROXY, tfProxyHttpHost.getText());
|
---|
[2801] | 353 | if (pa == null) {
|
---|
| 354 | tfProxyHttpUser.setText("");
|
---|
| 355 | tfProxyHttpPassword.setText("");
|
---|
| 356 | } else {
|
---|
| 357 | tfProxyHttpUser.setText(pa.getUserName() == null ? "" : pa.getUserName());
|
---|
| 358 | tfProxyHttpPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword()));
|
---|
| 359 | }
|
---|
[4245] | 360 | } catch(CredentialsAgentException e) {
|
---|
[6643] | 361 | Main.error(e);
|
---|
[2801] | 362 | tfProxyHttpUser.setText("");
|
---|
| 363 | tfProxyHttpPassword.setText("");
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[6890] | 367 | protected final void updateEnabledState() {
|
---|
[2801] | 368 | boolean isHttpProxy = rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected();
|
---|
| 369 | for (Component c: pnlHttpProxyConfigurationPanel.getComponents()) {
|
---|
| 370 | c.setEnabled(isHttpProxy);
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | boolean isSocksProxy = rbProxyPolicy.get(ProxyPolicy.USE_SOCKS_PROXY).isSelected();
|
---|
| 374 | for (Component c: pnlSocksProxyConfigurationPanel.getComponents()) {
|
---|
| 375 | c.setEnabled(isSocksProxy);
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS).setEnabled(DefaultProxySelector.willJvmRetrieveSystemProxies());
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | class ProxyPolicyChangeListener implements ItemListener {
|
---|
[4246] | 382 | @Override
|
---|
[2801] | 383 | public void itemStateChanged(ItemEvent arg0) {
|
---|
| 384 | updateEnabledState();
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[6525] | 388 | /**
|
---|
| 389 | * Constructs a new {@code ProxyPreferencesPanel}.
|
---|
| 390 | */
|
---|
[2801] | 391 | public ProxyPreferencesPanel() {
|
---|
| 392 | setLayout(new GridBagLayout());
|
---|
| 393 | setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
| 394 | add(buildProxySettingsPanel(), GBC.eop().anchor(GridBagConstraints.NORTHWEST).fill(GridBagConstraints.BOTH));
|
---|
| 395 |
|
---|
| 396 | initFromPreferences();
|
---|
| 397 | updateEnabledState();
|
---|
| 398 |
|
---|
| 399 | HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#ProxySettings"));
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | /**
|
---|
| 403 | * Saves the current values to the preferences
|
---|
| 404 | */
|
---|
| 405 | public void saveToPreferences() {
|
---|
| 406 | ProxyPolicy policy = null;
|
---|
| 407 | for (ProxyPolicy pp: ProxyPolicy.values()) {
|
---|
| 408 | if (rbProxyPolicy.get(pp).isSelected()) {
|
---|
| 409 | policy = pp;
|
---|
| 410 | break;
|
---|
| 411 | }
|
---|
| 412 | }
|
---|
| 413 | if (policy == null) {
|
---|
| 414 | policy = ProxyPolicy.NO_PROXY;
|
---|
| 415 | }
|
---|
| 416 | Main.pref.put(PROXY_POLICY, policy.getName());
|
---|
| 417 | Main.pref.put(PROXY_HTTP_HOST, tfProxyHttpHost.getText());
|
---|
| 418 | Main.pref.put(PROXY_HTTP_PORT, tfProxyHttpPort.getText());
|
---|
| 419 | Main.pref.put(PROXY_SOCKS_HOST, tfProxySocksHost.getText());
|
---|
| 420 | Main.pref.put(PROXY_SOCKS_PORT, tfProxySocksPort.getText());
|
---|
| 421 |
|
---|
| 422 | // update the proxy selector
|
---|
| 423 | ProxySelector selector = ProxySelector.getDefault();
|
---|
| 424 | if (selector instanceof DefaultProxySelector) {
|
---|
| 425 | ((DefaultProxySelector)selector).initFromPreferences();
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[4245] | 428 | CredentialsAgent cm = CredentialsManager.getInstance();
|
---|
[2801] | 429 | try {
|
---|
| 430 | PasswordAuthentication pa = new PasswordAuthentication(
|
---|
| 431 | tfProxyHttpUser.getText().trim(),
|
---|
| 432 | tfProxyHttpPassword.getPassword()
|
---|
| 433 | );
|
---|
[4690] | 434 | cm.store(RequestorType.PROXY, tfProxyHttpHost.getText(), pa);
|
---|
[4245] | 435 | } catch(CredentialsAgentException e) {
|
---|
[6643] | 436 | Main.error(e);
|
---|
[2801] | 437 | }
|
---|
| 438 | }
|
---|
| 439 | }
|
---|