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
.resources
.IWorkspaceRunnable
;
17 import org
.eclipse
.core
.runtime
.CoreException
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.IStatus
;
20 import org
.eclipse
.core
.runtime
.Status
;
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 IWorkspaceRunnable op
;
43 public void selectionChanged(final IAction act
, final ISelection sel
) {
44 // work performed in setActivePart
47 public void setActivePart(final IAction act
, final IWorkbenchPart part
) {
49 ISelection sel
= part
.getSite().getPage().getSelection();
51 if (sel
instanceof IStructuredSelection
&& !sel
.isEmpty()) {
52 selection
= ((IStructuredSelection
) sel
).toList();
54 selection
= Collections
.EMPTY_LIST
;
56 op
= createOperation(act
, selection
);
57 act
.setEnabled(op
!= null && wp
!= null);
61 * Instantiate an operation on an action on provided objects.
65 * @return a {@link IWorkspaceRunnable} for invoking this operation later on
67 protected abstract IWorkspaceRunnable
createOperation(final IAction act
,
68 final List selection
);
71 * A method to invoke when the operation is finished.
73 protected void postOperation() {
77 public void run(final IAction act
) {
81 wp
.getSite().getWorkbenchWindow().run(true, false,
82 new IRunnableWithProgress() {
83 public void run(final IProgressMonitor monitor
)
84 throws InvocationTargetException
{
87 } catch (CoreException ce
) {
88 throw new InvocationTargetException(ce
);
95 } catch (Throwable e
) {
96 final String msg
= NLS
.bind(UIText
.GenericOperationFailed
, act
100 if (e
instanceof InvocationTargetException
) {
104 if (e
instanceof CoreException
) {
105 status
= ((CoreException
) e
).getStatus();
106 e
= status
.getException();
108 status
= new Status(IStatus
.ERROR
, Activator
.getPluginId(),
112 Activator
.logError(msg
, e
);
113 ErrorDialog
.openError(wp
.getSite().getShell(), act
.getText(),
114 msg
, status
, status
.getSeverity());