RepositoryTreeNode: fix compareTo() implemenation
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / AbstractOperationAction.java
blob38f3312a39e42b84eccfdb3af7c12986baa8a880
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;
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 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();
48 } else {
49 selection = Collections.EMPTY_LIST;
53 public void setActivePart(final IAction act, final IWorkbenchPart part) {
54 wp = part;
57 /**
58 * Instantiate an operation on an action on provided objects.
59 * @param selection
61 * @return a {@link IEGitOperation} for invoking this operation later on
63 protected abstract IEGitOperation createOperation(final List selection);
65 /**
66 * A method to invoke when the operation is finished.
68 protected void postOperation() {
69 // Empty
72 public void run(final IAction act) {
73 op = createOperation(selection);
74 if (op != null) {
75 try {
76 try {
77 wp.getSite().getWorkbenchWindow().run(true, false,
78 new IRunnableWithProgress() {
79 public void run(final IProgressMonitor monitor)
80 throws InvocationTargetException {
81 try {
82 op.execute(monitor);
83 } catch (CoreException ce) {
84 throw new InvocationTargetException(ce);
87 });
88 } finally {
89 postOperation();
91 } catch (Throwable e) {
92 final String msg = NLS.bind(UIText.GenericOperationFailed, act
93 .getText());
94 final IStatus status;
96 if (e instanceof InvocationTargetException) {
97 e = e.getCause();
100 if (e instanceof CoreException) {
101 status = ((CoreException) e).getStatus();
102 } else {
103 status = new Status(IStatus.ERROR, Activator.getPluginId(),
104 1, msg, e);
107 Activator.logError(msg, e);
108 ErrorDialog.openError(wp.getSite().getShell(), act.getText(),
109 msg, status, status.getSeverity());