1 /*******************************************************************************
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *******************************************************************************/
10 package org
.eclipse
.egit
.ui
.internal
.actions
;
12 import java
.lang
.reflect
.InvocationTargetException
;
13 import java
.util
.Collections
;
14 import java
.util
.List
;
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
.egit
.core
.op
.IEGitOperation
;
21 import org
.eclipse
.egit
.ui
.Activator
;
22 import org
.eclipse
.egit
.ui
.UIText
;
23 import org
.eclipse
.jface
.action
.IAction
;
24 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
25 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
26 import org
.eclipse
.jface
.viewers
.ISelection
;
27 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
28 import org
.eclipse
.osgi
.util
.NLS
;
29 import org
.eclipse
.ui
.IObjectActionDelegate
;
30 import org
.eclipse
.ui
.IWorkbenchPart
;
33 * Common functionality for EGit operations.
35 public abstract class AbstractOperationAction
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 selection
);
66 * A method to invoke when the operation is finished.
68 protected void postOperation() {
72 public void run(final IAction act
) {
73 op
= createOperation(selection
);
77 wp
.getSite().getWorkbenchWindow().run(true, false,
78 new IRunnableWithProgress() {
79 public void run(final IProgressMonitor monitor
)
80 throws InvocationTargetException
{
83 } catch (CoreException ce
) {
84 throw new InvocationTargetException(ce
);
91 } catch (Throwable e
) {
92 final String msg
= NLS
.bind(UIText
.GenericOperationFailed
, act
96 if (e
instanceof InvocationTargetException
) {
100 if (e
instanceof CoreException
) {
101 status
= ((CoreException
) e
).getStatus();
103 status
= new Status(IStatus
.ERROR
, Activator
.getPluginId(),
107 Activator
.logError(msg
, e
);
108 ErrorDialog
.openError(wp
.getSite().getShell(), act
.getText(),
109 msg
, status
, status
.getSeverity());