KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > progress > WorkbenchJob


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.progress;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
15 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.ui.PlatformUI;
18
19 /**
20  * WorkbenchJob is a type of job that implements a done listener
21  * and does the shutdown checks before scheduling. This is used if
22  * a job is not meant to run when the Workbench is shutdown.
23  * @since 3.0
24  */

25 public abstract class WorkbenchJob extends UIJob {
26
27     /**
28      * Create a new instance of the receiver with the
29      * supplied display and name.
30      * Normally this constructor would not be used as
31      * it is best to let the job find the display from
32      * the workbench
33      * @param jobDisplay Display. The display to run the
34      * job with.
35      * @param name String
36      */

37     public WorkbenchJob(Display jobDisplay, String JavaDoc name) {
38         super(jobDisplay, name);
39         addDefaultJobChangeListener();
40     }
41
42     /**
43      * Add a new instance of the reciever with the
44      * supplied name.
45      * @param name String
46      */

47     public WorkbenchJob(String JavaDoc name) {
48         super(name);
49         addDefaultJobChangeListener();
50     }
51
52     /**
53      * Add a job change listeners that handles a done
54      * event if the result was IStatus.OK.
55      *
56      */

57     private void addDefaultJobChangeListener() {
58         addJobChangeListener(new JobChangeAdapter() {
59             /* (non-Javadoc)
60              * @see org.eclipse.core.runtime.jobs.JobChangeAdapter#done(org.eclipse.core.runtime.jobs.IJobChangeEvent)
61              */

62             public void done(IJobChangeEvent event) {
63
64                 //Abort if it is not running
65
if (!PlatformUI.isWorkbenchRunning()) {
66                     return;
67                 }
68
69                 if (event.getResult().getCode() == IStatus.OK) {
70                     performDone(event);
71                 }
72             }
73         });
74     }
75
76     /**
77      * Perform done with the supplied event. This will
78      * only occur if the returned status was OK.
79      * This is called only if the job is finished with an IStatus.OK
80      * result and the workbench is still running.
81      * @param event IJobChangeEvent
82      */

83     public void performDone(IJobChangeEvent event) {
84         //Do nothing by default.
85
}
86
87     /* (non-Javadoc)
88      * @see org.eclipse.core.internal.jobs.InternalJob#shouldSchedule()
89      */

90     public boolean shouldSchedule() {
91         return super.shouldSchedule() && PlatformUI.isWorkbenchRunning();
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.core.runtime.jobs.Job#shouldRun()
96      */

97     public boolean shouldRun() {
98         return super.shouldRun() && PlatformUI.isWorkbenchRunning();
99     }
100
101 }
102
Popular Tags