1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
13 import java.lang.reflect.InvocationTargetException;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.OperationCanceledException;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.ISchedulingRule;
21 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.jface.operation.IRunnableContext;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.jface.operation.IThreadListener;
30 import org.eclipse.jdt.core.JavaCore;
32 import org.eclipse.jdt.internal.ui.JavaUIStatus;
35 * An {@link IRunnableWithProgress} that adapts an {@link IWorkspaceRunnable} so that is can be
36 * executed inside an {@link IRunnableContext}. The runnable is run as an
37 * {@linkplain JavaCore#run(IWorkspaceRunnable, ISchedulingRule, IProgressMonitor) atomic Java model operation}.
39 * {@link OperationCanceledException}s thrown by the
40 * adapted runnable are caught and re-thrown as {@link InterruptedException}s.
42 public class WorkbenchRunnableAdapter implements IRunnableWithProgress, IThreadListener {
44 private boolean fTransfer= false;
45 private IWorkspaceRunnable fWorkspaceRunnable;
46 private ISchedulingRule fRule;
49 * Runs a workspace runnable with the workspace lock.
51 * @param runnable the runnable
53 public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable) {
54 this(runnable, ResourcesPlugin.getWorkspace().getRoot());
58 * Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at
61 * @param runnable the runnable
62 * @param rule the scheduling rule, or <code>null</code>
64 public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule) {
65 fWorkspaceRunnable= runnable;
70 * Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at
73 * @param runnable the runnable
74 * @param rule the scheduling rule, or <code>null</code>
75 * @param transfer <code>true</code> iff the rule is to be transfered to the modal context
78 public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule, boolean transfer) {
79 fWorkspaceRunnable= runnable;
85 * Returns the scheduling rule, or <code>null</code> if none.
87 * @return the scheduling rule, or <code>null</code> if none
89 public ISchedulingRule getSchedulingRule() {
96 public void threadChange(Thread thread) {
98 Job.getJobManager().transferRule(fRule, thread);
102 * @see IRunnableWithProgress#run(IProgressMonitor)
104 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
106 JavaCore.run(fWorkspaceRunnable, fRule, monitor);
107 } catch (OperationCanceledException e) {
108 throw new InterruptedException(e.getMessage());
109 } catch (CoreException e) {
110 throw new InvocationTargetException(e);
114 public void runAsUserJob(String name, final Object jobFamiliy) {
115 Job job= new Job(name) {
117 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
120 protected IStatus run(IProgressMonitor monitor) {
122 WorkbenchRunnableAdapter.this.run(monitor);
123 } catch (InvocationTargetException e) {
124 Throwable cause= e.getCause();
125 if (cause instanceof CoreException) {
126 return ((CoreException) cause).getStatus();
128 return JavaUIStatus.createError(IStatus.ERROR, cause);
130 } catch (InterruptedException e) {
131 return Status.CANCEL_STATUS;
135 return Status.OK_STATUS;
138 public boolean belongsTo(Object family) {
139 return jobFamiliy == family;
146 // TODO: should block until user pressed 'to background'