KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > scheduler > preferences > AutomaticUpdatesPreferencePage


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.update.internal.scheduler.preferences;
12
13 import org.eclipse.core.runtime.Preferences;
14 import org.eclipse.jface.dialogs.Dialog;
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.*;
19 import org.eclipse.swt.widgets.*;
20 import org.eclipse.ui.*;
21 import org.eclipse.update.internal.scheduler.*;
22 import org.eclipse.update.internal.scheduler.UpdateSchedulerPlugin;
23
24 public class AutomaticUpdatesPreferencePage
25     extends PreferencePage
26     implements IWorkbenchPreferencePage {
27
28     private Button enabledCheck;
29     private Button onStartupRadio;
30     private Button onScheduleRadio;
31     private Combo dayCombo;
32     private Label atLabel;
33     private Combo hourCombo;
34     private Button searchOnlyRadio;
35     private Button searchAndDownloadRadio;
36     private Group updateScheduleGroup;
37     private Group downloadGroup;
38
39     public void init(IWorkbench workbench) {
40     }
41
42     protected Control createContents(Composite parent) {
43         Composite container = new Composite(parent, SWT.NULL);
44         GridLayout layout = new GridLayout();
45         layout.marginWidth = layout.marginHeight = 0;
46         container.setLayout(layout);
47
48         enabledCheck = new Button(container, SWT.CHECK);
49         enabledCheck.setText(UpdateSchedulerMessages.AutomaticUpdatesPreferencePage_findUpdates);
50
51         createSpacer(container, 1);
52
53         updateScheduleGroup = new Group(container, SWT.NONE);
54         updateScheduleGroup.setText(UpdateSchedulerMessages.AutomaticUpdatesPreferencePage_UpdateSchedule);
55         layout = new GridLayout();
56         layout.numColumns = 3;
57         updateScheduleGroup.setLayout(layout);
58         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
59         updateScheduleGroup.setLayoutData(gd);
60
61         onStartupRadio = new Button(updateScheduleGroup, SWT.RADIO);
62         onStartupRadio.setText(
63             UpdateSchedulerMessages.AutomaticUpdatesPreferencePage_findOnStart);
64         gd = new GridData();
65         gd.horizontalSpan = 3;
66         onStartupRadio.setLayoutData(gd);
67         onStartupRadio.addSelectionListener(new SelectionAdapter() {
68             public void widgetSelected(SelectionEvent e) {
69                 pageChanged();
70             }
71         });
72
73         onScheduleRadio = new Button(updateScheduleGroup, SWT.RADIO);
74         onScheduleRadio.setText(UpdateSchedulerMessages.AutomaticUpdatesPreferencePage_findOnSchedule);
75         gd = new GridData();
76         gd.horizontalSpan = 3;
77         onScheduleRadio.setLayoutData(gd);
78         onScheduleRadio.addSelectionListener(new SelectionAdapter() {
79             public void widgetSelected(SelectionEvent e) {
80                 pageChanged();
81             }
82         });
83
84         dayCombo = new Combo(updateScheduleGroup, SWT.READ_ONLY);
85         dayCombo.setItems(SchedulerStartup.DAYS);
86         gd = new GridData();
87         gd.widthHint = 200;
88         gd.horizontalIndent = 30;
89         dayCombo.setLayoutData(gd);
90         
91         atLabel = new Label(updateScheduleGroup, SWT.NULL);
92         atLabel.setText(UpdateSchedulerMessages.AutomaticUpdatesPreferencePage_at);
93         
94         hourCombo = new Combo(updateScheduleGroup, SWT.READ_ONLY);
95         hourCombo.setItems(SchedulerStartup.HOURS);
96         gd = new GridData();
97         gd.widthHint = 100;
98         hourCombo.setLayoutData(gd);
99     
100         createSpacer(container, 1);
101         
102         downloadGroup = new Group(container, SWT.NONE);
103         downloadGroup.setText(UpdateSchedulerMessages.AutomaticUpdatesPreferencePage_downloadOptions);
104         layout = new GridLayout();
105         layout.numColumns = 3;
106         downloadGroup.setLayout(layout);
107         gd = new GridData(GridData.FILL_HORIZONTAL);
108         downloadGroup.setLayoutData(gd);
109
110         searchOnlyRadio = new Button(downloadGroup, SWT.RADIO);
111         searchOnlyRadio.setText(
112             UpdateSchedulerMessages.AutomaticUpdatesPreferencePage_searchAndNotify);
113         gd = new GridData();
114         gd.horizontalSpan = 3;
115         searchOnlyRadio.setLayoutData(gd);
116         searchOnlyRadio.addSelectionListener(new SelectionAdapter() {
117             public void widgetSelected(SelectionEvent e) {
118                 pageChanged();
119             }
120         });
121
122         searchAndDownloadRadio = new Button(downloadGroup, SWT.RADIO);
123         searchAndDownloadRadio.setText(UpdateSchedulerMessages.AutomaticUpdatesPreferencePage_downloadAndNotify);
124         gd = new GridData();
125         gd.horizontalSpan = 3;
126         searchAndDownloadRadio.setLayoutData(gd);
127         searchAndDownloadRadio.addSelectionListener(new SelectionAdapter() {
128             public void widgetSelected(SelectionEvent e) {
129                 pageChanged();
130             }
131         });
132             
133         initialize();
134
135         enabledCheck.addSelectionListener(new SelectionAdapter() {
136             public void widgetSelected(SelectionEvent e) {
137                 pageChanged();
138             }
139         });
140
141         Dialog.applyDialogFont(container);
142         return container;
143     }
144
145     protected void createSpacer(Composite composite, int columnSpan) {
146         Label label = new Label(composite, SWT.NONE);
147         GridData gd = new GridData();
148         gd.horizontalSpan = columnSpan;
149         label.setLayoutData(gd);
150     }
151
152     private void initialize() {
153         Preferences pref = UpdateSchedulerPlugin.getDefault().getPluginPreferences();
154         enabledCheck.setSelection(pref.getBoolean(UpdateSchedulerPlugin.P_ENABLED));
155         setSchedule(pref.getString(UpdateSchedulerPlugin.P_SCHEDULE));
156
157         dayCombo.setText(SchedulerStartup.DAYS[getDay(pref, false)]);
158         hourCombo.setText(SchedulerStartup.HOURS[getHour(pref, false)]);
159         
160         searchOnlyRadio.setSelection(!pref.getBoolean(UpdateSchedulerPlugin.P_DOWNLOAD));
161         searchAndDownloadRadio.setSelection(pref.getBoolean(UpdateSchedulerPlugin.P_DOWNLOAD));
162
163         pageChanged();
164     }
165
166     private void setSchedule(String JavaDoc value) {
167         if (value.equals(UpdateSchedulerPlugin.VALUE_ON_STARTUP))
168             onStartupRadio.setSelection(true);
169         else
170             onScheduleRadio.setSelection(true);
171     }
172
173     private void pageChanged() {
174         boolean master = enabledCheck.getSelection();
175         updateScheduleGroup.setEnabled(master);
176         onStartupRadio.setEnabled(master);
177         onScheduleRadio.setEnabled(master);
178         dayCombo.setEnabled(master && onScheduleRadio.getSelection());
179         atLabel.setEnabled(master && onScheduleRadio.getSelection());
180         hourCombo.setEnabled(master && onScheduleRadio.getSelection());
181         downloadGroup.setEnabled(master);
182         searchOnlyRadio.setEnabled(master);
183         searchAndDownloadRadio.setEnabled(master);
184     }
185
186     protected void performDefaults() {
187         super.performDefaults();
188         Preferences pref = UpdateSchedulerPlugin.getDefault().getPluginPreferences();
189         enabledCheck.setSelection(pref.getDefaultBoolean(UpdateSchedulerPlugin.P_ENABLED));
190         
191         setSchedule(pref.getDefaultString(UpdateSchedulerPlugin.P_SCHEDULE));
192         onScheduleRadio.setSelection(pref.getDefaultBoolean(UpdateSchedulerPlugin.P_SCHEDULE));
193         
194         dayCombo.setText(SchedulerStartup.DAYS[getDay(pref, true)]);
195         hourCombo.setText(SchedulerStartup.HOURS[getHour(pref, true)]);
196         
197         searchOnlyRadio.setSelection(!pref.getDefaultBoolean(UpdateSchedulerPlugin.P_DOWNLOAD));
198         searchAndDownloadRadio.setSelection(pref.getDefaultBoolean(UpdateSchedulerPlugin.P_DOWNLOAD));
199         pageChanged();
200     }
201
202     /**
203      * Method declared on IPreferencePage.
204      * Subclasses should override
205      */

206     public boolean performOk() {
207         Preferences pref = UpdateSchedulerPlugin.getDefault().getPluginPreferences();
208         pref.setValue(UpdateSchedulerPlugin.P_ENABLED, enabledCheck.getSelection());
209         if (onStartupRadio.getSelection())
210             pref.setValue(UpdateSchedulerPlugin.P_SCHEDULE, UpdateSchedulerPlugin.VALUE_ON_STARTUP);
211         else
212             pref.setValue(UpdateSchedulerPlugin.P_SCHEDULE, UpdateSchedulerPlugin.VALUE_ON_SCHEDULE);
213             
214         pref.setValue(SchedulerStartup.P_DAY, dayCombo.getText());
215         pref.setValue(SchedulerStartup.P_HOUR, hourCombo.getText());
216         
217         pref.setValue(UpdateSchedulerPlugin.P_DOWNLOAD, searchAndDownloadRadio.getSelection());
218         
219         UpdateSchedulerPlugin.getDefault().savePluginPreferences();
220         
221         UpdateSchedulerPlugin.getScheduler().scheduleUpdateJob();
222         return true;
223     }
224     
225     private int getDay(Preferences pref, boolean useDefault) {
226         String JavaDoc day = useDefault? pref.getDefaultString(SchedulerStartup.P_DAY): pref.getString(SchedulerStartup.P_DAY);
227         for (int i=0; i<SchedulerStartup.DAYS.length; i++)
228             if (SchedulerStartup.DAYS[i].equals(day))
229                 return i;
230         return 0;
231     }
232     
233     private int getHour(Preferences pref, boolean useDefault) {
234         String JavaDoc hour = useDefault? pref.getDefaultString(SchedulerStartup.P_HOUR): pref.getString(SchedulerStartup.P_HOUR);
235         for (int i=0; i<SchedulerStartup.HOURS.length; i++)
236             if (SchedulerStartup.HOURS[i].equals(hour))
237                 return i;
238         return 0;
239     }
240 }
241
Popular Tags