comparison org.eclipse.jface/src/org/eclipse/jface/operation/AccumulatingProgressMonitor.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 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 org.eclipse.jface.operation.AccumulatingProgressMonitor;
14
15
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IProgressMonitorWithBlocking;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.ProgressMonitorWrapper;
22 import org.eclipse.jface.dialogs.Dialog;
23
24 import java.lang.all;
25 import java.util.Set;
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 SWT 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 SWT event
36 * queue. In this case, the progress monitor just updates the internal state of
37 * the runnable that waits in the SWT 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 SWT 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 */
125 public override void beginTask(String name, int totalWork) {
126 synchronized (this) {
127 collector = null;
128 }
129 display.asyncExec(new class(name,totalWork) Runnable {
130 String name_;
131 int totalWork_;
132 this(String a, int b){
133 name_=a;
134 totalWork_=b;
135 }
136 public void run() {
137 currentTask = name_;
138 getWrappedProgressMonitor().beginTask(name_, totalWork_);
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 */
169 public override void done() {
170 synchronized (this) {
171 collector = null;
172 }
173 display.asyncExec(new class Runnable {
174 public void run() {
175 getWrappedProgressMonitor().done();
176 }
177 });
178 }
179
180 /* (non-Javadoc)
181 * Method declared on IProgressMonitor.
182 */
183 public override synchronized void internalWorked(double work) {
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 */
194 public override void setTaskName(String name) {
195 synchronized (this) {
196 collector = null;
197 }
198 display.asyncExec(new class(name) Runnable {
199 String name_;
200 this(String a){
201 name_=a;
202 }
203 public void run() {
204 currentTask = name_;
205 getWrappedProgressMonitor().setTaskName(name_);
206 }
207 });
208 }
209
210 /* (non-Javadoc)
211 * Method declared on IProgressMonitor.
212 */
213 public override synchronized void subTask(String name) {
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 */
224 public override synchronized void worked(int work) {
225 internalWorked(work);
226 }
227
228 /* (non-Javadoc)
229 * @see org.eclipse.core.runtime.ProgressMonitorWrapper#clearBlocked()
230 */
231 public override void clearBlocked() {
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
241 display.asyncExec(new class(pm) Runnable {
242 IProgressMonitor pm_;
243 this(IProgressMonitor a){ pm_=a; }
244 /* (non-Javadoc)
245 * @see java.lang.Runnable#run()
246 */
247 public void run() {
248 (cast(IProgressMonitorWithBlocking) pm_).clearBlocked();
249 Dialog.getBlockedHandler().clearBlocked();
250 }
251 });
252 }
253
254 /* (non-Javadoc)
255 * @see org.eclipse.core.runtime.ProgressMonitorWrapper#setBlocked(org.eclipse.core.runtime.IStatus)
256 */
257 public override void setBlocked(IStatus reason) {
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
266 display.asyncExec(new class(pm,reason) Runnable {
267 IProgressMonitor pm_;
268 IStatus reason_;
269 this(IProgressMonitor a,IStatus b){
270 pm_=a;
271 reason_=b;
272 }
273 /* (non-Javadoc)
274 * @see java.lang.Runnable#run()
275 */
276 public void run() {
277 (cast(IProgressMonitorWithBlocking) pm_).setBlocked(reason_);
278 //Do not give a shell as we want it to block until it opens.
279 Dialog.getBlockedHandler().showBlocked(pm_, reason_, currentTask);
280 }
281 });
282 }
283 }