Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 60347be34dc65c31f9fe5e742f5b633eccedc17c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.core;


import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;

/**
 * Provides an infinite progress monitor by subdividing by half repeatedly.
 *
 * The ticks parameter represents the number of ticks shown in the progress dialog
 * (or propogated up to a parent IProgressMonitor). The totalWork parameter provided
 * in actually a hint used to determine how work is translated into ticks.
 * The number of totalWork that can actually be worked is n*totalWork/2 where
 * 2^n = totalWork. What this means is that if you provide a totalWork of 32 (2^5) than
 * the maximum number of ticks is 5*32/2 = 80.
 *
 */
public class InfiniteSubProgressMonitor extends SubProgressMonitor {

	int totalWork;
	int halfWay;
	int currentIncrement;
	int nextProgress;
	int worked;

	/**
	 * Constructor for InfiniteSubProgressMonitor.
	 * @param monitor
	 * @param ticks
	 */
	public InfiniteSubProgressMonitor(IProgressMonitor monitor, int ticks) {
		this(monitor, ticks, 0);
	}

	/**
	 * Constructor for InfiniteSubProgressMonitor.
	 * @param monitor
	 * @param ticks
	 * @param style
	 */
	public InfiniteSubProgressMonitor(IProgressMonitor monitor, int ticks, int style) {
		super(monitor, ticks, style);
	}

	public void beginTask(String name, int totalWork) {
		super.beginTask(name, totalWork);
		this.totalWork = totalWork;
		this.halfWay = totalWork / 2;
		this.currentIncrement = 1;
		this.nextProgress = currentIncrement;
		this.worked = 0;
	}

	public void worked(int work) {
		if (worked >= totalWork) return;
		if (--nextProgress <= 0) {
			super.worked(1);
			worked++;
			if (worked >= halfWay) {
				// we have passed the current halfway point, so double the
				// increment and reset the halfway point.
				currentIncrement *= 2;
				halfWay += (totalWork - halfWay) / 2;
			}
			// reset the progress counter to another full increment
			nextProgress = currentIncrement;
		}
	}

	/**
	 * Don't allow clearing of the subtask. This will stop the flickering
	 * of the subtask in the progress dialogs.
	 *
	 * @see IProgressMonitor#subTask(String)
	 */
	public void subTask(String name) {
		if(name != null && ! name.equals("")) { //$NON-NLS-1$
			super.subTask(name);
		}
	}
}

Back to the top