Git Repositories View: Simple fetch and push
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / AbstractResourceOperationAction.java
blob7ab1f5ebccb8eca8d8b7b6f04f71d90ab68a643e
1 /*******************************************************************************
2 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org.eclipse.egit.ui.internal.actions;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.egit.core.op.IEGitOperation;
22 import org.eclipse.egit.ui.Activator;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.ui.IObjectActionDelegate;
27 import org.eclipse.ui.IWorkbenchPart;
29 /**
30 * Common functionality resource based EGit operations.
33 public abstract class AbstractResourceOperationAction implements IObjectActionDelegate {
34 /**
35 * The active workbench part
37 protected IWorkbenchPart wp;
39 private IEGitOperation op;
41 private List selection;
43 public void selectionChanged(final IAction act, final ISelection sel) {
44 if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
45 selection = ((IStructuredSelection) sel).toList();
46 } else {
47 selection = Collections.EMPTY_LIST;
51 public void setActivePart(final IAction act, final IWorkbenchPart part) {
52 wp = part;
55 /**
56 * Instantiate an operation on an action on provided objects.
57 * @param selection
59 * @return a {@link IEGitOperation} for invoking this operation later on
61 protected abstract IEGitOperation createOperation(final List<IResource> selection);
63 /**
64 * @return the name of the execution Job
66 protected abstract String getJobName();
68 /**
69 * A method to invoke when the operation is finished.
70 * The method is called outside the UI thread.
72 protected void postOperation() {
73 // Empty
76 public void run(final IAction act) {
77 op = createOperation(getSelectedResources());
78 if(op==null)
79 return;
80 String jobname = getJobName();
81 Job job = new Job(jobname) {
82 @Override
83 protected IStatus run(IProgressMonitor monitor) {
84 try {
85 op.execute(monitor);
86 postOperation();
87 } catch (CoreException e) {
88 return Activator.createErrorStatus(e.getStatus()
89 .getMessage(), e);
91 return Status.OK_STATUS;
94 job.setUser(true);
95 job.setRule(op.getSchedulingRule());
96 job.schedule();
99 private List<IResource> getSelectedResources() {
100 List<IResource> resources = new ArrayList<IResource>();
101 for(Object object: selection) {
102 if(object instanceof IResource)
103 resources.add((IResource) object);
105 return resources;