KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > RestartableJob


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.ui.views.markers.internal;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.ui.internal.ide.StatusUtil;
23 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
24
25 /**
26  * Represents a job that can be restarted. When a job is "restarted", the currently running
27  * instance is cancelled and a new instance is scheduled once the previous one terminates.
28  * This does not inherit from the Jobs API. Instead of subclassing this class, a pointer to
29  * a IRunnableWithProgress should be passed into the constructor.
30  */

31 public final class RestartableJob {
32     IRunnableWithProgress runnable;
33
34     Job theJob;
35
36     boolean restartNeeded = false;
37
38     private Object JavaDoc lock = new Object JavaDoc();
39
40     private IProgressMonitor currentMonitor = null;
41
42     IWorkbenchSiteProgressService progressService;
43
44     /**
45      * Constructs a new RestartableJob with the given name that will run the given
46      * runnable.
47      *
48      * @param name
49      * @param newRunnable
50      * @param service IWorkbenchSiteProgressService the service we are
51      * going to use to show progress or <code>null</code>.
52      */

53     public RestartableJob(String JavaDoc name, IRunnableWithProgress newRunnable,
54             IWorkbenchSiteProgressService service) {
55         this.runnable = newRunnable;
56         progressService = service;
57
58         createJob(name);
59
60         theJob.addJobChangeListener(new JobChangeAdapter() {
61             public void done(IJobChangeEvent e) {
62                 synchronized (lock) {
63                     currentMonitor = null;
64                     if (restartNeeded) {
65                         scheduleInService();
66                     }
67                 }
68             }
69         });
70     }
71
72     /**
73      * Instantiates the actual Job object.
74      *
75      * @param name
76      */

77     private void createJob(String JavaDoc name) {
78         theJob = new Job(name) {
79             protected IStatus run(IProgressMonitor innerMonitor) {
80                 try {
81                     synchronized (lock) {
82                         restartNeeded = false;
83                         currentMonitor = innerMonitor;
84                     }
85                     runnable.run(innerMonitor);
86                 } catch (InvocationTargetException JavaDoc e) {
87                     return StatusUtil.newStatus(IStatus.ERROR, e.toString(), e
88                             .getTargetException());
89                 } catch (InterruptedException JavaDoc e) {
90                     return Status.CANCEL_STATUS;
91                 }
92                 if (innerMonitor.isCanceled()) {
93                     return Status.CANCEL_STATUS;
94                 } else {
95                     return Status.OK_STATUS;
96                 }
97             }
98         };
99
100         theJob.setPriority(Job.DECORATE);
101         theJob.setSystem(true);
102     }
103
104     /**
105      * Aborts the currently running job (if any) by cancelling its progress
106      * monitor, and reschedules it. If there is no currently running job,
107      * it will be started.
108      */

109     public void restart() {
110         synchronized (lock) {
111             if (currentMonitor == null) {
112                 scheduleInService();
113             } else if (!restartNeeded) {
114                 restartNeeded = true;
115                 theJob.cancel();
116             }
117         }
118     }
119
120     /**
121      * Schedules the job. Does nothing if the job is already running.
122      */

123     public void schedule() {
124         synchronized (lock) {
125             if (currentMonitor == null) {
126                 scheduleInService();
127             } else {
128                 if (currentMonitor.isCanceled()) {
129                     restartNeeded = true;
130                 }
131             }
132         }
133     }
134
135     /**
136      * Schedule theJob using the progress service if there
137      * is one.
138      */

139     private void scheduleInService() {
140         if (progressService == null)
141             theJob.schedule();
142         else
143             progressService.schedule(theJob, 0, true);
144     }
145
146     /**
147      * Cancels the job. If the job is currently running, it will be
148      * terminated as soon as possible.
149      */

150     public void cancel() {
151         synchronized (lock) {
152             theJob.cancel();
153             restartNeeded = false;
154         }
155     }
156 }
157
Popular Tags