KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > ProxyPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12
13 import org.eclipse.jface.dialogs.Dialog;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.jface.preference.PreferencePage;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.*;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.*;
21 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
22 import org.eclipse.ui.*;
23
24 public class ProxyPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
25
26     private Label proxyTypeLabel;
27     private Label proxyHostLabel;
28     private Label proxyPortLabel;
29     private Label proxyUserLabel;
30     private Label proxyPassLabel;
31
32     private Button enableProxy;
33     private Combo proxyTypeCombo;
34     private Text proxyHostText;
35     private Text proxyPortText;
36     private Button enableAuth;
37     private Text proxyUserText;
38     private Text proxyPassText;
39
40   /*
41    * @see PreferencePage#createContents(Composite)
42    */

43   protected Control createContents(Composite parent) {
44       Composite composite = new Composite(parent, SWT.NULL);
45       GridLayout layout = new GridLayout();
46       layout.marginWidth = 0;
47       layout.marginHeight = 0;
48       layout.numColumns = 2;
49       composite.setLayout(layout);
50       composite.setLayoutData(new GridData());
51     
52       GridData data = new GridData();
53       data.horizontalAlignment = GridData.FILL;
54       composite.setLayoutData(data);
55       
56       createProxyPage(composite);
57        
58       initializeDefaults();
59       PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.PROXY_PREFERENCE_PAGE);
60       Dialog.applyDialogFont(parent);
61       return composite;
62     }
63   
64   
65     private void initializeDefaults() {
66         IPreferenceStore store = getPreferenceStore();
67
68         enableProxy.setSelection(store.getBoolean(ICVSUIConstants.PREF_USE_PROXY));
69
70         proxyTypeCombo.select(store.getString(ICVSUIConstants.PREF_PROXY_TYPE).equals(CVSProviderPlugin.PROXY_TYPE_HTTP)? 0:1);
71         proxyHostText.setText(store.getString(ICVSUIConstants.PREF_PROXY_HOST));
72         proxyPortText.setText(store.getString(ICVSUIConstants.PREF_PROXY_PORT));
73         
74         enableAuth.setSelection(store.getBoolean(ICVSUIConstants.PREF_PROXY_AUTH));
75         proxyUserText.setText(CVSProviderPlugin.getPlugin().getProxyUser());
76         proxyPassText.setText(CVSProviderPlugin.getPlugin().getProxyPassword());
77
78         // FIXME
79
updateControls();
80     }
81   
82     /*
83      * @see IWorkbenchPreferencePage#init(IWorkbench)
84      */

85     public void init(IWorkbench workbench) {
86     }
87   
88     /*
89      * @see IPreferencePage#performOk()
90      */

91     public boolean performOk() {
92         IPreferenceStore store = getPreferenceStore();
93         
94         store.setValue(ICVSUIConstants.PREF_USE_PROXY, enableProxy.getSelection());
95         
96         store.setValue(ICVSUIConstants.PREF_PROXY_TYPE, proxyTypeCombo.getText());
97         store.setValue(ICVSUIConstants.PREF_PROXY_HOST, proxyHostText.getText());
98         store.setValue(ICVSUIConstants.PREF_PROXY_PORT, proxyPortText.getText());
99
100         store.setValue(ICVSUIConstants.PREF_PROXY_AUTH, enableAuth.getSelection());
101         
102         CVSProviderPlugin plugin = CVSProviderPlugin.getPlugin();
103         
104         plugin.setUseProxy(enableProxy.getSelection());
105
106         plugin.setProxyType(proxyTypeCombo.getText());
107         plugin.setProxyHost(proxyHostText.getText());
108         plugin.setProxyPort(proxyPortText.getText());
109
110         plugin.setUseProxyAuth(enableAuth.getSelection());
111         plugin.setProxyAuth(proxyUserText.getText(), proxyPassText.getText());
112                 
113         CVSUIPlugin.getPlugin().savePluginPreferences();
114         return super.performOk();
115     }
116     
117     /*
118      * @see PreferencePage#performDefaults()
119      */

120     protected void performDefaults() {
121         super.performDefaults();
122         IPreferenceStore store = getPreferenceStore();
123         store.setToDefault(ICVSUIConstants.PREF_USE_PROXY);
124         store.setToDefault(ICVSUIConstants.PREF_PROXY_TYPE);
125         store.setToDefault(ICVSUIConstants.PREF_PROXY_HOST);
126         store.setToDefault(ICVSUIConstants.PREF_PROXY_PORT);
127         store.setToDefault(ICVSUIConstants.PREF_PROXY_AUTH);
128         CVSProviderPlugin.getPlugin().setProxyAuth("",""); //$NON-NLS-1$ //$NON-NLS-2$
129

130         initializeDefaults();
131     }
132     
133
134     /*
135      * @see PreferencePage#doGetPreferenceStore()
136      */

137     protected IPreferenceStore doGetPreferenceStore() {
138       return CVSUIPlugin.getPlugin().getPreferenceStore();
139     }
140   
141     private void createProxyPage(Composite group) {
142     
143         enableProxy = new Button(group, SWT.CHECK);
144         enableProxy.setText(CVSUIMessages.CVSProxyPreferencePage_enableProxy);
145         GridData gd = new GridData();
146         gd.horizontalSpan = 2;
147         enableProxy.setLayoutData(gd);
148         enableProxy.addSelectionListener(new SelectionAdapter() {
149           public void widgetSelected(SelectionEvent e) {
150             updateControls();
151           }
152         });
153     
154         proxyTypeLabel = new Label(group, SWT.NONE);
155         proxyTypeLabel.setText(CVSUIMessages.CVSProxyPreferencePage_proxyTpe);
156         
157         proxyTypeCombo = new Combo(group, SWT.READ_ONLY);
158         proxyTypeCombo.setFont(group.getFont());
159         gd = new GridData(GridData.FILL_HORIZONTAL);
160         gd.horizontalSpan = 1;
161         proxyTypeCombo.setLayoutData(gd);
162         proxyTypeCombo.add(CVSProviderPlugin.PROXY_TYPE_HTTP);
163         proxyTypeCombo.add(CVSProviderPlugin.PROXY_TYPE_SOCKS5);
164         proxyTypeCombo.select(0);
165         proxyTypeCombo.addModifyListener(new ModifyListener() {
166           public void modifyText(ModifyEvent e) {
167             if(proxyPortText == null)
168               return;
169             Combo combo = (Combo) (e.getSource());
170             String JavaDoc foo = combo.getText();
171             if(foo.equals(CVSProviderPlugin.PROXY_TYPE_HTTP)) {
172               proxyPortText.setText(CVSProviderPlugin.HTTP_DEFAULT_PORT);
173             } else if(foo.equals(CVSProviderPlugin.PROXY_TYPE_SOCKS5)) {
174               proxyPortText.setText(CVSProviderPlugin.SOCKS5_DEFAULT_PORT);
175             }
176           }
177         });
178     
179         proxyHostLabel = new Label(group, SWT.NONE);
180         proxyHostLabel.setText(CVSUIMessages.CVSProxyPreferencePage_proxyHost);
181     
182         proxyHostText = new Text(group, SWT.SINGLE | SWT.BORDER);
183         proxyHostText.setFont(group.getFont());
184         gd = new GridData(GridData.FILL_HORIZONTAL);
185         gd.horizontalSpan = 1;
186         proxyHostText.setLayoutData(gd);
187     
188         proxyPortLabel = new Label(group, SWT.NONE);
189         proxyPortLabel.setText(CVSUIMessages.CVSProxyPreferencePage_proxyPort);
190     
191         proxyPortText = new Text(group, SWT.SINGLE | SWT.BORDER);
192         proxyPortText.setFont(group.getFont());
193         gd = new GridData(GridData.FILL_HORIZONTAL);
194         gd.horizontalSpan = 1;
195         proxyPortText.setLayoutData(gd);
196     
197         proxyPortText.addModifyListener(new ModifyListener() {
198           public void modifyText(ModifyEvent e) {
199             if(isValidPort(proxyPortText.getText())) {
200               setErrorMessage(null);
201             }
202           }
203         });
204     
205         createSpacer(group, 2);
206     
207         enableAuth = new Button(group, SWT.CHECK);
208         enableAuth.setText(CVSUIMessages.CVSProxyPreferencePage_enableProxyAuth);
209         gd = new GridData();
210         gd.horizontalSpan = 2;
211         enableAuth.setLayoutData(gd);
212         enableAuth.addSelectionListener(new SelectionAdapter() {
213           public void widgetSelected(SelectionEvent e) {
214             updateControls();
215           }
216         });
217     
218         proxyUserLabel = new Label(group, SWT.NONE);
219         proxyUserLabel.setText(CVSUIMessages.CVSProxyPreferencePage_proxyUser);
220     
221         proxyUserText = new Text(group, SWT.SINGLE | SWT.BORDER);
222         proxyUserText.setFont(group.getFont());
223         gd = new GridData(GridData.FILL_HORIZONTAL);
224         gd.horizontalSpan = 1;
225         proxyUserText.setLayoutData(gd);
226     
227         proxyPassLabel = new Label(group, SWT.NONE);
228         proxyPassLabel.setText(CVSUIMessages.CVSProxyPreferencePage_proxyPass);
229     
230         proxyPassText = new Text(group, SWT.SINGLE | SWT.BORDER);
231         proxyPassText.setEchoChar('*');
232         proxyPassText.setFont(group.getFont());
233         gd = new GridData(GridData.FILL_HORIZONTAL);
234         gd.horizontalSpan = 1;
235         proxyPassText.setLayoutData(gd);
236     
237         // performDefaults();
238
}
239
240     private boolean isValidPort(String JavaDoc port){
241         int i = -1;
242         try {
243             i = Integer.parseInt(port);
244         } catch (NumberFormatException JavaDoc ee) {
245 // setErrorMessage(Policy.bind("CVSSSH2PreferencePage.103")); //$NON-NLS-1$
246
// return false;
247
}
248         if(i < 0 || i > 65535){
249             setErrorMessage(CVSUIMessages.CVSProxyPreferencePage_proxyPortError);
250             return false;
251         }
252         return true;
253     }
254     
255     protected void createSpacer(Composite composite, int columnSpan) {
256       Label label = new Label(composite, SWT.NONE);
257       GridData gd = new GridData();
258       gd.horizontalSpan = columnSpan;
259       label.setLayoutData(gd);
260     }
261     
262     private void updateControls() {
263       boolean enable = enableProxy.getSelection();
264       proxyTypeLabel.setEnabled(enable);
265       proxyTypeCombo.setEnabled(enable);
266       proxyPortLabel.setEnabled(enable);
267       proxyPortText.setEnabled(enable);
268       proxyHostLabel.setEnabled(enable);
269       proxyHostText.setEnabled(enable);
270
271       enableAuth.setEnabled(enable);
272       enable&=enableAuth.getSelection();
273       proxyUserLabel.setEnabled(enable);
274       proxyUserText.setEnabled(enable);
275       proxyPassLabel.setEnabled(enable);
276       proxyPassText.setEnabled(enable);
277     }
278     
279 }
280
Popular Tags