10
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2004, 2005 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.viewers.deferred.FastProgressReporter;
|
|
14
|
|
15 import dwtx.core.runtime.IProgressMonitor;
|
|
16
|
|
17 import dwt.dwthelper.utils;
|
|
18
|
|
19 /**
|
|
20 * A more efficient alternative to an IProgressMonitor. In particular, the implementation
|
|
21 * is designed to make isCanceled() run as efficiently as possible. Currently package-visible
|
|
22 * because the implementation is incomplete.
|
|
23 *
|
|
24 * @since 3.1
|
|
25 */
|
|
26 final class FastProgressReporter {
|
|
27 private IProgressMonitor monitor;
|
|
28 private /+volatile+/ bool canceled = false;
|
|
29 private int cancelCheck = 0;
|
|
30 // private String taskName;
|
|
31 //
|
|
32 // private int taskDepth = 0;
|
|
33 // private int subTaskSize = 1;
|
|
34 // private int totalWork = 1;
|
|
35 // private int parentWork = 1;
|
|
36 // private int monitorUnitsRemaining;
|
|
37
|
|
38 private static int CANCEL_CHECK_PERIOD = 40;
|
|
39
|
|
40 /**
|
|
41 * Constructs a null FastProgressReporter
|
|
42 */
|
|
43 public this() {
|
|
44 }
|
|
45
|
|
46 /**
|
|
47 * Constructs a FastProgressReporter that wraps the given progress monitor
|
|
48 *
|
|
49 * @param monitor the monitor to wrap
|
|
50 * @param totalProgress the total progress to be reported
|
|
51 */
|
|
52 public this(IProgressMonitor monitor, int totalProgress) {
|
|
53 this.monitor = monitor;
|
|
54 //monitorUnitsRemaining = totalProgress;
|
|
55 canceled = monitor.isCanceled();
|
|
56 }
|
|
57
|
|
58 // /**
|
|
59 // * Every call to beginTask must have a corresponding call to endTask, with the
|
|
60 // * same argument.
|
|
61 // *
|
|
62 // * @param totalWork
|
|
63 // * @since 3.1
|
|
64 // */
|
|
65 // public void beginTask(int totalWork) {
|
|
66 //
|
|
67 // if (monitor is null) {
|
|
68 // return;
|
|
69 // }
|
|
70 //
|
|
71 // taskDepth++;
|
|
72 //
|
|
73 // if (totalWork is 0) {
|
|
74 // return;
|
|
75 // }
|
|
76 //
|
|
77 // this.totalWork *= totalWork;
|
|
78 // }
|
|
79 //
|
|
80 // public void beginSubTask(int subTaskWork) {
|
|
81 // subTaskSize *= subTaskWork;
|
|
82 // }
|
|
83 //
|
|
84 // public void endSubTask(int subTaskWork) {
|
|
85 // subTaskSize /= subTaskWork;
|
|
86 // }
|
|
87 //
|
|
88 // public void worked(int amount) {
|
|
89 // amount *= subTaskSize;
|
|
90 //
|
|
91 // if (amount > totalWork) {
|
|
92 // amount = totalWork;
|
|
93 // }
|
|
94 //
|
|
95 // int consumed = monitorUnitsRemaining * amount / totalWork;
|
|
96 //
|
|
97 // if (consumed > 0) {
|
|
98 // monitor.worked(consumed);
|
|
99 // monitorUnitsRemaining -= consumed;
|
|
100 // }
|
|
101 // totalWork -= amount;
|
|
102 // }
|
|
103 //
|
|
104 // public void endTask(int totalWork) {
|
|
105 // taskDepth--;
|
|
106 //
|
|
107 // if (taskDepth is 0) {
|
|
108 // if (monitor !is null && monitorUnitsRemaining > 0) {
|
|
109 // monitor.worked(monitorUnitsRemaining);
|
|
110 // }
|
|
111 // }
|
|
112 //
|
|
113 // if (totalWork is 0) {
|
|
114 // return;
|
|
115 // }
|
|
116 //
|
|
117 // this.totalWork /= totalWork;
|
|
118 //
|
|
119 // }
|
|
120
|
|
121 /**
|
|
122 * Return whether the progress monitor has been canceled.
|
|
123 *
|
|
124 * @return <code>true</code> if the monitor has been cancelled, <code>false</code> otherwise.
|
|
125 */
|
|
126 public bool isCanceled() {
|
|
127 if (monitor is null) {
|
|
128 return canceled;
|
|
129 }
|
|
130
|
|
131 cancelCheck++;
|
|
132 if (cancelCheck > CANCEL_CHECK_PERIOD) {
|
|
133 canceled = monitor.isCanceled();
|
|
134 cancelCheck = 0;
|
|
135 }
|
|
136 return canceled;
|
|
137 }
|
|
138
|
|
139 /**
|
|
140 * Cancel the progress monitor.
|
|
141 */
|
|
142 public void cancel() {
|
|
143 canceled = true;
|
|
144
|
|
145 if (monitor is null) {
|
|
146 return;
|
|
147 }
|
|
148 monitor.setCanceled(true);
|
|
149 }
|
|
150 }
|