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.Dimension;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.awt.event.ItemEvent;
|
---|
11 | import java.awt.event.ItemListener;
|
---|
12 | import java.net.ProxySelector;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.Map;
|
---|
15 |
|
---|
16 | import javax.swing.ButtonGroup;
|
---|
17 | import javax.swing.JLabel;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JPasswordField;
|
---|
20 | import javax.swing.JRadioButton;
|
---|
21 | import javax.swing.JSeparator;
|
---|
22 | import javax.swing.JTextField;
|
---|
23 | import javax.swing.SwingConstants;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.gui.JMultilineLabel;
|
---|
27 | import org.openstreetmap.josm.io.DefaultProxySelector;
|
---|
28 | import org.openstreetmap.josm.tools.GBC;
|
---|
29 |
|
---|
30 | public class ProxyPreferences implements PreferenceSetting {
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | public static class Factory implements PreferenceSettingFactory {
|
---|
35 | public PreferenceSetting createPreferenceSetting() {
|
---|
36 | return new ProxyPreferences();
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public enum ProxyPolicy {
|
---|
41 | NO_PROXY("no-proxy"),
|
---|
42 | USE_SYSTEM_SETTINGS("use-system-settings"),
|
---|
43 | USE_HTTP_PROXY("use-http-proxy"),
|
---|
44 | USE_SOCKS_PROXY("use-socks-proxy");
|
---|
45 |
|
---|
46 | private String policyName;
|
---|
47 | ProxyPolicy(String policyName) {
|
---|
48 | this.policyName = policyName;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public String getName() {
|
---|
52 | return policyName;
|
---|
53 | }
|
---|
54 |
|
---|
55 | static public ProxyPolicy fromName(String policyName) {
|
---|
56 | if (policyName == null) return null;
|
---|
57 | policyName = policyName.trim().toLowerCase();
|
---|
58 | for(ProxyPolicy pp: values()) {
|
---|
59 | if (pp.getName().equals(policyName))
|
---|
60 | return pp;
|
---|
61 | }
|
---|
62 | return null;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static final String PROXY_POLICY = "proxy.policy";
|
---|
67 | public static final String PROXY_HTTP_HOST = "proxy.http.host";
|
---|
68 | public static final String PROXY_HTTP_PORT = "proxy.http.port";
|
---|
69 | public static final String PROXY_SOCKS_HOST = "proxy.socks.host";
|
---|
70 | public static final String PROXY_SOCKS_PORT = "proxy.socks.port";
|
---|
71 | public static final String PROXY_USER = "proxy.user";
|
---|
72 | public static final String PROXY_PASS = "proxy.pass";
|
---|
73 |
|
---|
74 | private ButtonGroup bgProxyPolicy;
|
---|
75 | private Map<ProxyPolicy, JRadioButton> rbProxyPolicy;
|
---|
76 | private JTextField tfProxyHttpHost;
|
---|
77 | private JTextField tfProxyHttpPort;
|
---|
78 | private JTextField tfProxySocksHost;
|
---|
79 | private JTextField tfProxySocksPort;
|
---|
80 | private JTextField tfProxyHttpUser;
|
---|
81 | private JPasswordField tfProxyHttpPassword;
|
---|
82 |
|
---|
83 | protected JPanel buildHttpProxyConfigurationPanel() {
|
---|
84 | JPanel pnl = new JPanel(new GridBagLayout()) {
|
---|
85 | @Override
|
---|
86 | public Dimension getMinimumSize() {
|
---|
87 | return getPreferredSize();
|
---|
88 | }
|
---|
89 | };
|
---|
90 | GridBagConstraints gc = new GridBagConstraints();
|
---|
91 |
|
---|
92 | gc.anchor = GridBagConstraints.WEST;
|
---|
93 | gc.insets = new Insets(5,5,0,0);
|
---|
94 | pnl.add(new JLabel("Host:"), gc);
|
---|
95 |
|
---|
96 | gc.gridx = 1;
|
---|
97 | pnl.add(tfProxyHttpHost = new JTextField(20),gc);
|
---|
98 |
|
---|
99 | gc.gridy = 1;
|
---|
100 | gc.gridx = 0;
|
---|
101 | pnl.add(new JLabel("Port:"), gc);
|
---|
102 |
|
---|
103 | gc.gridx = 1;
|
---|
104 | gc.weightx = 0.0;
|
---|
105 | pnl.add(tfProxyHttpPort = new JTextField(5),gc);
|
---|
106 |
|
---|
107 | gc.gridy = 2;
|
---|
108 | gc.gridx = 0;
|
---|
109 | gc.gridwidth = 2;
|
---|
110 | gc.fill = GridBagConstraints.BOTH;
|
---|
111 | gc.weightx = 1.0;
|
---|
112 | gc.weighty = 1.0;
|
---|
113 | pnl.add(new JMultilineLabel(tr("Please enter a username and a password if your proxy requires authentication.")), gc);
|
---|
114 |
|
---|
115 | gc.gridy = 3;
|
---|
116 | gc.gridx = 0;
|
---|
117 | gc.gridwidth = 1;
|
---|
118 | gc.weightx = 0.0;
|
---|
119 | gc.fill = GridBagConstraints.NONE;
|
---|
120 | pnl.add(new JLabel("User:"), gc);
|
---|
121 |
|
---|
122 | gc.gridy = 3;
|
---|
123 | gc.gridx = 1;
|
---|
124 | pnl.add(tfProxyHttpUser = new JTextField(20),gc);
|
---|
125 |
|
---|
126 | gc.gridy = 4;
|
---|
127 | gc.gridx = 0;
|
---|
128 | pnl.add(new JLabel("Password:"), gc);
|
---|
129 |
|
---|
130 | gc.gridx = 1;
|
---|
131 | pnl.add(tfProxyHttpPassword = new JPasswordField(20),gc);
|
---|
132 | return pnl;
|
---|
133 | }
|
---|
134 |
|
---|
135 | protected JPanel buildSocksProxyConfigurationPanel() {
|
---|
136 | JPanel pnl = new JPanel(new GridBagLayout()) {
|
---|
137 | @Override
|
---|
138 | public Dimension getMinimumSize() {
|
---|
139 | return getPreferredSize();
|
---|
140 | }
|
---|
141 | };
|
---|
142 | GridBagConstraints gc = new GridBagConstraints();
|
---|
143 | gc.anchor = GridBagConstraints.WEST;
|
---|
144 | gc.insets = new Insets(5,5,0,0);
|
---|
145 | pnl.add(new JLabel("Host:"), gc);
|
---|
146 |
|
---|
147 | gc.gridx = 1;
|
---|
148 | pnl.add(tfProxySocksHost = new JTextField(20),gc);
|
---|
149 |
|
---|
150 | gc.gridy = 1;
|
---|
151 | gc.gridx = 0;
|
---|
152 | pnl.add(new JLabel("Port:"), gc);
|
---|
153 |
|
---|
154 | gc.gridx = 1;
|
---|
155 | pnl.add(tfProxySocksPort = new JTextField(5),gc);
|
---|
156 |
|
---|
157 | // add an extra spacer, otherwise the layout is broken
|
---|
158 | gc.gridy = 2;
|
---|
159 | gc.gridx = 0;
|
---|
160 | gc.gridwidth = 2;
|
---|
161 | gc.fill = GridBagConstraints.BOTH;
|
---|
162 | gc.weightx = 1.0;
|
---|
163 | gc.weighty = 1.0;
|
---|
164 | pnl.add(new JPanel(), gc);
|
---|
165 | return pnl;
|
---|
166 | }
|
---|
167 |
|
---|
168 | protected JPanel buildProxySettingsPanel() {
|
---|
169 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
170 | GridBagConstraints gc = new GridBagConstraints();
|
---|
171 |
|
---|
172 | bgProxyPolicy = new ButtonGroup();
|
---|
173 | rbProxyPolicy = new HashMap<ProxyPolicy, JRadioButton>();
|
---|
174 | ProxyPolicyChangeListener policyChangeListener = new ProxyPolicyChangeListener();
|
---|
175 | for (ProxyPolicy pp: ProxyPolicy.values()) {
|
---|
176 | rbProxyPolicy.put(pp, new JRadioButton());
|
---|
177 | bgProxyPolicy.add(rbProxyPolicy.get(pp));
|
---|
178 | rbProxyPolicy.get(pp).addItemListener(policyChangeListener);
|
---|
179 | }
|
---|
180 | gc.gridx = 0;
|
---|
181 | gc.gridy = 0;
|
---|
182 | gc.fill = GridBagConstraints.NONE;
|
---|
183 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
184 | pnl.add(rbProxyPolicy.get(ProxyPolicy.NO_PROXY),gc);
|
---|
185 | gc.gridx = 1;
|
---|
186 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
187 | gc.weightx = 1.0;
|
---|
188 | pnl.add(new JLabel(tr("No proxy")), gc);
|
---|
189 |
|
---|
190 | gc.gridx = 0;
|
---|
191 | gc.gridy = 1;
|
---|
192 | gc.fill = GridBagConstraints.NONE;
|
---|
193 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
194 | pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS),gc);
|
---|
195 | gc.gridx = 1;
|
---|
196 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
197 | gc.weightx = 1.0;
|
---|
198 | gc.weighty = 0.0;
|
---|
199 | String msg;
|
---|
200 | if (DefaultProxySelector.willJvmRetrieveSystemProxies()) {
|
---|
201 | msg = tr("Use standard system settings");
|
---|
202 | } else {
|
---|
203 | msg = tr("Use standard system settings (disabled. Start JOSM with <tt>-Djava.net.useSystemProxies=true</tt> to enable)");
|
---|
204 | }
|
---|
205 | pnl.add(new JMultilineLabel("<html>" + msg + "</html>"), gc);
|
---|
206 |
|
---|
207 | gc.gridx = 0;
|
---|
208 | gc.gridy = 2;
|
---|
209 | gc.fill = GridBagConstraints.NONE;
|
---|
210 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
211 | pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY),gc);
|
---|
212 | gc.gridx = 1;
|
---|
213 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
214 | gc.weightx = 1.0;
|
---|
215 | pnl.add(new JLabel(tr("Manually configure a HTTP proxy")),gc);
|
---|
216 |
|
---|
217 | gc.gridx = 1;
|
---|
218 | gc.gridy = 3;
|
---|
219 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
220 | gc.weightx = 1.0;
|
---|
221 | gc.weighty = 0.0;
|
---|
222 | pnl.add(buildHttpProxyConfigurationPanel(),gc);
|
---|
223 |
|
---|
224 | gc.gridx = 0;
|
---|
225 | gc.gridy = 4;
|
---|
226 | gc.fill = GridBagConstraints.NONE;
|
---|
227 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
228 | pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_SOCKS_PROXY),gc);
|
---|
229 | gc.gridx = 1;
|
---|
230 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
231 | gc.weightx = 1.0;
|
---|
232 | pnl.add(new JLabel(tr("Use a SOCKS proxy")),gc);
|
---|
233 |
|
---|
234 | gc.gridx = 1;
|
---|
235 | gc.gridy = 5;
|
---|
236 | gc.fill = GridBagConstraints.BOTH;
|
---|
237 | gc.anchor = GridBagConstraints.WEST;
|
---|
238 | gc.weightx = 1.0;
|
---|
239 | gc.weighty = 0.0;
|
---|
240 | pnl.add(buildSocksProxyConfigurationPanel(),gc);
|
---|
241 |
|
---|
242 | return pnl;
|
---|
243 | }
|
---|
244 |
|
---|
245 | protected void initFromPreferences() {
|
---|
246 | String policy = Main.pref.get(PROXY_POLICY, null);
|
---|
247 | ProxyPolicy pp = ProxyPolicy.fromName(policy);
|
---|
248 | pp = pp == null? ProxyPolicy.NO_PROXY: pp;
|
---|
249 | rbProxyPolicy.get(pp).setSelected(true);
|
---|
250 | String value = Main.pref.get("proxy.host", null);
|
---|
251 | if (value != null) {
|
---|
252 | // legacy support
|
---|
253 | tfProxyHttpHost.setText(value);
|
---|
254 | Main.pref.put("proxy.host", null);
|
---|
255 | } else {
|
---|
256 | tfProxyHttpHost.setText(Main.pref.get(PROXY_HTTP_HOST, ""));
|
---|
257 | }
|
---|
258 | value = Main.pref.get("proxy.port", null);
|
---|
259 | if (value != null) {
|
---|
260 | // legacy support
|
---|
261 | tfProxyHttpPort.setText(value);
|
---|
262 | Main.pref.put("proxy.port", null);
|
---|
263 | } else {
|
---|
264 | tfProxyHttpPort.setText(Main.pref.get(PROXY_HTTP_PORT, ""));
|
---|
265 | }
|
---|
266 | tfProxySocksHost.setText(Main.pref.get(PROXY_SOCKS_HOST, ""));
|
---|
267 | tfProxySocksPort.setText(Main.pref.get(PROXY_SOCKS_PORT, ""));
|
---|
268 | tfProxyHttpUser.setText(Main.pref.get(PROXY_USER, ""));
|
---|
269 | tfProxyHttpPassword.setText(Main.pref.get(PROXY_PASS, ""));
|
---|
270 |
|
---|
271 | if (pp.equals(ProxyPolicy.USE_SYSTEM_SETTINGS) && ! DefaultProxySelector.willJvmRetrieveSystemProxies()) {
|
---|
272 | System.err.println(tr("Warning: JOSM is configured to use proxies from the system setting, but the JVM is not configured to retrieve them. Resetting preferences to ''No proxy''"));
|
---|
273 | pp = ProxyPolicy.NO_PROXY;
|
---|
274 | rbProxyPolicy.get(pp).setSelected(true);
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | protected void updateEnabledState() {
|
---|
279 | tfProxyHttpHost.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected());
|
---|
280 | tfProxyHttpPort.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected());
|
---|
281 | tfProxyHttpUser.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected());
|
---|
282 | tfProxyHttpPassword.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected());
|
---|
283 | tfProxySocksHost.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_SOCKS_PROXY).isSelected());
|
---|
284 | tfProxySocksPort.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_SOCKS_PROXY).isSelected());
|
---|
285 |
|
---|
286 | rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS).setEnabled(DefaultProxySelector.willJvmRetrieveSystemProxies());
|
---|
287 | }
|
---|
288 |
|
---|
289 | class ProxyPolicyChangeListener implements ItemListener {
|
---|
290 | public void itemStateChanged(ItemEvent arg0) {
|
---|
291 | updateEnabledState();
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | public void addGui(PreferenceDialog gui) {
|
---|
296 | gui.connection.add(new JSeparator(SwingConstants.HORIZONTAL), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
297 | gui.connection.add(new JLabel(tr("Proxy Settings")), GBC.eol());
|
---|
298 | gui.connection.add(buildProxySettingsPanel(), GBC.eol().insets(20,10,0,0));
|
---|
299 |
|
---|
300 | initFromPreferences();
|
---|
301 | updateEnabledState();
|
---|
302 | }
|
---|
303 |
|
---|
304 | public boolean ok() {
|
---|
305 | ProxyPolicy policy = null;
|
---|
306 | for (ProxyPolicy pp: ProxyPolicy.values()) {
|
---|
307 | if (rbProxyPolicy.get(pp).isSelected()) {
|
---|
308 | policy = pp;
|
---|
309 | break;
|
---|
310 | }
|
---|
311 | }
|
---|
312 | if (policy == null) {
|
---|
313 | policy = ProxyPolicy.NO_PROXY;
|
---|
314 | }
|
---|
315 | Main.pref.put(PROXY_POLICY, policy.getName());
|
---|
316 | Main.pref.put(PROXY_HTTP_HOST, tfProxyHttpHost.getText());
|
---|
317 | Main.pref.put(PROXY_HTTP_PORT, tfProxyHttpPort.getText());
|
---|
318 | Main.pref.put(PROXY_SOCKS_HOST, tfProxySocksHost.getText());
|
---|
319 | Main.pref.put(PROXY_SOCKS_PORT, tfProxySocksPort.getText());
|
---|
320 | Main.pref.put(PROXY_USER, tfProxyHttpUser.getText());
|
---|
321 | Main.pref.put(PROXY_PASS, String.valueOf(tfProxyHttpPassword.getPassword()));
|
---|
322 |
|
---|
323 | // remove these legacy property keys
|
---|
324 | Main.pref.put("proxy.anonymous", null);
|
---|
325 | Main.pref.put("proxy.enable", null);
|
---|
326 |
|
---|
327 | // update the proxy selector
|
---|
328 | ProxySelector selector = ProxySelector.getDefault();
|
---|
329 | if (selector instanceof DefaultProxySelector) {
|
---|
330 | ((DefaultProxySelector)selector).initFromPreferences();
|
---|
331 | }
|
---|
332 | return false;
|
---|
333 | }
|
---|
334 | }
|
---|