KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > deferred > FastProgressReporter


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  *******************************************************************************/

11 package org.eclipse.jface.viewers.deferred;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14
15 /**
16  * A more efficient alternative to an IProgressMonitor. In particular, the implementation
17  * is designed to make isCanceled() run as efficiently as possible. Currently package-visible
18  * because the implementation is incomplete.
19  *
20  * @since 3.1
21  */

22 final class FastProgressReporter {
23     private IProgressMonitor monitor;
24     private volatile boolean canceled = false;
25     private int cancelCheck = 0;
26 // private String taskName;
27
//
28
// private int taskDepth = 0;
29
// private int subTaskSize = 1;
30
// private int totalWork = 1;
31
// private int parentWork = 1;
32
// private int monitorUnitsRemaining;
33

34     private static int CANCEL_CHECK_PERIOD = 40;
35     
36     /**
37      * Constructs a null FastProgressReporter
38      */

39     public FastProgressReporter() {
40     }
41     
42     /**
43      * Constructs a FastProgressReporter that wraps the given progress monitor
44      *
45      * @param monitor the monitor to wrap
46      * @param totalProgress the total progress to be reported
47      */

48     public FastProgressReporter(IProgressMonitor monitor, int totalProgress) {
49         this.monitor = monitor;
50         //monitorUnitsRemaining = totalProgress;
51
canceled = monitor.isCanceled();
52     }
53     
54 // /**
55
// * Every call to beginTask must have a corresponding call to endTask, with the
56
// * same argument.
57
// *
58
// * @param totalWork
59
// * @since 3.1
60
// */
61
// public void beginTask(int totalWork) {
62
//
63
// if (monitor == null) {
64
// return;
65
// }
66
//
67
// taskDepth++;
68
//
69
// if (totalWork == 0) {
70
// return;
71
// }
72
//
73
// this.totalWork *= totalWork;
74
// }
75
//
76
// public void beginSubTask(int subTaskWork) {
77
// subTaskSize *= subTaskWork;
78
// }
79
//
80
// public void endSubTask(int subTaskWork) {
81
// subTaskSize /= subTaskWork;
82
// }
83
//
84
// public void worked(int amount) {
85
// amount *= subTaskSize;
86
//
87
// if (amount > totalWork) {
88
// amount = totalWork;
89
// }
90
//
91
// int consumed = monitorUnitsRemaining * amount / totalWork;
92
//
93
// if (consumed > 0) {
94
// monitor.worked(consumed);
95
// monitorUnitsRemaining -= consumed;
96
// }
97
// totalWork -= amount;
98
// }
99
//
100
// public void endTask(int totalWork) {
101
// taskDepth--;
102
//
103
// if (taskDepth == 0) {
104
// if (monitor != null && monitorUnitsRemaining > 0) {
105
// monitor.worked(monitorUnitsRemaining);
106
// }
107
// }
108
//
109
// if (totalWork == 0) {
110
// return;
111
// }
112
//
113
// this.totalWork /= totalWork;
114
//
115
// }
116

117     /**
118      * Return whether the progress monitor has been canceled.
119      *
120      * @return <code>true</code> if the monitor has been cancelled, <code>false</code> otherwise.
121      */

122     public boolean isCanceled() {
123         if (monitor == null) {
124             return canceled;
125         }
126         
127         cancelCheck++;
128         if (cancelCheck > CANCEL_CHECK_PERIOD) {
129             canceled = monitor.isCanceled();
130             cancelCheck = 0;
131         }
132         return canceled;
133     }
134     
135     /**
136      * Cancel the progress monitor.
137      */

138     public void cancel() {
139         canceled = true;
140         
141         if (monitor == null) {
142             return;
143         }
144         monitor.setCanceled(true);
145     }
146 }
147
Popular Tags