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
;
32 * Common functionality resource based EGit operations.
35 public abstract class AbstractRevCommitOperationAction
implements IObjectActionDelegate
{
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();
49 selection
= Collections
.EMPTY_LIST
;
53 public void setActivePart(final IAction act
, final IWorkbenchPart part
) {
58 * Instantiate an operation on an action on provided objects.
61 * @return a {@link IEGitOperation} for invoking this operation later on
63 protected abstract IEGitOperation
createOperation(final List
<RevCommit
> selection
);
66 * @return the name of the execution Job
68 protected abstract String
getJobName();
71 * A method to invoke when the operation is finished.
72 * The method is called outside the UI thread.
74 protected void postOperation() {
78 public void run(final IAction act
) {
79 op
= createOperation(getSelectedCommits());
82 String jobname
= getJobName();
83 Job job
= new Job(jobname
) {
85 protected IStatus
run(IProgressMonitor monitor
) {
89 } catch (CoreException e
) {
90 return Activator
.createErrorStatus(e
.getStatus()
93 return Status
.OK_STATUS
;
97 job
.setRule(op
.getSchedulingRule());
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
);
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();