KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > preferences > MainPreferencePage


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

11 package org.eclipse.update.internal.ui.preferences;
12
13 import java.net.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.dialogs.*;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.preference.PreferencePage;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.BusyIndicator;
21 import org.eclipse.swt.events.*;
22 import org.eclipse.swt.layout.*;
23 import org.eclipse.swt.widgets.*;
24 import org.eclipse.ui.*;
25 import org.eclipse.update.core.SiteManager;
26 import org.eclipse.update.internal.core.UpdateCore;
27 import org.eclipse.update.internal.operations.UpdateUtils;
28 import org.eclipse.update.internal.ui.UpdateUI;
29 import org.eclipse.update.internal.ui.UpdateUIMessages;
30
31 /**
32  * Insert the type's description here.
33  * @see PreferencePage
34  */

35 public class MainPreferencePage
36     extends PreferencePage
37     implements IWorkbenchPreferencePage {
38
39     //private Label historySizeLabel;
40
private Text historySizeText;
41     private Button checkSignatureCheckbox;
42     private Button automaticallyChooseMirrorCheckbox;
43     private Button equivalentButton;
44     private Button compatibleButton;
45     private Text updatePolicyText;
46
47     // these two values are for compatibility with old code
48
public static final String JavaDoc EQUIVALENT_VALUE = "equivalent"; //$NON-NLS-1$
49
public static final String JavaDoc COMPATIBLE_VALUE = "compatible"; //$NON-NLS-1$
50

51     /**
52      * The constructor.
53      */

54     public MainPreferencePage() {
55         super();
56     }
57
58     /**
59      * Insert the method's description here.
60      */

61     public void init(IWorkbench workbench) {
62     }
63
64     /* (non-Javadoc)
65      * Method declared on PreferencePage.
66      */

67     protected Control createContents(Composite parent) {
68         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, "org.eclipse.update.ui.MainPreferencePage"); //$NON-NLS-1$
69

70         Composite mainComposite = new Composite(parent, SWT.NULL);
71         mainComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
72         GridLayout layout = new GridLayout();
73         layout.marginHeight = 0;
74         layout.marginWidth = 0;
75         layout.numColumns = 2;
76         mainComposite.setLayout(layout);
77
78         Label historySizeLabel = new Label(mainComposite, SWT.NONE);
79         historySizeLabel.setText(UpdateUIMessages.MainPreferencePage_historySize);
80         historySizeText = new Text(mainComposite, SWT.SINGLE | SWT.BORDER);
81         historySizeText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
82         historySizeText.addModifyListener(new ModifyListener() {
83             public void modifyText(ModifyEvent e) {
84                 historySizeChanged();
85             }
86         });
87
88         checkSignatureCheckbox =
89             new Button(mainComposite, SWT.CHECK | SWT.LEFT);
90         checkSignatureCheckbox.setText(UpdateUIMessages.MainPreferencePage_checkSignature);
91         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
92         gd.horizontalSpan = 2;
93         checkSignatureCheckbox.setLayoutData(gd);
94         checkSignatureCheckbox.addSelectionListener(new SelectionAdapter() {
95             public void widgetSelected(SelectionEvent e) {
96                 if (checkSignatureCheckbox.getSelection() == false) {
97                     warnSignatureCheck(getShell());
98                 }
99             }
100         });
101         
102         automaticallyChooseMirrorCheckbox =
103             new Button(mainComposite, SWT.CHECK | SWT.LEFT);
104         automaticallyChooseMirrorCheckbox.setText(UpdateUIMessages.MainPreferencePage_automaticallyChooseMirror);
105         gd = new GridData();
106         gd.horizontalSpan = 2;
107         automaticallyChooseMirrorCheckbox.setLayoutData(gd);
108
109         createSpacer(mainComposite, 2);
110
111         Group group = new Group(mainComposite, SWT.NONE);
112         group.setText(UpdateUIMessages.MainPreferencePage_updateVersions);
113         group.setLayout(new GridLayout());
114         gd = new GridData(GridData.FILL_HORIZONTAL);
115         gd.horizontalSpan = 2;
116         group.setLayoutData(gd);
117
118         equivalentButton = new Button(group, SWT.RADIO);
119         equivalentButton.setText(UpdateUIMessages.MainPreferencePage_updateVersions_equivalent);
120
121         compatibleButton = new Button(group, SWT.RADIO);
122         compatibleButton.setText(UpdateUIMessages.MainPreferencePage_updateVersions_compatible);
123
124         createSpacer(mainComposite, 2);
125
126         group = new Group(mainComposite, SWT.NONE);
127         group.setText(UpdateUIMessages.MainPreferencePage_updatePolicy);
128         layout = new GridLayout();
129         layout.numColumns = 2;
130         group.setLayout(layout);
131         gd = new GridData(GridData.FILL_HORIZONTAL);
132         gd.horizontalSpan = 2;
133         group.setLayoutData(gd);
134
135         Label label = new Label(group, SWT.NULL);
136         label.setText(UpdateUIMessages.MainPreferencePage_updatePolicyURL);
137         updatePolicyText = new Text(group, SWT.SINGLE | SWT.BORDER);
138
139         gd = new GridData(GridData.FILL_HORIZONTAL);
140         updatePolicyText.setLayoutData(gd);
141
142         //createSpacer(mainComposite, 2);
143
//createHttpProxy(mainComposite, 2);
144
initialize();
145         updatePolicyText.addModifyListener(new ModifyListener() {
146             public void modifyText(ModifyEvent e) {
147                 textChanged();
148             }
149         });
150         return mainComposite;
151     }
152
153     public void createControl(Composite parent) {
154         super.createControl(parent);
155         Dialog.applyDialogFont(getControl());
156     }
157
158     protected void createSpacer(Composite composite, int columnSpan) {
159         Label label = new Label(composite, SWT.NONE);
160         GridData gd = new GridData();
161         gd.horizontalSpan = columnSpan;
162         label.setLayoutData(gd);
163     }
164
165     protected void createHttpProxy(Composite composite, int columnSpan) {
166         Group group = new Group(composite, SWT.NONE);
167         group.setText(UpdateUIMessages.MainPreferencePage_proxyGroup);
168         GridLayout layout = new GridLayout();
169         layout.numColumns = 2;
170         group.setLayout(layout);
171         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
172         gd.horizontalSpan = columnSpan;
173         group.setLayoutData(gd);
174     }
175
176     private int getHistoryCount() {
177         try {
178             Integer JavaDoc count = new Integer JavaDoc(historySizeText.getText());
179             return count.intValue();
180         } catch (NumberFormatException JavaDoc e) {
181         }
182         return UpdateCore.getPlugin().getPluginPreferences().getDefaultInt(
183             UpdateCore.P_HISTORY_SIZE);
184     }
185     
186     private void historySizeChanged() {
187         
188         try {
189             int historySize = Integer.parseInt(historySizeText.getText());
190             if (historySize < 0) {
191                 setValid(false);
192                 setErrorMessage(UpdateUIMessages.MainPreferencePage_invalidHistorySize);
193                 return;
194             }
195         } catch (NumberFormatException JavaDoc e) {
196             setValid(false);
197             setErrorMessage(UpdateUIMessages.MainPreferencePage_invalidHistorySize);
198             return;
199         }
200         setValid(true);
201         setErrorMessage(null);
202     }
203
204     public boolean performOk() {
205         BusyIndicator.showWhile(getControl().getDisplay(), new Runnable JavaDoc() {
206             public void run() {
207                 try {
208                     SiteManager.getLocalSite().setMaximumHistoryCount(
209                         getHistoryCount());
210                 } catch (CoreException e) {
211                     UpdateUI.logException(e);
212                 }
213             }
214         });
215
216         Preferences prefs = UpdateCore.getPlugin().getPluginPreferences();
217         prefs.setValue(
218             UpdateCore.P_CHECK_SIGNATURE,
219             checkSignatureCheckbox.getSelection());
220         prefs.setValue(UpdateCore.P_AUTOMATICALLY_CHOOSE_MIRROR, automaticallyChooseMirrorCheckbox.getSelection());
221         prefs.setValue(UpdateCore.P_HISTORY_SIZE, historySizeText.getText());
222         prefs.setValue(
223             UpdateCore.P_UPDATE_VERSIONS,
224             equivalentButton.getSelection()
225                 ? EQUIVALENT_VALUE
226                 : COMPATIBLE_VALUE);
227         prefs.setValue(
228             UpdateUtils.P_UPDATE_POLICY_URL,
229             updatePolicyText.getText());
230
231         UpdateCore.getPlugin().savePluginPreferences();
232         return super.performOk();
233     }
234
235
236     private void initialize() {
237         Preferences prefs = UpdateCore.getPlugin().getPluginPreferences();
238
239         checkSignatureCheckbox.setSelection(
240             prefs.getBoolean(UpdateCore.P_CHECK_SIGNATURE));
241         automaticallyChooseMirrorCheckbox.setSelection(
242                 prefs.getBoolean(UpdateCore.P_AUTOMATICALLY_CHOOSE_MIRROR));
243
244         historySizeText.setText(prefs.getString(UpdateCore.P_HISTORY_SIZE));
245
246         boolean isCompatible =
247             UpdateCore.COMPATIBLE_VALUE.equals(
248                 prefs.getString(UpdateCore.P_UPDATE_VERSIONS));
249         equivalentButton.setSelection(!isCompatible);
250         compatibleButton.setSelection(isCompatible);
251
252         String JavaDoc text = prefs.getString(UpdateUtils.P_UPDATE_POLICY_URL);
253         updatePolicyText.setText(text);
254     }
255
256     private void textChanged() {
257         String JavaDoc text = updatePolicyText.getText();
258         if (text.length() > 0) {
259             try {
260                 new URL(text);
261             } catch (MalformedURLException e) {
262                 setValid(false);
263                 setErrorMessage(UpdateUIMessages.UpdateSettingsPreferencePage_invalid);
264                 return;
265             }
266         }
267         setValid(true);
268         setErrorMessage(null);
269     }
270
271     public void performDefaults() {
272         super.performDefaults();
273         Preferences prefs = UpdateCore.getPlugin().getPluginPreferences();
274
275         updatePolicyText.setText(""); //$NON-NLS-1$
276

277         checkSignatureCheckbox.setSelection(true);
278         automaticallyChooseMirrorCheckbox.setSelection(false);
279         historySizeText.setText(
280             prefs.getDefaultString(UpdateCore.P_HISTORY_SIZE));
281         
282         equivalentButton.setSelection(true);
283         compatibleButton.setSelection(false);
284     }
285
286     private void warnSignatureCheck(Shell shell) {
287         MessageDialog.openWarning(shell, UpdateUIMessages.MainPreferencePage_digitalSignature_title,
288         UpdateUIMessages.MainPreferencePage_digitalSignature_message);
289     }
290 }
291
Popular Tags