1 11 package org.eclipse.team.ui; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URL ; 15 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.core.runtime.jobs.*; 19 import org.eclipse.jface.action.IAction; 20 import org.eclipse.jface.operation.IRunnableContext; 21 import org.eclipse.jface.operation.IRunnableWithProgress; 22 import org.eclipse.swt.widgets.Display; 23 import org.eclipse.swt.widgets.Shell; 24 import org.eclipse.team.internal.ui.TeamUIPlugin; 25 import org.eclipse.team.internal.ui.Utils; 26 import org.eclipse.team.internal.ui.actions.*; 27 import org.eclipse.ui.IWorkbenchPart; 28 import org.eclipse.ui.IWorkbenchSite; 29 import org.eclipse.ui.progress.IProgressConstants; 30 31 50 public abstract class TeamOperation extends JobChangeAdapter implements IRunnableWithProgress { 51 52 private IWorkbenchPart part; 53 private IRunnableContext context; 54 55 59 private static class TeamOperationJobContext extends JobRunnableContext { 60 61 private final TeamOperation operation; 62 private IAction gotoAction; 63 64 public TeamOperationJobContext(TeamOperation operation) { 65 super(operation.getJobName(), operation, operation.getSite()); 66 this.operation = operation; 67 } 68 69 protected void configureJob(Job job) { 70 super.configureJob(job); 71 if (operation.isKeepOneProgressServiceEntry()) 72 job.setProperty(IProgressConstants.KEEPONE_PROPERTY, Boolean.TRUE); 73 else if(operation.getKeepOperation()) 74 job.setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE); 75 gotoAction = operation.getGotoAction(); 76 if(gotoAction != null) 77 job.setProperty(IProgressConstants.ACTION_PROPERTY, gotoAction); 78 URL icon = operation.getOperationIcon(); 79 if(icon != null) 80 job.setProperty(IProgressConstants.ICON_PROPERTY, icon); 81 } 82 83 86 protected boolean belongsTo(IContextJob job, Object family) { 87 if (family instanceof IContextJob) { 88 IContextJob otherJob = (IContextJob)family; 89 IRunnableWithProgress runnable = otherJob.getRunnable(); 90 if (runnable instanceof TeamOperation) { 91 return operation.isSameFamilyAs((TeamOperation)runnable); 92 } 93 } 94 return operation.belongsTo(family); 95 } 96 97 100 protected IStatus getCompletionStatus() { 101 if (gotoAction != null) { 102 return new Status(IStatus.OK, TeamUIPlugin.ID, IStatus.OK, gotoAction.getText(), null); 103 } 104 return super.getCompletionStatus(); 105 } 106 107 110 protected boolean isUser() { 111 return operation.isUserInitiated(); 112 } 113 } 114 115 120 protected TeamOperation(IWorkbenchPart part) { 121 this(part, null); 122 } 123 124 128 protected TeamOperation(IRunnableContext context) { 129 this(null, context); 130 } 131 132 138 protected TeamOperation(IWorkbenchPart part, IRunnableContext context) { 139 this.part = part; 140 this.context = context; 141 } 142 143 148 public IWorkbenchPart getPart() { 149 return part; 150 } 151 152 157 public final void run() throws InvocationTargetException , InterruptedException { 158 if (shouldRun()) { 159 getRunnableContext().run(this); 160 } 161 } 162 163 171 protected boolean shouldRun() { 172 return true; 173 } 174 175 191 protected ISchedulingRule getSchedulingRule() { 192 return null; 193 } 194 195 202 protected boolean isPostponeAutobuild() { 203 return true; 204 } 205 206 216 protected boolean canRunAsJob() { 217 return false; 218 } 219 220 226 protected String getJobName() { 227 return ""; } 229 230 236 protected IAction getGotoAction() { 237 return null; 238 } 239 240 246 protected URL getOperationIcon() { 247 return null; 248 } 249 250 259 protected boolean getKeepOperation() { 260 return false; 261 } 262 263 275 public boolean isKeepOneProgressServiceEntry() { 276 return false; 277 } 278 279 289 protected boolean isSameFamilyAs(TeamOperation operation) { 290 return false; 291 } 292 293 306 public boolean belongsTo(Object family) { 307 return false; 308 } 309 310 317 public boolean isUserInitiated() { 318 return true; 319 } 320 321 326 protected Shell getShell() { 327 final Shell[] shell = new Shell[] { null }; 328 if (Display.getCurrent() == null) { 329 Display.getDefault().syncExec(new Runnable () { 330 public void run() { 331 shell[0] = Utils.getShell(getSite()); 332 } 333 }); 334 } else { 335 shell[0] = Utils.getShell(getSite()); 336 } 337 return shell[0]; 338 } 339 340 349 private ITeamRunnableContext getRunnableContext() { 350 if (context == null && canRunAsJob()) { 351 JobRunnableContext context = new TeamOperationJobContext(this); 352 context.setPostponeBuild(isPostponeAutobuild()); 353 context.setSchedulingRule(getSchedulingRule()); 354 return context; 355 } else { 356 ProgressDialogRunnableContext context = new ProgressDialogRunnableContext(); 357 context.setPostponeBuild(isPostponeAutobuild()); 358 context.setSchedulingRule(getSchedulingRule()); 359 if (this.context != null) { 360 context.setRunnableContext(this.context); 361 } 362 return context; 363 } 364 } 365 366 private IWorkbenchSite getSite() { 367 IWorkbenchSite site = null; 368 if(part != null) { 369 site = part.getSite(); 370 } 371 return site; 372 } 373 } 374 | Popular Tags |