1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/*******************************************************************************
* Copyright (c) 2008, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* Sonatype, Inc. - ongoing development
*******************************************************************************/
package org.eclipse.equinox.internal.p2.ui.dialogs;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashSet;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.jobs.*;
import org.eclipse.equinox.internal.p2.ui.*;
import org.eclipse.equinox.internal.p2.ui.model.ElementUtils;
import org.eclipse.equinox.internal.p2.ui.model.IUElementListRoot;
import org.eclipse.equinox.p2.engine.ProvisioningContext;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.operations.ProfileChangeOperation;
import org.eclipse.equinox.p2.ui.*;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.*;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.statushandlers.StatusManager;
/**
* Common superclass for a wizard that performs a provisioning
* operation.
*
* @since 3.5
*/
public abstract class ProvisioningOperationWizard extends Wizard {
private static final String WIZARD_SETTINGS_SECTION = "WizardSettings"; //$NON-NLS-1$
protected ProvisioningUI ui;
protected IUElementListRoot root;
protected ProfileChangeOperation operation;
protected Object[] planSelections;
protected ISelectableIUsPage mainPage;
protected IResolutionErrorReportingPage errorPage;
protected ResolutionResultsWizardPage resolutionPage;
private ProvisioningContext provisioningContext;
protected LoadMetadataRepositoryJob repoPreloadJob;
IStatus couldNotResolveStatus = Status.OK_STATUS; // we haven't tried and failed
boolean waitingForOtherJobs = false;
public ProvisioningOperationWizard(ProvisioningUI ui, ProfileChangeOperation operation, Object[] initialSelections, LoadMetadataRepositoryJob job) {
super();
this.ui = ui;
initializeResolutionModelElements(initialSelections);
this.operation = operation;
this.repoPreloadJob = job;
setForcePreviousAndNextButtons(true);
setNeedsProgressMonitor(true);
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.wizard.Wizard#addPages()
*/
public void addPages() {
mainPage = createMainPage(root, planSelections);
addPage(mainPage);
errorPage = createErrorReportingPage();
if (errorPage != mainPage)
addPage(errorPage);
resolutionPage = createResolutionPage();
addPage(resolutionPage);
}
protected abstract IResolutionErrorReportingPage createErrorReportingPage();
protected abstract ISelectableIUsPage createMainPage(IUElementListRoot input, Object[] selections);
protected abstract ResolutionResultsWizardPage createResolutionPage();
public boolean performFinish() {
return resolutionPage.performFinish();
}
protected LoadMetadataRepositoryJob getRepositoryPreloadJob() {
return repoPreloadJob;
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.wizard.Wizard#getNextPage(org.eclipse.jface.wizard.IWizardPage)
*/
public IWizardPage getNextPage(IWizardPage page) {
// If we are moving from the main page or error page, we may need to resolve before
// advancing.
if (page == mainPage || page == errorPage) {
ISelectableIUsPage currentPage = (ISelectableIUsPage) page;
// Do we need to resolve?
if (operation == null || (operation != null && shouldRecomputePlan(currentPage))) {
recomputePlan(getContainer());
} else {
// the selections have not changed from an IU point of view, but we want
// to reinitialize the resolution model elements to ensure they are up to
// date.
initializeResolutionModelElements(planSelections);
}
IStatus status = operation.getResolutionResult();
if (status == null || status.getSeverity() == IStatus.ERROR) {
return errorPage;
} else if (status.getSeverity() == IStatus.CANCEL) {
return page;
} else {
return resolutionPage;
}
}
return super.getNextPage(page);
}
/**
* The selections in the main page have changed. We might need to
* change the completion state of the resolution page.
*/
public void mainPageSelectionsChanged() {
if (resolutionPage != null) {
// If the page selections are different than what we may have resolved
// against, then this page is not complete.
boolean old = resolutionPage.isPageComplete();
resolutionPage.setPageComplete(!pageSelectionsHaveChanged(mainPage));
// If the state has truly changed, update the buttons.
if (old != resolutionPage.isPageComplete()) {
IWizardContainer container = getContainer();
if (container != null && container.getCurrentPage() != null)
getContainer().updateButtons();
}
}
}
private boolean shouldRecomputePlan(ISelectableIUsPage page) {
boolean previouslyWaiting = waitingForOtherJobs;
boolean previouslyCanceled = getCurrentStatus().getSeverity() == IStatus.CANCEL;
waitingForOtherJobs = ui.hasScheduledOperations();
return waitingForOtherJobs || previouslyWaiting || previouslyCanceled || pageSelectionsHaveChanged(page) || provisioningContextChanged();
}
private boolean pageSelectionsHaveChanged(ISelectableIUsPage page) {
HashSet<IInstallableUnit> selectedIUs = new HashSet<IInstallableUnit>();
Object[] currentSelections = page.getCheckedIUElements();
selectedIUs.addAll(ElementUtils.elementsToIUs(currentSelections));
HashSet<IInstallableUnit> lastIUSelections = new HashSet<IInstallableUnit>();
if (planSelections != null)
lastIUSelections.addAll(ElementUtils.elementsToIUs(planSelections));
return !(selectedIUs.equals(lastIUSelections));
}
private boolean provisioningContextChanged() {
ProvisioningContext currentProvisioningContext = getProvisioningContext();
if (currentProvisioningContext == null && provisioningContext == null)
return false;
if (currentProvisioningContext != null && provisioningContext != null)
return !Arrays.equals(provisioningContext.getMetadataRepositories(), currentProvisioningContext.getMetadataRepositories());
// One is null and the other is not
return true;
}
protected void planChanged() {
resolutionPage.updateStatus(root, operation);
if (errorPage != resolutionPage) {
IUElementListRoot newRoot = shouldUpdateErrorPageModelOnPlanChange() ? root : null;
errorPage.updateStatus(newRoot, operation);
}
}
protected boolean shouldUpdateErrorPageModelOnPlanChange() {
return errorPage != mainPage;
}
protected abstract void initializeResolutionModelElements(Object[] selectedElements);
protected ProvisioningContext getProvisioningContext() {
return null;
}
/**
* Recompute the provisioning plan based on the items in the IUElementListRoot and the given provisioning context.
* Report progress using the specified runnable context. This method may be called before the page is created.
*
* @param runnableContext
*/
public void recomputePlan(IRunnableContext runnableContext) {
couldNotResolveStatus = Status.OK_STATUS;
provisioningContext = getProvisioningContext();
initializeResolutionModelElements(mainPage.getCheckedIUElements());
if (planSelections.length == 0) {
operation = null;
couldNotResolve(ProvUIMessages.ResolutionWizardPage_NoSelections);
} else {
operation = getProfileChangeOperation(planSelections);
operation.setProvisioningContext(provisioningContext);
try {
runnableContext.run(true, true, new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) {
operation.resolveModal(monitor);
}
});
} catch (InterruptedException e) {
// Nothing to report if thread was interrupted
} catch (InvocationTargetException e) {
ProvUI.handleException(e.getCause(), null, StatusManager.SHOW | StatusManager.LOG);
couldNotResolve(null);
}
}
planChanged();
}
protected abstract ProfileChangeOperation getProfileChangeOperation(Object[] elements);
void couldNotResolve(String message) {
if (message != null) {
couldNotResolveStatus = new Status(IStatus.ERROR, ProvUIActivator.PLUGIN_ID, message, null);
} else {
couldNotResolveStatus = new Status(IStatus.ERROR, ProvUIActivator.PLUGIN_ID, ProvUIMessages.ProvisioningOperationWizard_UnexpectedFailureToResolve, null);
}
StatusManager.getManager().handle(couldNotResolveStatus, StatusManager.LOG);
}
public IStatus getCurrentStatus() {
if (operation != null && operation.getResolutionResult() != null)
return operation.getResolutionResult();
return couldNotResolveStatus;
}
public String getDialogSettingsSectionName() {
return getClass().getName() + "." + WIZARD_SETTINGS_SECTION; //$NON-NLS-1$
}
public void saveBoundsRelatedSettings() {
IWizardPage[] pages = getPages();
for (int i = 0; i < pages.length; i++) {
if (pages[i] instanceof ProvisioningWizardPage)
((ProvisioningWizardPage) pages[i]).saveBoundsRelatedSettings();
}
}
protected Policy getPolicy() {
return ui.getPolicy();
}
protected String getProfileId() {
return ui.getProfileId();
}
protected boolean shouldShowProvisioningPlanChildren() {
return ProvUI.getQueryContext(getPolicy()).getShowProvisioningPlanChildren();
}
/*
* Overridden to start the preload job after page control creation.
* This allows any listeners on repo events to be set up before a
* batch load occurs. The job creator uses a property to indicate if
* the job needs scheduling (the client may have already completed the job
* before the UI was opened).
* (non-Javadoc)
* @see org.eclipse.jface.wizard.Wizard#createPageControls(org.eclipse.swt.widgets.Composite)
*/
public void createPageControls(Composite pageContainer) {
super.createPageControls(pageContainer);
if (repoPreloadJob != null) {
if (repoPreloadJob.getProperty(LoadMetadataRepositoryJob.WIZARD_CLIENT_SHOULD_SCHEDULE) != null) {
// job has not been scheduled. Set a listener so we can report accumulated errors and
// schedule it.
repoPreloadJob.addJobChangeListener(new JobChangeAdapter() {
public void done(IJobChangeEvent e) {
asyncReportLoadFailures();
}
});
repoPreloadJob.schedule();
} else {
// job has been scheduled, might already be done.
if (repoPreloadJob.getState() == Job.NONE) {
// job is done, report failures found so far
// do it asynchronously since we are in the middle of creation
asyncReportLoadFailures();
} else {
// job is waiting, sleeping, running, report failures when
// it's done
repoPreloadJob.addJobChangeListener(new JobChangeAdapter() {
public void done(IJobChangeEvent e) {
asyncReportLoadFailures();
}
});
}
}
}
}
void asyncReportLoadFailures() {
if (repoPreloadJob != null && getShell() != null && !getShell().isDisposed()) {
getShell().getDisplay().asyncExec(new Runnable() {
public void run() {
if (PlatformUI.isWorkbenchRunning() && getShell() != null && !getShell().isDisposed())
repoPreloadJob.reportAccumulatedStatus();
}
});
}
}
}
|