Refactor execute to RepositoryAction
[egit.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / internal / actions / AbstractOperationAction.java
blob0e963234452d5a9c7443c04a20f23d9471fca410
1 /*
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;
39 /**
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) {
48 final List selection;
49 if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
50 selection = ((IStructuredSelection) sel).toList();
51 } else {
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) {
59 wp = part;
62 /**
63 * Instantiate an operation on an action on provided objects.
65 * @param act
66 * @param selection
67 * @return a {@link IWorkspaceRunnable} for invoking this operation later on
69 protected abstract IWorkspaceRunnable createOperation(final IAction act,
70 final List selection);
72 /**
73 * A method to invoke when the operation is finished.
75 protected void postOperation() {
76 // Empty
79 public void run(final IAction act) {
80 if (op != null) {
81 try {
82 try {
83 wp.getSite().getWorkbenchWindow().run(true, false,
84 new IRunnableWithProgress() {
85 public void run(final IProgressMonitor monitor)
86 throws InvocationTargetException {
87 try {
88 op.run(monitor);
89 } catch (CoreException ce) {
90 throw new InvocationTargetException(ce);
93 });
94 } finally {
95 postOperation();
97 } catch (Throwable e) {
98 final String msg = NLS.bind(UIText.GenericOperationFailed, act
99 .getText());
100 final IStatus status;
102 if (e instanceof InvocationTargetException) {
103 e = e.getCause();
106 if (e instanceof CoreException) {
107 status = ((CoreException) e).getStatus();
108 e = status.getException();
109 } else {
110 status = new Status(IStatus.ERROR, Activator.getPluginId(),
111 1, msg, e);
114 Activator.logError(msg, e);
115 ErrorDialog.openError(wp.getSite().getShell(), act.getText(),
116 msg, status, status.getSeverity());