tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / operations / TmfWorkspaceModifyOperation.java
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 *******************************************************************************/
12 package org.eclipse.linuxtools.internal.tmf.ui.project.operations;
13
14 import java.lang.reflect.InvocationTargetException;
15
16 import org.eclipse.core.resources.IWorkspace;
17 import org.eclipse.core.resources.IWorkspaceRunnable;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.OperationCanceledException;
22 import org.eclipse.core.runtime.jobs.ISchedulingRule;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.ui.actions.WorkspaceModifyOperation;
25
26 /**
27 * Operation to modify the workspace that refreshes workspace at the end of the operation.
28 *
29 * For refreshing periodically use {@link WorkspaceModifyOperation} instead.
30 *
31 * @author Bernd Hufmann
32 *
33 */
34 public abstract class TmfWorkspaceModifyOperation implements IRunnableWithProgress {
35
36 private ISchedulingRule rule;
37
38 /**
39 * Creates a new operation.
40 */
41 protected TmfWorkspaceModifyOperation() {
42 this(ResourcesPlugin.getWorkspace().getRoot());
43 }
44
45 /**
46 * Creates a new operation that will run using the provided scheduling rule.
47 *
48 * @param rule
49 * The ISchedulingRule to use or <code>null</code>.
50 */
51 protected TmfWorkspaceModifyOperation(ISchedulingRule rule) {
52 this.rule = rule;
53 }
54
55 @Override
56 public synchronized final void run(IProgressMonitor monitor)
57 throws InvocationTargetException, InterruptedException {
58 final InvocationTargetException[] iteHolder = new InvocationTargetException[1];
59 try {
60 IWorkspaceRunnable workspaceRunnable = new IWorkspaceRunnable() {
61 @Override
62 public void run(IProgressMonitor pm) throws CoreException {
63 try {
64 execute(pm);
65 } catch (InvocationTargetException e) {
66 // Pass it outside the workspace runnable
67 iteHolder[0] = e;
68 } catch (InterruptedException e) {
69 // Re-throw as OperationCanceledException, which will be
70 // caught and re-thrown as InterruptedException below.
71 throw new OperationCanceledException(e.getMessage());
72 }
73 // CoreException and OperationCanceledException are propagated
74 }
75 };
76
77 IWorkspace workspace = ResourcesPlugin.getWorkspace();
78 workspace.run(workspaceRunnable, rule, IWorkspace.AVOID_UPDATE, monitor);
79 } catch (CoreException e) {
80 throw new InvocationTargetException(e);
81 } catch (OperationCanceledException e) {
82 throw new InterruptedException(e.getMessage());
83 }
84 // Re-throw the InvocationTargetException, if any occurred
85 if (iteHolder[0] != null) {
86 throw iteHolder[0];
87 }
88 }
89
90 /**
91 * Performs the steps that are to be treated as a single logical workspace
92 * change.
93 * <p>
94 * Subclasses must implement this method.
95 * </p>
96 *
97 * @param monitor
98 * the progress monitor to use to display progress and field user
99 * requests to cancel
100 * @exception CoreException
101 * if the operation fails due to a CoreException
102 * @exception InvocationTargetException
103 * if the operation fails due to an exception other than
104 * CoreException
105 * @exception InterruptedException
106 * if the operation detects a request to cancel, using
107 * <code>IProgressMonitor.isCanceled()</code>, it should exit
108 * by throwing <code>InterruptedException</code>. It is also
109 * possible to throw <code>OperationCanceledException</code>,
110 * which gets mapped to <code>InterruptedException</code> by
111 * the <code>run</code> method.
112 */
113 protected abstract void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException;
114 }
This page took 0.035774 seconds and 5 git commands to generate.