]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/actions/WorkbenchRunnableAdapter.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / actions / WorkbenchRunnableAdapter.java
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
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;
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.IRunnableContext;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.jface.operation.IThreadListener;
29
30 import org.eclipse.jdt.core.JavaCore;
31
32 import org.eclipse.jdt.internal.ui.JavaUIStatus;
33
34 /**
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}.
38  * <p>
39  * {@link OperationCanceledException}s thrown by the
40  * adapted runnable are caught and re-thrown as {@link InterruptedException}s.
41  */
42 public class WorkbenchRunnableAdapter implements IRunnableWithProgress, IThreadListener {
43
44         private boolean fTransfer= false;
45         private IWorkspaceRunnable fWorkspaceRunnable;
46         private ISchedulingRule fRule;
47
48         /**
49          * Runs a workspace runnable with the workspace lock.
50          * 
51          * @param runnable the runnable
52          */
53         public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable) {
54                 this(runnable, ResourcesPlugin.getWorkspace().getRoot());
55         }
56
57         /**
58          * Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at
59          * all.
60          * 
61          * @param runnable the runnable
62          * @param rule the scheduling rule, or <code>null</code>
63          */
64         public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule) {
65                 fWorkspaceRunnable= runnable;
66                 fRule= rule;
67         }
68
69         /**
70          * Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at
71          * all.
72          * 
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
76          *            thread
77          */
78         public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule, boolean transfer) {
79                 fWorkspaceRunnable= runnable;
80                 fRule= rule;
81                 fTransfer= transfer;
82         }
83
84         /**
85          * Returns the scheduling rule, or <code>null</code> if none.
86          * 
87          * @return the scheduling rule, or <code>null</code> if none
88          */
89         public ISchedulingRule getSchedulingRule() {
90                 return fRule;
91         }
92
93         /**
94          * {@inheritDoc}
95          */
96         public void threadChange(Thread thread) {
97                 if (fTransfer)
98                         Job.getJobManager().transferRule(fRule, thread);
99         }
100
101         /*
102          * @see IRunnableWithProgress#run(IProgressMonitor)
103          */
104         public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
105                 try {
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);
111                 }
112         }
113
114         public void runAsUserJob(String name, final Object jobFamiliy) {
115                 Job job= new Job(name) {
116                         /* (non-Javadoc)
117                          * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
118                          */
119                         @Override
120                         protected IStatus run(IProgressMonitor monitor) {
121                                 try {
122                                         WorkbenchRunnableAdapter.this.run(monitor);
123                                 } catch (InvocationTargetException e) {
124                                         Throwable cause= e.getCause();
125                                         if (cause instanceof CoreException) {
126                                                 return ((CoreException) cause).getStatus();
127                                         } else {
128                                                 return JavaUIStatus.createError(IStatus.ERROR, cause);
129                                         }
130                                 } catch (InterruptedException e) {
131                                         return Status.CANCEL_STATUS;
132                                 } finally {
133                                         monitor.done();
134                                 }
135                                 return Status.OK_STATUS;
136                         }
137                         @Override
138                         public boolean belongsTo(Object family) {
139                                 return jobFamiliy == family;
140                         }
141                 };
142                 job.setRule(fRule);
143                 job.setUser(true);
144                 job.schedule();
145
146                 // TODO: should block until user pressed 'to background'
147         }
148 }