Replace getWorkbenchWindow().run by using a Job
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / ResetAction.java
blobbe5955ea9407b7d6b50307ad028080099140d664
1 /*******************************************************************************
2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.actions;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.egit.core.op.ResetOperation;
19 import org.eclipse.egit.core.op.ResetOperation.ResetType;
20 import org.eclipse.egit.ui.Activator;
21 import org.eclipse.egit.ui.UIText;
22 import org.eclipse.egit.ui.internal.decorators.GitLightweightDecorator;
23 import org.eclipse.egit.ui.internal.dialogs.BranchSelectionDialog;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.dialogs.IDialogConstants;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jgit.lib.Repository;
28 import org.eclipse.osgi.util.NLS;
30 /**
31 * An action to reset the current branch to a specific revision.
33 * @see ResetOperation
35 public class ResetAction extends RepositoryAction {
37 @Override
38 public void execute(IAction action) {
39 final Repository repository = getRepository(true);
40 if (repository == null)
41 return;
42 if (!repository.getRepositoryState().canResetHead()) {
43 MessageDialog.openError(getShell(), UIText.ResetAction_errorResettingHead,
44 NLS.bind(UIText.ResetAction_repositoryState, repository.getRepositoryState().getDescription()));
45 return;
47 BranchSelectionDialog branchSelectionDialog = new BranchSelectionDialog(getShell(), repository, true);
48 if (branchSelectionDialog.open() == IDialogConstants.OK_ID) {
49 final String refName = branchSelectionDialog.getRefName();
50 final ResetType type = branchSelectionDialog.getResetType();
51 String jobname = NLS.bind(UIText.ResetAction_reset, refName);
52 final ResetOperation operation = new ResetOperation(repository,
53 refName, type);
54 Job job = new Job(jobname) {
55 @Override
56 protected IStatus run(IProgressMonitor monitor) {
57 try {
58 operation.execute(monitor);
59 GitLightweightDecorator.refresh();
60 } catch (CoreException e) {
61 return Activator.createErrorStatus(e.getStatus()
62 .getMessage(), e);
64 return Status.OK_STATUS;
67 job.setRule(operation.getSchedulingRule());
68 job.setUser(true);
69 job.schedule();
73 @Override
74 public boolean isEnabled() {
75 return getRepository(false) != null;