1 11 package org.eclipse.ui.views.markers.internal; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.runtime.IProgressMonitor; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.core.runtime.jobs.IJobChangeEvent; 19 import org.eclipse.core.runtime.jobs.Job; 20 import org.eclipse.core.runtime.jobs.JobChangeAdapter; 21 import org.eclipse.jface.operation.IRunnableWithProgress; 22 import org.eclipse.ui.internal.ide.StatusUtil; 23 import org.eclipse.ui.progress.IWorkbenchSiteProgressService; 24 25 31 public final class RestartableJob { 32 IRunnableWithProgress runnable; 33 34 Job theJob; 35 36 boolean restartNeeded = false; 37 38 private Object lock = new Object (); 39 40 private IProgressMonitor currentMonitor = null; 41 42 IWorkbenchSiteProgressService progressService; 43 44 53 public RestartableJob(String name, IRunnableWithProgress newRunnable, 54 IWorkbenchSiteProgressService service) { 55 this.runnable = newRunnable; 56 progressService = service; 57 58 createJob(name); 59 60 theJob.addJobChangeListener(new JobChangeAdapter() { 61 public void done(IJobChangeEvent e) { 62 synchronized (lock) { 63 currentMonitor = null; 64 if (restartNeeded) { 65 scheduleInService(); 66 } 67 } 68 } 69 }); 70 } 71 72 77 private void createJob(String name) { 78 theJob = new Job(name) { 79 protected IStatus run(IProgressMonitor innerMonitor) { 80 try { 81 synchronized (lock) { 82 restartNeeded = false; 83 currentMonitor = innerMonitor; 84 } 85 runnable.run(innerMonitor); 86 } catch (InvocationTargetException e) { 87 return StatusUtil.newStatus(IStatus.ERROR, e.toString(), e 88 .getTargetException()); 89 } catch (InterruptedException e) { 90 return Status.CANCEL_STATUS; 91 } 92 if (innerMonitor.isCanceled()) { 93 return Status.CANCEL_STATUS; 94 } else { 95 return Status.OK_STATUS; 96 } 97 } 98 }; 99 100 theJob.setPriority(Job.DECORATE); 101 theJob.setSystem(true); 102 } 103 104 109 public void restart() { 110 synchronized (lock) { 111 if (currentMonitor == null) { 112 scheduleInService(); 113 } else if (!restartNeeded) { 114 restartNeeded = true; 115 theJob.cancel(); 116 } 117 } 118 } 119 120 123 public void schedule() { 124 synchronized (lock) { 125 if (currentMonitor == null) { 126 scheduleInService(); 127 } else { 128 if (currentMonitor.isCanceled()) { 129 restartNeeded = true; 130 } 131 } 132 } 133 } 134 135 139 private void scheduleInService() { 140 if (progressService == null) 141 theJob.schedule(); 142 else 143 progressService.schedule(theJob, 0, true); 144 } 145 146 150 public void cancel() { 151 synchronized (lock) { 152 theJob.cancel(); 153 restartNeeded = false; 154 } 155 } 156 } 157 | Popular Tags |