1 11 package org.eclipse.ui.progress; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.Status; 17 import org.eclipse.core.runtime.jobs.Job; 18 import org.eclipse.swt.widgets.Display; 19 import org.eclipse.ui.PlatformUI; 20 import org.eclipse.ui.internal.WorkbenchPlugin; 21 import org.eclipse.ui.internal.misc.UIStats; 22 import org.eclipse.ui.internal.progress.ProgressMessages; 23 24 29 public abstract class UIJob extends Job { 30 private Display cachedDisplay; 31 32 41 public UIJob(String name) { 42 super(name); 43 } 44 45 54 public UIJob(Display jobDisplay, String name) { 55 this(name); 56 setDisplay(jobDisplay); 57 } 58 59 66 public static IStatus errorStatus(Throwable exception) { 67 return WorkbenchPlugin.getStatus(exception); 68 } 69 70 75 public final IStatus run(final IProgressMonitor monitor) { 76 if (monitor.isCanceled()) { 77 return Status.CANCEL_STATUS; 78 } 79 Display asyncDisplay = getDisplay(); 80 if (asyncDisplay == null || asyncDisplay.isDisposed()) { 81 return Status.CANCEL_STATUS; 82 } 83 asyncDisplay.asyncExec(new Runnable () { 84 public void run() { 85 IStatus result = null; 86 try { 87 setThread(Thread.currentThread()); 90 if (monitor.isCanceled()) { 91 result = Status.CANCEL_STATUS; 92 } else { 93 UIStats.start(UIStats.UI_JOB, getName()); 94 result = runInUIThread(monitor); 95 } 96 97 } finally { 98 UIStats.end(UIStats.UI_JOB, UIJob.this, getName()); 99 if (result == null) { 100 result = new Status(IStatus.ERROR, 101 PlatformUI.PLUGIN_ID, IStatus.ERROR, 102 ProgressMessages.Error, 103 null); 104 } 105 done(result); 106 } 107 } 108 }); 109 return Job.ASYNC_FINISH; 110 } 111 112 118 public abstract IStatus runInUIThread(IProgressMonitor monitor); 119 120 129 public void setDisplay(Display runDisplay) { 130 Assert.isNotNull(runDisplay); 131 cachedDisplay = runDisplay; 132 } 133 134 142 public Display getDisplay() { 143 if (cachedDisplay == null && PlatformUI.isWorkbenchRunning()) { 145 return PlatformUI.getWorkbench().getDisplay(); 146 } 147 return cachedDisplay; 148 } 149 } 150 | Popular Tags |