KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > net > ProxyPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * yyyymmdd bug Email and other contact information
11  * -------- -------- -----------------------------------------------------------
12  * 20070123 154100 eu@md.pp.ru - Eugene Kuleshov, Initial UI coding
13  * 20070201 154100 pmoogk@ca.ibm.com - Peter Moogk, Port internet code from WTP to Eclipse base.
14  * 20070219 174674 pmoogk@ca.ibm.com - Peter Moogk
15  * 20070402 180622 brockj@tpg.com.au - Brock Janiczak, Inconsistent enablement states in network preference page
16  * 20070416 177897 ymnk@jcraft.com - Atsuhiko Yamanaka, Improve UI of Proxy Preferences Page
17  *******************************************************************************/

18 package org.eclipse.ui.internal.net;
19
20 import org.eclipse.core.net.proxy.IProxyData;
21 import org.eclipse.core.net.proxy.IProxyService;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.jface.dialogs.ErrorDialog;
24 import org.eclipse.jface.preference.PreferencePage;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.ModifyEvent;
27 import org.eclipse.swt.events.ModifyListener;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.ui.IWorkbench;
38 import org.eclipse.ui.IWorkbenchPreferencePage;
39 import org.eclipse.ui.PlatformUI;
40
41 public class ProxyPreferencePage extends PreferencePage implements
42         IWorkbenchPreferencePage {
43
44     private static final String JavaDoc PROXY_PREFERENCE_PAGE_CONTEXT_ID
45         = "org.eclipse.ui.net.proxy_preference_page_context"; //$NON-NLS-1$
46

47     Entry[] entryList;
48     Button directConnectionToButton;
49     private Button manualProxyConfigurationButton;
50     Button useSameProxyButton;
51     private Label nonHostLabel;
52     private NonProxyHostsComposite nonHostComposite;
53     Button enableProxyAuth;
54     private Label useridLabel;
55     private Label passwordLabel;
56     Text userid;
57     Text password;
58     private IProxyService proxyService;
59
60     public ProxyPreferencePage() {
61         super(NetUIMessages.ProxyPreferencePage_2);
62         setPreferenceStore(Activator.getDefault().getPreferenceStore());
63     }
64
65     public void init(IWorkbench workbench) {
66         // Nothing to do
67
}
68
69     protected Control createContents(Composite parent) {
70
71         proxyService = Activator.getDefault().getProxyService();
72         if (proxyService == null) {
73             Label l = new Label(parent, SWT.NONE);
74             l.setText(NetUIMessages.ProxyPreferencePage_40);
75             return l;
76         }
77
78         IProxyData[] proxyData = proxyService.getProxyData();
79         entryList = new Entry[proxyData.length];
80         for (int i = 0; i < proxyData.length; i++) {
81             IProxyData pd = proxyData[i];
82             entryList[i] = new Entry(pd);
83         }
84
85         Composite composite = new Composite(parent, SWT.NONE);
86         GridLayout layout = new GridLayout();
87         layout.numColumns = 1;
88         layout.marginRight = 5;
89         layout.marginTop = 5;
90         layout.marginWidth = 0;
91         composite.setLayout(layout);
92         GridData data = new GridData(GridData.FILL_BOTH);
93         composite.setLayoutData(data);
94
95         directConnectionToButton = new Button(composite, SWT.RADIO);
96         directConnectionToButton.setLayoutData(new GridData());
97         directConnectionToButton.setText(NetUIMessages.ProxyPreferencePage_3);
98         directConnectionToButton
99                 .setToolTipText(NetUIMessages.ProxyPreferencePage_1);
100         directConnectionToButton.addSelectionListener(new SelectionAdapter() {
101             public void widgetSelected(SelectionEvent e) {
102                 enableControls(!directConnectionToButton.getSelection());
103             }
104         });
105
106         manualProxyConfigurationButton = new Button(composite, SWT.RADIO);
107         manualProxyConfigurationButton
108                 .setText(NetUIMessages.ProxyPreferencePage_4);
109         manualProxyConfigurationButton
110                 .setToolTipText(NetUIMessages.ProxyPreferencePage_0);
111
112         final Composite manualProxyConfigurationComposite = new Composite(
113                 composite, SWT.NONE);
114         final GridLayout gridLayout = new GridLayout();
115         gridLayout.marginWidth = 0;
116         gridLayout.marginHeight = 0;
117         gridLayout.numColumns = 4;
118         manualProxyConfigurationComposite.setLayout(gridLayout);
119         final GridData gridData_2 = new GridData(GridData.FILL_BOTH);
120         gridData_2.horizontalIndent = 17;
121         manualProxyConfigurationComposite.setLayoutData(gridData_2);
122
123         entryList[0].addProtocolEntry(manualProxyConfigurationComposite);
124
125         new Label(manualProxyConfigurationComposite, SWT.NONE);
126         useSameProxyButton = new Button(manualProxyConfigurationComposite,
127                 SWT.CHECK);
128         useSameProxyButton.setText(NetUIMessages.ProxyPreferencePage_5);
129         GridData gridData = new GridData();
130         gridData.horizontalSpan = 3;
131         useSameProxyButton.setLayoutData(gridData);
132         useSameProxyButton.setToolTipText(NetUIMessages.ProxyPreferencePage_23);
133         useSameProxyButton.addSelectionListener(new SelectionAdapter() {
134             public void widgetSelected(SelectionEvent e) {
135                 toggleUseSameProtocol();
136             }
137         });
138
139         for (int index = 1; index < entryList.length; index++) {
140             entryList[index]
141                     .addProtocolEntry(manualProxyConfigurationComposite);
142         }
143
144         Label separator = new Label(manualProxyConfigurationComposite,
145                 SWT.HORIZONTAL | SWT.SEPARATOR);
146         separator.setLayoutData(newGridData(4, 5, true, false));
147         nonHostLabel = new Label(manualProxyConfigurationComposite, SWT.NONE);
148         nonHostLabel.setText(NetUIMessages.ProxyPreferencePage_6);
149         nonHostLabel.setToolTipText(NetUIMessages.ProxyPreferencePage_24);
150         nonHostLabel.setLayoutData(newGridData(4, 5, true, false));
151         nonHostComposite = new NonProxyHostsComposite(
152                 manualProxyConfigurationComposite, SWT.NONE);
153         nonHostComposite.setLayoutData(newGridData(4, -1, true, true));
154
155         Label separator2 = new Label(manualProxyConfigurationComposite,
156                 SWT.HORIZONTAL | SWT.SEPARATOR);
157         separator2.setLayoutData(newGridData(4, 5, true, false));
158         enableProxyAuth = new Button(manualProxyConfigurationComposite,
159                 SWT.CHECK);
160         enableProxyAuth.setText(NetUIMessages.ProxyPreferencePage_7);
161         enableProxyAuth.setLayoutData(newGridData(4, 5, true, false));
162         enableProxyAuth.setToolTipText(NetUIMessages.ProxyPreferencePage_25);
163         enableProxyAuth.addSelectionListener(new SelectionAdapter() {
164             public void widgetSelected(SelectionEvent e) {
165                 enableUseridPassword(enableProxyAuth.getSelection());
166             }
167         });
168
169         final Composite userIdPassword = new Composite(
170                 manualProxyConfigurationComposite, SWT.NONE);
171         GridLayout layout2 = new GridLayout(2, false);
172         layout2.marginWidth = 0;
173         layout2.marginHeight = 0;
174         userIdPassword.setLayoutData(newGridData(4, -1, true, false));
175         userIdPassword.setLayout(layout2);
176
177         useridLabel = new Label(userIdPassword, SWT.NONE);
178         useridLabel.setText(NetUIMessages.ProxyPreferencePage_8);
179         userid = new Text(userIdPassword, SWT.BORDER);
180         passwordLabel = new Label(userIdPassword, SWT.NONE);
181         passwordLabel.setText(NetUIMessages.ProxyPreferencePage_9);
182         password = new Text(userIdPassword, SWT.BORDER);
183
184         userid.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
185         userid.setToolTipText(NetUIMessages.ProxyPreferencePage_26);
186         password.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
187         password.setToolTipText(NetUIMessages.ProxyPreferencePage_27);
188         password.setEchoChar('*');
189
190         ModifyListener modifyListener = new ModifyListener() {
191             public void modifyText(ModifyEvent e) {
192                 if (useSameProxyButton.getSelection()) {
193                     for (int index = 1; index < entryList.length; index++) {
194                         entryList[index].copyValuesFrom(entryList[0]);
195                     }
196                 }
197             }
198         };
199
200         entryList[0].hostname.addModifyListener(modifyListener);
201         entryList[0].port.addModifyListener(modifyListener);
202
203         initializeValues(proxyService.isProxiesEnabled());
204         applyDialogFont(composite);
205         
206         // F1
207
PlatformUI.getWorkbench().getHelpSystem().setHelp(
208                 getControl(), PROXY_PREFERENCE_PAGE_CONTEXT_ID);
209
210         return composite;
211     }
212
213     protected void performApply() {
214         if (proxyService == null)
215             return;
216         boolean proxiesEnabled = manualProxyConfigurationButton.getSelection();
217
218         // Save the contents of the text fields to the proxy data.
219
IProxyData[] proxyData = new IProxyData[entryList.length];
220         for (int index = 0; index < entryList.length; index++) {
221             entryList[index].applyValues();
222             proxyData[index] = entryList[index].getProxy();
223         }
224
225         proxyService.setProxiesEnabled(proxiesEnabled);
226         if (proxiesEnabled) {
227             try {
228                 proxyService.setProxyData(proxyData);
229                 proxyService.setNonProxiedHosts(
230                         nonHostComposite.getList());
231             } catch (CoreException e) {
232                 ErrorDialog.openError(getShell(), null, null, e.getStatus());
233             }
234         }
235         Activator.getDefault().savePluginPreferences();
236     }
237
238     protected void performDefaults() {
239         directConnectionToButton.setSelection(true);
240         manualProxyConfigurationButton.setSelection(false);
241         useSameProxyButton.setSelection(false);
242         enableProxyAuth.setSelection(false);
243
244         for (int index = 0; index < entryList.length; index++) {
245             entryList[index].reset();
246         }
247
248         nonHostComposite.setList(new String JavaDoc[] { "localhost", "127.0.0.1" }); //$NON-NLS-1$ //$NON-NLS-2$
249
userid.setText(""); //$NON-NLS-1$
250
password.setText(""); //$NON-NLS-1$
251
enableControls(false);
252     }
253
254     public boolean performOk() {
255         performApply();
256         return super.performOk();
257     }
258
259     /**
260      * This method is run once when when this preference page is displayed. It
261      * will restore the state of this page using previously saved preferences.
262      *
263      * @param proxiesEnabled indicates if manual proxies are enabled or not.
264      */

265     private void initializeValues(boolean proxiesEnabled) {
266
267         directConnectionToButton.setSelection(!proxiesEnabled);
268         manualProxyConfigurationButton.setSelection(proxiesEnabled);
269
270         String JavaDoc[] nonHostLists = null;
271         if (proxyService != null)
272             nonHostLists = proxyService.getNonProxiedHosts();
273         this.nonHostComposite.setList(nonHostLists == null ? new String JavaDoc[] {
274                 "localhost", "127.0.0.1" } : nonHostLists); //$NON-NLS-1$ //$NON-NLS-2$
275
if (!proxiesEnabled) {
276             this.useSameProxyButton.setSelection(false);
277             this.enableProxyAuth.setSelection(false);
278             this.userid.setText(""); //$NON-NLS-1$
279
this.password.setText(""); //$NON-NLS-1$
280
} else {
281             IProxyData data = entryList[0].getProxy();
282             this.enableProxyAuth.setSelection(data.isRequiresAuthentication());
283             this.userid.setText(data.getUserId() == null ? "" : data //$NON-NLS-1$
284
.getUserId());
285             this.password.setText(data.getPassword() == null ? "" : data //$NON-NLS-1$
286
.getPassword());
287
288         }
289         boolean useSameProtocol = true;
290         for (int i = 1; i < entryList.length; i++) {
291             Entry entry = entryList[i];
292             if (!entry.isUseSameProtocol(entryList[0])) {
293                 useSameProtocol = false;
294                 break;
295             }
296         }
297         this.useSameProxyButton.setSelection(useSameProtocol);
298         for (int index = 1; index < entryList.length; index++) {
299             Entry entry = entryList[index];
300             entry.loadPreviousValues();
301             entry.updateEnablement(proxiesEnabled, useSameProtocol);
302         }
303
304         enableControls(proxiesEnabled);
305     }
306
307     void enableControls(boolean enabled) {
308         for (int index = 0; index < entryList.length; index++) {
309             entryList[index].updateEnablement(enabled, useSameProxyButton.getSelection());
310         }
311
312         useSameProxyButton.setEnabled(enabled);
313         nonHostLabel.setEnabled(enabled);
314         nonHostComposite.enableComposite(enabled);
315         enableProxyAuth.setEnabled(enabled);
316
317         enableUseridPassword(enableProxyAuth.getSelection() && enabled);
318     }
319
320     void enableUseridPassword(boolean enabled) {
321         useridLabel.setEnabled(enabled);
322         userid.setEnabled(enabled);
323         passwordLabel.setEnabled(enabled);
324         password.setEnabled(enabled);
325     }
326
327     protected void toggleUseSameProtocol() {
328         boolean useSameProtocol = useSameProxyButton.getSelection();
329         for (int index = 1; index < entryList.length; index++) {
330             entryList[index].setUseSameProtocol(useSameProtocol, entryList[0]);
331         }
332     }
333     
334     public void updateErrorMessage() {
335         for (int index = 0; index < entryList.length; index++) {
336             String JavaDoc message = entryList[index].getErrorMessage();
337             if (message != null) {
338                 setErrorMessage(message);
339                 return;
340             }
341         }
342         setErrorMessage(null);
343     }
344
345     private GridData newGridData(int span, int verticalIndent,
346             boolean horizontal, boolean vertical) {
347         int style = 0;
348
349         style |= horizontal ? GridData.FILL_HORIZONTAL : 0;
350         style |= vertical ? GridData.FILL_VERTICAL : 0;
351
352         GridData gridData = new GridData(style);
353
354         if (span != -1) {
355             gridData.horizontalSpan = span;
356         }
357
358         if (verticalIndent != -1) {
359             gridData.verticalIndent = verticalIndent;
360         }
361
362         return gridData;
363     }
364
365     private class Entry {
366         Label nameLabel;
367         Text hostname;
368         Label portLabel;
369         Text port;
370         String JavaDoc prevHostname;
371         int prevPort;
372         private final IProxyData proxyData;
373         private String JavaDoc errorMessage;
374
375         public Entry(IProxyData proxyData) {
376             this.proxyData = proxyData;
377         }
378
379         public void updateEnablement(boolean proxiesEnabled, boolean useSameProtocol) {
380             if (isHttpProxy()) {
381                 setEnabled(proxiesEnabled);
382             } else {
383                 if (proxiesEnabled) {
384                     setUseSameProtocol(useSameProtocol);
385                 } else {
386                     setEnabled(false);
387                 }
388             }
389             updateMessage();
390         }
391
392         public void setUseSameProtocol(boolean useSameProtocol, Entry httpEntry) {
393             setUseSameProtocol(useSameProtocol);
394             if (isSocksProxy() || isHttpProxy())
395                 return;
396             if (useSameProtocol) {
397                 recordPreviousValues();
398                 copyValuesFrom(entryList[0]);
399             } else {
400                 restorePreviousValues();
401             }
402         }
403         
404         public void setUseSameProtocol(boolean useSameProtocol) {
405             if (isSocksProxy()) {
406                 setEnabled(true);
407             } else {
408                 setEnabled(!useSameProtocol);
409             }
410         }
411
412         public void loadPreviousValues() {
413             IProxyData proxy = getProxy();
414             prevHostname = proxy.getHost();
415             prevPort = proxy.getPort();
416         }
417         
418         public void restorePreviousValues() {
419             hostname.setText(prevHostname);
420             port.setText(prevPort == -1 ? "" : String //$NON-NLS-1$
421
.valueOf(prevPort));
422         }
423
424         public void recordPreviousValues() {
425             prevHostname = hostname.getText();
426             try {
427                 prevPort = Integer.parseInt(port.getText());
428             } catch (NumberFormatException JavaDoc e) {
429                 prevPort = -1;
430             }
431         }
432
433         /**
434          * The user has chosen to use the same entry for all protocols
435          * @param entry the entry (the http entry to be exact)
436          */

437         public void copyValuesFrom(Entry entry) {
438             // For SOCKS, don't use the general entry
439
if (!isSocksProxy()){
440                 hostname.setText( entry.hostname.getText() );
441                 port.setText( entry.port.getText() );
442             }
443         }
444         
445         public boolean isUseSameProtocol(Entry entry) {
446             // Always answer true for the SOCKS proxy since we never disable
447
if (isSocksProxy())
448                 return true;
449             return (hostsEqual(proxyData.getHost(), entry.getProxy().getHost())
450                     && portsEqual(proxyData.getPort(), entry.getProxy().getPort()));
451         }
452
453         private boolean portsEqual(int port1, int port2) {
454             return port1 == port2;
455         }
456
457         private boolean hostsEqual(String JavaDoc host1, String JavaDoc host2) {
458             if (host1 == host2)
459                 // If there are no hosts, don't enable the use same hosts button
460
return false;
461             if (host1 == null || host2 == null)
462                 return false;
463             return host1.equals(host2);
464         }
465         
466         boolean isSocksProxy() {
467             return getProxy().getType().equals(IProxyData.SOCKS_PROXY_TYPE);
468         }
469         
470         boolean isHttpProxy() {
471             return getProxy().getType().equals(IProxyData.HTTP_PROXY_TYPE);
472         }
473         
474         boolean isHttpsProxy() {
475             return getProxy().getType().equals(IProxyData.HTTPS_PROXY_TYPE);
476         }
477
478         public IProxyData getProxy() {
479             return proxyData;
480         }
481
482         public boolean updateMessage() {
483             if (hostname.isEnabled()) {
484                 if (isSocksProxy() || isHttpProxy()|| !useSameProxyButton.getSelection()) {
485                     String JavaDoc hostString = hostname.getText();
486                     if (hostString.startsWith(" ") || hostString.endsWith(" ")) { //$NON-NLS-1$ //$NON-NLS-2$
487
setErrorMessage(NetUIMessages.ProxyPreferencePage_41);
488                         return false;
489                     }
490                     String JavaDoc portString = port.getText();
491                     if (portString.length() > 0) {
492                         try {
493                             int port = Integer.valueOf(portString).intValue();
494                             if (port < 0) {
495                                 setErrorMessage(NetUIMessages.ProxyPreferencePage_42);
496                                 return false;
497                             }
498                         } catch (NumberFormatException JavaDoc e) {
499                             setErrorMessage(NetUIMessages.ProxyPreferencePage_43);
500                             return false;
501                         }
502                     }
503                 }
504             }
505             setErrorMessage(null);
506             return true;
507         }
508         
509         private void setErrorMessage(String JavaDoc message) {
510             errorMessage = message;
511             updateErrorMessage();
512         }
513         
514         public void applyValues() {
515             IProxyData proxy = getProxy();
516             boolean enableAuth = enableProxyAuth.getSelection();
517
518             String JavaDoc hostString;
519             String JavaDoc portString;
520             if (useSameProxyButton.getSelection() && !isSocksProxy()) {
521                 hostString = entryList[0].hostname.getText();
522                 portString = entryList[0].port.getText();
523             } else {
524                 hostString = hostname.getText();
525                 portString = port.getText();
526             }
527             
528             proxy.setHost(hostString);
529             try {
530                 int port = Integer.valueOf(portString).intValue();
531                 proxy.setPort(port);
532             } catch (NumberFormatException JavaDoc e) {
533                 proxy.setPort(-1);
534             }
535             proxy.setUserid(enableAuth ? userid.getText() : null);
536             proxy.setPassword(enableAuth ? password.getText() : null);
537         }
538         
539         public void addProtocolEntry(Composite parent) {
540             GridData gridData;
541             IProxyData proxy = getProxy();
542
543             nameLabel = new Label(parent, SWT.NONE);
544             nameLabel.setText(getLabel(proxy));
545
546             hostname = new Text(parent, SWT.BORDER);
547             gridData = new GridData(GridData.FILL_HORIZONTAL);
548             gridData.widthHint = 120;
549             hostname.setLayoutData(gridData);
550             hostname.setText(getHostName(proxy));
551             hostname.addModifyListener(new ModifyListener() {
552                 public void modifyText(ModifyEvent e) {
553                     Entry.this.updateMessage();
554                 }
555             });
556
557             portLabel = new Label(parent, SWT.NONE);
558             portLabel.setText(NetUIMessages.ProxyPreferencePage_22);
559
560             port = new Text(parent, SWT.BORDER);
561             gridData = new GridData();
562             gridData.widthHint = 50;
563             port.setLayoutData(gridData);
564             port.setText(getPortString(proxy));
565             port.addModifyListener(new ModifyListener() {
566                 public void modifyText(ModifyEvent e) {
567                     Entry.this.updateMessage();
568                 }
569             });
570         }
571
572         private String JavaDoc getLabel(IProxyData data) {
573             if (data.getType().equals(IProxyData.HTTP_PROXY_TYPE)) {
574                 return NetUIMessages.ProxyPreferencePage_37;
575             }
576             if (data.getType().equals(IProxyData.HTTPS_PROXY_TYPE)) {
577                 return NetUIMessages.ProxyPreferencePage_38;
578             }
579             if (data.getType().equals(IProxyData.SOCKS_PROXY_TYPE)) {
580                 return NetUIMessages.ProxyPreferencePage_39;
581             }
582             return ""; //$NON-NLS-1$
583
}
584         
585         private String JavaDoc getPortString(IProxyData proxy) {
586             int portInt = proxy.getPort();
587             String JavaDoc portString;
588             if (portInt == -1) {
589                 portString = ""; //$NON-NLS-1$
590
} else {
591                 portString = String.valueOf(portInt);
592             }
593             return portString;
594         }
595
596         private String JavaDoc getHostName(IProxyData proxy) {
597             String JavaDoc hostnameString = proxy.getHost();
598             if (hostnameString == null) {
599                 hostnameString = ""; //$NON-NLS-1$
600
}
601             return hostnameString;
602         }
603         
604         public void reset() {
605             hostname.setText(""); //$NON-NLS-1$
606
port.setText(""); //$NON-NLS-1$
607
prevHostname = ""; //$NON-NLS-1$
608
prevPort = -1;
609         }
610         
611         private void setEnabled(boolean enabled) {
612             hostname.setEnabled(enabled);
613             nameLabel.setEnabled(enabled);
614             portLabel.setEnabled(enabled);
615             port.setEnabled(enabled);
616         }
617
618         public String JavaDoc getErrorMessage() {
619             return errorMessage;
620         }
621     }
622 }
623
Popular Tags