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
;
30 * Common functionality resource based EGit operations.
33 public abstract class AbstractResourceOperationAction
implements IObjectActionDelegate
{
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();
47 selection
= Collections
.EMPTY_LIST
;
51 public void setActivePart(final IAction act
, final IWorkbenchPart part
) {
56 * Instantiate an operation on an action on provided objects.
59 * @return a {@link IEGitOperation} for invoking this operation later on
61 protected abstract IEGitOperation
createOperation(final List
<IResource
> selection
);
64 * @return the name of the execution Job
66 protected abstract String
getJobName();
69 * A method to invoke when the operation is finished.
70 * The method is called outside the UI thread.
72 protected void postOperation() {
76 public void run(final IAction act
) {
77 op
= createOperation(getSelectedResources());
80 String jobname
= getJobName();
81 Job job
= new Job(jobname
) {
83 protected IStatus
run(IProgressMonitor monitor
) {
87 } catch (CoreException e
) {
88 return Activator
.createErrorStatus(e
.getStatus()
91 return Status
.OK_STATUS
;
95 job
.setRule(op
.getSchedulingRule());
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
);