KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > scheduler > AutomaticUpdatesJob


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.scheduler;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.core.runtime.jobs.Job;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.wizard.WizardDialog;
19 import org.eclipse.swt.custom.BusyIndicator;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.update.core.IFeature;
22 import org.eclipse.update.internal.core.*;
23 import org.eclipse.update.internal.operations.UpdateUtils;
24 import org.eclipse.update.internal.ui.wizards.*;
25 import org.eclipse.update.operations.*;
26 import org.eclipse.update.search.*;
27
28 public class AutomaticUpdatesJob extends Job {
29     
30     private class AutomaticSearchResultCollector implements IUpdateSearchResultCollector {
31         public void accept(IFeature feature) {
32             IInstallFeatureOperation operation =
33                 OperationsManager
34                     .getOperationFactory()
35                     .createInstallOperation(null, feature, null, null, null);
36             updates.add(operation);
37         }
38     }
39     
40     // job family
41
public static final Object JavaDoc family = new Object JavaDoc();
42     private IUpdateSearchResultCollector resultCollector;
43     
44     private static final IStatus OK_STATUS =
45         new Status(
46             IStatus.OK,
47             UpdateScheduler.getPluginId(),
48             IStatus.OK,
49             "", //$NON-NLS-1$
50
null);
51     private UpdateSearchRequest searchRequest;
52     private ArrayList JavaDoc updates;
53
54     public AutomaticUpdatesJob() {
55         super(UpdateScheduler.getString("AutomaticUpdatesJob.AutomaticUpdateSearch")); //$NON-NLS-1$
56
updates = new ArrayList JavaDoc();
57         setPriority(Job.DECORATE);
58     }
59
60
61     /**
62      * Returns the standard display to be used. The method first checks, if
63      * the thread calling this method has an associated disaply. If so, this
64      * display is returned. Otherwise the method returns the default display.
65      */

66     public static Display getStandardDisplay() {
67         Display display;
68         display = Display.getCurrent();
69         if (display == null)
70             display = Display.getDefault();
71         return display;
72     }
73
74     public boolean belongsTo(Object JavaDoc family) {
75         return AutomaticUpdatesJob.family == family;
76     }
77     
78     public IStatus run(IProgressMonitor monitor) {
79         if (UpdateCore.DEBUG) {
80             UpdateCore.debug("Automatic update search started."); //$NON-NLS-1$
81
}
82         searchRequest = UpdateUtils.createNewUpdatesRequest(null);
83         try {
84             if (resultCollector == null)
85                 resultCollector = new AutomaticSearchResultCollector();
86             searchRequest.performSearch(resultCollector, monitor);
87             if (UpdateCore.DEBUG) {
88                 UpdateCore.debug("Automatic update search finished - " //$NON-NLS-1$
89
+ updates.size()
90                 + " results."); //$NON-NLS-1$
91
}
92             if (updates.size() > 0) {
93                 boolean download = UpdateCore.getPlugin().getPluginPreferences().getBoolean(UpdateScheduler.P_DOWNLOAD);
94                 // silently download if download enabled
95
if (download)
96                 {
97                     if (UpdateCore.DEBUG) {
98                         UpdateCore.debug("Automatic download of updates started."); //$NON-NLS-1$
99
}
100                     for (int i=0; i<updates.size(); i++) {
101                         IInstallFeatureOperation op = (IInstallFeatureOperation)updates.get(i);
102                         IFeature feature = op.getFeature();
103                         UpdateUtils.downloadFeatureContent(feature, monitor);
104                     }
105                     if (UpdateCore.DEBUG) {
106                         UpdateCore.debug("Automatic download of updates finished."); //$NON-NLS-1$
107
}
108                 }
109                 // prompt the user
110
if (!InstallWizard.isRunning()) {
111                     if (download) {
112                         getStandardDisplay().asyncExec(new Runnable JavaDoc() {
113                             public void run() {
114                                 asyncNotifyDownloadUser();
115                             }
116                         });
117                     } else {
118                         getStandardDisplay().asyncExec(new Runnable JavaDoc() {
119                             public void run() {
120                                 asyncNotifyUser();
121                             }
122                         });
123                     }
124                 }
125                 return Job.ASYNC_FINISH;
126             }
127         } catch (CoreException e) {
128             return e.getStatus();
129         }
130         return OK_STATUS;
131     }
132
133     private void asyncNotifyUser() {
134         // ask the user to install updates
135
getStandardDisplay().beep();
136         if (MessageDialog
137             .openQuestion(
138                 UpdateScheduler.getActiveWorkbenchShell(),
139                 UpdateScheduler.getString("AutomaticUpdatesJob.EclipseUpdates1"), //$NON-NLS-1$
140
UpdateScheduler.getString("AutomaticUpdatesJob.UpdatesAvailable"))) { //$NON-NLS-1$
141
BusyIndicator.showWhile(getStandardDisplay(), new Runnable JavaDoc() {
142                 public void run() {
143                     openInstallWizard();
144                 }
145             });
146         }
147         // notify the manager that the job is done
148
done(OK_STATUS);
149     }
150     
151     private void asyncNotifyDownloadUser() {
152         // ask the user to install updates
153
getStandardDisplay().beep();
154         if (MessageDialog
155             .openQuestion(
156                 UpdateScheduler.getActiveWorkbenchShell(),
157                 UpdateScheduler.getString("AutomaticUpdatesJob.EclipseUpdates2"), //$NON-NLS-1$
158
UpdateScheduler.getString("AutomaticUpdatesJob.UpdatesDownloaded"))) { //$NON-NLS-1$
159
BusyIndicator.showWhile(getStandardDisplay(), new Runnable JavaDoc() {
160                 public void run() {
161                     openInstallWizard();
162                 }
163             });
164         } else {
165             // Don't discard downloaded data, as next time we compare timestamps.
166

167             // discard all the downloaded data from cache (may include old data as well)
168
//Utilities.flushLocalFile();
169
}
170         // notify the manager that the job is done
171
done(OK_STATUS);
172     }
173
174     private void openInstallWizard() {
175         if (InstallWizard.isRunning())
176             // job ends and a new one is rescheduled
177
return;
178             
179         InstallWizard wizard = new InstallWizard(searchRequest, updates);
180         WizardDialog dialog =
181             new ResizableInstallWizardDialog(
182                 UpdateScheduler.getActiveWorkbenchShell(),
183                 wizard,
184                 UpdateScheduler.getString("AutomaticUpdatesJob.Updates")); //$NON-NLS-1$
185
dialog.create();
186         dialog.open();
187     }
188 }
189
Popular Tags