Initial EGit contribution to eclipse.org
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / AbstractOperationAction.java
blobbe0cf4dc98ce9c00ce5f010270578ddbdb842bd8
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;
32 /**
33 * Common functionality for EGit operations.
35 public abstract class AbstractOperationAction implements IObjectActionDelegate {
36 /**
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) {
48 wp = part;
49 ISelection sel = part.getSite().getPage().getSelection();
50 final List selection;
51 if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
52 selection = ((IStructuredSelection) sel).toList();
53 } else {
54 selection = Collections.EMPTY_LIST;
56 op = createOperation(act, selection);
57 act.setEnabled(op != null && wp != null);
60 /**
61 * Instantiate an operation on an action on provided objects.
63 * @param act
64 * @param selection
65 * @return a {@link IWorkspaceRunnable} for invoking this operation later on
67 protected abstract IWorkspaceRunnable createOperation(final IAction act,
68 final List selection);
70 /**
71 * A method to invoke when the operation is finished.
73 protected void postOperation() {
74 // Empty
77 public void run(final IAction act) {
78 if (op != null) {
79 try {
80 try {
81 wp.getSite().getWorkbenchWindow().run(true, false,
82 new IRunnableWithProgress() {
83 public void run(final IProgressMonitor monitor)
84 throws InvocationTargetException {
85 try {
86 op.run(monitor);
87 } catch (CoreException ce) {
88 throw new InvocationTargetException(ce);
91 });
92 } finally {
93 postOperation();
95 } catch (Throwable e) {
96 final String msg = NLS.bind(UIText.GenericOperationFailed, act
97 .getText());
98 final IStatus status;
100 if (e instanceof InvocationTargetException) {
101 e = e.getCause();
104 if (e instanceof CoreException) {
105 status = ((CoreException) e).getStatus();
106 e = status.getException();
107 } else {
108 status = new Status(IStatus.ERROR, Activator.getPluginId(),
109 1, msg, e);
112 Activator.logError(msg, e);
113 ErrorDialog.openError(wp.getSite().getShell(), act.getText(),
114 msg, status, status.getSeverity());