Revision 6b462070
Added by Cherian Mathew over 8 years ago
eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/update/UpdateHandler.java | ||
---|---|---|
8 | 8 |
*/ |
9 | 9 |
package eu.etaxonomy.taxeditor.update; |
10 | 10 |
|
11 |
import java.lang.reflect.InvocationTargetException; |
|
11 | 12 |
import java.net.URI; |
12 | 13 |
|
14 |
import org.eclipse.core.commands.AbstractHandler; |
|
15 |
import org.eclipse.core.commands.ExecutionEvent; |
|
16 |
import org.eclipse.core.commands.ExecutionException; |
|
17 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
13 | 18 |
import org.eclipse.core.runtime.IStatus; |
14 | 19 |
import org.eclipse.core.runtime.OperationCanceledException; |
20 |
import org.eclipse.core.runtime.jobs.IJobChangeEvent; |
|
21 |
import org.eclipse.core.runtime.jobs.JobChangeAdapter; |
|
15 | 22 |
import org.eclipse.equinox.internal.p2.ui.ProvUI; |
16 |
import org.eclipse.equinox.internal.p2.ui.dialogs.UpdateSingleIUWizard; |
|
17 | 23 |
import org.eclipse.equinox.p2.core.IProvisioningAgent; |
18 | 24 |
import org.eclipse.equinox.p2.core.ProvisionException; |
25 |
import org.eclipse.equinox.p2.operations.ProvisioningJob; |
|
19 | 26 |
import org.eclipse.equinox.p2.operations.ProvisioningSession; |
20 | 27 |
import org.eclipse.equinox.p2.operations.UpdateOperation; |
21 | 28 |
import org.eclipse.equinox.p2.repository.IRepositoryManager; |
22 | 29 |
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; |
23 |
import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob; |
|
24 | 30 |
import org.eclipse.equinox.p2.ui.ProvisioningUI; |
25 | 31 |
import org.eclipse.jface.dialogs.MessageDialog; |
26 |
import org.eclipse.jface.wizard.WizardDialog; |
|
32 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
|
33 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
|
34 |
import org.eclipse.swt.SWT; |
|
35 |
import org.eclipse.swt.widgets.Display; |
|
27 | 36 |
import org.eclipse.ui.IWorkbench; |
28 | 37 |
import org.eclipse.ui.PlatformUI; |
29 | 38 |
import org.osgi.framework.BundleContext; |
... | ... | |
36 | 45 |
* UpdateHandler invokes the check for updates UI |
37 | 46 |
* |
38 | 47 |
*/ |
39 |
public class UpdateHandler extends PreloadingRepositoryHandler { |
|
40 |
|
|
48 |
public class UpdateHandler extends AbstractHandler { |
|
41 | 49 |
|
50 |
private static final String ERROR_PERFORMING_UPDATES = "Error performing updates"; |
|
51 |
/** |
|
52 |
* {@inheritDoc} |
|
53 |
*/ |
|
42 | 54 |
@Override |
43 |
protected void doExecute(LoadMetadataRepositoryJob job) { |
|
55 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
|
56 |
// update using a progress monitor |
|
57 |
IRunnableWithProgress runnable = new IRunnableWithProgress() { |
|
58 |
@Override |
|
59 |
public void run(IProgressMonitor monitor) throws InvocationTargetException, |
|
60 |
InterruptedException { |
|
61 |
doExecute(monitor); |
|
62 |
} |
|
63 |
}; |
|
64 |
|
|
65 |
try { |
|
66 |
new ProgressMonitorDialog(null).run(true, true, runnable); |
|
67 |
} catch (InvocationTargetException | InterruptedException e) { |
|
68 |
showError(e); |
|
69 |
} |
|
70 |
return null; |
|
71 |
} |
|
72 |
|
|
73 |
private void doExecute(IProgressMonitor monitor) { |
|
44 | 74 |
|
45 | 75 |
//UpdateOperation operation = new UpdateOperation(ProvisioningUI.getDefaultUI().getSession()); |
46 | 76 |
|
47 | 77 |
BundleContext bundleContext = TaxonomicEditorPlugin.getContext(); |
48 | 78 |
ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME); |
49 | 79 |
if (reference == null) { |
50 |
MessageDialog.openInformation( |
|
51 |
null, |
|
52 |
"Information", |
|
53 |
"No provisioning agent found. This application is not set up for updates."); |
|
80 |
showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "No provisioning agent found. This application is not set up for updates."); |
|
54 | 81 |
return; |
55 | 82 |
} |
56 | 83 |
|
57 | 84 |
final IProvisioningAgent agent = (IProvisioningAgent) bundleContext.getService(reference); |
58 |
IWorkbench workbench = PlatformUI.getWorkbench(); |
|
85 |
final IWorkbench workbench = PlatformUI.getWorkbench();
|
|
59 | 86 |
|
60 | 87 |
ProvisioningSession session = new ProvisioningSession(agent); |
61 | 88 |
// update all user-visible installable units |
... | ... | |
68 | 95 |
for(URI repo : repos) { |
69 | 96 |
try { |
70 | 97 |
metaManager.refreshRepository(repo, null); |
71 |
} catch (ProvisionException pe) { |
|
72 |
MessagingUtils.errorDialog("Error reloading repository cache", |
|
73 |
this, |
|
74 |
pe.getMessage(), |
|
75 |
TaxonomicEditorPlugin.PLUGIN_ID, |
|
76 |
pe, |
|
77 |
true); |
|
78 |
return; |
|
79 |
|
|
80 |
} catch (OperationCanceledException oce) { |
|
81 |
MessagingUtils.errorDialog("Error reloading repository cache", |
|
82 |
this, |
|
83 |
oce.getMessage(), |
|
84 |
TaxonomicEditorPlugin.PLUGIN_ID, |
|
85 |
oce, |
|
86 |
true); |
|
87 |
return; |
|
98 |
} catch (ProvisionException | OperationCanceledException e) { |
|
99 |
showError(e); |
|
88 | 100 |
} |
89 | 101 |
} |
90 | 102 |
|
... | ... | |
92 | 104 |
operation.getProvisioningContext().setArtifactRepositories(repos); |
93 | 105 |
operation.getProvisioningContext().setMetadataRepositories(repos); |
94 | 106 |
|
95 |
IStatus status = operation.resolveModal(null); |
|
107 |
IStatus status = operation.resolveModal(monitor); |
|
108 |
|
|
109 |
// if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) { |
|
110 |
// if (UpdateSingleIUWizard.validFor(operation)) { |
|
111 |
// // Special case for only updating a single root |
|
112 |
// UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation); |
|
113 |
// WizardDialog dialog = new WizardDialog(getShell(), wizard); |
|
114 |
// dialog.create(); |
|
115 |
// dialog.open(); |
|
116 |
// } else { |
|
117 |
// // Open the normal version of the update wizard |
|
118 |
// getProvisioningUI().openUpdateWizard(false, operation, job); |
|
119 |
// } |
|
120 |
// } |
|
121 |
|
|
122 |
if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) { |
|
123 |
showMessage(MessageDialog.INFORMATION, "Checking for updates", "No updates were found"); |
|
124 |
return; |
|
125 |
} |
|
96 | 126 |
|
97 |
if (getProvisioningUI().getPolicy().continueWorkingWithOperation(operation, getShell())) { |
|
98 |
if (UpdateSingleIUWizard.validFor(operation)) { |
|
99 |
// Special case for only updating a single root |
|
100 |
UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), operation); |
|
101 |
WizardDialog dialog = new WizardDialog(getShell(), wizard); |
|
102 |
dialog.create(); |
|
103 |
dialog.open(); |
|
127 |
final ProvisioningJob provisioningJob = operation.getProvisioningJob(monitor); |
|
128 |
if (provisioningJob != null) { |
|
129 |
Display.getDefault().syncExec(new Runnable() { |
|
130 |
@Override |
|
131 |
public void run() { |
|
132 |
boolean performUpdate = MessageDialog.openQuestion( |
|
133 |
null, |
|
134 |
"Updates available", |
|
135 |
"There are updates available. Do you want to install them now?"); |
|
136 |
if (performUpdate) { |
|
137 |
provisioningJob.addJobChangeListener(new JobChangeAdapter() { |
|
138 |
@Override |
|
139 |
public void done(IJobChangeEvent event) { |
|
140 |
if (event.getResult().isOK()) { |
|
141 |
Display.getDefault().syncExec(new Runnable() { |
|
142 |
@Override |
|
143 |
public void run() { |
|
144 |
boolean restart = MessageDialog.openQuestion(null, |
|
145 |
"Updates installed, restart?", |
|
146 |
"Updates have been installed successfully, do you want to restart?"); |
|
147 |
if (restart) { |
|
148 |
workbench.restart(); |
|
149 |
} |
|
150 |
} |
|
151 |
}); |
|
152 |
} else { |
|
153 |
MessageDialog.openInformation( |
|
154 |
null, |
|
155 |
ERROR_PERFORMING_UPDATES, |
|
156 |
event.getResult().getMessage()); |
|
157 |
} |
|
158 |
} |
|
159 |
}); |
|
160 |
provisioningJob.schedule(); |
|
161 |
} |
|
162 |
} |
|
163 |
}); |
|
164 |
} else { |
|
165 |
if (operation.hasResolved()) { |
|
166 |
showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "Couldn't get provisioning job: " + operation.getResolutionResult()); |
|
104 | 167 |
} else { |
105 |
// Open the normal version of the update wizard |
|
106 |
getProvisioningUI().openUpdateWizard(false, operation, job); |
|
168 |
showMessage(MessageDialog.ERROR, ERROR_PERFORMING_UPDATES, "Couldn't resolve provisioning job"); |
|
107 | 169 |
} |
108 | 170 |
} |
109 | 171 |
} |
172 |
|
|
173 |
private void showMessage(final int kind, final String title, final String info) { |
|
174 |
Display.getDefault().syncExec(new Runnable() { |
|
175 |
@Override |
|
176 |
public void run() { |
|
177 |
MessageDialog.open(kind, null, title, info, SWT.NONE); |
|
178 |
} |
|
179 |
}); |
|
180 |
} |
|
181 |
|
|
182 |
private void showError(final Throwable t) { |
|
183 |
Display.getDefault().syncExec(new Runnable() { |
|
184 |
@Override |
|
185 |
public void run() { |
|
186 |
MessagingUtils.errorDialog(ERROR_PERFORMING_UPDATES, |
|
187 |
this, |
|
188 |
t.getMessage(), |
|
189 |
TaxonomicEditorPlugin.PLUGIN_ID, |
|
190 |
t, |
|
191 |
true); |
|
192 |
} |
|
193 |
}); |
|
194 |
} |
|
195 |
|
|
196 |
|
|
110 | 197 |
} |
Also available in: Unified diff
Move to simpler update process