2 * Copyright (C) 2006 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.egit
.ui
.internal
.actions
;
19 import java
.lang
.reflect
.InvocationTargetException
;
20 import java
.util
.Collections
;
21 import java
.util
.List
;
23 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.IStatus
;
27 import org
.eclipse
.core
.runtime
.Status
;
28 import org
.eclipse
.jface
.action
.IAction
;
29 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
30 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
31 import org
.eclipse
.jface
.viewers
.ISelection
;
32 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
33 import org
.eclipse
.osgi
.util
.NLS
;
34 import org
.eclipse
.ui
.IObjectActionDelegate
;
35 import org
.eclipse
.ui
.IWorkbenchPart
;
36 import org
.spearce
.egit
.ui
.Activator
;
37 import org
.spearce
.egit
.ui
.UIText
;
40 * Common functionality for EGit operations.
42 public abstract class AbstractOperationAction
implements IObjectActionDelegate
{
43 private IWorkbenchPart wp
;
45 private IWorkspaceRunnable op
;
47 public void selectionChanged(final IAction act
, final ISelection sel
) {
49 if (sel
instanceof IStructuredSelection
&& !sel
.isEmpty()) {
50 selection
= ((IStructuredSelection
) sel
).toList();
52 selection
= Collections
.EMPTY_LIST
;
54 op
= createOperation(act
, selection
);
55 act
.setEnabled(op
!= null && wp
!= null);
58 public void setActivePart(final IAction act
, final IWorkbenchPart part
) {
63 * Instantiate an operation on an action on provided objects.
67 * @return a {@link IWorkspaceRunnable} for invoking this operation later on
69 protected abstract IWorkspaceRunnable
createOperation(final IAction act
,
70 final List selection
);
73 * A method to invoke when the operation is finished.
75 protected void postOperation() {
79 public void run(final IAction act
) {
83 wp
.getSite().getWorkbenchWindow().run(true, false,
84 new IRunnableWithProgress() {
85 public void run(final IProgressMonitor monitor
)
86 throws InvocationTargetException
{
89 } catch (CoreException ce
) {
90 throw new InvocationTargetException(ce
);
97 } catch (Throwable e
) {
98 final String msg
= NLS
.bind(UIText
.GenericOperationFailed
, act
100 final IStatus status
;
102 if (e
instanceof InvocationTargetException
) {
106 if (e
instanceof CoreException
) {
107 status
= ((CoreException
) e
).getStatus();
108 e
= status
.getException();
110 status
= new Status(IStatus
.ERROR
, Activator
.getPluginId(),
114 Activator
.logError(msg
, e
);
115 ErrorDialog
.openError(wp
.getSite().getShell(), act
.getText(),
116 msg
, status
, status
.getSeverity());