1 11 package org.eclipse.team.internal.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.resources.WorkspaceJob; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.core.runtime.jobs.*; 18 import org.eclipse.jface.operation.IRunnableWithProgress; 19 import org.eclipse.team.core.TeamException; 20 import org.eclipse.team.internal.ui.Utils; 21 import org.eclipse.ui.IWorkbenchSite; 22 23 26 public class JobRunnableContext implements ITeamRunnableContext { 27 28 private IJobChangeListener listener; 29 private IWorkbenchSite site; 30 private String jobName; 31 private ISchedulingRule schedulingRule; 32 private boolean postponeBuild; 33 34 38 public interface IContextJob { 39 IRunnableWithProgress getRunnable(); 40 } 41 42 45 private class BasicJob extends Job implements IContextJob { 46 private final IRunnableWithProgress runnable; 47 public BasicJob(String name, IRunnableWithProgress runnable) { 48 super(name); 49 this.runnable = runnable; 50 } 51 public IStatus run(IProgressMonitor monitor) { 52 return JobRunnableContext.this.run(runnable, monitor); 53 } 54 public boolean belongsTo(Object family) { 55 return JobRunnableContext.this.belongsTo(this, family); 56 } 57 public IRunnableWithProgress getRunnable() { 58 return runnable; 59 } 60 } 61 62 65 private class ResourceJob extends WorkspaceJob implements IContextJob { 66 private final IRunnableWithProgress runnable; 67 public ResourceJob(String name, IRunnableWithProgress runnable) { 68 super(name); 69 this.runnable = runnable; 70 } 71 public IStatus runInWorkspace(IProgressMonitor monitor) { 72 return JobRunnableContext.this.run(runnable, monitor); 73 } 74 public boolean belongsTo(Object family) { 75 return JobRunnableContext.this.belongsTo(this, family); 76 } 77 public IRunnableWithProgress getRunnable() { 78 return runnable; 79 } 80 } 81 public JobRunnableContext(String jobName, IJobChangeListener listener, IWorkbenchSite site) { 82 this.jobName = jobName; 83 this.listener = listener; 84 this.site = site; 85 } 86 87 90 public void run(IRunnableWithProgress runnable) { 91 Job job; 92 if (schedulingRule == null && !postponeBuild) { 93 job = new BasicJob(jobName, runnable); 94 } else { 95 job = new ResourceJob(jobName, runnable); 96 } 97 if (listener != null) { 98 job.addJobChangeListener(listener); 99 } 100 configureJob(job); 101 Utils.schedule(job, site); 102 } 103 104 110 protected void configureJob(Job job) { 111 if (schedulingRule != null) { 112 job.setRule(schedulingRule); 113 } 114 job.setUser(isUser()); 115 } 116 117 122 public void setPostponeBuild(boolean postponeBuild) { 123 this.postponeBuild = postponeBuild; 124 } 125 126 129 protected boolean isUser() { 130 return true; 131 } 132 133 138 public void setSchedulingRule(ISchedulingRule schedulingRule) { 139 this.schedulingRule = schedulingRule; 140 } 141 142 IStatus run(IRunnableWithProgress runnable, IProgressMonitor monitor) { 143 try { 144 runnable.run(monitor); 145 } catch (InvocationTargetException e) { 146 return TeamException.asTeamException(e).getStatus(); 147 } catch (InterruptedException e) { 148 return Status.CANCEL_STATUS; 149 } 150 return getCompletionStatus(); 151 } 152 153 159 protected IStatus getCompletionStatus() { 160 return Status.OK_STATUS; 161 } 162 163 168 protected boolean belongsTo(IContextJob job, Object family) { 169 return false; 170 } 171 172 } 173 | Popular Tags |