1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.project
.operations
;
15 import java
.lang
.reflect
.InvocationTargetException
;
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
;
28 * Operation to modify the workspace that refreshes workspace at the end of the operation.
30 * For refreshing periodically use {@link WorkspaceModifyOperation} instead.
32 * @author Bernd Hufmann
35 public abstract class TmfWorkspaceModifyOperation
implements IRunnableWithProgress
{
37 private ISchedulingRule rule
;
40 * Creates a new operation.
42 protected TmfWorkspaceModifyOperation() {
43 this(ResourcesPlugin
.getWorkspace().getRoot());
47 * Creates a new operation that will run using the provided scheduling rule.
50 * The ISchedulingRule to use or <code>null</code>.
52 protected TmfWorkspaceModifyOperation(ISchedulingRule rule
) {
57 public final synchronized void run(IProgressMonitor monitor
)
58 throws InvocationTargetException
, InterruptedException
{
59 final InvocationTargetException
[] iteHolder
= new InvocationTargetException
[1];
61 IWorkspaceRunnable workspaceRunnable
= new IWorkspaceRunnable() {
63 public void run(IProgressMonitor pm
) throws CoreException
{
66 } catch (InvocationTargetException e
) {
67 // Pass it outside the workspace runnable
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());
74 // CoreException and OperationCanceledException are propagated
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());
85 // Re-throw the InvocationTargetException, if any occurred
86 if (iteHolder
[0] != null) {
92 * Performs the steps that are to be treated as a single logical workspace
95 * Subclasses must implement this method.
99 * the progress monitor to use to display progress and field user
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
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.
114 protected abstract void execute(IProgressMonitor monitor
) throws CoreException
, InvocationTargetException
, InterruptedException
;