01: package org.eclipse.ui.examples.jobs.actions;
02:
03: import org.eclipse.core.resources.IWorkspace;
04: import org.eclipse.core.resources.ResourcesPlugin;
05: import org.eclipse.core.resources.WorkspaceJob;
06: import org.eclipse.core.runtime.CoreException;
07: import org.eclipse.core.runtime.IProgressMonitor;
08: import org.eclipse.core.runtime.IStatus;
09: import org.eclipse.core.runtime.Status;
10: import org.eclipse.core.runtime.jobs.Job;
11: import org.eclipse.jface.action.IAction;
12: import org.eclipse.jface.viewers.ISelection;
13: import org.eclipse.ui.IWorkbenchWindow;
14: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
15:
16: /**
17: * Our sample action implements workbench action delegate.
18: * The action proxy will be created by the workbench and
19: * shown in the UI. When the user tries to use the action,
20: * this delegate will be created and execution will be
21: * delegated to it.
22: * @see IWorkbenchWindowActionDelegate
23: */
24: public class JobAction implements IWorkbenchWindowActionDelegate {
25: public void run(IAction action) {
26: final IWorkspace workspace = ResourcesPlugin.getWorkspace();
27: Job job = new WorkspaceJob("Background job") { //$NON-NLS-1$
28: public IStatus runInWorkspace(IProgressMonitor monitor)
29: throws CoreException {
30: monitor.beginTask("Doing something in background", 100); //$NON-NLS-1$
31: for (int i = 0; i < 100; i++) {
32: try {
33: Thread.sleep(100);
34: } catch (InterruptedException e) {
35: return Status.CANCEL_STATUS;
36: }
37: monitor.worked(1);
38: }
39: return Status.OK_STATUS;
40: }
41: };
42: job.setRule(workspace.getRoot());
43: job.schedule();
44: }
45:
46: public void selectionChanged(IAction action, ISelection selection) {
47: //do nothing
48: }
49:
50: public void dispose() {
51: //do nothing
52: }
53:
54: public void init(IWorkbenchWindow window) {
55: //do nothing
56: }
57: }
|