use of org.eclipse.egit.core.op.StashDropOperation in project egit by eclipse.
the class StashDropHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final RevCommit commit = getSelectedItem(RevCommit.class, event);
if (commit == null)
return null;
Repository repo = getSelectedItem(Repository.class, event);
if (repo == null)
return null;
final Shell parent = getPart(event).getSite().getShell();
final int stashIndex = getStashIndex(repo, commit.getId());
if (!confirmStashDrop(parent, stashIndex))
return null;
final StashDropOperation op = new StashDropOperation(repo, stashIndex);
Job job = new WorkspaceJob(MessageFormat.format(UIText.StashDropCommand_jobTitle, commit.name())) {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) {
try {
op.execute(monitor);
} catch (CoreException e) {
Activator.logError(MessageFormat.format(UIText.StashDropCommand_dropFailed, // $NON-NLS-1$
"stash@{" + stashIndex + "}"), // $NON-NLS-1$
e);
}
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
if (JobFamilies.STASH.equals(family))
return true;
return super.belongsTo(family);
}
};
final IWorkbenchPart part = getPart(event);
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()) {
if (part instanceof CommitEditor) {
((CommitEditor) part).close(false);
}
}
}
});
job.setUser(true);
job.setRule(op.getSchedulingRule());
job.schedule();
return null;
}
use of org.eclipse.egit.core.op.StashDropOperation in project egit by eclipse.
the class StashDropCommand method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final List<StashedCommitNode> nodes = getSelectedNodes(event);
if (nodes.isEmpty())
return null;
final Repository repo = nodes.get(0).getRepository();
if (repo == null)
return null;
// Confirm deletion of selected nodes
final AtomicBoolean confirmed = new AtomicBoolean();
final Shell shell = getActiveShell(event);
shell.getDisplay().syncExec(new Runnable() {
@Override
public void run() {
String message;
if (nodes.size() > 1)
message = MessageFormat.format(UIText.StashDropCommand_confirmMultiple, Integer.toString(nodes.size()));
else
message = MessageFormat.format(UIText.StashDropCommand_confirmSingle, Integer.toString(nodes.get(0).getIndex()));
confirmed.set(MessageDialog.openConfirm(shell, UIText.StashDropCommand_confirmTitle, message));
}
});
if (!confirmed.get())
return null;
Job job = new Job(UIText.StashDropCommand_jobTitle) {
@Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask(UIText.StashDropCommand_jobTitle, nodes.size());
// Sort by highest to lowest stash commit index.
// This avoids shifting problems that cause the indices of the
// selected nodes not match the indices in the repository
Collections.sort(nodes, new Comparator<StashedCommitNode>() {
@Override
public int compare(StashedCommitNode n1, StashedCommitNode n2) {
return n1.getIndex() < n2.getIndex() ? 1 : -1;
}
});
for (StashedCommitNode node : nodes) {
final int index = node.getIndex();
if (index < 0)
return null;
final RevCommit commit = node.getObject();
if (commit == null)
return null;
final String stashName = node.getObject().getName();
final StashDropOperation op = new StashDropOperation(repo, node.getIndex());
monitor.subTask(stashName);
try {
op.execute(monitor);
} catch (CoreException e) {
Activator.logError(MessageFormat.format(UIText.StashDropCommand_dropFailed, node.getObject().name()), e);
}
tryToCloseEditor(node);
monitor.worked(1);
}
monitor.done();
return Status.OK_STATUS;
}
private void tryToCloseEditor(final StashedCommitNode node) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorReference[] editorReferences = activePage.getEditorReferences();
for (IEditorReference editorReference : editorReferences) {
IEditorInput editorInput = null;
try {
editorInput = editorReference.getEditorInput();
} catch (PartInitException e) {
Activator.handleError(e.getMessage(), e, true);
}
if (editorInput instanceof CommitEditorInput) {
CommitEditorInput comEditorInput = (CommitEditorInput) editorInput;
if (comEditorInput.getCommit().getRevCommit().equals(node.getObject())) {
activePage.closeEditor(editorReference.getEditor(false), false);
}
}
}
}
});
}
@Override
public boolean belongsTo(Object family) {
if (JobFamilies.STASH.equals(family))
return true;
return super.belongsTo(family);
}
};
job.setUser(true);
job.setRule((new StashDropOperation(repo, nodes.get(0).getIndex())).getSchedulingRule());
job.schedule();
return null;
}
Aggregations