Mercurial > projects > dwt-addons
annotate dwtx/jface/operation/AccumulatingProgressMonitor.d @ 70:46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 01:36:46 +0200 |
parents | ef6c06252a87 |
children |
rev | line source |
---|---|
6 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
56
diff
changeset
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others. |
6 | 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.operation.AccumulatingProgressMonitor; | |
14 | |
15 | |
16 import dwt.widgets.Display; | |
17 import dwtx.core.runtime.Assert; | |
18 import dwtx.core.runtime.IProgressMonitor; | |
19 import dwtx.core.runtime.IProgressMonitorWithBlocking; | |
20 import dwtx.core.runtime.IStatus; | |
21 import dwtx.core.runtime.ProgressMonitorWrapper; | |
44 | 22 import dwtx.jface.dialogs.Dialog; |
6 | 23 |
24 import dwt.dwthelper.utils; | |
25 import dwt.dwthelper.Runnable; | |
26 | |
27 /** | |
28 * A progress monitor that accumulates <code>worked</code> and <code>subtask</code> | |
29 * calls in the following way by wrapping a standard progress monitor: | |
30 * <ul> | |
31 * <li> When a <code>worked</code> or <code>subtask</code> call occurs the first time, | |
32 * the progress monitor posts a runnable into the asynchronous DWT event queue. | |
33 * </li> | |
34 * <li> Subsequent calls to <code>worked</code> or <code>subtask</code> do not post | |
35 * a new runnable as long as a previous runnable still exists in the DWT event | |
36 * queue. In this case, the progress monitor just updates the internal state of | |
37 * the runnable that waits in the DWT event queue for its execution. If no runnable | |
38 * exists, a new one is created and posted into the event queue. | |
39 * </ul> | |
40 * <p> | |
41 * This class is internal to the framework; clients outside JFace should not | |
42 * use this class. | |
43 * </p> | |
44 */ | |
45 /* package */class AccumulatingProgressMonitor : ProgressMonitorWrapper { | |
46 | |
47 /** | |
48 * The display. | |
49 */ | |
50 private Display display; | |
51 | |
52 /** | |
53 * The collector, or <code>null</code> if none. | |
54 */ | |
55 private Collector collector; | |
56 | |
57 private String currentTask = ""; //$NON-NLS-1$ | |
58 | |
59 private class Collector : Runnable { | |
60 private String subTask_; | |
61 | |
62 private double worked_; | |
63 | |
64 private IProgressMonitor monitor; | |
65 | |
66 /** | |
67 * Create a new collector. | |
68 * @param subTask | |
69 * @param work | |
70 * @param monitor | |
71 */ | |
72 public this(String subTask_, double work, IProgressMonitor monitor) { | |
73 this.subTask_ = subTask_; | |
74 this.worked_ = work; | |
75 this.monitor = monitor; | |
76 } | |
77 | |
78 /** | |
79 * Add worked to the work. | |
80 * @param workedIncrement | |
81 */ | |
82 public void worked(double workedIncrement) { | |
83 this.worked_ = this.worked_ + workedIncrement; | |
84 } | |
85 | |
86 /** | |
87 * Set the subTask name. | |
88 * @param subTaskName | |
89 */ | |
90 public void subTask(String subTaskName) { | |
91 this.subTask_ = subTaskName; | |
92 } | |
93 | |
94 /** | |
95 * Run the collector. | |
96 */ | |
97 public void run() { | |
98 clearCollector(this); | |
99 if (subTask_ !is null) { | |
100 monitor.subTask(subTask_); | |
101 } | |
102 if (worked_ > 0) { | |
103 monitor.internalWorked(worked_); | |
104 } | |
105 } | |
106 } | |
107 | |
108 /** | |
109 * Creates an accumulating progress monitor wrapping the given one | |
110 * that uses the given display. | |
111 * | |
112 * @param monitor the actual progress monitor to be wrapped | |
113 * @param display the DWT display used to forward the calls | |
114 * to the wrapped progress monitor | |
115 */ | |
116 public this(IProgressMonitor monitor, Display display) { | |
117 super(monitor); | |
118 Assert.isNotNull(display); | |
119 this.display = display; | |
120 } | |
121 | |
122 /* (non-Javadoc) | |
123 * Method declared on IProgressMonitor. | |
124 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
125 public override void beginTask(String name, int totalWork) { |
6 | 126 synchronized (this) { |
127 collector = null; | |
128 } | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
56
diff
changeset
|
129 display.asyncExec(new class(name,totalWork) Runnable { |
56 | 130 String name_; |
131 int totalWork_; | |
132 this(String a, int b){ | |
133 name_=a; | |
134 totalWork_=b; | |
135 } | |
6 | 136 public void run() { |
56 | 137 currentTask = name_; |
138 getWrappedProgressMonitor().beginTask(name_, totalWork_); | |
6 | 139 } |
140 }); | |
141 } | |
142 | |
143 /** | |
144 * Clears the collector object used to accumulate work and subtask calls | |
145 * if it matches the given one. | |
146 * @param collectorToClear | |
147 */ | |
148 private synchronized void clearCollector(Collector collectorToClear) { | |
149 // Check if the accumulator is still using the given collector. | |
150 // If not, don't clear it. | |
151 if (this.collector is collectorToClear) { | |
152 this.collector = null; | |
153 } | |
154 } | |
155 | |
156 /** | |
157 * Creates a collector object to accumulate work and subtask calls. | |
158 * @param subTask | |
159 * @param work | |
160 */ | |
161 private void createCollector(String subTask, double work) { | |
162 collector = new Collector(subTask, work, getWrappedProgressMonitor()); | |
163 display.asyncExec(collector); | |
164 } | |
165 | |
166 /* (non-Javadoc) | |
167 * Method declared on IProgressMonitor. | |
168 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
169 public override void done() { |
6 | 170 synchronized (this) { |
171 collector = null; | |
172 } | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
56
diff
changeset
|
173 display.asyncExec(new class Runnable { |
6 | 174 public void run() { |
175 getWrappedProgressMonitor().done(); | |
176 } | |
177 }); | |
178 } | |
179 | |
180 /* (non-Javadoc) | |
181 * Method declared on IProgressMonitor. | |
182 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
183 public override synchronized void internalWorked(double work) { |
6 | 184 if (collector is null) { |
185 createCollector(null, work); | |
186 } else { | |
187 collector.worked(work); | |
188 } | |
189 } | |
190 | |
191 /* (non-Javadoc) | |
192 * Method declared on IProgressMonitor. | |
193 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
194 public override void setTaskName(String name) { |
6 | 195 synchronized (this) { |
196 collector = null; | |
197 } | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
56
diff
changeset
|
198 display.asyncExec(new class(name) Runnable { |
56 | 199 String name_; |
200 this(String a){ | |
201 name_=a; | |
202 } | |
6 | 203 public void run() { |
56 | 204 currentTask = name_; |
205 getWrappedProgressMonitor().setTaskName(name_); | |
6 | 206 } |
207 }); | |
208 } | |
209 | |
210 /* (non-Javadoc) | |
211 * Method declared on IProgressMonitor. | |
212 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
213 public override synchronized void subTask(String name) { |
6 | 214 if (collector is null) { |
215 createCollector(name, 0); | |
216 } else { | |
217 collector.subTask(name); | |
218 } | |
219 } | |
220 | |
221 /* (non-Javadoc) | |
222 * Method declared on IProgressMonitor. | |
223 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
224 public override synchronized void worked(int work) { |
6 | 225 internalWorked(work); |
226 } | |
227 | |
228 /* (non-Javadoc) | |
229 * @see dwtx.core.runtime.ProgressMonitorWrapper#clearBlocked() | |
230 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
231 public override void clearBlocked() { |
6 | 232 |
233 //If this is a monitor that can report blocking do so. | |
234 //Don't bother with a collector as this should only ever | |
235 //happen once and prevent any more progress. | |
236 IProgressMonitor pm = getWrappedProgressMonitor(); | |
237 if (!(cast(IProgressMonitorWithBlocking)pm )) { | |
238 return; | |
239 } | |
240 | |
39 | 241 display.asyncExec(new class(pm) Runnable { |
6 | 242 IProgressMonitor pm_; |
39 | 243 this(IProgressMonitor a){ pm_=a; } |
6 | 244 /* (non-Javadoc) |
245 * @see java.lang.Runnable#run() | |
246 */ | |
247 public void run() { | |
248 (cast(IProgressMonitorWithBlocking) pm_).clearBlocked(); | |
44 | 249 Dialog.getBlockedHandler().clearBlocked(); |
6 | 250 } |
251 }); | |
252 } | |
253 | |
254 /* (non-Javadoc) | |
255 * @see dwtx.core.runtime.ProgressMonitorWrapper#setBlocked(dwtx.core.runtime.IStatus) | |
256 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
257 public override void setBlocked(IStatus reason) { |
6 | 258 //If this is a monitor that can report blocking do so. |
259 //Don't bother with a collector as this should only ever | |
260 //happen once and prevent any more progress. | |
261 IProgressMonitor pm = getWrappedProgressMonitor(); | |
262 if (!(cast(IProgressMonitorWithBlocking)pm )) { | |
263 return; | |
264 } | |
265 | |
56 | 266 display.asyncExec(new class(pm,reason) Runnable { |
6 | 267 IProgressMonitor pm_; |
56 | 268 IStatus reason_; |
269 this(IProgressMonitor a,IStatus b){ | |
270 pm_=a; | |
271 reason_=b; | |
272 } | |
6 | 273 /* (non-Javadoc) |
274 * @see java.lang.Runnable#run() | |
275 */ | |
276 public void run() { | |
56 | 277 (cast(IProgressMonitorWithBlocking) pm_).setBlocked(reason_); |
6 | 278 //Do not give a shell as we want it to block until it opens. |
56 | 279 Dialog.getBlockedHandler().showBlocked(pm_, reason_, currentTask); |
6 | 280 } |
281 }); | |
282 } | |
283 } |