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 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.operations
;
14 import java
.lang
.reflect
.InvocationTargetException
;
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
;
27 * Operation to modify the workspace that refreshes workspace at the end of the operation.
29 * For refreshing periodically use {@link WorkspaceModifyOperation} instead.
31 * @author Bernd Hufmann
34 public abstract class TmfWorkspaceModifyOperation
implements IRunnableWithProgress
{
36 private ISchedulingRule rule
;
39 * Creates a new operation.
41 protected TmfWorkspaceModifyOperation() {
42 this(ResourcesPlugin
.getWorkspace().getRoot());
46 * Creates a new operation that will run using the provided scheduling rule.
49 * The ISchedulingRule to use or <code>null</code>.
51 protected TmfWorkspaceModifyOperation(ISchedulingRule rule
) {
56 public synchronized final void run(IProgressMonitor monitor
)
57 throws InvocationTargetException
, InterruptedException
{
58 final InvocationTargetException
[] iteHolder
= new InvocationTargetException
[1];
60 IWorkspaceRunnable workspaceRunnable
= new IWorkspaceRunnable() {
62 public void run(IProgressMonitor pm
) throws CoreException
{
65 } catch (InvocationTargetException e
) {
66 // Pass it outside the workspace runnable
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());
73 // CoreException and OperationCanceledException are propagated
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());
84 // Re-throw the InvocationTargetException, if any occurred
85 if (iteHolder
[0] != null) {
91 * Performs the steps that are to be treated as a single logical workspace
94 * Subclasses must implement this method.
98 * the progress monitor to use to display progress and field user
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
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.
113 protected abstract void execute(IProgressMonitor monitor
) throws CoreException
, InvocationTargetException
, InterruptedException
;