KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > EventLoopProgressMonitor


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.ui.internal.dialogs;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IProgressMonitorWithBlocking;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.ProgressMonitorWrapper;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.ui.internal.ExceptionHandler;
20
21 /**
22  * Used to run an event loop whenever progress monitor methods
23  * are invoked. <p>
24  * This is needed since editor save operations are done in the UI thread.
25  * Although save operations should be written to do the work in the non-UI thread,
26  * this was not done for 1.0, so this was added to keep the UI live
27  * (including allowing the cancel button to work).
28  */

29 public class EventLoopProgressMonitor extends ProgressMonitorWrapper implements
30         IProgressMonitorWithBlocking {
31     /**
32      * Threshold for how often the event loop is spun, in ms.
33      */

34     private static int T_THRESH = 100;
35
36     /**
37      * Maximum amount of time to spend processing events, in ms.
38      */

39     private static int T_MAX = 50;
40
41     /**
42      * Last time the event loop was spun.
43      */

44     private long lastTime = System.currentTimeMillis();
45
46     /**
47      * The task name is the name of the current task
48      * in the event loop.
49      */

50     private String JavaDoc taskName;
51
52     /**
53      * Constructs a new instance of the receiver and forwards to monitor.
54      * @param monitor
55      */

56     public EventLoopProgressMonitor(IProgressMonitor monitor) {
57         super(monitor);
58     }
59
60     /**
61      * @see IProgressMonitor#beginTask
62      */

63     public void beginTask(String JavaDoc name, int totalWork) {
64         super.beginTask(name, totalWork);
65         taskName = name;
66         runEventLoop();
67     }
68
69     /* (non-Javadoc)
70      * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#clearBlocked()
71      */

72     public void clearBlocked() {
73         Dialog.getBlockedHandler().clearBlocked();
74     }
75
76     /**
77      * @see IProgressMonitor#done
78      */

79     public void done() {
80         super.done();
81         taskName = null;
82         runEventLoop();
83     }
84
85     /**
86      * @see IProgressMonitor#internalWorked
87      */

88     public void internalWorked(double work) {
89         super.internalWorked(work);
90         runEventLoop();
91     }
92
93     /**
94      * @see IProgressMonitor#isCanceled
95      */

96     public boolean isCanceled() {
97         runEventLoop();
98         return super.isCanceled();
99     }
100
101     /**
102      * Runs an event loop.
103      */

104     private void runEventLoop() {
105         // Only run the event loop so often, as it is expensive on some platforms
106
// (namely Motif).
107
long t = System.currentTimeMillis();
108         if (t - lastTime < T_THRESH) {
109             return;
110         }
111         lastTime = t;
112         // Run the event loop.
113
Display disp = Display.getDefault();
114         if (disp == null) {
115             return;
116         }
117
118         //Initialize an exception handler from the window class.
119
ExceptionHandler handler = ExceptionHandler.getInstance();
120
121         for (;;) {
122             try {
123                 if (!disp.readAndDispatch()) {
124                     break;
125                 }
126             } catch (Throwable JavaDoc e) {//Handle the exception the same way as the workbench
127
handler.handleException(e);
128                 break;
129             }
130
131             // Only run the event loop for so long.
132
// Otherwise, this would never return if some other thread was
133
// constantly generating events.
134
if (System.currentTimeMillis() - t > T_MAX) {
135                 break;
136             }
137         }
138     }
139
140     /* (non-Javadoc)
141      * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#setBlocked(org.eclipse.core.runtime.IStatus)
142      */

143     public void setBlocked(IStatus reason) {
144         Dialog.getBlockedHandler().showBlocked(this, reason, taskName);
145     }
146
147     /**
148      * @see IProgressMonitor#setCanceled
149      */

150     public void setCanceled(boolean b) {
151         super.setCanceled(b);
152         taskName = null;
153         runEventLoop();
154     }
155
156     /**
157      * @see IProgressMonitor#setTaskName
158      */

159     public void setTaskName(String JavaDoc name) {
160         super.setTaskName(name);
161         taskName = name;
162         runEventLoop();
163     }
164
165     /**
166      * @see IProgressMonitor#subTask
167      */

168     public void subTask(String JavaDoc name) {
169         //Be prepared in case the first task was null
170
if (taskName == null) {
171             taskName = name;
172         }
173         super.subTask(name);
174         runEventLoop();
175     }
176
177     /**
178      * @see IProgressMonitor#worked
179      */

180     public void worked(int work) {
181         super.worked(work);
182         runEventLoop();
183     }
184
185     /**
186      * Return the name of the current task.
187      * @return Returns the taskName.
188      */

189     protected String JavaDoc getTaskName() {
190         return taskName;
191     }
192 }
193
Popular Tags