Make tracing 3.4 compatible and plug-in local
[egit/spearce.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / ResetAction.java
blob576816c6264d53db62a346a5443855b079f7d6c5
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 java.lang.reflect.InvocationTargetException;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.egit.core.op.ResetOperation;
18 import org.eclipse.egit.core.op.ResetOperation.ResetType;
19 import org.eclipse.egit.ui.UIText;
20 import org.eclipse.egit.ui.internal.decorators.GitLightweightDecorator;
21 import org.eclipse.egit.ui.internal.dialogs.BranchSelectionDialog;
22 import org.eclipse.egit.ui.internal.trace.GitTraceLocation;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.operation.IRunnableWithProgress;
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 run(IAction action) {
39 final Repository repository = getRepository(true);
40 if (repository == null)
41 return;
43 if (!repository.getRepositoryState().canResetHead()) {
44 MessageDialog.openError(getShell(), UIText.ResetAction_errorResettingHead,
45 NLS.bind(UIText.ResetAction_repositoryState, repository.getRepositoryState().getDescription()));
46 return;
49 BranchSelectionDialog branchSelectionDialog = new BranchSelectionDialog(getShell(), repository);
50 if (branchSelectionDialog.open() == IDialogConstants.OK_ID) {
51 final String refName = branchSelectionDialog.getRefName();
52 final ResetType type = branchSelectionDialog.getResetType();
54 try {
55 getTargetPart().getSite().getWorkbenchWindow().run(true, false,
56 new IRunnableWithProgress() {
57 public void run(final IProgressMonitor monitor)
58 throws InvocationTargetException {
59 try {
60 new ResetOperation(repository, refName, type).run(monitor);
61 GitLightweightDecorator.refresh();
62 } catch (CoreException e) {
63 if (GitTraceLocation.UI.isActive())
64 GitTraceLocation.getTrace().trace(GitTraceLocation.UI.getLocation(), e.getMessage(), e);
65 throw new InvocationTargetException(e);
68 });
69 } catch (InvocationTargetException e) {
70 MessageDialog.openError(getShell(),UIText.ResetAction_resetFailed, e.getMessage());
71 } catch (InterruptedException e) {
72 MessageDialog.openError(getShell(),UIText.ResetAction_resetFailed, e.getMessage());
78 @Override
79 public boolean isEnabled() {
80 return getRepository(false) != null;