Check for unsaved changes before Commit
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / AbstractRevCommitOperationAction.java
blob360c88f7a7bb801ec15d3f5c2b8935dc75eedb8c
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.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.IEGitOperation;
21 import org.eclipse.egit.ui.Activator;
22 import org.eclipse.egit.ui.internal.history.RevObjectSelectionProvider;
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.jgit.lib.Repository;
27 import org.eclipse.jgit.revwalk.RevCommit;
28 import org.eclipse.ui.IObjectActionDelegate;
29 import org.eclipse.ui.IWorkbenchPart;
31 /**
32 * Common functionality resource based EGit operations.
35 public abstract class AbstractRevCommitOperationAction implements IObjectActionDelegate {
36 /**
37 * The active workbench part
39 protected IWorkbenchPart wp;
41 private IEGitOperation op;
43 private List selection;
45 public void selectionChanged(final IAction act, final ISelection sel) {
46 if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
47 selection = ((IStructuredSelection) sel).toList();
48 } else {
49 selection = Collections.EMPTY_LIST;
53 public void setActivePart(final IAction act, final IWorkbenchPart part) {
54 wp = part;
57 /**
58 * Instantiate an operation on an action on provided objects.
59 * @param selection
61 * @return a {@link IEGitOperation} for invoking this operation later on
63 protected abstract IEGitOperation createOperation(final List<RevCommit> selection);
65 /**
66 * @return the name of the execution Job
68 protected abstract String getJobName();
70 /**
71 * A method to invoke when the operation is finished.
72 * The method is called outside the UI thread.
74 protected void postOperation() {
75 // Empty
78 public void run(final IAction act) {
79 op = createOperation(getSelectedCommits());
80 if(op==null)
81 return;
82 String jobname = getJobName();
83 Job job = new Job(jobname) {
84 @Override
85 protected IStatus run(IProgressMonitor monitor) {
86 try {
87 op.execute(monitor);
88 postOperation();
89 } catch (CoreException e) {
90 return Activator.createErrorStatus(e.getStatus()
91 .getMessage(), e);
93 return Status.OK_STATUS;
96 job.setUser(true);
97 job.setRule(op.getSchedulingRule());
98 job.schedule();
101 private List<RevCommit> getSelectedCommits() {
102 List<RevCommit> commits = new ArrayList<RevCommit>();
103 for(Object object: selection) {
104 if(object instanceof RevCommit)
105 commits.add((RevCommit) object);
107 return commits;
111 * Find out which repository is involved here
113 * @return the Git repository associated with the selected RevObject
115 protected Repository getActiveRepository() {
116 RevObjectSelectionProvider selectionProvider = (RevObjectSelectionProvider) wp
117 .getSite().getSelectionProvider();
118 return selectionProvider.getActiveRepository();