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