KickJava   Java API By Example, From Geeks To Geeks.

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


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.Assert;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.internal.WorkbenchPlugin;
21 import org.eclipse.ui.internal.misc.UIStats;
22 import org.eclipse.ui.internal.progress.ProgressMessages;
23
24 /**
25  * The UIJob is a Job that runs within the UI Thread via an asyncExec.
26  *
27  * @since 3.0
28  */

29 public abstract class UIJob extends Job {
30     private Display cachedDisplay;
31
32     /**
33      * Create a new instance of the receiver with the supplied name. The display
34      * used will be the one from the workbench if this is available. UIJobs with
35      * this constructor will determine thier display at runtime.
36      *
37      * @param name
38      * the job name
39      *
40      */

41     public UIJob(String JavaDoc name) {
42         super(name);
43     }
44
45     /**
46      * Create a new instance of the receiver with the supplied Display.
47      *
48      * @param jobDisplay
49      * the display
50      * @param name
51      * the job name
52      * @see Job
53      */

54     public UIJob(Display jobDisplay, String JavaDoc name) {
55         this(name);
56         setDisplay(jobDisplay);
57     }
58
59     /**
60      * Convenience method to return a status for an exception.
61      *
62      * @param exception
63      * @return IStatus an error status built from the exception
64      * @see Job
65      */

66     public static IStatus errorStatus(Throwable JavaDoc exception) {
67         return WorkbenchPlugin.getStatus(exception);
68     }
69
70     /**
71      * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
72      * Note: this message is marked final. Implementors should use
73      * runInUIThread() instead.
74      */

75     public final IStatus run(final IProgressMonitor monitor) {
76         if (monitor.isCanceled()) {
77             return Status.CANCEL_STATUS;
78         }
79         Display asyncDisplay = getDisplay();
80         if (asyncDisplay == null || asyncDisplay.isDisposed()) {
81             return Status.CANCEL_STATUS;
82         }
83         asyncDisplay.asyncExec(new Runnable JavaDoc() {
84             public void run() {
85                 IStatus result = null;
86                 try {
87                     //As we are in the UI Thread we can
88
//always know what to tell the job.
89
setThread(Thread.currentThread());
90                     if (monitor.isCanceled()) {
91                         result = Status.CANCEL_STATUS;
92                     } else {
93                         UIStats.start(UIStats.UI_JOB, getName());
94                         result = runInUIThread(monitor);
95                     }
96
97                 } finally {
98                     UIStats.end(UIStats.UI_JOB, UIJob.this, getName());
99                     if (result == null) {
100                         result = new Status(IStatus.ERROR,
101                                 PlatformUI.PLUGIN_ID, IStatus.ERROR,
102                                 ProgressMessages.Error,
103                                 null);
104                     }
105                     done(result);
106                 }
107             }
108         });
109         return Job.ASYNC_FINISH;
110     }
111
112     /**
113      * Run the job in the UI Thread.
114      *
115      * @param monitor
116      * @return IStatus
117      */

118     public abstract IStatus runInUIThread(IProgressMonitor monitor);
119
120     /**
121      * Sets the display to execute the asyncExec in. Generally this is not'
122      * used if there is a valid display avaialble via PlatformUI.isWorkbenchRunning().
123      *
124      * @param runDisplay
125      * Display
126      * @see UIJob#getDisplay()
127      * @see PlatformUI#isWorkbenchRunning()
128      */

129     public void setDisplay(Display runDisplay) {
130         Assert.isNotNull(runDisplay);
131         cachedDisplay = runDisplay;
132     }
133
134     /**
135      * Returns the display for use by the receiver when running in an
136      * asyncExec. If it is not set then the display set in the workbench
137      * is used.
138      * If the display is null the job will not be run.
139      *
140      * @return Display or <code>null</code>.
141      */

142     public Display getDisplay() {
143         //If it was not set get it from the workbench
144
if (cachedDisplay == null && PlatformUI.isWorkbenchRunning()) {
145             return PlatformUI.getWorkbench().getDisplay();
146         }
147         return cachedDisplay;
148     }
149 }
150
Popular Tags