final boolean restore = Activator.getDefault().getPreferenceStore()
.getBoolean(UIPreferences.CHECKOUT_PROJECT_RESTORE);
final BranchOperation bop = new BranchOperation(repository, target,
!restore);
Job job = new WorkspaceJob(jobname) {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) {
try {
if (restore) {
final BranchProjectTracker tracker = new BranchProjectTracker(
repository);
final AtomicReference<IMemento> memento = new AtomicReference<IMemento>();
bop.addPreExecuteTask(new PreExecuteTask() {
public void preExecute(Repository pRepo,
IProgressMonitor pMonitor)
throws CoreException {
// Snapshot current projects before checkout
// begins
memento.set(tracker.snapshot());
}
});
bop.addPostExecuteTask(new PostExecuteTask() {
public void postExecute(Repository pRepo,
IProgressMonitor pMonitor)
throws CoreException {
IMemento snapshot = memento.get();
if (snapshot == null)
return;
// Save previous branch's projects and restore
// current branch's projects
tracker.save(snapshot).restore(pMonitor);
}
});
}
bop.execute(monitor);
} catch (CoreException e) {
switch (bop.getResult().getStatus()) {
case CONFLICTS:
case NONDELETED:
break;
default:
return Activator.createErrorStatus(
UIText.BranchAction_branchFailed, e);
}
} finally {
GitLightweightDecorator.refresh();
}
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
if (JobFamilies.CHECKOUT.equals(family))
return true;
return super.belongsTo(family);
}
};
job.setUser(true);
// Set scheduling rule to workspace because we may have to re-create
// projects using BranchProjectTracker.
if (restore)
job.setRule(ResourcesPlugin.getWorkspace().getRoot());
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent cevent) {
show(bop.getResult());
}
});
job.schedule();
}