Commit | Line | Data |
---|---|---|
15b23e97 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Bernd Hufmann - Initial API and implementation | |
11 | *******************************************************************************/ | |
024a2c6b AM |
12 | |
13 | package org.eclipse.tracecompass.tmf.ui.project.operations; | |
15b23e97 BH |
14 | |
15 | import java.lang.reflect.InvocationTargetException; | |
16 | ||
17 | import org.eclipse.core.resources.IWorkspace; | |
18 | import org.eclipse.core.resources.IWorkspaceRunnable; | |
19 | import org.eclipse.core.resources.ResourcesPlugin; | |
20 | import org.eclipse.core.runtime.CoreException; | |
21 | import org.eclipse.core.runtime.IProgressMonitor; | |
22 | import org.eclipse.core.runtime.OperationCanceledException; | |
23 | import org.eclipse.core.runtime.jobs.ISchedulingRule; | |
24 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
25 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
26 | ||
27 | /** | |
28 | * Operation to modify the workspace that refreshes workspace at the end of the operation. | |
29 | * | |
30 | * For refreshing periodically use {@link WorkspaceModifyOperation} instead. | |
31 | * | |
32 | * @author Bernd Hufmann | |
024a2c6b | 33 | * @since 2.3 |
15b23e97 BH |
34 | */ |
35 | public abstract class TmfWorkspaceModifyOperation implements IRunnableWithProgress { | |
36 | ||
37 | private ISchedulingRule rule; | |
38 | ||
39 | /** | |
40 | * Creates a new operation. | |
41 | */ | |
42 | protected TmfWorkspaceModifyOperation() { | |
43 | this(ResourcesPlugin.getWorkspace().getRoot()); | |
44 | } | |
45 | ||
46 | /** | |
47 | * Creates a new operation that will run using the provided scheduling rule. | |
48 | * | |
49 | * @param rule | |
50 | * The ISchedulingRule to use or <code>null</code>. | |
51 | */ | |
52 | protected TmfWorkspaceModifyOperation(ISchedulingRule rule) { | |
53 | this.rule = rule; | |
54 | } | |
55 | ||
56 | @Override | |
36ef128d | 57 | public final synchronized void run(IProgressMonitor monitor) |
15b23e97 BH |
58 | throws InvocationTargetException, InterruptedException { |
59 | final InvocationTargetException[] iteHolder = new InvocationTargetException[1]; | |
60 | try { | |
61 | IWorkspaceRunnable workspaceRunnable = new IWorkspaceRunnable() { | |
62 | @Override | |
63 | public void run(IProgressMonitor pm) throws CoreException { | |
64 | try { | |
65 | execute(pm); | |
66 | } catch (InvocationTargetException e) { | |
67 | // Pass it outside the workspace runnable | |
68 | iteHolder[0] = e; | |
69 | } catch (InterruptedException e) { | |
70 | // Re-throw as OperationCanceledException, which will be | |
71 | // caught and re-thrown as InterruptedException below. | |
72 | throw new OperationCanceledException(e.getMessage()); | |
73 | } | |
74 | // CoreException and OperationCanceledException are propagated | |
75 | } | |
76 | }; | |
77 | ||
78 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); | |
79 | workspace.run(workspaceRunnable, rule, IWorkspace.AVOID_UPDATE, monitor); | |
80 | } catch (CoreException e) { | |
81 | throw new InvocationTargetException(e); | |
82 | } catch (OperationCanceledException e) { | |
83 | throw new InterruptedException(e.getMessage()); | |
84 | } | |
85 | // Re-throw the InvocationTargetException, if any occurred | |
86 | if (iteHolder[0] != null) { | |
87 | throw iteHolder[0]; | |
88 | } | |
89 | } | |
90 | ||
91 | /** | |
92 | * Performs the steps that are to be treated as a single logical workspace | |
93 | * change. | |
94 | * <p> | |
95 | * Subclasses must implement this method. | |
96 | * </p> | |
97 | * | |
98 | * @param monitor | |
99 | * the progress monitor to use to display progress and field user | |
100 | * requests to cancel | |
101 | * @exception CoreException | |
102 | * if the operation fails due to a CoreException | |
103 | * @exception InvocationTargetException | |
104 | * if the operation fails due to an exception other than | |
105 | * CoreException | |
106 | * @exception InterruptedException | |
107 | * if the operation detects a request to cancel, using | |
108 | * <code>IProgressMonitor.isCanceled()</code>, it should exit | |
109 | * by throwing <code>InterruptedException</code>. It is also | |
110 | * possible to throw <code>OperationCanceledException</code>, | |
111 | * which gets mapped to <code>InterruptedException</code> by | |
112 | * the <code>run</code> method. | |
113 | */ | |
114 | protected abstract void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException; | |
115 | } |