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 * See LICENSE for the full license text, also available.
9 *******************************************************************************/
10 package org
.spearce
.egit
.ui
.internal
.actions
;
12 import java
.lang
.reflect
.InvocationTargetException
;
14 import org
.eclipse
.core
.runtime
.CoreException
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.jface
.action
.IAction
;
17 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
18 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
19 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
20 import org
.eclipse
.swt
.widgets
.Display
;
21 import org
.spearce
.egit
.core
.op
.BranchOperation
;
22 import org
.spearce
.egit
.ui
.internal
.decorators
.GitLightweightDecorator
;
23 import org
.spearce
.egit
.ui
.internal
.dialogs
.BranchSelectionDialog
;
24 import org
.spearce
.jgit
.lib
.Repository
;
27 * Action for selecting a branch and checking it out.
29 * @see BranchOperation
31 public class BranchAction
extends RepositoryAction
{
33 public void run(IAction action
) {
34 final Repository repository
= getRepository(true);
35 if (repository
== null)
38 if (!repository
.getRepositoryState().canCheckout()) {
39 MessageDialog
.openError(getShell(), "Cannot checkout now",
41 + repository
.getRepositoryState().getDescription());
45 BranchSelectionDialog dialog
= new BranchSelectionDialog(getShell(), repository
);
46 dialog
.setShowResetType(false);
47 if (dialog
.open() != IDialogConstants
.OK_ID
) {
51 final String refName
= dialog
.getRefName();
53 getTargetPart().getSite().getWorkbenchWindow().run(true, false,
54 new IRunnableWithProgress() {
55 public void run(final IProgressMonitor monitor
)
56 throws InvocationTargetException
{
58 new BranchOperation(repository
, refName
).run(monitor
);
59 GitLightweightDecorator
.refresh();
60 } catch (final CoreException ce
) {
62 Display
.getDefault().asyncExec(new Runnable() {
64 handle(ce
, "Error while switching branches", "Unable to switch branches");
70 } catch (InvocationTargetException e
) {
72 } catch (InterruptedException e
) {
78 public boolean isEnabled() {
79 return getRepository(false) != null;