1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
/*******************************************************************************
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.egit.ui.internal.actions;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIText;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
/**
* Common functionality for EGit operations.
*/
public abstract class AbstractOperationAction implements IObjectActionDelegate {
/**
* The active workbench part
*/
protected IWorkbenchPart wp;
private IWorkspaceRunnable op;
private List selection;
public void selectionChanged(final IAction act, final ISelection sel) {
if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
selection = ((IStructuredSelection) sel).toList();
} else {
selection = Collections.EMPTY_LIST;
}
}
public void setActivePart(final IAction act, final IWorkbenchPart part) {
wp = part;
}
/**
* Instantiate an operation on an action on provided objects.
* @param selection
*
* @return a {@link IWorkspaceRunnable} for invoking this operation later on
*/
protected abstract IWorkspaceRunnable createOperation(final List selection);
/**
* A method to invoke when the operation is finished.
*/
protected void postOperation() {
// Empty
}
public void run(final IAction act) {
op = createOperation(selection);
if (op != null) {
try {
try {
wp.getSite().getWorkbenchWindow().run(true, false,
new IRunnableWithProgress() {
public void run(final IProgressMonitor monitor)
throws InvocationTargetException {
try {
op.run(monitor);
} catch (CoreException ce) {
throw new InvocationTargetException(ce);
}
}
});
} finally {
postOperation();
}
} catch (Throwable e) {
final String msg = NLS.bind(UIText.GenericOperationFailed, act
.getText());
final IStatus status;
if (e instanceof InvocationTargetException) {
e = e.getCause();
}
if (e instanceof CoreException) {
status = ((CoreException) e).getStatus();
} else {
status = new Status(IStatus.ERROR, Activator.getPluginId(),
1, msg, e);
}
Activator.logError(msg, e);
ErrorDialog.openError(wp.getSite().getShell(), act.getText(),
msg, status, status.getSeverity());
}
}
}
}
|