Mercurial > projects > dwt2
comparison org.eclipse.equinox.common/src/org/eclipse/core/runtime/SubMonitor.d @ 105:bbe49769ec18
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 08 Nov 2009 12:42:30 +0100 |
parents | bc29606a740c |
children |
comparison
equal
deleted
inserted
replaced
104:88652073d1c2 | 105:bbe49769ec18 |
---|---|
2 * Copyright (c) 2006, 2007 IBM Corporation and others. | 2 * Copyright (c) 2006, 2007 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * Stefan Xenos - initial API and implementation | 9 * Stefan Xenos - initial API and implementation |
10 * Stefan Xenos - bug 174539 - add a 1-argument convert(...) method | 10 * Stefan Xenos - bug 174539 - add a 1-argument convert(...) method |
11 * Stefan Xenos - bug 174040 - SubMonitor#convert doesn't always set task name | 11 * Stefan Xenos - bug 174040 - SubMonitor#convert doesn't always set task name |
12 * Stefan Xenos - bug 206942 - updated javadoc to recommend better constants for infinite progress | 12 * Stefan Xenos - bug 206942 - updated javadoc to recommend better constants for infinite progress |
13 * Port to the D programming language: | |
14 * Frank Benoit <benoit@tionex.de> | |
15 *******************************************************************************/ | 13 *******************************************************************************/ |
16 module org.eclipse.core.runtime.SubMonitor; | 14 // Port to the D programming language: |
17 | 15 // Frank Benoit <benoit@tionex.de> |
18 import org.eclipse.core.runtime.IProgressMonitorWithBlocking; | 16 module org.eclipse.core.runtimeSubMonitor; |
19 import org.eclipse.core.runtime.IProgressMonitor; | |
20 import org.eclipse.core.runtime.IStatus; | |
21 import org.eclipse.core.runtime.NullProgressMonitor; | |
22 | 17 |
23 import java.lang.all; | 18 import java.lang.all; |
24 | 19 |
20 import org.eclipse.core.runtimeIProgressMonitorWithBlocking; // packageimport | |
21 import org.eclipse.core.runtimeIProgressMonitor; // packageimport | |
22 import org.eclipse.core.runtimeIStatus; // packageimport | |
23 import org.eclipse.core.runtimeNullProgressMonitor; // packageimport | |
24 | |
25 /** | 25 /** |
26 * <p>A progress monitor that uses a given amount of work ticks from a parent monitor. This is intended as a | 26 * <p>A progress monitor that uses a given amount of work ticks from a parent monitor. This is intended as a |
27 * safer, easier-to-use alternative to SubProgressMonitor. The main benefits of SubMonitor over | 27 * safer, easier-to-use alternative to SubProgressMonitor. The main benefits of SubMonitor over |
28 * SubProgressMonitor are:</p> | 28 * SubProgressMonitor are:</p> |
29 * <ul> | 29 * <ul> |
30 * <li>It is not necessary to call beginTask() or done() on an instance of SubMonitor.</li> | 30 * <li>It is not necessary to call beginTask() or done() on an instance of SubMonitor.</li> |
31 * <li>SubMonitor has a simpler syntax for creating nested monitors.</li> | 31 * <li>SubMonitor has a simpler syntax for creating nested monitors.</li> |
32 * <li>SubMonitor is more efficient for deep recursion chains.</li> | 32 * <li>SubMonitor is more efficient for deep recursion chains.</li> |
33 * <li>SubMonitor has a setWorkRemining method that allows the remaining space on the monitor to be | 33 * <li>SubMonitor has a setWorkRemining method that allows the remaining space on the monitor to be |
34 * redistributed without reporting any work.</li> | 34 * redistributed without reporting any work.</li> |
35 * <li>SubMonitor protects the caller from common progress reporting bugs in a called method. For example, | 35 * <li>SubMonitor protects the caller from common progress reporting bugs in a called method. For example, |
36 * if a called method fails to call done() on the given monitor or fails to consume all the ticks on | 36 * if a called method fails to call done() on the given monitor or fails to consume all the ticks on |
37 * the given monitor, the parent will correct the problem after the method returns.</li> | 37 * the given monitor, the parent will correct the problem after the method returns.</li> |
38 * </ul> | 38 * </ul> |
39 * <p></p> | 39 * <p></p> |
40 * <p><b>USAGE:</b></p> | 40 * <p><b>USAGE:</b></p> |
41 * | 41 * |
42 * <p>When implementing a method that accepts an IProgressMonitor:</p> | 42 * <p>When implementing a method that accepts an IProgressMonitor:</p> |
43 * <ul> | 43 * <ul> |
44 * <li>At the start of your method, use <code>SubMonitor.convert(...).</code> to convert the IProgressMonitor | 44 * <li>At the start of your method, use <code>SubMonitor.convert(...).</code> to convert the IProgressMonitor |
45 * into a SubMonitor. </li> | 45 * into a SubMonitor. </li> |
46 * <li>Use <code>SubMonitor.newChild(...)</code> whenever you need to call another method that | 46 * <li>Use <code>SubMonitor.newChild(...)</code> whenever you need to call another method that |
47 * accepts an IProgressMonitor.</li> | 47 * accepts an IProgressMonitor.</li> |
48 * </ul> | 48 * </ul> |
49 * <p></p> | 49 * <p></p> |
50 * <p><b>DEFAULT BEHAVIOR:</b></p> | 50 * <p><b>DEFAULT BEHAVIOR:</b></p> |
51 * | 51 * |
52 * <p>When writing JavaDoc for a method that accepts an IProgressMonitor, you should assume the | 52 * <p>When writing JavaDoc for a method that accepts an IProgressMonitor, you should assume the |
53 * following default behavior unless the method's JavaDoc says otherwise:</p> | 53 * following default behavior unless the method's JavaDoc says otherwise:</p> |
54 * <ul> | 54 * <ul> |
55 * <li>It WILL call beginTask on the IProgressMonitor.</li> | 55 * <li>It WILL call beginTask on the IProgressMonitor.</li> |
56 * <li>It WILL NOT accept a null argument.</li> | 56 * <li>It WILL NOT accept a null argument.</li> |
57 * <li>It WILL call done on the IProgressMonitor.</li> | 57 * <li>It WILL call done on the IProgressMonitor.</li> |
58 * </ul> | 58 * </ul> |
59 * <p></p> | 59 * <p></p> |
60 * <p><b>BEST PRACTISES:</b></p> | 60 * <p><b>BEST PRACTISES:</b></p> |
61 * | 61 * |
62 * <p>We recommend that newly-written methods follow the given contract:</p> | 62 * <p>We recommend that newly-written methods follow the given contract:</p> |
63 * <ul> | 63 * <ul> |
64 * <li>It WILL call beginTask on the IProgressMonitor.</li> | 64 * <li>It WILL call beginTask on the IProgressMonitor.</li> |
65 * <li>It WILL accept a null argument, indicating that no progress should be reported and the operation cannot be cancelled.</li> | 65 * <li>It WILL accept a null argument, indicating that no progress should be reported and the operation cannot be cancelled.</li> |
66 * <li>It WILL NOT call done on the IProgressMonitor, leaving this responsibility up to the caller.</li> | 66 * <li>It WILL NOT call done on the IProgressMonitor, leaving this responsibility up to the caller.</li> |
67 * </ul> | 67 * </ul> |
68 * <p>If you wish to follow these conventions, you may copy and paste the following text into your method's JavaDoc:</p> | 68 * <p>If you wish to follow these conventions, you may copy and paste the following text into your method's JavaDoc:</p> |
69 * | 69 * |
70 * <pre>@param monitor the progress monitor to use for reporting progress to the user. It is the caller's responsibility | 70 * <pre>@param monitor the progress monitor to use for reporting progress to the user. It is the caller's responsibility |
71 * to call done() on the given monitor. Accepts <code>null</code>, indicating that no progress should be | 71 * to call done() on the given monitor. Accepts <code>null</code>, indicating that no progress should be |
72 * reported and that the operation cannot be cancelled.</pre> | 72 * reported and that the operation cannot be cancelled.</pre> |
73 * | 73 * |
74 * <p></p> | 74 * <p></p> |
75 * <p><b>Example: Recommended usage</b></p> | 75 * <p><b>Example: Recommended usage</b></p> |
76 * | 76 * |
77 * <p>This example demonstrates how the recommended usage of <code>SubMonitor</code> makes it unnecessary to call | 77 * <p>This example demonstrates how the recommended usage of <code>SubMonitor</code> makes it unnecessary to call |
78 * IProgressMonitor.done() in most situations.</p> | 78 * IProgressMonitor.done() in most situations.</p> |
79 * | 79 * |
80 * <p>It is never necessary to call done() on a monitor obtained from <code>convert</code> or <code>progress.newChild()</code>. | 80 * <p>It is never necessary to call done() on a monitor obtained from <code>convert</code> or <code>progress.newChild()</code>. |
81 * In this example, there is no guarantee that <code>monitor</code> is an instance of <code>SubMonitor</code>, making it | 81 * In this example, there is no guarantee that <code>monitor</code> is an instance of <code>SubMonitor</code>, making it |
82 * necessary to call <code>monitor.done()</code>. The JavaDoc contract makes this the responsibility of the caller.</p> | 82 * necessary to call <code>monitor.done()</code>. The JavaDoc contract makes this the responsibility of the caller.</p> |
83 * | 83 * |
84 * <pre> | 84 * <pre> |
85 * // param monitor the progress monitor to use for reporting progress to the user. It is the caller's responsibility | 85 * // param monitor the progress monitor to use for reporting progress to the user. It is the caller's responsibility |
86 * // to call done() on the given monitor. Accepts <code>null</code>, indicating that no progress should be | 86 * // to call done() on the given monitor. Accepts <code>null</code>, indicating that no progress should be |
87 * // reported and that the operation cannot be cancelled. | 87 * // reported and that the operation cannot be cancelled. |
88 * // | 88 * // |
89 * void doSomething(IProgressMonitor monitor) { | 89 * void doSomething(IProgressMonitor monitor) { |
90 * // Convert the given monitor into a progress instance | 90 * // Convert the given monitor into a progress instance |
91 * SubMonitor progress = SubMonitor.convert(monitor, 100); | 91 * SubMonitor progress = SubMonitor.convert(monitor, 100); |
92 * | 92 * |
93 * // Use 30% of the progress to do some work | 93 * // Use 30% of the progress to do some work |
94 * doSomeWork(progress.newChild(30)); | 94 * doSomeWork(progress.newChild(30)); |
95 * | 95 * |
96 * // Advance the monitor by another 30% | 96 * // Advance the monitor by another 30% |
97 * progress.worked(30); | 97 * progress.worked(30); |
98 * | 98 * |
99 * // Use the remaining 40% of the progress to do some more work | 99 * // Use the remaining 40% of the progress to do some more work |
100 * doSomeWork(progress.newChild(40)); | 100 * doSomeWork(progress.newChild(40)); |
101 * } | 101 * } |
102 * </pre> | 102 * </pre> |
103 * | 103 * |
104 * | 104 * |
105 * <p></p> | 105 * <p></p> |
106 * <p><b>Example: Default usage</b></p> | 106 * <p><b>Example: Default usage</b></p> |
107 * | 107 * |
108 * <p>You will often need to implement a method that does not explicitly stipulate that calling done() is the responsibility | 108 * <p>You will often need to implement a method that does not explicitly stipulate that calling done() is the responsibility |
109 * of the caller. In this case, you should use the following pattern:</p> | 109 * of the caller. In this case, you should use the following pattern:</p> |
110 * | 110 * |
111 * <pre> | 111 * <pre> |
112 * // param monitor the progress monitor to use for reporting progress to the user, or <code>null</code> indicating | 112 * // param monitor the progress monitor to use for reporting progress to the user, or <code>null</code> indicating |
113 * // that no progress should be reported and the operation cannot be cancelled. | 113 * // that no progress should be reported and the operation cannot be cancelled. |
114 * // | 114 * // |
115 * void doSomething(IProgressMonitor monitor) { | 115 * void doSomething(IProgressMonitor monitor) { |
116 * // Convert the given monitor into a progress instance | 116 * // Convert the given monitor into a progress instance |
117 * SubMonitor progress = SubMonitor.convert(monitor, 100); | 117 * SubMonitor progress = SubMonitor.convert(monitor, 100); |
118 * try { | 118 * try { |
119 * // Use 30% of the progress to do some work | 119 * // Use 30% of the progress to do some work |
120 * doSomeWork(progress.newChild(30)); | 120 * doSomeWork(progress.newChild(30)); |
121 * | 121 * |
122 * // Advance the monitor by another 30% | 122 * // Advance the monitor by another 30% |
123 * progress.worked(30); | 123 * progress.worked(30); |
124 * | 124 * |
125 * // Use the remaining 40% of the progress to do some more work | 125 * // Use the remaining 40% of the progress to do some more work |
126 * doSomeWork(progress.newChild(40)); | 126 * doSomeWork(progress.newChild(40)); |
127 * | 127 * |
128 * } finally { | 128 * } finally { |
129 * if (monitor !is null) { | 129 * if (monitor != null) { |
130 * monitor.done(); | 130 * monitor.done(); |
131 * } | 131 * } |
132 * } | 132 * } |
133 * } | 133 * } |
134 * </pre> | 134 * </pre> |
135 * | 135 * |
136 * <p></p> | 136 * <p></p> |
137 * <p><b>Example: Branches</b></p> | 137 * <p><b>Example: Branches</b></p> |
138 * | 138 * |
139 * <p>This example demonstrates how to smoothly report progress in situations where some of the work is optional.</p> | 139 * <p>This example demonstrates how to smoothly report progress in situations where some of the work is optional.</p> |
140 * | 140 * |
141 * <pre> | 141 * <pre> |
142 * void doSomething(IProgressMonitor monitor) { | 142 * void doSomething(IProgressMonitor monitor) { |
143 * SubMonitor progress = SubMonitor.convert(monitor, 100); | 143 * SubMonitor progress = SubMonitor.convert(monitor, 100); |
144 * | 144 * |
145 * if (condition) { | 145 * if (condition) { |
146 * // Use 50% of the progress to do some work | 146 * // Use 50% of the progress to do some work |
147 * doSomeWork(progress.newChild(50)); | 147 * doSomeWork(progress.newChild(50)); |
148 * } | 148 * } |
149 * | 149 * |
150 * // Don't report any work, but ensure that we have 50 ticks remaining on the progress monitor. | 150 * // Don't report any work, but ensure that we have 50 ticks remaining on the progress monitor. |
151 * // If we already consumed 50 ticks in the above branch, this is a no-op. Otherwise, the remaining | 151 * // If we already consumed 50 ticks in the above branch, this is a no-op. Otherwise, the remaining |
152 * // space in the monitor is redistributed into 50 ticks. | 152 * // space in the monitor is redistributed into 50 ticks. |
153 * | 153 * |
154 * progress.setWorkRemaining(50); | 154 * progress.setWorkRemaining(50); |
155 * | 155 * |
156 * // Use the remainder of the progress monitor to do the rest of the work | 156 * // Use the remainder of the progress monitor to do the rest of the work |
157 * doSomeWork(progress.newChild(50)); | 157 * doSomeWork(progress.newChild(50)); |
158 * } | 158 * } |
159 * </pre> | 159 * </pre> |
160 * | 160 * |
161 * <p>Please beware of the following anti-pattern:</p> | 161 * <p>Please beware of the following anti-pattern:</p> |
162 * | 162 * |
163 * <pre> | 163 * <pre> |
164 * if (condition) { | 164 * if (condition) { |
165 * // Use 50% of the progress to do some work | 165 * // Use 50% of the progress to do some work |
166 * doSomeWork(progress.newChild(50)); | 166 * doSomeWork(progress.newChild(50)); |
167 * } else { | 167 * } else { |
168 * // Bad: Causes the progress monitor to appear to start at 50%, wasting half of the | 168 * // Bad: Causes the progress monitor to appear to start at 50%, wasting half of the |
169 * // space in the monitor. | 169 * // space in the monitor. |
170 * progress.worked(50); | 170 * progress.worked(50); |
171 * } | 171 * } |
172 * </pre> | 172 * </pre> |
173 * | 173 * |
174 * | 174 * |
175 * <p></p> | 175 * <p></p> |
176 * <p><b>Example: Loops</b></p> | 176 * <p><b>Example: Loops</b></p> |
177 * | 177 * |
178 * <p>This example demonstrates how to report progress in a loop.</p> | 178 * <p>This example demonstrates how to report progress in a loop.</p> |
179 * | 179 * |
180 * <pre> | 180 * <pre> |
181 * void doSomething(IProgressMonitor monitor, Collection someCollection) { | 181 * void doSomething(IProgressMonitor monitor, Collection someCollection) { |
182 * SubMonitor progress = SubMonitor.convert(monitor, 100); | 182 * SubMonitor progress = SubMonitor.convert(monitor, 100); |
183 * | 183 * |
184 * // Create a new progress monitor that uses 70% of the total progress and will allocate one tick | 184 * // Create a new progress monitor that uses 70% of the total progress and will allocate one tick |
185 * // for each element of the given collection. | 185 * // for each element of the given collection. |
186 * SubMonitor loopProgress = progress.newChild(70).setWorkRemaining(someCollection.size()); | 186 * SubMonitor loopProgress = progress.newChild(70).setWorkRemaining(someCollection.size()); |
187 * | 187 * |
188 * for (Iterator iter = someCollection.iterator(); iter.hasNext();) { | 188 * for (Iterator iter = someCollection.iterator(); iter.hasNext();) { |
189 * Object next = iter.next(); | 189 * Object next = iter.next(); |
190 * | 190 * |
191 * doWorkOnElement(next, loopProgress.newChild(1)); | 191 * doWorkOnElement(next, loopProgress.newChild(1)); |
192 * } | 192 * } |
193 * | 193 * |
194 * // Use the remaining 30% of the progress monitor to do some work outside the loop | 194 * // Use the remaining 30% of the progress monitor to do some work outside the loop |
195 * doSomeWork(progress.newChild(30)); | 195 * doSomeWork(progress.newChild(30)); |
196 * } | 196 * } |
197 * </pre> | 197 * </pre> |
198 * | 198 * |
199 * | 199 * |
200 * <p></p> | 200 * <p></p> |
201 * <p><b>Example: Infinite progress</b></p> | 201 * <p><b>Example: Infinite progress</b></p> |
202 * | 202 * |
203 * <p>This example demonstrates how to report logarithmic progress in situations where the number of ticks | 203 * <p>This example demonstrates how to report logarithmic progress in situations where the number of ticks |
204 * cannot be easily computed in advance.</p> | 204 * cannot be easily computed in advance.</p> |
205 * | 205 * |
206 * <pre> | 206 * <pre> |
207 * void doSomething(IProgressMonitor monitor, LinkedListNode node) { | 207 * void doSomething(IProgressMonitor monitor, LinkedListNode node) { |
208 * SubMonitor progress = SubMonitor.convert(monitor); | 208 * SubMonitor progress = SubMonitor.convert(monitor); |
209 * | 209 * |
210 * while (node !is null) { | 210 * while (node !is null) { |
211 * // Regardless of the amount of progress reported so far, | 211 * // Regardless of the amount of progress reported so far, |
212 * // use 0.01% of the space remaining in the monitor to process the next node. | 212 * // use 0.01% of the space remaining in the monitor to process the next node. |
213 * progress.setWorkRemaining(10000); | 213 * progress.setWorkRemaining(10000); |
214 * | 214 * |
215 * doWorkOnElement(node, progress.newChild(1)); | 215 * doWorkOnElement(node, progress.newChild(1)); |
216 * | 216 * |
217 * node = node.next; | 217 * node = node.next; |
218 * } | 218 * } |
219 * } | 219 * } |
220 * </pre> | 220 * </pre> |
221 * | 221 * |
222 * <p> | 222 * <p> |
223 * This class can be used without OSGi running. | 223 * This class can be used without OSGi running. |
224 * </p> | 224 * </p> |
225 * | 225 * |
226 * @since org.eclipse.equinox.common 3.3 | 226 * @since org.eclipse.equinox.common 3.3 |
227 */ | 227 */ |
228 public final class SubMonitor : IProgressMonitorWithBlocking { | 228 public final class SubMonitor : IProgressMonitorWithBlocking { |
229 | 229 |
230 /** | 230 /** |
249 | 249 |
250 /** | 250 /** |
251 * Remembers the last subtask name. Prevents the SubMonitor from setting the same | 251 * Remembers the last subtask name. Prevents the SubMonitor from setting the same |
252 * subtask string more than once in a row. | 252 * subtask string more than once in a row. |
253 */ | 253 */ |
254 private String subTask_ = null; | 254 private String subTask = null; |
255 | 255 |
256 /** | 256 /** |
257 * Creates a RootInfo struct that delegates to the given progress | 257 * Creates a RootInfo struct that delegates to the given progress |
258 * monitor. | 258 * monitor. |
259 * | 259 * |
260 * @param root progress monitor to delegate to | 260 * @param root progress monitor to delegate to |
261 */ | 261 */ |
262 public this(IProgressMonitor root) { | 262 public this(IProgressMonitor root) { |
263 this.root = root; | 263 this.root = root; |
264 } | 264 } |
278 this.taskName = taskName; | 278 this.taskName = taskName; |
279 root.setTaskName(taskName); | 279 root.setTaskName(taskName); |
280 } | 280 } |
281 | 281 |
282 public void subTask(String name) { | 282 public void subTask(String name) { |
283 if (eq(subTask_, name)) { | 283 if (eq(subTask, name)) { |
284 return; | 284 return; |
285 } | 285 } |
286 | 286 |
287 this.subTask_ = name; | 287 this.subTask = name; |
288 root.subTask(name); | 288 root.subTask(name); |
289 } | 289 } |
290 | 290 |
291 public void worked(int i) { | 291 public void worked(int i) { |
292 root.worked(i); | 292 root.worked(i); |
293 } | 293 } |
294 | 294 |
295 public void clearBlocked() { | 295 public void clearBlocked() { |
296 if ( auto mon = cast(IProgressMonitorWithBlocking)root ) | 296 if ( null !is cast(IProgressMonitorWithBlocking)root ) |
297 mon.clearBlocked(); | 297 (cast(IProgressMonitorWithBlocking) root).clearBlocked(); |
298 } | 298 } |
299 | 299 |
300 public void setBlocked(IStatus reason) { | 300 public void setBlocked(IStatus reason) { |
301 if ( auto mon = cast(IProgressMonitorWithBlocking)root ) | 301 if ( null !is cast(IProgressMonitorWithBlocking)root ) |
302 mon.setBlocked(reason); | 302 (cast(IProgressMonitorWithBlocking) root).setBlocked(reason); |
303 } | 303 } |
304 | 304 |
305 } | 305 } |
306 | 306 |
307 /** | 307 /** |
320 */ | 320 */ |
321 private double usedForChildren = 0.0; | 321 private double usedForChildren = 0.0; |
322 | 322 |
323 /** | 323 /** |
324 * Number of ticks allocated for this instance's children. This is the total number | 324 * Number of ticks allocated for this instance's children. This is the total number |
325 * of ticks that may be passed into worked(int) or newChild(int). | 325 * of ticks that may be passed into worked(int) or newChild(int). |
326 */ | 326 */ |
327 private int totalForChildren; | 327 private int totalForChildren; |
328 | 328 |
329 /** | 329 /** |
330 * Children created by newChild will be completed automatically the next time | 330 * Children created by newChild will be completed automatically the next time |
331 * the parent progress monitor is touched. This points to the last incomplete child | 331 * the parent progress monitor is touched. This points to the last incomplete child |
332 * created with newChild. | 332 * created with newChild. |
333 */ | 333 */ |
334 private IProgressMonitor lastSubMonitor = null; | 334 private IProgressMonitor lastSubMonitor = null; |
335 | 335 |
336 /** | 336 /** |
337 * Used to communicate with the root of this progress monitor tree | 337 * Used to communicate with the root of this progress monitor tree |
338 */ | 338 */ |
339 private const RootInfo root; | 339 private final RootInfo root; |
340 | 340 |
341 /** | 341 /** |
342 * A bitwise combination of the SUPPRESS_* flags. | 342 * A bitwise combination of the SUPPRESS_* flags. |
343 */ | 343 */ |
344 private const int flags; | 344 private final int flags; |
345 | 345 |
346 /** | 346 /** |
347 * May be passed as a flag to newChild. Indicates that the calls | 347 * May be passed as a flag to newChild. Indicates that the calls |
348 * to subTask on the child should be ignored. Without this flag, | 348 * to subTask on the child should be ignored. Without this flag, |
349 * calling subTask on the child will result in a call to subTask | 349 * calling subTask on the child will result in a call to subTask |
350 * on its parent. | 350 * on its parent. |
351 */ | 351 */ |
352 public static const int SUPPRESS_SUBTASK = 0x0001; | 352 public static final int SUPPRESS_SUBTASK = 0x0001; |
353 | 353 |
354 /** | 354 /** |
355 * May be passed as a flag to newChild. Indicates that strings | 355 * May be passed as a flag to newChild. Indicates that strings |
356 * passed into beginTask should be ignored. If this flag is | 356 * passed into beginTask should be ignored. If this flag is |
357 * specified, then the progress monitor instance will accept null | 357 * specified, then the progress monitor instance will accept null |
358 * as the first argument to beginTask. Without this flag, any | 358 * as the first argument to beginTask. Without this flag, any |
359 * string passed to beginTask will result in a call to | 359 * string passed to beginTask will result in a call to |
360 * setTaskName on the parent. | 360 * setTaskName on the parent. |
361 */ | 361 */ |
362 public static const int SUPPRESS_BEGINTASK = 0x0002; | 362 public static final int SUPPRESS_BEGINTASK = 0x0002; |
363 | 363 |
364 /** | 364 /** |
365 * May be passed as a flag to newChild. Indicates that strings | 365 * May be passed as a flag to newChild. Indicates that strings |
366 * passed into setTaskName should be ignored. If this string | 366 * passed into setTaskName should be ignored. If this string |
367 * is omitted, then a call to setTaskName on the child will | 367 * is omitted, then a call to setTaskName on the child will |
368 * result in a call to setTaskName on the parent. | 368 * result in a call to setTaskName on the parent. |
369 */ | 369 */ |
370 public static const int SUPPRESS_SETTASKNAME = 0x0004; | 370 public static final int SUPPRESS_SETTASKNAME = 0x0004; |
371 | 371 |
372 /** | 372 /** |
373 * May be passed as a flag to newChild. Indicates that strings | 373 * May be passed as a flag to newChild. Indicates that strings |
374 * passed to setTaskName, subTask, and beginTask should all be ignored. | 374 * passed to setTaskName, subTask, and beginTask should all be ignored. |
375 */ | 375 */ |
376 public static const int SUPPRESS_ALL_LABELS = SUPPRESS_SETTASKNAME | SUPPRESS_BEGINTASK | SUPPRESS_SUBTASK; | 376 public static final int SUPPRESS_ALL_LABELS = SUPPRESS_SETTASKNAME | SUPPRESS_BEGINTASK | SUPPRESS_SUBTASK; |
377 | 377 |
378 /** | 378 /** |
379 * May be passed as a flag to newChild. Indicates that strings | 379 * May be passed as a flag to newChild. Indicates that strings |
380 * passed to setTaskName, subTask, and beginTask should all be propogated | 380 * passed to setTaskName, subTask, and beginTask should all be propogated |
381 * to the parent. | 381 * to the parent. |
382 */ | 382 */ |
383 public static const int SUPPRESS_NONE = 0; | 383 public static final int SUPPRESS_NONE = 0; |
384 | 384 |
385 /** | 385 /** |
386 * Creates a new SubMonitor that will report its progress via | 386 * Creates a new SubMonitor that will report its progress via |
387 * the given RootInfo. | 387 * the given RootInfo. |
388 * @param rootInfo the root of this progress monitor tree | 388 * @param rootInfo the root of this progress monitor tree |
396 this.totalForChildren = availableToChildren; | 396 this.totalForChildren = availableToChildren; |
397 this.flags = flags; | 397 this.flags = flags; |
398 } | 398 } |
399 | 399 |
400 /** | 400 /** |
401 * <p>Converts an unknown (possibly null) IProgressMonitor into a SubMonitor. It is | 401 * <p>Converts an unknown (possibly null) IProgressMonitor into a SubMonitor. It is |
402 * not necessary to call done() on the result, but the caller is responsible for calling | 402 * not necessary to call done() on the result, but the caller is responsible for calling |
403 * done() on the argument. Calls beginTask on the argument.</p> | 403 * done() on the argument. Calls beginTask on the argument.</p> |
404 * | 404 * |
405 * <p>This method should generally be called at the beginning of a method that accepts | 405 * <p>This method should generally be called at the beginning of a method that accepts |
406 * an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.</p> | 406 * an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.</p> |
407 * | 407 * |
408 * @param monitor monitor to convert to a SubMonitor instance or null. Treats null | 408 * @param monitor monitor to convert to a SubMonitor instance or null. Treats null |
409 * as a new instance of <code>NullProgressMonitor</code>. | 409 * as a new instance of <code>NullProgressMonitor</code>. |
410 * @return a SubMonitor instance that adapts the argument | 410 * @return a SubMonitor instance that adapts the argument |
411 */ | 411 */ |
412 public static SubMonitor convert(IProgressMonitor monitor) { | 412 public static SubMonitor convert(IProgressMonitor monitor) { |
416 /** | 416 /** |
417 * <p>Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated | 417 * <p>Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated |
418 * with the given number of ticks. It is not necessary to call done() on the result, | 418 * with the given number of ticks. It is not necessary to call done() on the result, |
419 * but the caller is responsible for calling done() on the argument. Calls beginTask | 419 * but the caller is responsible for calling done() on the argument. Calls beginTask |
420 * on the argument.</p> | 420 * on the argument.</p> |
421 * | 421 * |
422 * <p>This method should generally be called at the beginning of a method that accepts | 422 * <p>This method should generally be called at the beginning of a method that accepts |
423 * an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.</p> | 423 * an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.</p> |
424 * | 424 * |
425 * @param monitor monitor to convert to a SubMonitor instance or null. Treats null | 425 * @param monitor monitor to convert to a SubMonitor instance or null. Treats null |
426 * as a new instance of <code>NullProgressMonitor</code>. | 426 * as a new instance of <code>NullProgressMonitor</code>. |
427 * @param work number of ticks that will be available in the resulting monitor | 427 * @param work number of ticks that will be available in the resulting monitor |
428 * @return a SubMonitor instance that adapts the argument | 428 * @return a SubMonitor instance that adapts the argument |
429 */ | 429 */ |
434 /** | 434 /** |
435 * <p>Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated | 435 * <p>Converts an unknown (possibly null) IProgressMonitor into a SubMonitor allocated |
436 * with the given number of ticks. It is not necessary to call done() on the result, | 436 * with the given number of ticks. It is not necessary to call done() on the result, |
437 * but the caller is responsible for calling done() on the argument. Calls beginTask | 437 * but the caller is responsible for calling done() on the argument. Calls beginTask |
438 * on the argument.</p> | 438 * on the argument.</p> |
439 * | 439 * |
440 * <p>This method should generally be called at the beginning of a method that accepts | 440 * <p>This method should generally be called at the beginning of a method that accepts |
441 * an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.</p> | 441 * an IProgressMonitor in order to convert the IProgressMonitor into a SubMonitor.</p> |
442 * | 442 * |
443 * @param monitor to convert into a SubMonitor instance or null. If given a null argument, | 443 * @param monitor to convert into a SubMonitor instance or null. If given a null argument, |
444 * the resulting SubMonitor will not report its progress anywhere. | 444 * the resulting SubMonitor will not report its progress anywhere. |
445 * @param taskName user readable name to pass to monitor.beginTask. Never null. | 445 * @param taskName user readable name to pass to monitor.beginTask. Never null. |
446 * @param work initial number of ticks to allocate for children of the SubMonitor | 446 * @param work initial number of ticks to allocate for children of the SubMonitor |
447 * @return a new SubMonitor instance that is a child of the given monitor | 447 * @return a new SubMonitor instance that is a child of the given monitor |
448 */ | 448 */ |
449 public static SubMonitor convert(IProgressMonitor monitor, String taskName, int work) { | 449 public static SubMonitor convert(IProgressMonitor monitor, String taskName, int work) { |
450 if (monitor is null) | 450 if (monitor is null) |
451 monitor = new NullProgressMonitor(); | 451 monitor = new NullProgressMonitor(); |
452 | 452 |
453 // Optimization: if the given monitor already a SubMonitor, no conversion is necessary | 453 // Optimization: if the given monitor already a SubMonitor, no conversion is necessary |
454 if ( cast(SubMonitor) monitor ) { | 454 if ( null !is cast(SubMonitor)monitor ) { |
455 monitor.beginTask(taskName, work); | 455 monitor.beginTask(taskName, work); |
456 return cast(SubMonitor) monitor; | 456 return cast(SubMonitor) monitor; |
457 } | 457 } |
458 | 458 |
459 monitor.beginTask(taskName, MINIMUM_RESOLUTION); | 459 monitor.beginTask(taskName, MINIMUM_RESOLUTION); |
461 } | 461 } |
462 | 462 |
463 /** | 463 /** |
464 * <p>Sets the work remaining for this SubMonitor instance. This is the total number | 464 * <p>Sets the work remaining for this SubMonitor instance. This is the total number |
465 * of ticks that may be reported by all subsequent calls to worked(int), newChild(int), etc. | 465 * of ticks that may be reported by all subsequent calls to worked(int), newChild(int), etc. |
466 * This may be called many times for the same SubMonitor instance. When this method | 466 * This may be called many times for the same SubMonitor instance. When this method |
467 * is called, the remaining space on the progress monitor is redistributed into the given | 467 * is called, the remaining space on the progress monitor is redistributed into the given |
468 * number of ticks.</p> | 468 * number of ticks.</p> |
469 * | 469 * |
470 * <p>It doesn't matter how much progress has already been reported with this SubMonitor | 470 * <p>It doesn't matter how much progress has already been reported with this SubMonitor |
471 * instance. If you call setWorkRemaining(100), you will be able to report 100 more ticks of | 471 * instance. If you call setWorkRemaining(100), you will be able to report 100 more ticks of |
472 * work before the progress meter reaches 100%.</p> | 472 * work before the progress meter reaches 100%.</p> |
473 * | 473 * |
474 * @param workRemaining total number of remaining ticks | 474 * @param workRemaining total number of remaining ticks |
475 * @return the receiver | 475 * @return the receiver |
476 */ | 476 */ |
477 public SubMonitor setWorkRemaining(int workRemaining) { | 477 public SubMonitor setWorkRemaining(int workRemaining) { |
478 // Ensure we don't try to allocate negative ticks | 478 // Ensure we don't try to allocate negative ticks |
479 workRemaining = Math.max(0, workRemaining); | 479 workRemaining = Math.max(0, workRemaining); |
480 | 480 |
481 // Ensure we don't cause division by zero | 481 // Ensure we don't cause division by zero |
482 if (totalForChildren > 0 && totalParent > usedForParent) { | 482 if (totalForChildren > 0 && totalParent > usedForParent) { |
483 // Note: We want the following value to remain invariant after this method returns | 483 // Note: We want the following value to remain invariant after this method returns |
484 double remainForParent = totalParent * (1.0 - (usedForChildren / totalForChildren)); | 484 double remainForParent = totalParent * (1.0d - (usedForChildren / totalForChildren)); |
485 usedForChildren = (workRemaining * (1.0 - remainForParent / (totalParent - usedForParent))); | 485 usedForChildren = (workRemaining * (1.0d - remainForParent / (totalParent - usedForParent))); |
486 } else | 486 } else |
487 usedForChildren = 0.0; | 487 usedForChildren = 0.0d; |
488 | 488 |
489 totalParent = totalParent - usedForParent; | 489 totalParent = totalParent - usedForParent; |
490 usedForParent = 0; | 490 usedForParent = 0; |
491 totalForChildren = workRemaining; | 491 totalForChildren = workRemaining; |
492 return this; | 492 return this; |
493 } | 493 } |
494 | 494 |
495 /** | 495 /** |
496 * Consumes the given number of child ticks, given as a double. Must only | 496 * Consumes the given number of child ticks, given as a double. Must only |
497 * be called if the monitor is in floating-point mode. | 497 * be called if the monitor is in floating-point mode. |
498 * | 498 * |
499 * @param ticks the number of ticks to consume | 499 * @param ticks the number of ticks to consume |
500 * @return ticks the number of ticks to be consumed from parent | 500 * @return ticks the number of ticks to be consumed from parent |
501 */ | 501 */ |
502 private int consume(double ticks) { | 502 private int consume(double ticks) { |
503 if (totalParent is 0 || totalForChildren is 0) // this monitor has no available work to report | 503 if (totalParent is 0 || totalForChildren is 0) // this monitor has no available work to report |
533 } | 533 } |
534 | 534 |
535 /** | 535 /** |
536 * Starts a new main task. The string argument is ignored | 536 * Starts a new main task. The string argument is ignored |
537 * if and only if the SUPPRESS_BEGINTASK flag has been set on this SubMonitor | 537 * if and only if the SUPPRESS_BEGINTASK flag has been set on this SubMonitor |
538 * instance. | 538 * instance. |
539 * | 539 * |
540 * <p>This method is equivalent calling setWorkRemaining(...) on the reciever. Unless | 540 * <p>This method is equivalent calling setWorkRemaining(...) on the reciever. Unless |
541 * the SUPPRESS_BEGINTASK flag is set, this will also be equivalent to calling | 541 * the SUPPRESS_BEGINTASK flag is set, this will also be equivalent to calling |
542 * setTaskName(...) on the parent.</p> | 542 * setTaskName(...) on the parent.</p> |
543 * | 543 * |
544 * @param name new main task name | 544 * @param name new main task name |
545 * @param totalWork number of ticks to allocate | 545 * @param totalWork number of ticks to allocate |
546 * | 546 * |
547 * @see org.eclipse.core.runtime.IProgressMonitor#beginTask(java.lang.String, int) | 547 * @see org.eclipse.core.runtime.IProgressMonitor#beginTask(java.lang.String, int) |
548 */ | 548 */ |
549 public void beginTask(String name, int totalWork) { | 549 public void beginTask(String name, int totalWork) { |
550 if ((flags & SUPPRESS_BEGINTASK) is 0 && name !is null) | 550 if ((flags & SUPPRESS_BEGINTASK) is 0 && name !is null) |
551 root.setTaskName(name); | 551 root.setTaskName(name); |
562 root.worked(delta); | 562 root.worked(delta); |
563 | 563 |
564 totalParent = 0; | 564 totalParent = 0; |
565 usedForParent = 0; | 565 usedForParent = 0; |
566 totalForChildren = 0; | 566 totalForChildren = 0; |
567 usedForChildren = 0.0; | 567 usedForChildren = 0.0d; |
568 } | 568 } |
569 | 569 |
570 /* (non-Javadoc) | 570 /* (non-Javadoc) |
571 * @see org.eclipse.core.runtime.IProgressMonitor#internalWorked(double) | 571 * @see org.eclipse.core.runtime.IProgressMonitor#internalWorked(double) |
572 */ | 572 */ |
573 public void internalWorked(double work) { | 573 public void internalWorked(double work) { |
574 cleanupActiveChild(); | 574 cleanupActiveChild(); |
575 | 575 |
576 int delta = consume((work > 0.0) ? work : 0.0); | 576 int delta = consume((work > 0.0d) ? work : 0.0d); |
577 if (delta !is 0) | 577 if (delta !is 0) |
578 root.worked(delta); | 578 root.worked(delta); |
579 } | 579 } |
580 | 580 |
581 /* (non-Javadoc) | 581 /* (non-Javadoc) |
592 public void worked(int work) { | 592 public void worked(int work) { |
593 internalWorked(work); | 593 internalWorked(work); |
594 } | 594 } |
595 | 595 |
596 /* (non-Javadoc) | 596 /* (non-Javadoc) |
597 * @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(bool) | 597 * @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(boolean) |
598 */ | 598 */ |
599 public void setCanceled(bool b) { | 599 public void setCanceled(bool b) { |
600 root.setCanceled(b); | 600 root.setCanceled(b); |
601 } | 601 } |
602 | 602 |
603 /** | 603 /** |
604 * <p>Creates a sub progress monitor that will consume the given number of ticks from the | 604 * <p>Creates a sub progress monitor that will consume the given number of ticks from the |
605 * receiver. It is not necessary to call <code>beginTask</code> or <code>done</code> on the | 605 * receiver. It is not necessary to call <code>beginTask</code> or <code>done</code> on the |
606 * result. However, the resulting progress monitor will not report any work after the first | 606 * result. However, the resulting progress monitor will not report any work after the first |
607 * call to done() or before ticks are allocated. Ticks may be allocated by calling beginTask | 607 * call to done() or before ticks are allocated. Ticks may be allocated by calling beginTask |
608 * or setWorkRemaining.</p> | 608 * or setWorkRemaining.</p> |
609 * | 609 * |
610 * <p>Each SubMonitor only has one active child at a time. Each time newChild() is called, the | 610 * <p>Each SubMonitor only has one active child at a time. Each time newChild() is called, the |
611 * result becomes the new active child and any unused progress from the previously-active child is | 611 * result becomes the new active child and any unused progress from the previously-active child is |
612 * consumed.</p> | 612 * consumed.</p> |
613 * | 613 * |
614 * <p>This is property makes it unnecessary to call done() on a SubMonitor instance, since child | 614 * <p>This is property makes it unnecessary to call done() on a SubMonitor instance, since child |
615 * monitors are automatically cleaned up the next time the parent is touched.</p> | 615 * monitors are automatically cleaned up the next time the parent is touched.</p> |
616 * | 616 * |
617 * <code><pre> | 617 * <code><pre> |
618 * //////////////////////////////////////////////////////////////////////////// | 618 * //////////////////////////////////////////////////////////////////////////// |
619 * // Example 1: Typical usage of newChild | 619 * // Example 1: Typical usage of newChild |
620 * void myMethod(IProgressMonitor parent) { | 620 * void myMethod(IProgressMonitor parent) { |
621 * SubMonitor progress = SubMonitor.convert(parent, 100); | 621 * SubMonitor progress = SubMonitor.convert(parent, 100); |
622 * doSomething(progress.newChild(50)); | 622 * doSomething(progress.newChild(50)); |
623 * doSomethingElse(progress.newChild(50)); | 623 * doSomethingElse(progress.newChild(50)); |
624 * } | 624 * } |
625 * | 625 * |
626 * //////////////////////////////////////////////////////////////////////////// | 626 * //////////////////////////////////////////////////////////////////////////// |
627 * // Example 2: Demonstrates the function of active children. Creating children | 627 * // Example 2: Demonstrates the function of active children. Creating children |
628 * // is sufficient to smoothly report progress, even if worked(...) and done() | 628 * // is sufficient to smoothly report progress, even if worked(...) and done() |
629 * // are never called. | 629 * // are never called. |
630 * void myMethod(IProgressMonitor parent) { | 630 * void myMethod(IProgressMonitor parent) { |
631 * SubMonitor progress = SubMonitor.convert(parent, 100); | 631 * SubMonitor progress = SubMonitor.convert(parent, 100); |
632 * | 632 * |
633 * for (int i = 0; i < 100; i++) { | 633 * for (int i = 0; i < 100; i++) { |
634 * // Creating the next child monitor will clean up the previous one, | 634 * // Creating the next child monitor will clean up the previous one, |
635 * // causing progress to be reported smoothly even if we don't do anything | 635 * // causing progress to be reported smoothly even if we don't do anything |
636 * // with the monitors we create | 636 * // with the monitors we create |
637 * progress.newChild(1); | 637 * progress.newChild(1); |
638 * } | 638 * } |
639 * } | 639 * } |
640 * | 640 * |
641 * //////////////////////////////////////////////////////////////////////////// | 641 * //////////////////////////////////////////////////////////////////////////// |
642 * // Example 3: Demonstrates a common anti-pattern | 642 * // Example 3: Demonstrates a common anti-pattern |
643 * void wrongMethod(IProgressMonitor parent) { | 643 * void wrongMethod(IProgressMonitor parent) { |
644 * SubMonitor progress = SubMonitor.convert(parent, 100); | 644 * SubMonitor progress = SubMonitor.convert(parent, 100); |
645 * | 645 * |
646 * // WRONG WAY: Won't have the intended effect, as only one of these progress | 646 * // WRONG WAY: Won't have the intended effect, as only one of these progress |
647 * // monitors may be active at a time and the other will report no progress. | 647 * // monitors may be active at a time and the other will report no progress. |
648 * callMethod(progress.newChild(50), computeValue(progress.newChild(50))); | 648 * callMethod(progress.newChild(50), computeValue(progress.newChild(50))); |
649 * } | 649 * } |
650 * | 650 * |
651 * void rightMethod(IProgressMonitor parent) { | 651 * void rightMethod(IProgressMonitor parent) { |
652 * SubMonitor progress = SubMonitor.convert(parent, 100); | 652 * SubMonitor progress = SubMonitor.convert(parent, 100); |
653 * | 653 * |
654 * // RIGHT WAY: Break up method calls so that only one SubMonitor is in use at a time. | 654 * // RIGHT WAY: Break up method calls so that only one SubMonitor is in use at a time. |
655 * Object someValue = computeValue(progress.newChild(50)); | 655 * Object someValue = computeValue(progress.newChild(50)); |
656 * callMethod(progress.newChild(50), someValue); | 656 * callMethod(progress.newChild(50), someValue); |
657 * } | 657 * } |
658 * </pre></code> | 658 * </pre></code> |
659 * | 659 * |
660 * @param totalWork number of ticks to consume from the reciever | 660 * @param totalWork number of ticks to consume from the reciever |
661 * @return new sub progress monitor that may be used in place of a new SubMonitor | 661 * @return new sub progress monitor that may be used in place of a new SubMonitor |
662 */ | 662 */ |
663 public SubMonitor newChild(int totalWork) { | 663 public SubMonitor newChild(int totalWork) { |
664 return newChild(totalWork, SUPPRESS_BEGINTASK); | 664 return newChild(totalWork, SUPPRESS_BEGINTASK); |
665 } | 665 } |
666 | 666 |
667 /** | 667 /** |
668 * <p>Creates a sub progress monitor that will consume the given number of ticks from the | 668 * <p>Creates a sub progress monitor that will consume the given number of ticks from the |
669 * receiver. It is not necessary to call <code>beginTask</code> or <code>done</code> on the | 669 * receiver. It is not necessary to call <code>beginTask</code> or <code>done</code> on the |
670 * result. However, the resulting progress monitor will not report any work after the first | 670 * result. However, the resulting progress monitor will not report any work after the first |
671 * call to done() or before ticks are allocated. Ticks may be allocated by calling beginTask | 671 * call to done() or before ticks are allocated. Ticks may be allocated by calling beginTask |
672 * or setWorkRemaining.</p> | 672 * or setWorkRemaining.</p> |
673 * | 673 * |
674 * <p>Each SubMonitor only has one active child at a time. Each time newChild() is called, the | 674 * <p>Each SubMonitor only has one active child at a time. Each time newChild() is called, the |
675 * result becomes the new active child and any unused progress from the previously-active child is | 675 * result becomes the new active child and any unused progress from the previously-active child is |
676 * consumed.</p> | 676 * consumed.</p> |
677 * | 677 * |
678 * <p>This is property makes it unnecessary to call done() on a SubMonitor instance, since child | 678 * <p>This is property makes it unnecessary to call done() on a SubMonitor instance, since child |
679 * monitors are automatically cleaned up the next time the parent is touched.</p> | 679 * monitors are automatically cleaned up the next time the parent is touched.</p> |
680 * | 680 * |
681 * <code><pre> | 681 * <code><pre> |
682 * //////////////////////////////////////////////////////////////////////////// | 682 * //////////////////////////////////////////////////////////////////////////// |
683 * // Example 1: Typical usage of newChild | 683 * // Example 1: Typical usage of newChild |
684 * void myMethod(IProgressMonitor parent) { | 684 * void myMethod(IProgressMonitor parent) { |
685 * SubMonitor progress = SubMonitor.convert(parent, 100); | 685 * SubMonitor progress = SubMonitor.convert(parent, 100); |
686 * doSomething(progress.newChild(50)); | 686 * doSomething(progress.newChild(50)); |
687 * doSomethingElse(progress.newChild(50)); | 687 * doSomethingElse(progress.newChild(50)); |
688 * } | 688 * } |
689 * | 689 * |
690 * //////////////////////////////////////////////////////////////////////////// | 690 * //////////////////////////////////////////////////////////////////////////// |
691 * // Example 2: Demonstrates the function of active children. Creating children | 691 * // Example 2: Demonstrates the function of active children. Creating children |
692 * // is sufficient to smoothly report progress, even if worked(...) and done() | 692 * // is sufficient to smoothly report progress, even if worked(...) and done() |
693 * // are never called. | 693 * // are never called. |
694 * void myMethod(IProgressMonitor parent) { | 694 * void myMethod(IProgressMonitor parent) { |
695 * SubMonitor progress = SubMonitor.convert(parent, 100); | 695 * SubMonitor progress = SubMonitor.convert(parent, 100); |
696 * | 696 * |
697 * for (int i = 0; i < 100; i++) { | 697 * for (int i = 0; i < 100; i++) { |
698 * // Creating the next child monitor will clean up the previous one, | 698 * // Creating the next child monitor will clean up the previous one, |
699 * // causing progress to be reported smoothly even if we don't do anything | 699 * // causing progress to be reported smoothly even if we don't do anything |
700 * // with the monitors we create | 700 * // with the monitors we create |
701 * progress.newChild(1); | 701 * progress.newChild(1); |
702 * } | 702 * } |
703 * } | 703 * } |
704 * | 704 * |
705 * //////////////////////////////////////////////////////////////////////////// | 705 * //////////////////////////////////////////////////////////////////////////// |
706 * // Example 3: Demonstrates a common anti-pattern | 706 * // Example 3: Demonstrates a common anti-pattern |
707 * void wrongMethod(IProgressMonitor parent) { | 707 * void wrongMethod(IProgressMonitor parent) { |
708 * SubMonitor progress = SubMonitor.convert(parent, 100); | 708 * SubMonitor progress = SubMonitor.convert(parent, 100); |
709 * | 709 * |
710 * // WRONG WAY: Won't have the intended effect, as only one of these progress | 710 * // WRONG WAY: Won't have the intended effect, as only one of these progress |
711 * // monitors may be active at a time and the other will report no progress. | 711 * // monitors may be active at a time and the other will report no progress. |
712 * callMethod(progress.newChild(50), computeValue(progress.newChild(50))); | 712 * callMethod(progress.newChild(50), computeValue(progress.newChild(50))); |
713 * } | 713 * } |
714 * | 714 * |
715 * void rightMethod(IProgressMonitor parent) { | 715 * void rightMethod(IProgressMonitor parent) { |
716 * SubMonitor progress = SubMonitor.convert(parent, 100); | 716 * SubMonitor progress = SubMonitor.convert(parent, 100); |
717 * | 717 * |
718 * // RIGHT WAY: Break up method calls so that only one SubMonitor is in use at a time. | 718 * // RIGHT WAY: Break up method calls so that only one SubMonitor is in use at a time. |
719 * Object someValue = computeValue(progress.newChild(50)); | 719 * Object someValue = computeValue(progress.newChild(50)); |
720 * callMethod(progress.newChild(50), someValue); | 720 * callMethod(progress.newChild(50), someValue); |
721 * } | 721 * } |
722 * </pre></code> | 722 * </pre></code> |
723 * | 723 * |
724 * @param totalWork number of ticks to consume from the reciever | 724 * @param totalWork number of ticks to consume from the reciever |
725 * @return new sub progress monitor that may be used in place of a new SubMonitor | 725 * @return new sub progress monitor that may be used in place of a new SubMonitor |
726 */ | 726 */ |
727 public SubMonitor newChild(int totalWork, int suppressFlags) { | 727 public SubMonitor newChild(int totalWork, int suppressFlags) { |
728 double totalWorkDouble = (totalWork > 0) ? totalWork : 0.0; | 728 double totalWorkDouble = (totalWork > 0) ? totalWork : 0.0d; |
729 totalWorkDouble = Math.min(totalWorkDouble, totalForChildren - usedForChildren); | 729 totalWorkDouble = Math.min(totalWorkDouble, totalForChildren - usedForChildren); |
730 cleanupActiveChild(); | 730 cleanupActiveChild(); |
731 | 731 |
732 // Compute the flags for the child. We want the net effect to be as though the child is | 732 // Compute the flags for the child. We want the net effect to be as though the child is |
733 // delegating to its parent, even though it is actually talking directly to the root. | 733 // delegating to its parent, even though it is actually talking directly to the root. |
734 // This means that we need to compute the flags such that - even if a label isn't | 734 // This means that we need to compute the flags such that - even if a label isn't |
735 // suppressed by the child - if that same label would have been suppressed when the | 735 // suppressed by the child - if that same label would have been suppressed when the |
736 // child delegated to its parent, the child must explicitly suppress the label. | 736 // child delegated to its parent, the child must explicitly suppress the label. |
737 int childFlags = SUPPRESS_NONE; | 737 int childFlags = SUPPRESS_NONE; |
738 | 738 |
739 if ((flags & SUPPRESS_SETTASKNAME) !is 0) { | 739 if ((flags & SUPPRESS_SETTASKNAME) !is 0) { |
740 // If the parent was ignoring labels passed to setTaskName, then the child will ignore | 740 // If the parent was ignoring labels passed to setTaskName, then the child will ignore |
741 // labels passed to either beginTask or setTaskName - since both delegate to setTaskName | 741 // labels passed to either beginTask or setTaskName - since both delegate to setTaskName |
778 */ | 778 */ |
779 public void setBlocked(IStatus reason) { | 779 public void setBlocked(IStatus reason) { |
780 root.setBlocked(reason); | 780 root.setBlocked(reason); |
781 } | 781 } |
782 | 782 |
783 protected static bool eq(String o1, String o2) { | 783 protected static bool eq(Object o1, Object o2) { |
784 if (o1.length is 0) | 784 if (o1 is null) |
785 return (o2.length is 0); | 785 return (o2 is null); |
786 if (o2.length is 0) | 786 if (o2 is null) |
787 return false; | 787 return false; |
788 return o1.equals(o2); | 788 return o1.opEquals(o2); |
789 } | 789 } |
790 } | 790 } |