Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fb7c94427bf18d4fdea9f707fd77831cfc40efda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20070123   154100 eu@md.pp.ru - Eugene Kuleshov, Initial UI coding
 * 20070201   154100 pmoogk@ca.ibm.com - Peter Moogk, Port internet code from WTP to Eclipse base.
 * 20070219   174674 pmoogk@ca.ibm.com - Peter Moogk
 *******************************************************************************/
package org.eclipse.net.internal.ui;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.net.core.IProxyData;
import org.eclipse.net.core.NetCore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class ProxyPreferencePage extends PreferencePage implements
		IWorkbenchPreferencePage {
	IProxyData[] proxyData;

	Entry[] entryList;

	Button directConnectionToButton;

	private Button manualProxyConfigurationButton;

	Button useSameProxyButton;

	private Text nonHostLabel;

	private NonProxyHostsComposite nonHostComposite;

	Button enableProxyAuth;

	private Label useridLabel;

	private Label passwordLabel;

	Text userid;

	Text password;

	public ProxyPreferencePage() {
		super(NetUIMessages.ProxyPreferencePage_2);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	public void init(IWorkbench workbench) {
		// Nothing to do
	}

	protected Control createContents(Composite parent) {

		proxyData = NetCore.getProxyManager().getProxyData();
		entryList = new Entry[proxyData.length];

		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		layout.marginRight = 5;
		layout.marginTop = 5;
		layout.marginWidth = 0;
		composite.setLayout(layout);
		GridData data = new GridData(GridData.FILL_BOTH);
		composite.setLayoutData(data);

		directConnectionToButton = new Button(composite, SWT.RADIO);
		directConnectionToButton.setLayoutData(new GridData());
		directConnectionToButton.setText(NetUIMessages.ProxyPreferencePage_3);
		directConnectionToButton
				.setToolTipText(NetUIMessages.ProxyPreferencePage_1);
		directConnectionToButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enableControls(!directConnectionToButton.getSelection());
			}
		});

		manualProxyConfigurationButton = new Button(composite, SWT.RADIO);
		manualProxyConfigurationButton
				.setText(NetUIMessages.ProxyPreferencePage_4);
		manualProxyConfigurationButton
				.setToolTipText(NetUIMessages.ProxyPreferencePage_0);

		final Composite manualProxyConfigurationComposite = new Composite(
				composite, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 0;
		gridLayout.numColumns = 4;
		manualProxyConfigurationComposite.setLayout(gridLayout);
		final GridData gridData_2 = new GridData();
		gridData_2.horizontalIndent = 17;
		manualProxyConfigurationComposite.setLayoutData(gridData_2);

		addProtocolEntry(manualProxyConfigurationComposite, 0);

		new Label(manualProxyConfigurationComposite, SWT.NONE);
		useSameProxyButton = new Button(manualProxyConfigurationComposite,
				SWT.CHECK);
		useSameProxyButton.setText(NetUIMessages.ProxyPreferencePage_5);
		GridData gridData = new GridData();
		gridData.horizontalSpan = 3;
		useSameProxyButton.setLayoutData(gridData);
		useSameProxyButton
				.setToolTipText(NetUIMessages.ProxyPreferencePage_23);
		useSameProxyButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				handleUseSameProtocol(false);
			}
		});

		for (int index = 1; index < proxyData.length; index++) {
			addProtocolEntry(manualProxyConfigurationComposite, index);
		}

		Label separator = new Label(manualProxyConfigurationComposite,
				SWT.HORIZONTAL | SWT.SEPARATOR);
		separator.setLayoutData(newGridData(4, 5, true, false));
		nonHostLabel = new Text(manualProxyConfigurationComposite,
				SWT.READ_ONLY);
		nonHostLabel.setText(NetUIMessages.ProxyPreferencePage_6);
		nonHostLabel
				.setToolTipText(NetUIMessages.ProxyPreferencePage_24);
		nonHostLabel.setLayoutData(newGridData(4, 5, true, false));
		nonHostComposite = new NonProxyHostsComposite(
				manualProxyConfigurationComposite, SWT.NONE);
		nonHostComposite.setLayoutData(newGridData(4, -1, true, false));

		Label separator2 = new Label(manualProxyConfigurationComposite,
				SWT.HORIZONTAL | SWT.SEPARATOR);
		separator2.setLayoutData(newGridData(4, 5, true, false));
		enableProxyAuth = new Button(manualProxyConfigurationComposite,
				SWT.CHECK);
		enableProxyAuth.setText(NetUIMessages.ProxyPreferencePage_7);
		enableProxyAuth.setLayoutData(newGridData(4, 5, true, false));
		enableProxyAuth
				.setToolTipText(NetUIMessages.ProxyPreferencePage_25);
		enableProxyAuth.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enableUseridPassword(enableProxyAuth.getSelection());
			}
		});

		final Composite userIdPassword = new Composite(
				manualProxyConfigurationComposite, SWT.NONE);
		GridLayout layout2 = new GridLayout(2, false);
		layout2.marginWidth = 0;
		layout2.marginHeight = 0;
		userIdPassword.setLayoutData(newGridData(4, -1, true, false));
		userIdPassword.setLayout(layout2);

		useridLabel = new Label(userIdPassword, SWT.NONE);
		useridLabel.setText(NetUIMessages.ProxyPreferencePage_8);
		userid = new Text(userIdPassword, SWT.BORDER);
		passwordLabel = new Label(userIdPassword, SWT.NONE);
		passwordLabel.setText(NetUIMessages.ProxyPreferencePage_9);
		password = new Text(userIdPassword, SWT.BORDER);

		userid.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		userid.setToolTipText(NetUIMessages.ProxyPreferencePage_26);
		password.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		password.setToolTipText(NetUIMessages.ProxyPreferencePage_27);
		password.setEchoChar('*');

    ModifyListener modifyListener = new ModifyListener()
    {
      public void modifyText(ModifyEvent e)
      {
        if( useSameProxyButton.getSelection() )
        {
          Entry  httpEntry    = entryList[0];
          String httpHostname = httpEntry.hostname.getText();
          String httpPort     = httpEntry.port.getText();
          
          for( int index = 1; index < entryList.length; index++ )
          {
            Entry entry = entryList[index];
            
            entry.hostname.setText( httpHostname );
            entry.port.setText( httpPort );
          }
        }
      }
    };
    
    entryList[0].hostname.addModifyListener( modifyListener );
    entryList[0].port.addModifyListener( modifyListener );
    
		restoreState(NetCore.getProxyManager().isProxiesEnabled());
		applyDialogFont(composite);

		return composite;
	}

	protected void performApply() {
		boolean proxiesEnabled = manualProxyConfigurationButton.getSelection();

		// Save the contents of the text fields to the proxy data.
		for (int index = 0; index < entryList.length; index++) {
			entryList[index].applyValues();
		}

		NetCore.getProxyManager().setProxiesEnabled(proxiesEnabled);
		if (proxiesEnabled) {
			try {
				NetCore.getProxyManager().setProxyData(proxyData);
				NetCore.getProxyManager().setNonProxiedHosts(
						nonHostComposite.getList());
			} catch (CoreException e) {
				ErrorDialog.openError(getShell(), null, null, e.getStatus());
			}
		}
		Activator.getDefault().savePluginPreferences();
	}

	protected void performDefaults() {
		directConnectionToButton.setSelection(true);
		manualProxyConfigurationButton.setSelection(false);
		useSameProxyButton.setSelection(false);
		enableProxyAuth.setSelection(false);

		for (int index = 0; index < entryList.length; index++) {
			Entry entry = entryList[index];

			entry.hostname.setText(""); //$NON-NLS-1$
			entry.port.setText(""); //$NON-NLS-1$
			entry.prevHostname = ""; //$NON-NLS-1$
			entry.prevPort = -1;
		}

		nonHostComposite.setList(new String[] { "localhost", "127.0.0.1" }); //$NON-NLS-1$ //$NON-NLS-2$
		userid.setText(""); //$NON-NLS-1$
		password.setText(""); //$NON-NLS-1$
		enableControls(false);
	}

	public boolean performOk() {
		performApply();
		return super.performOk();
	}

	/**
	 * This method is run once when when this preference page is displayed. It
	 * will restore the state of this page using previously saved preferences.
	 * 
	 * @param proxiesEnabled indicates if manual proxies are enabled or not.
	 */
	private void restoreState(boolean proxiesEnabled) {

		directConnectionToButton.setSelection(!proxiesEnabled);
		manualProxyConfigurationButton.setSelection(proxiesEnabled);

		String[] nonHostLists = NetCore.getProxyManager().getNonProxiedHosts();
		this.nonHostComposite.setList(nonHostLists == null ? new String[] {
				"localhost", "127.0.0.1" } : nonHostLists); //$NON-NLS-1$ //$NON-NLS-2$
		if (!proxiesEnabled) {
			this.useSameProxyButton.setSelection(false);
			this.enableProxyAuth.setSelection(false);
			this.userid.setText(""); //$NON-NLS-1$
			this.password.setText(""); //$NON-NLS-1$
		} else {
			boolean useSameProtocol = true;
			for (int i = 1; i < proxyData.length; i++) {
				IProxyData data = proxyData[i];
				boolean same = (hostsEqual(data.getHost(), proxyData[0]
						.getHost()) && portsEqual(data.getPort(), proxyData[0]
						.getPort()));
				if (!same) {
					useSameProtocol = false;
					break;
				}
			}
			this.useSameProxyButton.setSelection(useSameProtocol);
			IProxyData data = entryList[0].getProxy();
			this.enableProxyAuth.setSelection(data.isRequiresAuthentication());
			this.userid.setText(data.getUserId() == null ? "" : data //$NON-NLS-1$
					.getUserId());
			this.password.setText(data.getPassword() == null ? "" : data //$NON-NLS-1$
					.getPassword());

			// TODO: Why does this loop from 1 and not 0?
			for (int index = 1; index < entryList.length; index++) {
				entryList[index].restoreValues();
			}
		}

		enableControls(proxiesEnabled);
		handleUseSameProtocol(true);
	}

	private boolean portsEqual(int port1, int port2) {
		return port1 == port2;
	}

	private boolean hostsEqual(String host1, String host2) {
		if (host1 == host2)
			// If there are no hosts, don't enable the use same hosts button
			return false;
		if (host1 == null || host2 == null)
			return false;
		return host1.equals(host2);
	}

	void enableControls(boolean enabled) {
		for (int index = 0; index < entryList.length; index++) {
			Entry entry = entryList[index];

			entry.hostname.setEnabled(enabled);
			entry.nameLabel.setEnabled(enabled);
			entry.portLabel.setEnabled(enabled);
			entry.port.setEnabled(enabled);
		}

		useSameProxyButton.setEnabled(enabled);
		nonHostLabel.setEnabled(enabled);
		nonHostComposite.enableComposite(enabled);
		enableProxyAuth.setEnabled(enabled);

		enableUseridPassword(enableProxyAuth.getSelection() && enabled);
	}

	void enableUseridPassword(boolean enabled) {
		useridLabel.setEnabled(enabled);
		userid.setEnabled(enabled);
		passwordLabel.setEnabled(enabled);
		password.setEnabled(enabled);
	}

	void handleUseSameProtocol(boolean init) {
		String httpHostname = entryList[0].hostname.getText();
		String httpPort = entryList[0].port.getText();
		boolean proxiesEnabled = manualProxyConfigurationButton.getSelection();
		boolean useSameProtocol = useSameProxyButton.getSelection();
		boolean controlEnabled = proxiesEnabled && !useSameProtocol;

		for (int index = 1; index < entryList.length; index++) {
			Entry entry = entryList[index];

			entry.hostname.setEnabled(controlEnabled);
			entry.nameLabel.setEnabled(controlEnabled);
			entry.portLabel.setEnabled(controlEnabled);
			entry.port.setEnabled(controlEnabled);

			if (useSameProtocol) {
				if (!init) {
					entry.prevHostname = entry.hostname.getText();
					try {
						entry.prevPort = Integer.parseInt(entry.port.getText());
					} catch (NumberFormatException e) {
						entry.prevPort = -1;
					}
				}

				entry.hostname.setText(httpHostname);
				entry.port.setText(httpPort);
			} else {
				if (!init) {
					entry.hostname.setText(entry.prevHostname);
					entry.port.setText(entry.prevPort == -1 ? "" : String //$NON-NLS-1$
							.valueOf(entry.prevPort));
				}
			}
		}
	}

	private void addProtocolEntry(Composite parent, int index) {
		Entry entry = new Entry(index);
		GridData gridData = null;
		IProxyData proxy = entry.getProxy();
		String name = getLabel(proxy);

		String hostname = proxy.getHost();
		if (hostname == null) {
			hostname = ""; //$NON-NLS-1$
		}

		int portInt = proxy.getPort();
		String port;
		if (portInt == -1) {
			port = ""; //$NON-NLS-1$
		} else {
			port = String.valueOf(portInt);
		}

		entry.nameLabel = new Label(parent, SWT.NONE);
		entry.nameLabel.setText(name);

		entry.hostname = new Text(parent, SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.widthHint = 120;
		entry.hostname.setLayoutData(gridData);
		entry.hostname.setText(hostname);

		entry.portLabel = new Label(parent, SWT.NONE);
		entry.portLabel.setText(NetUIMessages.ProxyPreferencePage_22);

		entry.port = new Text(parent, SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.widthHint = 50;
		entry.port.setLayoutData(gridData);
		entry.port.setText(port);
		entryList[index] = entry;
	}
	
	private String getLabel(IProxyData data) {
		if (data.getType().equals(IProxyData.HTTP_PROXY_TYPE)) {
			return NetUIMessages.ProxyPreferencePage_37;
		}
		if (data.getType().equals(IProxyData.HTTPS_PROXY_TYPE)) {
			return NetUIMessages.ProxyPreferencePage_38;
		}
		if (data.getType().equals(IProxyData.SOCKS_PROXY_TYPE)) {
			return NetUIMessages.ProxyPreferencePage_39;
		}
		return ""; //$NON-NLS-1$
	}

	private GridData newGridData(int span, int verticalIndent,
			boolean horizontal, boolean vertical) {
		int style = 0;

		style |= horizontal ? GridData.FILL_HORIZONTAL : 0;
		style |= vertical ? GridData.FILL_VERTICAL : 0;

		GridData gridData = new GridData(style);

		if (span != -1) {
			gridData.horizontalSpan = span;
		}

		if (verticalIndent != -1) {
			gridData.verticalIndent = verticalIndent;
		}

		return gridData;
	}

	private class Entry {
		Label nameLabel;

		Text hostname;

		String prevHostname;

		Label portLabel;

		Text port;

		int prevPort;

		private int proxyIndex;

		public Entry(int index) {
			this.proxyIndex = index;
		}

		public IProxyData getProxy() {
			return proxyData[proxyIndex];
		}

		public void restoreValues() {
			IProxyData proxy = getProxy();
			prevHostname = proxy.getHost();
			prevPort = proxy.getPort();
		}

		public void applyValues() {
			IProxyData proxy = getProxy();
			boolean enableAuth = enableProxyAuth.getSelection();

			String hostString;
			String portString;
			if (useSameProxyButton.getSelection()) {
				hostString = entryList[0].hostname.getText();
				portString = entryList[0].port.getText();
			} else {
				hostString = hostname.getText();
				portString = port.getText();
			}
			proxy.setHost(hostString);
			try {
				int port = Integer.valueOf(portString).intValue();
				proxy.setPort(port);
			} catch (NumberFormatException e) {
				proxy.setPort(-1);
			}
			proxy.setUserid(enableAuth ? userid.getText() : null);
			proxy.setPassword(enableAuth ? password.getText() : null);
		}
	}
}

Back to the top