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
|
/*******************************************************************************
* Copyright (c) 2007, 2008 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
*******************************************************************************/
package org.eclipse.equinox.internal.provisional.p2.ui;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.equinox.internal.provisional.p2.ui.operations.ProvisioningOperation;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.WorkbenchJob;
/**
* Utility methods for running provisioning operations. All operations are
* run in jobs, with the operation determining whether the job is run in
* the background or in the UI.
*
* @since 3.4
*/
public class ProvisioningOperationRunner {
/**
* Schedule a job to execute the supplied ProvisioningOperation, and add it to the
* undo history if it supports undo.
*
* @param op The operation to execute
* @param shell provided by the caller in order to supply UI information for prompting the
* user if necessary. May be <code>null</code>.
*/
public static Job schedule(final ProvisioningOperation op, final Shell shell) {
Job job;
if (op.runInBackground()) {
job = new Job(op.getLabel()) {
protected IStatus run(IProgressMonitor monitor) {
try {
IStatus status;
if (op instanceof IUndoableOperation) {
status = PlatformUI.getWorkbench().getOperationSupport().getOperationHistory().execute((IUndoableOperation) op, monitor, ProvUI.getUIInfoAdapter(shell));
} else {
status = op.execute(monitor, ProvUI.getUIInfoAdapter(shell));
}
return status;
} catch (ExecutionException e) {
return ProvUI.handleException(e.getCause(), null);
}
}
};
job.setPriority(Job.LONG);
} else {
job = new WorkbenchJob(op.getLabel()) {
public IStatus runInUIThread(IProgressMonitor monitor) {
try {
IStatus status;
if (op instanceof IUndoableOperation) {
status = PlatformUI.getWorkbench().getOperationSupport().getOperationHistory().execute((IUndoableOperation) op, monitor, ProvUI.getUIInfoAdapter(shell));
} else {
status = op.execute(monitor, ProvUI.getUIInfoAdapter(shell));
}
return status;
} catch (ExecutionException e) {
return ProvUI.handleException(e.getCause(), null);
}
}
};
job.setPriority(Job.SHORT);
}
job.setUser(op.isUser());
job.schedule();
return job;
}
}
|