|
1 | 1 | package de.vogella.jobs.first.handler; |
2 | 2 |
|
3 | | - |
4 | 3 | import org.eclipse.core.commands.AbstractHandler; |
5 | 4 | import org.eclipse.core.commands.ExecutionEvent; |
6 | 5 | import org.eclipse.core.commands.ExecutionException; |
|
9 | 8 | import org.eclipse.core.runtime.Status; |
10 | 9 | import org.eclipse.core.runtime.jobs.Job; |
11 | 10 | import org.eclipse.jface.dialogs.MessageDialog; |
| 11 | +import org.eclipse.swt.widgets.Display; |
12 | 12 | import org.eclipse.ui.handlers.HandlerUtil; |
13 | 13 |
|
14 | 14 | public class RunJob1 extends AbstractHandler { |
15 | 15 |
|
16 | 16 | @Override |
17 | 17 | public Object execute(final ExecutionEvent event) throws ExecutionException { |
18 | | - Job job = new Job("First Job"){ |
| 18 | + Job job = new Job("First Job") { |
19 | 19 | @Override |
20 | 20 | protected IStatus run(IProgressMonitor monitor) { |
21 | | - |
22 | | - for (int i=0; i<10; i++){ |
| 21 | + for (int i = 0; i < 10; i++) { |
23 | 22 | try { |
24 | | - Thread.sleep(100); |
| 23 | + // We simulate a long running operation here |
| 24 | + Thread.sleep(1000); |
25 | 25 | } catch (InterruptedException e) { |
26 | 26 | e.printStackTrace(); |
27 | 27 | } |
28 | 28 | System.out.println("Doing something"); |
29 | 29 | } |
30 | | - HandlerUtil.getActiveShell(event).getDisplay().getDefault().asyncExec(new Runnable() { |
31 | | - public void run() { |
32 | | - MessageDialog.openInformation(HandlerUtil.getActiveShell(event), "Info", "Info for you"); |
33 | | - } |
| 30 | + // Use this to open a Shell in the UI thread |
| 31 | + Display.getDefault().asyncExec(new Runnable() { |
| 32 | + public void run() { |
| 33 | + MessageDialog.openInformation( |
| 34 | + HandlerUtil.getActiveShell(event), |
| 35 | + "Your Popup ", "Your job has finished."); |
| 36 | + } |
34 | 37 | }); |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | 38 | return Status.OK_STATUS; |
40 | 39 | } |
41 | | - |
42 | | - }; |
| 40 | + |
| 41 | + }; |
43 | 42 | job.setUser(true); |
44 | 43 | job.schedule(); |
45 | 44 | return null; |
|
0 commit comments