KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > WorkbenchRunnableAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ltk.internal.ui.refactoring;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.OperationCanceledException;
19 import org.eclipse.core.runtime.jobs.ISchedulingRule;
20 import org.eclipse.core.runtime.jobs.Job;
21
22 import org.eclipse.core.resources.IWorkspace;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24 import org.eclipse.core.resources.ResourcesPlugin;
25
26 import org.eclipse.jface.operation.IRunnableWithProgress;
27 import org.eclipse.jface.operation.IThreadListener;
28
29 /**
30  * An <code>IRunnableWithProgress</code> that adapts an <code>IWorkspaceRunnable</code>
31  * so that is can be executed inside <code>IRunnableContext</code>. <code>OperationCanceledException</code>
32  * thrown by the adapted runnable are caught and re-thrown as a <code>InterruptedException</code>.
33  */

34 public class WorkbenchRunnableAdapter implements IRunnableWithProgress, IThreadListener {
35     
36     private IWorkspaceRunnable fWorkspaceRunnable;
37     private ISchedulingRule fRule;
38     private boolean fTransfer;
39     
40     /**
41      * Runs a workspace runnable with the given lock or <code>null</code>
42      * to run with no lock at all.
43      *
44      * @param runnable the workspace runnable
45      * @param rule the scheduling rule
46      */

47     public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule) {
48         this(runnable, rule, true);
49     }
50     
51     /**
52      * Runs a workspace runnable with the given lock or <code>null</code>
53      * to run with no lock at all.
54      *
55      * @param runnable the workspace runnable
56      * @param rule the scheduling rule
57      * @param transfer <code>true</code> if the rule is to be transfered
58      * to the model context thread. Otherwise <code>false</code>
59      *
60      * @since 3.1
61      */

62     public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule, boolean transfer) {
63         Assert.isNotNull(runnable);
64         Assert.isNotNull(rule);
65         fWorkspaceRunnable= runnable;
66         fRule= rule;
67         fTransfer= transfer;
68     }
69     
70     public ISchedulingRule getSchedulingRule() {
71         return fRule;
72     }
73
74     /**
75      * {@inheritDoc}
76      */

77     public void threadChange(Thread JavaDoc thread) {
78         if (fTransfer)
79             Job.getJobManager().transferRule(fRule, thread);
80     }
81
82     /*
83      * @see IRunnableWithProgress#run(IProgressMonitor)
84      */

85     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
86         try {
87             ResourcesPlugin.getWorkspace().run(fWorkspaceRunnable, fRule, IWorkspace.AVOID_UPDATE, monitor);
88         } catch (OperationCanceledException e) {
89             throw new InterruptedException JavaDoc(e.getMessage());
90         } catch (CoreException e) {
91             throw new InvocationTargetException JavaDoc(e);
92         }
93     }
94 }
95
96
Popular Tags