Check for unsaved changes before Commit
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / BranchAction.java
blob9c78831f578f6fbefae54481266b693ea896000c
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.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.egit.core.op.BranchOperation;
21 import org.eclipse.egit.ui.Activator;
22 import org.eclipse.egit.ui.UIText;
23 import org.eclipse.egit.ui.internal.decorators.GitLightweightDecorator;
24 import org.eclipse.egit.ui.internal.dialogs.BranchSelectionDialog;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.jgit.lib.Repository;
29 import org.eclipse.osgi.util.NLS;
31 /**
32 * Action for selecting a branch and checking it out.
34 * @see BranchOperation
36 public class BranchAction extends RepositoryAction {
37 @Override
38 public void execute(IAction action) throws InvocationTargetException, InterruptedException {
39 final Repository repository = getRepository(true);
40 if (repository == null)
41 return;
43 if (!repository.getRepositoryState().canCheckout()) {
44 MessageDialog.openError(getShell(),
45 UIText.BranchAction_cannotCheckout, NLS.bind(
46 UIText.BranchAction_repositoryState, repository
47 .getRepositoryState().getDescription()));
48 return;
51 BranchSelectionDialog dialog = new BranchSelectionDialog(getShell(), repository);
52 if (dialog.open() != Window.OK) {
53 return;
56 final String refName = dialog.getRefName();
58 String jobname = NLS.bind(UIText.BranchAction_checkingOut, refName);
59 Job job = new Job(jobname) {
60 @Override
61 protected IStatus run(IProgressMonitor monitor) {
62 try {
63 new BranchOperation(repository, refName).execute(monitor);
64 } catch (CoreException e) {
65 return Activator.createErrorStatus(
66 UIText.BranchAction_branchFailed, e);
67 } finally {
68 GitLightweightDecorator.refresh();
70 return Status.OK_STATUS;
73 job.setUser(true);
74 job.schedule();
77 @Override
78 public boolean isEnabled() {
79 return getRepository(false) != null;