KickJava   Java API By Example, From Geeks To Geeks.

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


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.WorkspaceJob;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.core.runtime.jobs.*;
18 import org.eclipse.jface.operation.IRunnableWithProgress;
19 import org.eclipse.team.core.TeamException;
20 import org.eclipse.team.internal.ui.Utils;
21 import org.eclipse.ui.IWorkbenchSite;
22
23 /**
24  * This runnable context executes it's operation in the context of a background job.
25  */

26 public class JobRunnableContext implements ITeamRunnableContext {
27
28     private IJobChangeListener listener;
29     private IWorkbenchSite site;
30     private String JavaDoc jobName;
31     private ISchedulingRule schedulingRule;
32     private boolean postponeBuild;
33     
34     /*
35      * Interface that provides access to the runnable of the job so
36      * that subclasses can do belongsTo family checking.
37      */

38     public interface IContextJob {
39         IRunnableWithProgress getRunnable();
40     }
41     
42     /*
43      * Class that provides a basic job (i.e. no resource specific interactions)
44      */

45     private class BasicJob extends Job implements IContextJob {
46         private final IRunnableWithProgress runnable;
47         public BasicJob(String JavaDoc name, IRunnableWithProgress runnable) {
48             super(name);
49             this.runnable = runnable;
50         }
51         public IStatus run(IProgressMonitor monitor) {
52             return JobRunnableContext.this.run(runnable, monitor);
53         }
54         public boolean belongsTo(Object JavaDoc family) {
55             return JobRunnableContext.this.belongsTo(this, family);
56         }
57         public IRunnableWithProgress getRunnable() {
58             return runnable;
59         }
60     }
61     
62     /*
63      * Class that provides a resource job (i.e. resource specific interactions)
64      */

65     private class ResourceJob extends WorkspaceJob implements IContextJob {
66         private final IRunnableWithProgress runnable;
67         public ResourceJob(String JavaDoc name, IRunnableWithProgress runnable) {
68             super(name);
69             this.runnable = runnable;
70         }
71         public IStatus runInWorkspace(IProgressMonitor monitor) {
72             return JobRunnableContext.this.run(runnable, monitor);
73         }
74         public boolean belongsTo(Object JavaDoc family) {
75             return JobRunnableContext.this.belongsTo(this, family);
76         }
77         public IRunnableWithProgress getRunnable() {
78             return runnable;
79         }
80     }
81     public JobRunnableContext(String JavaDoc jobName, IJobChangeListener listener, IWorkbenchSite site) {
82         this.jobName = jobName;
83         this.listener = listener;
84         this.site = site;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.team.internal.ccvs.ui.operations.ITeamRunnableContext#run(java.lang.String, boolean, org.eclipse.jface.operation.IRunnableWithProgress)
89      */

90     public void run(IRunnableWithProgress runnable) {
91         Job job;
92         if (schedulingRule == null && !postponeBuild) {
93             job = new BasicJob(jobName, runnable);
94         } else {
95             job = new ResourceJob(jobName, runnable);
96         }
97         if (listener != null) {
98             job.addJobChangeListener(listener);
99         }
100         configureJob(job);
101         Utils.schedule(job, site);
102     }
103     
104     /**
105      * Configure the job. By default, the job is configured to be a user
106      * job meaning that it will make use of the progress service.
107      * Subclasses can tailor how the job appears in the progress service.
108      * @param job the job that will provide the execution context
109      */

110     protected void configureJob(Job job) {
111         if (schedulingRule != null) {
112             job.setRule(schedulingRule);
113         }
114         job.setUser(isUser());
115     }
116     
117     /**
118      * Set whether the auto-build will be postponed while this
119      * context is executing a runnable.
120      * @param postponeBuild whether to postpone the auto-build.
121      */

122     public void setPostponeBuild(boolean postponeBuild) {
123         this.postponeBuild = postponeBuild;
124     }
125     
126     /**
127      * Return whether this job context is user initiated. Subclasses may override.
128      */

129     protected boolean isUser() {
130         return true;
131     }
132     
133     /**
134      * Set the scheduling rule that will be obtained before the context
135      * executes a runnable or <code>null</code> if no scheduling rule is to be onbtained.
136      * @param schedulingRule The schedulingRule to be obtained or <code>null</code>.
137      */

138     public void setSchedulingRule(ISchedulingRule schedulingRule) {
139         this.schedulingRule = schedulingRule;
140     }
141     
142     /* private */ IStatus run(IRunnableWithProgress runnable, IProgressMonitor monitor) {
143         try {
144             runnable.run(monitor);
145         } catch (InvocationTargetException JavaDoc e) {
146             return TeamException.asTeamException(e).getStatus();
147         } catch (InterruptedException JavaDoc e) {
148             return Status.CANCEL_STATUS;
149         }
150         return getCompletionStatus();
151     }
152     
153     /**
154      * Return the completions status for the job.
155      * By default, <code>Status.OK_STATUS</code>
156      * is returned.
157      * @return the completions status for the job
158      */

159     protected IStatus getCompletionStatus() {
160         return Status.OK_STATUS;
161     }
162
163     /**
164      * Return whether the job for this context is in the given family.
165      * By default, <code>false</code> is returned. Subclasses may override.
166      * @param family the job family being queried
167      */

168     protected boolean belongsTo(IContextJob job, Object JavaDoc family) {
169         return false;
170     }
171
172 }
173
Popular Tags