1
|
|
2
|
/*******************************************************************************
|
3
|
* Copyright (c) 2000, 2007 IBM Corporation and others.
|
4
|
* All rights reserved. This program and the accompanying materials
|
5
|
* are made available under the terms of the Eclipse Public License v1.0
|
6
|
* which accompanies this distribution, and is available at
|
7
|
* http://www.eclipse.org/legal/epl-v10.html
|
8
|
*
|
9
|
* Contributors:
|
10
|
* IBM Corporation - initial API and implementation
|
11
|
*******************************************************************************/
|
12
|
package eu.etaxonomy.cdm.common.monitor;
|
13
|
|
14
|
|
15
|
/**
|
16
|
* For new implementations consider using {@link SubMonitor}.
|
17
|
*
|
18
|
* A progress monitor that uses a given amount of work ticks
|
19
|
* from a parent monitor. It can be used as follows:
|
20
|
* <pre>
|
21
|
* try {
|
22
|
* pm.beginTask("Main Task", 100);
|
23
|
* doSomeWork(pm, 30);
|
24
|
* SubProgressMonitor subMonitor= new SubProgressMonitor(pm, 40);
|
25
|
* try {
|
26
|
* subMonitor.beginTask("", 300);
|
27
|
* doSomeWork(subMonitor, 300);
|
28
|
* } finally {
|
29
|
* subMonitor.done();
|
30
|
* }
|
31
|
* doSomeWork(pm, 30);
|
32
|
* } finally {
|
33
|
* pm.done();
|
34
|
* }
|
35
|
* </pre>
|
36
|
* <p>
|
37
|
* This class can be used without OSGi running.
|
38
|
* </p><p>
|
39
|
* This class may be instantiated or subclassed by clients.
|
40
|
* </p>
|
41
|
*
|
42
|
* @see SubMonitor
|
43
|
*/
|
44
|
public class SubProgressMonitor extends ProgressMonitorWrapper {
|
45
|
private static final long serialVersionUID = 3258788051657003998L;
|
46
|
|
47
|
/**
|
48
|
* Style constant indicating that calls to <code>subTask</code>
|
49
|
* should not have any effect.
|
50
|
*
|
51
|
* @see #SubProgressMonitor(IProgressMonitor,int,int)
|
52
|
*/
|
53
|
public static final int SUPPRESS_SUBTASK_LABEL = 1 << 1;
|
54
|
/**
|
55
|
* Style constant indicating that the main task label
|
56
|
* should be prepended to the subtask label.
|
57
|
*
|
58
|
* @see #SubProgressMonitor(IProgressMonitor,int,int)
|
59
|
*/
|
60
|
public static final int PREPEND_MAIN_LABEL_TO_SUBTASK = 1 << 2;
|
61
|
|
62
|
private int parentTicks = 0;
|
63
|
private double sentToParent = 0.0;
|
64
|
private double scale = 0.0;
|
65
|
private int nestedBeginTasks = 0;
|
66
|
private boolean usedUp = false;
|
67
|
private boolean hasSubTask = false;
|
68
|
private int style;
|
69
|
private String mainTaskLabel;
|
70
|
|
71
|
public static SubProgressMonitor NewStarted(IProgressMonitor monitor, int parentTicks,
|
72
|
String name, int childTicks){
|
73
|
SubProgressMonitor result = new SubProgressMonitor(monitor, parentTicks);
|
74
|
result.beginTask(name, childTicks);
|
75
|
return result;
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Creates a new sub-progress monitor for the given monitor. The sub
|
80
|
* progress monitor uses the given number of work ticks from its
|
81
|
* parent monitor.
|
82
|
*
|
83
|
* @param monitor the parent progress monitor
|
84
|
* @param ticks the number of work ticks allocated from the
|
85
|
* parent monitor
|
86
|
*/
|
87
|
public SubProgressMonitor(IProgressMonitor monitor, int ticks) {
|
88
|
this (monitor, ticks, 0);
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Creates a new sub-progress monitor for the given monitor. The sub
|
93
|
* progress monitor uses the given number of work ticks from its
|
94
|
* parent monitor.
|
95
|
*
|
96
|
* @param monitor the parent progress monitor
|
97
|
* @param ticks the number of work ticks allocated from the
|
98
|
* parent monitor
|
99
|
* @param style one of
|
100
|
* <ul>
|
101
|
* <li> <code>SUPPRESS_SUBTASK_LABEL</code> </li>
|
102
|
* <li> <code>PREPEND_MAIN_LABEL_TO_SUBTASK</code> </li>
|
103
|
* </ul>
|
104
|
* @see #SUPPRESS_SUBTASK_LABEL
|
105
|
* @see #PREPEND_MAIN_LABEL_TO_SUBTASK
|
106
|
*/
|
107
|
public SubProgressMonitor(IProgressMonitor monitor, int ticks,
|
108
|
int style) {
|
109
|
super (monitor);
|
110
|
this .parentTicks = (ticks > 0) ? ticks : 0;
|
111
|
this .style = style;
|
112
|
}
|
113
|
|
114
|
/* (Intentionally not javadoc'd)
|
115
|
* Implements the method <code>IProgressMonitor.beginTask</code>.
|
116
|
*
|
117
|
* Starts a new main task. Since this progress monitor is a sub
|
118
|
* progress monitor, the given name will NOT be used to update
|
119
|
* the progress bar's main task label. That means the given
|
120
|
* string will be ignored. If style <code>PREPEND_MAIN_LABEL_TO_SUBTASK
|
121
|
* <code> is specified, then the given string will be prepended to
|
122
|
* every string passed to <code>subTask(String)</code>.
|
123
|
*/
|
124
|
@Override
|
125
|
public void beginTask(String name, int totalWork) {
|
126
|
nestedBeginTasks++;
|
127
|
// Ignore nested begin task calls.
|
128
|
if (nestedBeginTasks > 1) {
|
129
|
return;
|
130
|
}
|
131
|
// be safe: if the argument would cause math errors (zero or
|
132
|
// negative), just use 0 as the scale. This disables progress for
|
133
|
// this submonitor.
|
134
|
scale = totalWork <= 0 ? 0 : (double) parentTicks
|
135
|
/ (double) totalWork;
|
136
|
if ((style & PREPEND_MAIN_LABEL_TO_SUBTASK) != 0) {
|
137
|
mainTaskLabel = name;
|
138
|
}
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
public void done() {
|
143
|
// Ignore if more done calls than beginTask calls or if we are still
|
144
|
// in some nested beginTasks
|
145
|
if (nestedBeginTasks == 0 || --nestedBeginTasks > 0) {
|
146
|
return;
|
147
|
}
|
148
|
// Send any remaining ticks and clear out the subtask text
|
149
|
double remaining = parentTicks - sentToParent;
|
150
|
if (remaining > 0) {
|
151
|
super.internalWorked(remaining);
|
152
|
}
|
153
|
//clear the sub task if there was one
|
154
|
if (hasSubTask){
|
155
|
subTask(""); //$NON-NLS-1$
|
156
|
}
|
157
|
sentToParent = 0;
|
158
|
}
|
159
|
|
160
|
@Override
|
161
|
public void internalWorked(double work) {
|
162
|
if (usedUp || nestedBeginTasks != 1) {
|
163
|
return;
|
164
|
}
|
165
|
|
166
|
double realWork = (work > 0.0d) ? scale * work : 0.0d;
|
167
|
super.internalWorked(realWork);
|
168
|
sentToParent += realWork;
|
169
|
if (sentToParent >= parentTicks) {
|
170
|
usedUp = true;
|
171
|
}
|
172
|
}
|
173
|
|
174
|
@Override
|
175
|
public void subTask(String name) {
|
176
|
if ((style & SUPPRESS_SUBTASK_LABEL) != 0) {
|
177
|
return;
|
178
|
}
|
179
|
hasSubTask = true;
|
180
|
String label = name;
|
181
|
if ((style & PREPEND_MAIN_LABEL_TO_SUBTASK) != 0
|
182
|
&& mainTaskLabel != null && mainTaskLabel.length() > 0) {
|
183
|
label = mainTaskLabel + ' ' + label;
|
184
|
}
|
185
|
super.subTask(label);
|
186
|
}
|
187
|
|
188
|
@Override
|
189
|
public void worked(int work) {
|
190
|
internalWorked(work);
|
191
|
}
|
192
|
|
193
|
@Override
|
194
|
public void warning(String message) {
|
195
|
// TODO Auto-generated method stub
|
196
|
}
|
197
|
|
198
|
@Override
|
199
|
public void warning(String message, Throwable throwable) {
|
200
|
// TODO Auto-generated method stub
|
201
|
}
|
202
|
|
203
|
}
|