KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > 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.jdt.internal.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
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;
22
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 import org.eclipse.jdt.core.JavaCore;
30
31 import org.eclipse.jdt.internal.ui.JavaUIStatus;
32
33 /**
34  * An <code>IRunnableWithProgress</code> that adapts and <code>IWorkspaceRunnable</code>
35  * so that is can be executed inside <code>IRunnableContext</code>. <code>OperationCanceledException</code>
36  * thrown by the adapted runnable are caught and re-thrown as a <code>InterruptedException</code>.
37  */

38 public class WorkbenchRunnableAdapter implements IRunnableWithProgress, IThreadListener {
39     
40     private boolean fTransfer= false;
41     private IWorkspaceRunnable fWorkspaceRunnable;
42     private ISchedulingRule fRule;
43     
44     /**
45      * Runs a workspace runnable with the workspace lock.
46      */

47     public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable) {
48         this(runnable, ResourcesPlugin.getWorkspace().getRoot());
49     }
50     
51     /**
52      * Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at all.
53      */

54     public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule) {
55         fWorkspaceRunnable= runnable;
56         fRule= rule;
57     }
58     
59     /**
60      * Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at all.
61      * @param transfer <code>true</code> if the rule is to be transfered
62      * to the model context thread. Otherwise <code>false</code>
63      */

64     public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule, boolean transfer) {
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             JavaCore.run(fWorkspaceRunnable, fRule, 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     public void runAsUserJob(String JavaDoc name, final Object JavaDoc jobFamiliy) {
96         Job buildJob = new Job(name){
97             /* (non-Javadoc)
98              * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
99              */

100             protected IStatus run(IProgressMonitor monitor) {
101                 try {
102                     WorkbenchRunnableAdapter.this.run(monitor);
103                 } catch (InvocationTargetException JavaDoc e) {
104                     Throwable JavaDoc cause= e.getCause();
105                     if (cause instanceof CoreException) {
106                         return ((CoreException) cause).getStatus();
107                     } else {
108                         return JavaUIStatus.createError(IStatus.ERROR, cause);
109                     }
110                 } catch (InterruptedException JavaDoc e) {
111                     return Status.CANCEL_STATUS;
112                 } finally {
113                     monitor.done();
114                 }
115                 return Status.OK_STATUS;
116             }
117             public boolean belongsTo(Object JavaDoc family) {
118                 return jobFamiliy == family;
119             }
120         };
121         buildJob.setRule(fRule);
122         buildJob.setUser(true);
123         buildJob.schedule();
124         
125         // TODO: should block until user pressed 'to background'
126
}
127 }
128
Popular Tags