org.eclipse.egit.ui.internal.repository.tree.command.CheckoutCommand.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.egit.ui.internal.repository.tree.command.CheckoutCommand.java

Source

/*******************************************************************************
 * Copyright (c) 2010 SAP AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Mathias Kinzler (SAP AG) - initial implementation
 *******************************************************************************/
package org.eclipse.egit.ui.internal.repository.tree.command;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.egit.core.op.BranchOperation;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.JobFamilies;
import org.eclipse.egit.ui.UIText;
import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.osgi.util.NLS;

/**
 * Implements "Checkout"
 */
public class CheckoutCommand extends RepositoriesViewCommandHandler<RepositoryTreeNode> {
    public Object execute(final ExecutionEvent event) throws ExecutionException {
        final RepositoryTreeNode node = getSelectedNodes(event).get(0);
        if (!(node.getObject() instanceof Ref))
            return null;

        final Ref ref = (Ref) node.getObject();
        Repository repo = node.getRepository();
        String refName = ref.getLeaf().getName();
        final BranchOperation op;
        if (refName.startsWith(Constants.R_REFS))
            op = new BranchOperation(repo, ref.getName());
        else
            op = new BranchOperation(repo, ref.getLeaf().getObjectId());

        // for the sake of UI responsiveness, let's start a job
        Job job = new Job(NLS.bind(UIText.RepositoriesView_CheckingOutMessage, ref.getName())) {

            @Override
            protected IStatus run(IProgressMonitor monitor) {
                IWorkspaceRunnable wsr = new IWorkspaceRunnable() {

                    public void run(IProgressMonitor myMonitor) throws CoreException {
                        op.execute(myMonitor);
                    }
                };

                try {
                    ResourcesPlugin.getWorkspace().run(wsr, ResourcesPlugin.getWorkspace().getRoot(),
                            IWorkspace.AVOID_UPDATE, monitor);
                } catch (CoreException e1) {
                    return new Status(IStatus.ERROR, Activator.getPluginId(), e1.getMessage(), e1);
                }

                return Status.OK_STATUS;
            }

            @Override
            public boolean belongsTo(Object family) {
                if (family.equals(JobFamilies.CHECKOUT))
                    return true;
                return super.belongsTo(family);
            }
        };

        job.setUser(true);
        job.schedule();

        return null;
    }
}