KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > actions > ProgressDialogRunnableContext


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.team.internal.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IWorkspaceRunnable;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.jobs.ISchedulingRule;
20 import org.eclipse.jface.operation.IRunnableContext;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.progress.IProgressService;
24
25 /**
26  * This CVS runnable context blocks the UI and can therfore have a shell assigned to
27  * it (since the shell won't be closed by the user before the runnable completes.
28  */

29 public class ProgressDialogRunnableContext implements ITeamRunnableContext {
30
31     private IRunnableContext runnableContext;
32     private ISchedulingRule schedulingRule;
33     private boolean postponeBuild;
34     
35     public ProgressDialogRunnableContext() {
36     }
37     
38     /**
39      * Set whether the auto-build will be postponed while this
40      * context is executing a runnable.
41      * @param postponeBuild whether to postpone the auto-build.
42      */

43     public void setPostponeBuild(boolean postponeBuild) {
44         this.postponeBuild = postponeBuild;
45     }
46     
47     /**
48      * Set the scheduling rule that will be obtained before the context
49      * executes a runnable or <code>null</code> if no scheduling rule is to be onbtained.
50      * @param schedulingRule The schedulingRule to be obtained or <code>null</code>.
51      */

52     public void setSchedulingRule(ISchedulingRule schedulingRule) {
53         this.schedulingRule = schedulingRule;
54     }
55
56     /**
57      * Set the runnable context that is used to execute the runnable. By default,
58      * the workbench's progress service is used by clients can provide their own.
59      * @param runnableContext the runnable contentx used to execute runnables.
60      */

61     public void setRunnableContext(IRunnableContext runnableContext) {
62         this.runnableContext = runnableContext;
63     }
64     
65     /* (non-Javadoc)
66      * @see org.eclipse.team.internal.ui.actions.ITeamRunnableContext#run(org.eclipse.jface.operation.IRunnableWithProgress)
67      */

68     public void run(IRunnableWithProgress runnable) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
69         getRunnableContext().run(true /* fork */, true /* cancelable */, wrapRunnable(runnable));
70     }
71
72     private IRunnableContext getRunnableContext() {
73         if (runnableContext == null) {
74             return new IRunnableContext() {
75                 public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable)
76                         throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
77                     IProgressService manager = PlatformUI.getWorkbench().getProgressService();
78                     manager.busyCursorWhile(runnable);
79                 }
80             };
81         }
82         return runnableContext;
83     }
84
85     /*
86      * Return an IRunnableWithProgress that sets the task name for the progress monitor
87      * and runs in a workspace modify operation if requested.
88      */

89     private IRunnableWithProgress wrapRunnable(final IRunnableWithProgress runnable) {
90         return new IRunnableWithProgress() {
91             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
92                 try {
93                     if (schedulingRule == null && !postponeBuild) {
94                         runnable.run(monitor);
95                     } else {
96                         final Exception JavaDoc[] exception = new Exception JavaDoc[] { null };
97                         ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
98                             public void run(IProgressMonitor pm) throws CoreException {
99                                 try {
100                                     runnable.run(pm);
101                                 } catch (InvocationTargetException JavaDoc e) {
102                                     exception[0] = e;
103                                 } catch (InterruptedException JavaDoc e) {
104                                     exception[0] = e;
105                                 }
106                             }
107                         }, schedulingRule, 0 /* allow updates */, monitor);
108                         if (exception[0] != null) {
109                             if (exception[0] instanceof InvocationTargetException JavaDoc) {
110                                 throw (InvocationTargetException JavaDoc)exception[0];
111                             } else if (exception[0] instanceof InterruptedException JavaDoc) {
112                                 throw (InterruptedException JavaDoc)exception[0];
113                             }
114                         }
115                     }
116                 } catch (CoreException e) {
117                     throw new InvocationTargetException JavaDoc(e);
118                 }
119             }
120         };
121     }
122
123 }
124
Popular Tags